diff --git a/docs/resources/ga_accelerator.md b/docs/resources/ga_accelerator.md index cedff724557..77faccea63c 100644 --- a/docs/resources/ga_accelerator.md +++ b/docs/resources/ga_accelerator.md @@ -11,6 +11,8 @@ Manages a GA accelerator resource within HuaweiCloud. ## Example Usage +### Accelerator With IPV4 + ```hcl variable "name" {} variable "description" {} @@ -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: @@ -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 diff --git a/huaweicloud/services/acceptance/ga/resource_huaweicloud_ga_accelerator_test.go b/huaweicloud/services/acceptance/ga/resource_huaweicloud_ga_accelerator_test.go index 0088dbd1426..b6b567997ff 100644 --- a/huaweicloud/services/acceptance/ga/resource_huaweicloud_ga_accelerator_test.go +++ b/huaweicloud/services/acceptance/ga/resource_huaweicloud_ga_accelerator_test.go @@ -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"), ), }, { @@ -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 = { @@ -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 = { diff --git a/huaweicloud/services/ga/resource_huaweicloud_ga_accelerator.go b/huaweicloud/services/ga/resource_huaweicloud_ga_accelerator.go index f85fa8c7520..54c421dd173 100644 --- a/huaweicloud/services/ga/resource_huaweicloud_ga_accelerator.go +++ b/huaweicloud/services/ga/resource_huaweicloud_ga_accelerator.go @@ -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" @@ -54,7 +53,7 @@ func ResourceAccelerator() *schema.Resource { }, "ip_sets": { Type: schema.TypeList, - MaxItems: 1, + MaxItems: 2, Elem: AcceleratorAccelerateIpSchema(), Required: true, ForceNew: true, @@ -123,9 +122,6 @@ 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, @@ -133,9 +129,6 @@ func AcceleratorAccelerateIpSchema() *schema.Resource { ForceNew: true, Default: "IPV4", Description: `Specifies the IP address version.`, - ValidateFunc: validation.StringInSlice([]string{ - "IPV4", - }, false), }, "ip_address": { Type: schema.TypeString, @@ -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 { @@ -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)), @@ -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 }