-
Notifications
You must be signed in to change notification settings - Fork 164
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(dws): add new datasource to query cns of the dws cluster (#5558)
- Loading branch information
1 parent
021f930
commit 5a3e9e0
Showing
4 changed files
with
245 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
huaweicloud/services/acceptance/dws/data_source_huaweicloud_dws_cluster_cns_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
139
huaweicloud/services/dws/data_source_huaweicloud_dws_cluster_cns.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |