Skip to content

Commit

Permalink
feat(as): add a new datasource to get list of planned tasks (#5001)
Browse files Browse the repository at this point in the history
  • Loading branch information
ruwenqiang123 authored Jun 14, 2024
1 parent 6b10ef5 commit 07ea879
Show file tree
Hide file tree
Showing 4 changed files with 449 additions and 0 deletions.
89 changes: 89 additions & 0 deletions docs/data-sources/as_planned_tasks.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
---
subcategory: "Auto Scaling"
layout: "huaweicloud"
page_title: "HuaweiCloud: huaweicloud_as_planned_tasks"
description: |-
Use this data source to get a list of planned tasks.
---

# huaweicloud_as_planned_tasks

Use this data source to get a list of planned tasks.

## Example Usage

```hcl
variable "scaling_group_id" {}
variable "task_id" {}
data "huaweicloud_as_planned_tasks" "test" {
scaling_group_id = var.scaling_group_id
task_id = var.task_id
}
```

## Argument Reference

The following arguments are supported:

* `region` - (Optional, String) Specifies the region in which to query the resource.
If omitted, the provider-level region will be used.

* `scaling_group_id` - (Required, String) Specifies the ID of the AS group to which the planned tasks belong.

* `task_id` - (Optional, String) Specifies the ID of the planned task.

* `name` - (Optional, String) Specifies the name of the planned task.

## Attribute Reference

In addition to all arguments above, the following attributes are exported:

* `id` - The data source ID.

* `scheduled_tasks` - All planned tasks that match the filter parameters.

The [scheduled_tasks](#scheduled_tasks_struct) structure is documented below.

<a name="scheduled_tasks_struct"></a>
The `scheduled_tasks` block supports:

* `id` - The ID of the planned task.

* `scaling_group_id` - The ID of the AS group to which the planned task belongs.

* `name` - The name of the planned task.

* `scheduled_policy` - The planned task policy.

The [scheduled_policy](#scheduled_tasks_scheduled_policy_struct) structure is documented below.

* `instance_number` - The instance number settings of the AS group.

The [instance_number](#scheduled_tasks_instance_number_struct) structure is documented below.

* `created_at` - The creation time of the planned task, in RFC3339 format.

<a name="scheduled_tasks_scheduled_policy_struct"></a>
The `scheduled_policy` block supports:

* `start_time` - The start time of the valid period of the planned task, in RFC3339 format.

* `end_time` - The end time of the valid period of the planned task, in RFC3339 format.

* `launch_time` - The execute time of the planned task.
If **recurrence_type** is left empty or null, the time format is RFC3339.
If **recurrence_type** is specified, the time format is **HH:mm**.

* `recurrence_type` - The triggering type of planned task.

* `recurrence_value` - The frequency at which planned task are triggered.

<a name="scheduled_tasks_instance_number_struct"></a>
The `instance_number` block supports:

* `max` - The maximum number of instances in the AS group.

* `min` - The minimum number of instances in the AS group.

* `desire` - The expected number of instances in the AS group.
1 change: 1 addition & 0 deletions huaweicloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,7 @@ func Provider() *schema.Provider {
"huaweicloud_as_instances": as.DataSourceASInstances(),
"huaweicloud_as_lifecycle_hooks": as.DataSourceLifeCycleHooks(),
"huaweicloud_as_notifications": as.DataSourceAsNotifications(),
"huaweicloud_as_planned_tasks": as.DataSourceAsPlannedTasks(),
"huaweicloud_as_policies": as.DataSourceASPolicies(),
"huaweicloud_as_policy_execute_logs": as.DataSourcePolicyExecuteLogs(),

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package as

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"

"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/acceptance"
)

func TestAccDataSourcePlannedTasks_basic(t *testing.T) {
var (
dataSourceName = "data.huaweicloud_as_planned_tasks.test"
rName = acceptance.RandomAccResourceName()
taskName = acceptance.RandomAccResourceNameWithDash()
dc = acceptance.InitDataSourceCheck(dataSourceName)

byTaskId = "data.huaweicloud_as_planned_tasks.filter_by_task_id"
dcByTaskId = acceptance.InitDataSourceCheck(byTaskId)

byName = "data.huaweicloud_as_planned_tasks.filter_by_name"
dcByName = acceptance.InitDataSourceCheck(byName)
)
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
acceptance.TestAccPreCheck(t)
},
ProviderFactories: acceptance.TestAccProviderFactories,
Steps: []resource.TestStep{
{
Config: testDataSourcePlannedTasks_basic(rName, taskName),
Check: resource.ComposeTestCheckFunc(
dc.CheckResourceExists(),
resource.TestCheckResourceAttrSet(dataSourceName, "scheduled_tasks.#"),
resource.TestCheckResourceAttrSet(dataSourceName, "scheduled_tasks.0.id"),
resource.TestCheckResourceAttrSet(dataSourceName, "scheduled_tasks.0.name"),
resource.TestCheckResourceAttrSet(dataSourceName, "scheduled_tasks.0.scaling_group_id"),
resource.TestCheckResourceAttrSet(dataSourceName, "scheduled_tasks.0.scheduled_policy.#"),
resource.TestCheckResourceAttrSet(dataSourceName, "scheduled_tasks.0.instance_number.#"),
resource.TestCheckResourceAttrSet(dataSourceName, "scheduled_tasks.0.created_at"),

dcByTaskId.CheckResourceExists(),
resource.TestCheckOutput("task_id_filter_is_useful", "true"),

dcByName.CheckResourceExists(),
resource.TestCheckOutput("name_filter_is_useful", "true"),
),
},
},
})
}

func testDataSourcePlannedTasks_basic(name, rName string) string {
return fmt.Sprintf(`
%s
data "huaweicloud_as_planned_tasks" "test" {
depends_on = [
huaweicloud_as_planned_task.test
]
scaling_group_id = huaweicloud_as_group.acc_as_group.id
}
locals {
task_id = data.huaweicloud_as_planned_tasks.test.scheduled_tasks[0].id
}
data "huaweicloud_as_planned_tasks" "filter_by_task_id" {
scaling_group_id = huaweicloud_as_group.acc_as_group.id
task_id = local.task_id
}
locals {
task_id_filter_result = [
for v in data.huaweicloud_as_planned_tasks.filter_by_task_id.scheduled_tasks[*].id : v == local.task_id
]
}
output "task_id_filter_is_useful" {
value = alltrue(local.task_id_filter_result) && length(local.task_id_filter_result) > 0
}
locals {
name = data.huaweicloud_as_planned_tasks.test.scheduled_tasks[0].name
}
data "huaweicloud_as_planned_tasks" "filter_by_name" {
scaling_group_id = huaweicloud_as_group.acc_as_group.id
name = local.name
}
locals {
name_filter_result = [
for v in data.huaweicloud_as_planned_tasks.filter_by_name.scheduled_tasks[*].name : v == local.name
]
}
output "name_filter_is_useful" {
value = alltrue(local.name_filter_result) && length(local.name_filter_result) > 0
}
`, testAccPlannedTask_basic(name, rName))
}
Loading

0 comments on commit 07ea879

Please sign in to comment.