Skip to content

Commit

Permalink
feat(dws): add new datasource to query cns of the dws cluster (#5558)
Browse files Browse the repository at this point in the history
  • Loading branch information
wuzhuanhong authored Sep 19, 2024
1 parent 021f930 commit 5a3e9e0
Show file tree
Hide file tree
Showing 4 changed files with 245 additions and 0 deletions.
53 changes: 53 additions & 0 deletions docs/data-sources/dws_cluster_cns.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
---
subcategory: "GaussDB(DWS)"
layout: "huaweicloud"
page_title: "HuaweiCloud: huaweicloud_dws_cluster_cns"
description: |-
Use this data source to query the list of CNs under specified DWS cluster within HuaweiCloud.
---

# huaweicloud_dws_cluster_cns

Use this data source to query the list of CNs under specified DWS cluster within HuaweiCloud.

## Example Usage

```hcl
variable "dws_cluster_id" {}
data "huaweicloud_dws_cluster_cns" "test" {
cluster_id = var.dws_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 CNs belong.

## Attribute Reference

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

* `id` - The data source ID.

* `min_num` - The minimum number of CNs supported by the cluster.

* `max_num` - The maximum number of CNs supported by the cluster.

* `cns` - The list of the CNs under specified DWS cluster.

The [cns](#cns_struct) structure is documented below.

<a name="cns_struct"></a>
The `cns` block supports:

* `id` - The ID of the CN.

* `name` - The name of the CN.

* `private_ip` - The private IP address of the CN.
1 change: 1 addition & 0 deletions huaweicloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -1032,6 +1032,7 @@ func Provider() *schema.Provider {

"huaweicloud_dws_alarm_subscriptions": dws.DataSourceAlarmSubscriptions(),
"huaweicloud_dws_availability_zones": dws.DataSourceDwsAvailabilityZones(),
"huaweicloud_dws_cluster_cns": dws.DataSourceDwsClusterCns(),
"huaweicloud_dws_cluster_logs": dws.DataSourceDwsClusterLogs(),
"huaweicloud_dws_clusters": dws.DataSourceDwsClusters(),
"huaweicloud_dws_disaster_recovery_tasks": dws.DataSourceDisasterRecoveryTasks(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
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 TestAccDataSourcClusterCns_basic(t *testing.T) {
dataSource := "data.huaweicloud_dws_cluster_cns.test"
dc := acceptance.InitDataSourceCheck(dataSource)
uuid, _ := uuid.GenerateUUID()

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
acceptance.TestAccPreCheck(t)
acceptance.TestAccPreCheckDwsClusterId(t)
},
ProviderFactories: acceptance.TestAccProviderFactories,
Steps: []resource.TestStep{
{
Config: testDataSourceClusterCns_basic(uuid),
ExpectError: regexp.MustCompile("Cluster does not exist or has been deleted"),
},
{
Config: testDataSourceClusterCns_basic(acceptance.HW_DWS_CLUSTER_ID),
Check: resource.ComposeTestCheckFunc(
dc.CheckResourceExists(),
resource.TestCheckResourceAttrSet(dataSource, "max_num"),
resource.TestCheckResourceAttrSet(dataSource, "min_num"),
resource.TestCheckResourceAttrSet(dataSource, "cns.#"),
resource.TestCheckResourceAttrSet(dataSource, "cns.0.id"),
resource.TestCheckResourceAttrSet(dataSource, "cns.0.name"),
resource.TestCheckResourceAttrSet(dataSource, "cns.0.private_ip"),
),
},
},
})
}

func testDataSourceClusterCns_basic(clusterId string) string {
return fmt.Sprintf(`
data "huaweicloud_dws_cluster_cns" "test" {
cluster_id = "%s"
}
`, clusterId)
}
139 changes: 139 additions & 0 deletions huaweicloud/services/dws/data_source_huaweicloud_dws_cluster_cns.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
// Generated by PMS #343
package dws

import (
"context"
"strings"

"github.com/hashicorp/go-multierror"
"github.com/hashicorp/go-uuid"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/tidwall/gjson"

"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/helper/httphelper"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/helper/schemas"
)

func DataSourceDwsClusterCns() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceDwsClusterCnsRead,

Schema: map[string]*schema.Schema{
"region": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: `Specifies the region in which to query the resource. If omitted, the provider-level region will be used.`,
},
"cluster_id": {
Type: schema.TypeString,
Required: true,
Description: `Specifies the DWS cluster ID to which the CNs belong.`,
},
"min_num": {
Type: schema.TypeInt,
Computed: true,
Description: `The minimum number of CNs supported by the cluster.`,
},
"max_num": {
Type: schema.TypeInt,
Computed: true,
Description: `The maximum number of CNs supported by the cluster.`,
},
"cns": {
Type: schema.TypeList,
Computed: true,
Description: `The list of the CNs under specified DWS cluster.`,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
Description: `The ID of the CN.`,
},
"name": {
Type: schema.TypeString,
Computed: true,
Description: `The name of the CN.`,
},
"private_ip": {
Type: schema.TypeString,
Computed: true,
Description: `The private IP address of the CN.`,
},
},
},
},
},
}
}

type ClusterCnsDSWrapper struct {
*schemas.ResourceDataWrapper
Config *config.Config
}

func newClusterCnsDSWrapper(d *schema.ResourceData, meta interface{}) *ClusterCnsDSWrapper {
return &ClusterCnsDSWrapper{
ResourceDataWrapper: schemas.NewSchemaWrapper(d),
Config: meta.(*config.Config),
}
}

func dataSourceDwsClusterCnsRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
wrapper := newClusterCnsDSWrapper(d, meta)
listClusterCnRst, err := wrapper.ListClusterCn()
if err != nil {
return diag.FromErr(err)
}

id, err := uuid.GenerateUUID()
if err != nil {
return diag.FromErr(err)
}
d.SetId(id)

err = wrapper.listClusterCnToSchema(listClusterCnRst)
if err != nil {
return diag.FromErr(err)
}

return nil
}

// @API DWS GET /v1.0/{project_id}/clusters/{cluster_id}/cns
func (w *ClusterCnsDSWrapper) ListClusterCn() (*gjson.Result, error) {
client, err := w.NewClient(w.Config, "dws")
if err != nil {
return nil, err
}

uri := "/v1.0/{project_id}/clusters/{cluster_id}/cns"
uri = strings.ReplaceAll(uri, "{cluster_id}", w.Get("cluster_id").(string))
return httphelper.New(client).
Method("GET").
URI(uri).
Request().
Result()
}

func (w *ClusterCnsDSWrapper) listClusterCnToSchema(body *gjson.Result) error {
d := w.ResourceData
mErr := multierror.Append(nil,
d.Set("region", w.Config.GetRegion(w.ResourceData)),
d.Set("min_num", body.Get("min_num").Value()),
d.Set("max_num", body.Get("max_num").Value()),
d.Set("cns", schemas.SliceToList(body.Get("instances"),
func(cns gjson.Result) any {
return map[string]any{
"id": cns.Get("id").Value(),
"name": cns.Get("name").Value(),
"private_ip": cns.Get("private_ip").Value(),
}
},
)),
)
return mErr.ErrorOrNil()
}

0 comments on commit 5a3e9e0

Please sign in to comment.