Skip to content

Commit

Permalink
feat(dws): add datasource to query workload plans (#5381)
Browse files Browse the repository at this point in the history
  • Loading branch information
wuzhuanhong authored Aug 15, 2024
1 parent b96ecfe commit f1ff8ec
Show file tree
Hide file tree
Showing 4 changed files with 425 additions and 0 deletions.
58 changes: 58 additions & 0 deletions docs/data-sources/dws_workload_plans.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
---
subcategory: "GaussDB(DWS)"
layout: "huaweicloud"
page_title: "HuaweiCloud: huaweicloud_dws_workload_plans"
description: |-
Using this data source to query the list of workload plans within HuaweiCloud.
---

# huaweicloud_dws_workload_plans

Using this data source to query the list of workload plans within HuaweiCloud.

## Example Usage

```hcl
variable "cluster_id" {}
data "huaweicloud_dws_workload_plans" "test" {
cluster_id = var.cluster_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.

* `cluster_id` - (Required, String) Specifies the DWS cluster ID to which the workload plans belong.

* `logical_cluster_name` - (Optional, String) Specifies the logical cluster name to which the workload plans belong.
This parameter is only available when the DWS cluster is logical cluster.

## Attribute Reference

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

* `id` - The data source ID.

* `plans` - All workload plans that match the filter parameters.

The [plans](#plans_struct) structure is documented below.

<a name="plans_struct"></a>
The `plans` block supports:

* `id` - The ID of the workload plan.

* `name` - The name of the workload plan.

* `cluster_id` - The cluster ID to which the workload plan belongs.

* `current_stage_name` - The name of the current plan stage corresponding to the workload plan.

* `status` - The status of the workload plan.
+ **enabled**: The workload plan has been started.
+ **disabled**: The workload plan has not been started.
1 change: 1 addition & 0 deletions huaweicloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -993,6 +993,7 @@ func Provider() *schema.Provider {
"huaweicloud_dws_quotas": dws.DataSourceDwsQuotas(),
"huaweicloud_dws_snapshot_policies": dws.DataSourceDwsSnapshotPolicies(),
"huaweicloud_dws_snapshots": dws.DataSourceDwsSnapshots(),
"huaweicloud_dws_workload_plans": dws.DataSourceDwsWorkloadPlans(),
"huaweicloud_dws_workload_queues": dws.DataSourceWorkloadQueues(),

"huaweicloud_workspace_desktops": workspace.DataSourceDesktops(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,211 @@
package dws

import (
"fmt"
"regexp"
"testing"

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

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

func TestAccDataSourceWorkloadPlans_basic(t *testing.T) {
var (
rName = acceptance.RandomAccResourceName()
dataSource = "data.huaweicloud_dws_workload_plans.test"
dc = acceptance.InitDataSourceCheck(dataSource)

byLogicalClusterName = "data.huaweicloud_dws_workload_plans.filter_by_logical_cluster_name"
dcByLogicalClusterName = acceptance.InitDataSourceCheck(byLogicalClusterName)
)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
acceptance.TestAccPreCheck(t)
acceptance.TestAccPreCheckDwsClusterId(t)
},
ProviderFactories: acceptance.TestAccProviderFactories,
Steps: []resource.TestStep{
{
Config: testDataSourceDwsWorkloadPlans_clusterNotFound(),
ExpectError: regexp.MustCompile("Cluster does not exist or has been deleted"),
},
{
Config: testDataSourceWorkloadPlans_basic(rName),
Check: resource.ComposeTestCheckFunc(
dc.CheckResourceExists(),
resource.TestMatchResourceAttr(dataSource, "plans.#", regexp.MustCompile(`^[1-9]([0-9]*)?$`)),
resource.TestCheckOutput("is_exist_plan", "true"),
dcByLogicalClusterName.CheckResourceExists(),
resource.TestCheckOutput("is_userful_logical_cluster_name", "true"),
resource.TestCheckResourceAttrSet(byLogicalClusterName, "plans.0.id"),
resource.TestCheckResourceAttrSet(byLogicalClusterName, "plans.0.name"),
resource.TestCheckResourceAttrSet(byLogicalClusterName, "plans.0.cluster_id"),
resource.TestCheckResourceAttrSet(byLogicalClusterName, "plans.0.status"),
resource.TestCheckResourceAttrSet(byLogicalClusterName, "plans.0.current_stage_name"),
),
},
},
})
}

func testDataSourceDwsWorkloadPlans_clusterNotFound() string {
randUUID, _ := uuid.GenerateUUID()
return fmt.Sprintf(`
data "huaweicloud_dws_workload_plans" "test" {
cluster_id = "%s"
}
`, randUUID)
}

func testDataSourceDwsWorkloadPlans_base(name string) string {
return fmt.Sprintf(`
data "huaweicloud_dws_logical_cluster_rings" "test" {
cluster_id = "%[1]s"
}
locals {
ring_hosts = [for v in data.huaweicloud_dws_logical_cluster_rings.test.cluster_rings : v.ring_hosts if v.is_available]
}
resource "huaweicloud_dws_logical_cluster" "test" {
cluster_id = "%[1]s"
logical_cluster_name = "%[2]s"
cluster_rings {
dynamic "ring_hosts" {
for_each = local.ring_hosts[0]
content {
host_name = ring_hosts.value.host_name
back_ip = ring_hosts.value.back_ip
cpu_cores = ring_hosts.value.cpu_cores
memory = ring_hosts.value.memory
disk_size = ring_hosts.value.disk_size
}
}
}
lifecycle {
ignore_changes = [
cluster_rings,
]
}
}
resource "huaweicloud_dws_workload_plan" "test" {
cluster_id = "%[1]s"
name = "%[2]s"
logical_cluster_name = huaweicloud_dws_logical_cluster.test.logical_cluster_name
lifecycle {
ignore_changes = [
logical_cluster_name,
]
}
}
resource "huaweicloud_dws_workload_queue" "test" {
cluster_id = "%[1]s"
name = "%[2]s"
logical_cluster_name = huaweicloud_dws_logical_cluster.test.logical_cluster_name
configuration {
resource_name = "cpu_limit"
resource_value = 10
}
configuration {
resource_name = "memory"
resource_value = 10
}
configuration {
resource_name = "tablespace"
resource_value = -1
}
configuration {
resource_name = "activestatements"
resource_value = -1
}
}
resource "huaweicloud_dws_workload_plan_stage" "test" {
count = 2
cluster_id = "%[1]s"
plan_id = huaweicloud_dws_workload_plan.test.id
name = "%[2]s${count.index}"
start_time = "01:00:00"
end_time = "02:00:00"
month = format("%%s", count.index + 1)
day = "1"
queues {
name = huaweicloud_dws_workload_queue.test.name
configuration {
resource_name = "cpu"
resource_value = 1
}
configuration {
resource_name = "cpu_limit"
resource_value = 0
}
configuration {
resource_name = "memory"
resource_value = 0
}
configuration {
resource_name = "concurrency"
resource_value = 10
}
configuration {
resource_name = "shortQueryConcurrencyNum"
resource_value = -1
}
}
}
resource "huaweicloud_dws_workload_plan_execution" "test" {
depends_on = [
huaweicloud_dws_workload_plan_stage.test,
]
cluster_id = "%[1]s"
plan_id = huaweicloud_dws_workload_plan.test.id
stage_id = huaweicloud_dws_workload_plan_stage.test[0].id
}
`, acceptance.HW_DWS_CLUSTER_ID, name)
}

func testDataSourceWorkloadPlans_basic(name string) string {
return fmt.Sprintf(`
%[1]s
data "huaweicloud_dws_workload_plans" "test" {
depends_on = [
huaweicloud_dws_workload_plan_execution.test
]
cluster_id = "%[2]s"
}
output "is_exist_plan" {
value = contains(data.huaweicloud_dws_workload_plans.test.plans[*].id, huaweicloud_dws_workload_plan.test.id)
}
data "huaweicloud_dws_workload_plans" "filter_by_logical_cluster_name" {
depends_on = [
huaweicloud_dws_workload_plan_execution.test
]
cluster_id = "%[2]s"
logical_cluster_name = huaweicloud_dws_logical_cluster.test.logical_cluster_name
}
# The logical_cluster_name parameter is queried through exact match.
output "is_userful_logical_cluster_name" {
value = length(data.huaweicloud_dws_workload_plans.filter_by_logical_cluster_name.plans) == 1
}
`, testDataSourceDwsWorkloadPlans_base(name), acceptance.HW_DWS_CLUSTER_ID)
}
Loading

0 comments on commit f1ff8ec

Please sign in to comment.