Skip to content

Commit fe7ffdd

Browse files
committed
feat(rocketmar): add new data source to get azs
1 parent 6827fdd commit fe7ffdd

File tree

4 files changed

+240
-0
lines changed

4 files changed

+240
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
---
2+
subcategory: "Distributed Message Service (DMS)"
3+
layout: "huaweicloud"
4+
page_title: "HuaweiCloud: huaweicloud_dms_rocketmq_availability_zones"
5+
description: |-
6+
Use this data source to get the list of DMS rocketMQ availability zones within HuaweiCloud.
7+
---
8+
9+
# huaweicloud_dms_rocketmq_availability_zones
10+
11+
Use this data source to get the list of DMS rocketMQ availability zones within HuaweiCloud.
12+
13+
## Example Usage
14+
15+
```hcl
16+
data "huaweicloud_dms_rocketmq_availability_zones" "test" {}
17+
```
18+
19+
## Argument Reference
20+
21+
The following arguments are supported:
22+
23+
* `region` - (Optional, String) Specifies the region where the availability zones are located.
24+
If omitted, the provider-level region will be used.
25+
26+
## Attributes Reference
27+
28+
The following attributes are exported:
29+
30+
* `availability_zones` - The list of availability zones.
31+
The [availability_zones](#rocketmq_availability_zones_attr) structure is documented below.
32+
33+
<a name="rocketmq_availability_zones_attr"></a>
34+
The `availability_zones` block supports:
35+
36+
* `id` - The ID of the availability zone.
37+
38+
* `name` - The name of the availability zone. e.g. `AZ1`.
39+
40+
* `code` - The code of the availability zone. e.g. `cn-north-4a`.
41+
42+
* `port` - The port of the availability zone.
43+
44+
* `sold_out` - Whether the availability zone is sold out.
45+
46+
* `resource_availability` - The resource availability of the availability zone.
47+
48+
* `default_az` - Whether the availability zone is the default availability zone.
49+
50+
* `remain_time` - The remaining time of the availability zone.
51+
52+
* `ipv6_enable` - Whether the availability zone supports IPv6.

huaweicloud/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -933,6 +933,7 @@ func Provider() *schema.Provider {
933933
"huaweicloud_dms_rabbitmq_queues": rabbitmq.DataSourceDmsRabbitmqQueues(),
934934
"huaweicloud_dms_rabbitmq_users": rabbitmq.DataSourceDmsRabbitmqUsers(),
935935

936+
"huaweicloud_dms_rocketmq_availability_zones": rocketmq.DataSourceRocketMQAvailabilityZones(),
936937
"huaweicloud_dms_rocketmq_broker": rocketmq.DataSourceDmsRocketMQBroker(),
937938
"huaweicloud_dms_rocketmq_instances": rocketmq.DataSourceDmsRocketMQInstances(),
938939
"huaweicloud_dms_rocketmq_topics": rocketmq.DataSourceDmsRocketMQTopics(),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package rocketmq
2+
3+
import (
4+
"regexp"
5+
"testing"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
8+
9+
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/acceptance"
10+
)
11+
12+
func TestAccDataRocketMQAvailabilityZones_basic(t *testing.T) {
13+
all := "data.huaweicloud_dms_rocketmq_availability_zones.test"
14+
dc := acceptance.InitDataSourceCheck(all)
15+
16+
resource.ParallelTest(t, resource.TestCase{
17+
PreCheck: func() { acceptance.TestAccPreCheck(t) },
18+
ProviderFactories: acceptance.TestAccProviderFactories,
19+
Steps: []resource.TestStep{
20+
{
21+
Config: testAccDataRocketMQAvailabilityZones_basic,
22+
Check: resource.ComposeTestCheckFunc(
23+
dc.CheckResourceExists(),
24+
resource.TestMatchResourceAttr(all, "availability_zones.#", regexp.MustCompile(`^[1-9]([0-9]*)?$`)),
25+
resource.TestCheckResourceAttrSet(all, "availability_zones.0.id"),
26+
resource.TestCheckResourceAttrSet(all, "availability_zones.0.name"),
27+
resource.TestCheckResourceAttrSet(all, "availability_zones.0.code"),
28+
resource.TestCheckResourceAttrSet(all, "availability_zones.0.port"),
29+
resource.TestCheckResourceAttrSet(all, "availability_zones.0.sold_out"),
30+
resource.TestCheckResourceAttrSet(all, "availability_zones.0.resource_availability"),
31+
resource.TestCheckResourceAttrSet(all, "availability_zones.0.default_az"),
32+
resource.TestCheckResourceAttrSet(all, "availability_zones.0.remain_time"),
33+
resource.TestCheckResourceAttrSet(all, "availability_zones.0.ipv6_enable"),
34+
),
35+
},
36+
},
37+
})
38+
}
39+
40+
const testAccDataRocketMQAvailabilityZones_basic = `
41+
data "huaweicloud_dms_rocketmq_availability_zones" "test" {}
42+
`
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
package rocketmq
2+
3+
import (
4+
"context"
5+
6+
"github.com/hashicorp/go-multierror"
7+
"github.com/hashicorp/go-uuid"
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
10+
11+
"github.com/chnsz/golangsdk"
12+
13+
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config"
14+
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/utils"
15+
)
16+
17+
// @API RocketMQ GET /v2/available-zones
18+
func DataSourceRocketMQAvailabilityZones() *schema.Resource {
19+
return &schema.Resource{
20+
ReadContext: dataSourceRocketMQAvailabilityZonesRead,
21+
22+
Schema: map[string]*schema.Schema{
23+
"region": {
24+
Type: schema.TypeString,
25+
Optional: true,
26+
Computed: true,
27+
Description: "The region where the availability zones are located.",
28+
},
29+
"availability_zones": {
30+
Type: schema.TypeList,
31+
Computed: true,
32+
Elem: &schema.Resource{
33+
Schema: map[string]*schema.Schema{
34+
"id": {
35+
Type: schema.TypeString,
36+
Computed: true,
37+
Description: "The ID of the availability zone.",
38+
},
39+
"name": {
40+
Type: schema.TypeString,
41+
Computed: true,
42+
Description: "The name of the availability zone.",
43+
},
44+
"code": {
45+
Type: schema.TypeString,
46+
Computed: true,
47+
Description: "The code of the availability zone.",
48+
},
49+
"port": {
50+
Type: schema.TypeString,
51+
Computed: true,
52+
Description: "The port number of the availability zone.",
53+
},
54+
"sold_out": {
55+
Type: schema.TypeBool,
56+
Computed: true,
57+
Description: "Whether the availability zone is sold out.",
58+
},
59+
"resource_availability": {
60+
Type: schema.TypeString,
61+
Computed: true,
62+
Description: "Whether there are available resources in the availability zone.",
63+
},
64+
"default_az": {
65+
Type: schema.TypeBool,
66+
Computed: true,
67+
Description: "Whether the availability zone is default.",
68+
},
69+
"remain_time": {
70+
Type: schema.TypeFloat,
71+
Computed: true,
72+
Description: "The remaining time of the availability zone.",
73+
},
74+
"ipv6_enable": {
75+
Type: schema.TypeBool,
76+
Computed: true,
77+
Description: "Whether IPv6 is supported in the availability zone.",
78+
},
79+
},
80+
},
81+
Description: "The list of availability zones.",
82+
},
83+
},
84+
}
85+
}
86+
87+
func dataSourceRocketMQAvailabilityZonesRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
88+
var (
89+
cfg = meta.(*config.Config)
90+
region = cfg.GetRegion(d)
91+
httpUrl = "v2/available-zones"
92+
listOpt = golangsdk.RequestOpts{
93+
KeepResponseBody: true,
94+
}
95+
)
96+
97+
client, err := cfg.NewServiceClient("dmsv2", region)
98+
if err != nil {
99+
return diag.Errorf("error creating DMS client: %s", err)
100+
}
101+
102+
listPath := client.Endpoint + httpUrl
103+
resp, err := client.Request("GET", listPath, &listOpt)
104+
if err != nil {
105+
return diag.Errorf("error querying availability zones: %s", err)
106+
}
107+
108+
respBody, err := utils.FlattenResponse(resp)
109+
if err != nil {
110+
return diag.FromErr(err)
111+
}
112+
113+
randomUUID, err := uuid.GenerateUUID()
114+
if err != nil {
115+
return diag.Errorf("unable to generate ID: %s", err)
116+
}
117+
118+
d.SetId(randomUUID)
119+
120+
mErr := multierror.Append(
121+
d.Set("region", region),
122+
d.Set("availability_zones", flattenRocketMQAvailabilityZones(utils.PathSearch("available_zones",
123+
respBody, make([]interface{}, 0)).([]interface{}))),
124+
)
125+
126+
return diag.FromErr(mErr.ErrorOrNil())
127+
}
128+
129+
func flattenRocketMQAvailabilityZones(azs []interface{}) []map[string]interface{} {
130+
result := make([]map[string]interface{}, len(azs))
131+
for i, az := range azs {
132+
result[i] = map[string]interface{}{
133+
"id": utils.PathSearch("id", az, nil),
134+
"name": utils.PathSearch("name", az, nil),
135+
"code": utils.PathSearch("code", az, nil),
136+
"port": utils.PathSearch("port", az, nil),
137+
"sold_out": utils.PathSearch("soldOut", az, nil),
138+
"resource_availability": utils.PathSearch("resource_availability", az, nil),
139+
"default_az": utils.PathSearch("default_az", az, nil),
140+
"remain_time": utils.PathSearch("remain_time", az, nil),
141+
"ipv6_enable": utils.PathSearch("ipv6_enable", az, nil),
142+
}
143+
}
144+
return result
145+
}

0 commit comments

Comments
 (0)