Skip to content

Commit 4dc5b48

Browse files
committed
feat(ELB): add params to elb ipgroups data source
1 parent 98f379d commit 4dc5b48

File tree

3 files changed

+41
-12
lines changed

3 files changed

+41
-12
lines changed

docs/data-sources/elb_ipgroups.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ The following arguments are supported:
3333

3434
* `description` - (Optional, String) Specifies the description of the IP address group.
3535

36+
* `enterprise_project_id` - (Optional, String) Specifies the enterprise project ID.
37+
3638
* `ip_address` - (Optional, String) Specifies the IP address of the IP address group.
3739

3840
## Attribute Reference
@@ -53,6 +55,8 @@ The `ipgroups` block supports:
5355

5456
* `description` - The description of the IP address group.
5557

58+
* `enterprise_project_id` - The enterprise project ID.
59+
5660
* `project_id` - The project ID of the IP address group.
5761

5862
* `listeners` - The IDs of listeners with which the IP address group is associated. The [listeners](#Elb_ipgroups_listeners)

huaweicloud/services/acceptance/elb/data_source_huaweicloud_elb_ipgroups_test.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ func TestAccDatasourceIpGroups_basic(t *testing.T) {
2626
resource.TestCheckResourceAttrSet(rName, "ipgroups.0.name"),
2727
resource.TestCheckResourceAttrSet(rName, "ipgroups.0.id"),
2828
resource.TestCheckResourceAttrSet(rName, "ipgroups.0.description"),
29+
resource.TestCheckResourceAttrSet(rName, "ipgroups.0.enterprise_project_id"),
2930
resource.TestCheckResourceAttrSet(rName, "ipgroups.0.project_id"),
3031
resource.TestCheckResourceAttrSet(rName, "ipgroups.0.ip_list.0.ip"),
3132
resource.TestCheckResourceAttrSet(rName, "ipgroups.0.created_at"),
@@ -71,10 +72,10 @@ output "ipgroup_id_filter_is_useful" {
7172
}
7273
7374
locals {
74-
ip_address = huaweicloud_elb_ipgroup.test.ip_list[0].ip
75+
ip_address = tolist(huaweicloud_elb_ipgroup.test.ip_list)[0].ip
7576
}
7677
data "huaweicloud_elb_ipgroups" "ip_address_filter" {
77-
ip_address = huaweicloud_elb_ipgroup.test.ip_list[0].ip
78+
ip_address = tolist(huaweicloud_elb_ipgroup.test.ip_list)[0].ip
7879
}
7980
output "ip_address_filter_is_useful" {
8081
value = length(data.huaweicloud_elb_ipgroups.ip_address_filter.ipgroups) > 0 && alltrue(
@@ -94,5 +95,17 @@ output "description_filter_is_useful" {
9495
)
9596
}
9697
98+
locals {
99+
enterprise_project_id = huaweicloud_elb_ipgroup.test.enterprise_project_id
100+
}
101+
data "huaweicloud_elb_ipgroups" "enterprise_project_id_filter" {
102+
enterprise_project_id = huaweicloud_elb_ipgroup.test.enterprise_project_id
103+
}
104+
output "enterprise_project_id_filter_is_useful" {
105+
value = length(data.huaweicloud_elb_ipgroups.enterprise_project_id_filter.ipgroups) > 0 && alltrue(
106+
[for v in data.huaweicloud_elb_ipgroups.enterprise_project_id_filter.ipgroups[*].enterprise_project_id :
107+
v == local.enterprise_project_id]
108+
)
109+
}
97110
`, testAccElbV3IpGroupConfig_basic(name), name)
98111
}

huaweicloud/services/elb/data_source_huaweicloud_elb_ipgroups.go

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ func DataSourceElbIpGroups() *schema.Resource {
3737
Type: schema.TypeString,
3838
Optional: true,
3939
},
40+
"enterprise_project_id": {
41+
Type: schema.TypeString,
42+
Optional: true,
43+
},
4044
"ip_address": {
4145
Type: schema.TypeString,
4246
Optional: true,
@@ -69,6 +73,10 @@ func ipgroupsSchema() *schema.Resource {
6973
Type: schema.TypeString,
7074
Computed: true,
7175
},
76+
"enterprise_project_id": {
77+
Type: schema.TypeString,
78+
Computed: true,
79+
},
7280
"project_id": {
7381
Type: schema.TypeString,
7482
Computed: true,
@@ -127,7 +135,7 @@ func dataSourceElbIpGroupsRead(_ context.Context, d *schema.ResourceData, meta i
127135
}
128136
listIpGroupsPath := listIpGroupsClient.Endpoint + listIpGroupsHttpUrl
129137
listIpGroupsPath = strings.ReplaceAll(listIpGroupsPath, "{project_id}", listIpGroupsClient.ProjectID)
130-
listIpGroupsQueryParams := buildListIpGroupsQueryParams(d)
138+
listIpGroupsQueryParams := buildListIpGroupsQueryParams(d, cfg.GetEnterpriseProjectID(d))
131139
listIpGroupsPath += listIpGroupsQueryParams
132140
listIpGroupsResp, err := pagination.ListAllItems(
133141
listIpGroupsClient,
@@ -162,7 +170,7 @@ func dataSourceElbIpGroupsRead(_ context.Context, d *schema.ResourceData, meta i
162170
return diag.FromErr(mErr.ErrorOrNil())
163171
}
164172

165-
func buildListIpGroupsQueryParams(d *schema.ResourceData) string {
173+
func buildListIpGroupsQueryParams(d *schema.ResourceData, enterpriseProjectId string) string {
166174
res := ""
167175
if v, ok := d.GetOk("ipgroup_id"); ok {
168176
res = fmt.Sprintf("%s&id=%v", res, v)
@@ -176,6 +184,9 @@ func buildListIpGroupsQueryParams(d *schema.ResourceData) string {
176184
if v, ok := d.GetOk("ip_address"); ok {
177185
res = fmt.Sprintf("%s&ip_list=%v", res, v)
178186
}
187+
if enterpriseProjectId != "" {
188+
res = fmt.Sprintf("%s&enterprise_project_id=%v", res, enterpriseProjectId)
189+
}
179190
if res != "" {
180191
res = "?" + res[1:]
181192
}
@@ -194,14 +205,15 @@ func flattenListIpGroupsBody(resp interface{}) []interface{} {
194205
rst := make([]interface{}, 0, len(curArray))
195206
for _, v := range curArray {
196207
rst = append(rst, map[string]interface{}{
197-
"id": utils.PathSearch("id", v, nil),
198-
"name": utils.PathSearch("name", v, nil),
199-
"description": utils.PathSearch("description", v, nil),
200-
"project_id": utils.PathSearch("project_id", v, nil),
201-
"listeners": utils.PathSearch("listeners", v, nil),
202-
"ip_list": utils.PathSearch("ip_list", v, nil),
203-
"created_at": utils.PathSearch("created_at", v, nil),
204-
"updated_at": utils.PathSearch("updated_at", v, nil),
208+
"id": utils.PathSearch("id", v, nil),
209+
"name": utils.PathSearch("name", v, nil),
210+
"description": utils.PathSearch("description", v, nil),
211+
"enterprise_project_id": utils.PathSearch("enterprise_project_id", v, nil),
212+
"project_id": utils.PathSearch("project_id", v, nil),
213+
"listeners": utils.PathSearch("listeners", v, nil),
214+
"ip_list": utils.PathSearch("ip_list", v, nil),
215+
"created_at": utils.PathSearch("created_at", v, nil),
216+
"updated_at": utils.PathSearch("updated_at", v, nil),
205217
})
206218
}
207219
return rst

0 commit comments

Comments
 (0)