Skip to content

Commit

Permalink
feat(ga): the accelerator resource ip_type field supports IPV6
Browse files Browse the repository at this point in the history
  • Loading branch information
ruwenqiang123 committed Jan 3, 2025
1 parent d48b9f2 commit 517587b
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 30 deletions.
39 changes: 36 additions & 3 deletions docs/resources/ga_accelerator.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ Manages a GA accelerator resource within HuaweiCloud.

## Example Usage

### Accelerator With IPV4

```hcl
variable "name" {}
variable "description" {}
Expand All @@ -30,6 +32,33 @@ resource "huaweicloud_ga_accelerator" "test" {
}
```

#### Accelerator With IPV4 And IPV6

```hcl
variable "name" {}
variable "description" {}
resource "huaweicloud_ga_accelerator" "test" {
name = var.name
description = var.description
ip_sets {
ip_type = "IPV4"
area = "CM"
}
ip_sets {
ip_type = "IPV6"
area = "CM"
}
tags = {
foo = "bar"
key = "value"
}
}
```

## Argument Reference

The following arguments are supported:
Expand Down Expand Up @@ -62,10 +91,14 @@ The `AccelerateIp` block supports:

Changing this parameter will create a new resource.

* `ip_type` - (Optional, String, ForceNew) Specifies the IP address version. Only **IPV4** is supported for now.
Defaults to **IPV4**.

* `ip_type` - (Optional, String, ForceNew) Specifies the IP address version. Defaults to **IPV4**.
Changing this parameter will create a new resource.
The valid values are as follows:
+ **IPV4**
+ **IPV6**

-> If you want to set this parameter to **IPV6**, you must set **IPV4** at the same time.
Please refer to the document sample.

## Attribute Reference

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,14 @@ func TestAccAccelerator_basic(t *testing.T) {
resource.TestCheckResourceAttr(rName, "name", name),
resource.TestCheckResourceAttr(rName, "description", "terraform test"),
resource.TestCheckResourceAttr(rName, "ip_sets.0.area", "CM"),
resource.TestCheckResourceAttr(rName, "ip_sets.0.ip_type", "IPV4"),
resource.TestCheckResourceAttr(rName, "ip_sets.1.area", "CM"),
resource.TestCheckResourceAttr(rName, "ip_sets.1.ip_type", "IPV6"),
resource.TestCheckResourceAttr(rName, "tags.foo", "bar"),
resource.TestCheckResourceAttr(rName, "tags.key", "value"),
resource.TestCheckResourceAttr(rName, "status", "ACTIVE"),
resource.TestCheckResourceAttrSet(rName, "ip_sets.0.ip_address"),
resource.TestCheckResourceAttrSet(rName, "ip_sets.1.ip_address"),
),
},
{
Expand Down Expand Up @@ -94,7 +99,13 @@ resource "huaweicloud_ga_accelerator" "test" {
description = "terraform test"
ip_sets {
area = "CM"
ip_type = "IPV4"
area = "CM"
}
ip_sets {
ip_type = "IPV6"
area = "CM"
}
tags = {
Expand All @@ -112,7 +123,13 @@ resource "huaweicloud_ga_accelerator" "test" {
description = "terraform test update"
ip_sets {
area = "CM"
ip_type = "IPV4"
area = "CM"
}
ip_sets {
ip_type = "IPV6"
area = "CM"
}
tags = {
Expand Down
49 changes: 24 additions & 25 deletions huaweicloud/services/ga/resource_huaweicloud_ga_accelerator.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"

"github.com/chnsz/golangsdk"

Expand Down Expand Up @@ -54,7 +53,7 @@ func ResourceAccelerator() *schema.Resource {
},
"ip_sets": {
Type: schema.TypeList,
MaxItems: 1,
MaxItems: 2,
Elem: AcceleratorAccelerateIpSchema(),
Required: true,
ForceNew: true,
Expand Down Expand Up @@ -123,19 +122,13 @@ func AcceleratorAccelerateIpSchema() *schema.Resource {
Specifies the acceleration area. The value can be one of the following:
- **OUTOFCM**: Outside the Chinese mainland
- **CM**: Chinese mainland`,
ValidateFunc: validation.StringInSlice([]string{
"OUTOFCM", "CM",
}, false),
},
"ip_type": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Default: "IPV4",
Description: `Specifies the IP address version.`,
ValidateFunc: validation.StringInSlice([]string{
"IPV4",
}, false),
},
"ip_address": {
Type: schema.TypeString,
Expand Down Expand Up @@ -251,14 +244,18 @@ func buildCreateAcceleratorIpSetsChildBody(d *schema.ResourceData) []map[string]
return nil
}

raw := rawParams[0].(map[string]interface{})
params := map[string]interface{}{
"area": utils.ValueIgnoreEmpty(raw["area"]),
"ip_address": utils.ValueIgnoreEmpty(raw["ip_address"]),
"ip_type": utils.ValueIgnoreEmpty(raw["ip_type"]),
ipSets := make([]map[string]interface{}, len(rawParams))
for _, v := range rawParams {
raw := v.(map[string]interface{})
params := map[string]interface{}{
"area": utils.ValueIgnoreEmpty(raw["area"]),
"ip_address": utils.ValueIgnoreEmpty(raw["ip_address"]),
"ip_type": utils.ValueIgnoreEmpty(raw["ip_type"]),
}
ipSets = append(ipSets, params)
}

return []map[string]interface{}{params}
return ipSets
}

func createAcceleratorWaitingForStateCompleted(ctx context.Context, d *schema.ResourceData, meta interface{}, t time.Duration) error {
Expand Down Expand Up @@ -347,7 +344,7 @@ func resourceAcceleratorRead(_ context.Context, d *schema.ResourceData, meta int
mErr,
d.Set("name", utils.PathSearch("accelerator.name", respBody, nil)),
d.Set("description", utils.PathSearch("accelerator.description", respBody, nil)),
d.Set("ip_sets", flattenGetAcceleratorResponseBodyAccelerateIp(respBody)),
d.Set("ip_sets", flattenAccelerateIpSets(utils.PathSearch("accelerator.ip_sets", respBody, make([]interface{}, 0)))),
d.Set("enterprise_project_id", utils.PathSearch("accelerator.enterprise_project_id", respBody, nil)),
d.Set("tags", flattenGetAcceleratorResponseBodyResourceTag(respBody)),
d.Set("status", utils.PathSearch("accelerator.status", respBody, nil)),
Expand All @@ -360,20 +357,22 @@ func resourceAcceleratorRead(_ context.Context, d *schema.ResourceData, meta int
return diag.FromErr(mErr.ErrorOrNil())
}

func flattenGetAcceleratorResponseBodyAccelerateIp(resp interface{}) []interface{} {
var rst []interface{}
curArray := utils.PathSearch("accelerator.ip_sets", resp, make([]interface{}, 0)).([]interface{})
if len(curArray) < 1 {
func flattenAccelerateIpSets(resp interface{}) []map[string]interface{} {
rawArray, _ := resp.([]interface{})
if len(rawArray) == 0 {
return nil
}

rst = []interface{}{
map[string]interface{}{
"area": utils.PathSearch("area", curArray[0], nil),
"ip_address": utils.PathSearch("ip_address", curArray[0], nil),
"ip_type": utils.PathSearch("ip_type", curArray[0], nil),
},
rst := make([]map[string]interface{}, len(rawArray))
for i, v := range rawArray {
params := map[string]interface{}{
"area": utils.PathSearch("area", v, nil),
"ip_address": utils.PathSearch("ip_address", v, nil),
"ip_type": utils.PathSearch("ip_type", v, nil),
}
rst[i] = params
}

return rst
}

Expand Down

0 comments on commit 517587b

Please sign in to comment.