Skip to content

Commit 517587b

Browse files
committed
feat(ga): the accelerator resource ip_type field supports IPV6
1 parent d48b9f2 commit 517587b

File tree

3 files changed

+79
-30
lines changed

3 files changed

+79
-30
lines changed

docs/resources/ga_accelerator.md

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ Manages a GA accelerator resource within HuaweiCloud.
1111

1212
## Example Usage
1313

14+
### Accelerator With IPV4
15+
1416
```hcl
1517
variable "name" {}
1618
variable "description" {}
@@ -30,6 +32,33 @@ resource "huaweicloud_ga_accelerator" "test" {
3032
}
3133
```
3234

35+
#### Accelerator With IPV4 And IPV6
36+
37+
```hcl
38+
variable "name" {}
39+
variable "description" {}
40+
41+
resource "huaweicloud_ga_accelerator" "test" {
42+
name = var.name
43+
description = var.description
44+
45+
ip_sets {
46+
ip_type = "IPV4"
47+
area = "CM"
48+
}
49+
50+
ip_sets {
51+
ip_type = "IPV6"
52+
area = "CM"
53+
}
54+
55+
tags = {
56+
foo = "bar"
57+
key = "value"
58+
}
59+
}
60+
```
61+
3362
## Argument Reference
3463

3564
The following arguments are supported:
@@ -62,10 +91,14 @@ The `AccelerateIp` block supports:
6291

6392
Changing this parameter will create a new resource.
6493

65-
* `ip_type` - (Optional, String, ForceNew) Specifies the IP address version. Only **IPV4** is supported for now.
66-
Defaults to **IPV4**.
67-
94+
* `ip_type` - (Optional, String, ForceNew) Specifies the IP address version. Defaults to **IPV4**.
6895
Changing this parameter will create a new resource.
96+
The valid values are as follows:
97+
+ **IPV4**
98+
+ **IPV6**
99+
100+
-> If you want to set this parameter to **IPV6**, you must set **IPV4** at the same time.
101+
Please refer to the document sample.
69102

70103
## Attribute Reference
71104

huaweicloud/services/acceptance/ga/resource_huaweicloud_ga_accelerator_test.go

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,14 @@ func TestAccAccelerator_basic(t *testing.T) {
6363
resource.TestCheckResourceAttr(rName, "name", name),
6464
resource.TestCheckResourceAttr(rName, "description", "terraform test"),
6565
resource.TestCheckResourceAttr(rName, "ip_sets.0.area", "CM"),
66+
resource.TestCheckResourceAttr(rName, "ip_sets.0.ip_type", "IPV4"),
67+
resource.TestCheckResourceAttr(rName, "ip_sets.1.area", "CM"),
68+
resource.TestCheckResourceAttr(rName, "ip_sets.1.ip_type", "IPV6"),
6669
resource.TestCheckResourceAttr(rName, "tags.foo", "bar"),
6770
resource.TestCheckResourceAttr(rName, "tags.key", "value"),
6871
resource.TestCheckResourceAttr(rName, "status", "ACTIVE"),
72+
resource.TestCheckResourceAttrSet(rName, "ip_sets.0.ip_address"),
73+
resource.TestCheckResourceAttrSet(rName, "ip_sets.1.ip_address"),
6974
),
7075
},
7176
{
@@ -94,7 +99,13 @@ resource "huaweicloud_ga_accelerator" "test" {
9499
description = "terraform test"
95100
96101
ip_sets {
97-
area = "CM"
102+
ip_type = "IPV4"
103+
area = "CM"
104+
}
105+
106+
ip_sets {
107+
ip_type = "IPV6"
108+
area = "CM"
98109
}
99110
100111
tags = {
@@ -112,7 +123,13 @@ resource "huaweicloud_ga_accelerator" "test" {
112123
description = "terraform test update"
113124
114125
ip_sets {
115-
area = "CM"
126+
ip_type = "IPV4"
127+
area = "CM"
128+
}
129+
130+
ip_sets {
131+
ip_type = "IPV6"
132+
area = "CM"
116133
}
117134
118135
tags = {

huaweicloud/services/ga/resource_huaweicloud_ga_accelerator.go

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import (
1616
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1717
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
1818
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
19-
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
2019

2120
"github.com/chnsz/golangsdk"
2221

@@ -54,7 +53,7 @@ func ResourceAccelerator() *schema.Resource {
5453
},
5554
"ip_sets": {
5655
Type: schema.TypeList,
57-
MaxItems: 1,
56+
MaxItems: 2,
5857
Elem: AcceleratorAccelerateIpSchema(),
5958
Required: true,
6059
ForceNew: true,
@@ -123,19 +122,13 @@ func AcceleratorAccelerateIpSchema() *schema.Resource {
123122
Specifies the acceleration area. The value can be one of the following:
124123
- **OUTOFCM**: Outside the Chinese mainland
125124
- **CM**: Chinese mainland`,
126-
ValidateFunc: validation.StringInSlice([]string{
127-
"OUTOFCM", "CM",
128-
}, false),
129125
},
130126
"ip_type": {
131127
Type: schema.TypeString,
132128
Optional: true,
133129
ForceNew: true,
134130
Default: "IPV4",
135131
Description: `Specifies the IP address version.`,
136-
ValidateFunc: validation.StringInSlice([]string{
137-
"IPV4",
138-
}, false),
139132
},
140133
"ip_address": {
141134
Type: schema.TypeString,
@@ -251,14 +244,18 @@ func buildCreateAcceleratorIpSetsChildBody(d *schema.ResourceData) []map[string]
251244
return nil
252245
}
253246

254-
raw := rawParams[0].(map[string]interface{})
255-
params := map[string]interface{}{
256-
"area": utils.ValueIgnoreEmpty(raw["area"]),
257-
"ip_address": utils.ValueIgnoreEmpty(raw["ip_address"]),
258-
"ip_type": utils.ValueIgnoreEmpty(raw["ip_type"]),
247+
ipSets := make([]map[string]interface{}, len(rawParams))
248+
for _, v := range rawParams {
249+
raw := v.(map[string]interface{})
250+
params := map[string]interface{}{
251+
"area": utils.ValueIgnoreEmpty(raw["area"]),
252+
"ip_address": utils.ValueIgnoreEmpty(raw["ip_address"]),
253+
"ip_type": utils.ValueIgnoreEmpty(raw["ip_type"]),
254+
}
255+
ipSets = append(ipSets, params)
259256
}
260257

261-
return []map[string]interface{}{params}
258+
return ipSets
262259
}
263260

264261
func createAcceleratorWaitingForStateCompleted(ctx context.Context, d *schema.ResourceData, meta interface{}, t time.Duration) error {
@@ -347,7 +344,7 @@ func resourceAcceleratorRead(_ context.Context, d *schema.ResourceData, meta int
347344
mErr,
348345
d.Set("name", utils.PathSearch("accelerator.name", respBody, nil)),
349346
d.Set("description", utils.PathSearch("accelerator.description", respBody, nil)),
350-
d.Set("ip_sets", flattenGetAcceleratorResponseBodyAccelerateIp(respBody)),
347+
d.Set("ip_sets", flattenAccelerateIpSets(utils.PathSearch("accelerator.ip_sets", respBody, make([]interface{}, 0)))),
351348
d.Set("enterprise_project_id", utils.PathSearch("accelerator.enterprise_project_id", respBody, nil)),
352349
d.Set("tags", flattenGetAcceleratorResponseBodyResourceTag(respBody)),
353350
d.Set("status", utils.PathSearch("accelerator.status", respBody, nil)),
@@ -360,20 +357,22 @@ func resourceAcceleratorRead(_ context.Context, d *schema.ResourceData, meta int
360357
return diag.FromErr(mErr.ErrorOrNil())
361358
}
362359

363-
func flattenGetAcceleratorResponseBodyAccelerateIp(resp interface{}) []interface{} {
364-
var rst []interface{}
365-
curArray := utils.PathSearch("accelerator.ip_sets", resp, make([]interface{}, 0)).([]interface{})
366-
if len(curArray) < 1 {
360+
func flattenAccelerateIpSets(resp interface{}) []map[string]interface{} {
361+
rawArray, _ := resp.([]interface{})
362+
if len(rawArray) == 0 {
367363
return nil
368364
}
369365

370-
rst = []interface{}{
371-
map[string]interface{}{
372-
"area": utils.PathSearch("area", curArray[0], nil),
373-
"ip_address": utils.PathSearch("ip_address", curArray[0], nil),
374-
"ip_type": utils.PathSearch("ip_type", curArray[0], nil),
375-
},
366+
rst := make([]map[string]interface{}, len(rawArray))
367+
for i, v := range rawArray {
368+
params := map[string]interface{}{
369+
"area": utils.PathSearch("area", v, nil),
370+
"ip_address": utils.PathSearch("ip_address", v, nil),
371+
"ip_type": utils.PathSearch("ip_type", v, nil),
372+
}
373+
rst[i] = params
376374
}
375+
377376
return rst
378377
}
379378

0 commit comments

Comments
 (0)