From 849fc65aaf7d722c0c2b6e495f3a9d94b1a78e6f Mon Sep 17 00:00:00 2001 From: chenhanzhang Date: Thu, 26 Dec 2024 22:43:41 +0800 Subject: [PATCH] resource/alicloud_vpc: add new attribute dns_hostname_status, ipv4_cidr_mask, secondary_cidr_mask. --- alicloud/resource_alicloud_vpc.go | 275 +++++++++++++++---------- alicloud/resource_alicloud_vpc_test.go | 153 +++++++++++++- alicloud/service_alicloud_vpc_v2.go | 75 ++----- website/docs/r/vpc.html.markdown | 66 +++--- 4 files changed, 367 insertions(+), 202 deletions(-) diff --git a/alicloud/resource_alicloud_vpc.go b/alicloud/resource_alicloud_vpc.go index bbc9083822a7..4dd90baf840b 100644 --- a/alicloud/resource_alicloud_vpc.go +++ b/alicloud/resource_alicloud_vpc.go @@ -1,4 +1,3 @@ -// Package alicloud. This file is generated automatically. Please do not modify it manually, thank you! package alicloud import ( @@ -45,6 +44,12 @@ func resourceAliCloudVpcVpc() *schema.Resource { Type: schema.TypeString, Optional: true, }, + "dns_hostname_status": { + Type: schema.TypeString, + Optional: true, + Computed: true, + ValidateFunc: StringInSlice([]string{"ENABLED", "DISABLED", "MODIFYING"}, false), + }, "dry_run": { Type: schema.TypeBool, Optional: true, @@ -53,6 +58,10 @@ func resourceAliCloudVpcVpc() *schema.Resource { Type: schema.TypeBool, Optional: true, }, + "ipv4_cidr_mask": { + Type: schema.TypeInt, + Optional: true, + }, "ipv4_ipam_pool_id": { Type: schema.TypeString, Optional: true, @@ -83,6 +92,10 @@ func resourceAliCloudVpcVpc() *schema.Resource { Type: schema.TypeString, Optional: true, }, + "region_id": { + Type: schema.TypeString, + Computed: true, + }, "resource_group_id": { Type: schema.TypeString, Optional: true, @@ -90,8 +103,8 @@ func resourceAliCloudVpcVpc() *schema.Resource { }, "route_table_id": { Type: schema.TypeString, - Computed: true, ConflictsWith: []string{"router_table_id"}, + Computed: true, }, "router_id": { Type: schema.TypeString, @@ -104,6 +117,10 @@ func resourceAliCloudVpcVpc() *schema.Resource { Deprecated: "Field 'secondary_cidr_blocks' has been deprecated from provider version 1.185.0. Field 'secondary_cidr_blocks' has been deprecated from provider version 1.185.0 and it will be removed in the future version. Please use the new resource 'alicloud_vpc_ipv4_cidr_block'. `secondary_cidr_blocks` attributes and `alicloud_vpc_ipv4_cidr_block` resource cannot be used at the same time.", Elem: &schema.Schema{Type: schema.TypeString}, }, + "secondary_cidr_mask": { + Type: schema.TypeInt, + Optional: true, + }, "status": { Type: schema.TypeString, Computed: true, @@ -131,8 +148,8 @@ func resourceAliCloudVpcVpc() *schema.Resource { "vpc_name": { Type: schema.TypeString, Optional: true, - Computed: true, ConflictsWith: []string{"name"}, + Computed: true, }, "name": { Type: schema.TypeString, @@ -160,7 +177,7 @@ func resourceAliCloudVpcVpcCreate(d *schema.ResourceData, meta interface{}) erro var err error query := make(map[string]interface{}) request = make(map[string]interface{}) - query["RegionId"] = client.RegionId + request["RegionId"] = client.RegionId request["ClientToken"] = buildClientToken(action) if v, ok := d.GetOk("cidr_block"); ok { @@ -169,7 +186,9 @@ func resourceAliCloudVpcVpcCreate(d *schema.ResourceData, meta interface{}) erro if v, ok := d.GetOk("name"); ok || d.HasChange("name") { request["VpcName"] = v } - + if v, ok := d.GetOk("ipv6_cidr_block"); ok { + request["Ipv6CidrBlock"] = v + } if v, ok := d.GetOk("vpc_name"); ok { request["VpcName"] = v } @@ -202,6 +221,14 @@ func resourceAliCloudVpcVpcCreate(d *schema.ResourceData, meta interface{}) erro if v, ok := d.GetOkExists("dry_run"); ok { request["DryRun"] = v } + if v, ok := d.GetOk("dns_hostname_status"); ok { + request["EnableDnsHostname"] = convertVpcVpcEnableDnsHostnameRequest(v.(string)) + } + if v, ok := d.GetOkExists("ipv4_cidr_mask"); ok { + request["Ipv4CidrMask"] = v + } + runtime := util.RuntimeOptions{} + runtime.SetAutoretry(true) wait := incrementalWait(3*time.Second, 5*time.Second) err = resource.Retry(d.Timeout(schema.TimeoutCreate), func() *resource.RetryError { response, err = client.RpcPost("vpc", "2016-04-28", action, query, request, true) @@ -212,9 +239,9 @@ func resourceAliCloudVpcVpcCreate(d *schema.ResourceData, meta interface{}) erro } return resource.NonRetryableError(err) } - addDebug(action, response, request) return nil }) + addDebug(action, response, request) if err != nil { return WrapErrorf(err, DefaultErrorMsg, "alicloud_vpc", action, AlibabaCloudSdkGoERROR) @@ -238,7 +265,7 @@ func resourceAliCloudVpcVpcCreate(d *schema.ResourceData, meta interface{}) erro var err error query := make(map[string]interface{}) request = make(map[string]interface{}) - query["RegionId"] = client.RegionId + request["RegionId"] = client.RegionId request["ClientToken"] = buildClientToken(action) if v, ok := d.GetOk("ipv6_cidr_block"); ok { @@ -262,9 +289,9 @@ func resourceAliCloudVpcVpcCreate(d *schema.ResourceData, meta interface{}) erro } return resource.NonRetryableError(err) } - addDebug(action, response, request) return nil }) + addDebug(action, response, request) if err != nil { return WrapErrorf(err, DefaultErrorMsg, "alicloud_vpc", action, AlibabaCloudSdkGoERROR) @@ -294,21 +321,25 @@ func resourceAliCloudVpcVpcRead(d *schema.ResourceData, meta interface{}) error if objectRaw["CidrBlock"] != nil { d.Set("cidr_block", objectRaw["CidrBlock"]) } - if objectRaw["ClassicLinkEnabled"] != nil { - d.Set("classic_link_enabled", objectRaw["ClassicLinkEnabled"]) - } + d.Set("classic_link_enabled", objectRaw["ClassicLinkEnabled"]) if objectRaw["CreationTime"] != nil { d.Set("create_time", objectRaw["CreationTime"]) } if objectRaw["Description"] != nil { d.Set("description", objectRaw["Description"]) } + if objectRaw["DnsHostnameStatus"] != nil { + d.Set("dns_hostname_status", objectRaw["DnsHostnameStatus"]) + } if objectRaw["EnabledIpv6"] != nil { d.Set("enable_ipv6", objectRaw["EnabledIpv6"]) } if objectRaw["Ipv6CidrBlock"] != nil { d.Set("ipv6_cidr_block", objectRaw["Ipv6CidrBlock"]) } + if objectRaw["RegionId"] != nil { + d.Set("region_id", objectRaw["RegionId"]) + } if objectRaw["ResourceGroupId"] != nil { d.Set("resource_group_id", objectRaw["ResourceGroupId"]) } @@ -342,8 +373,8 @@ func resourceAliCloudVpcVpcRead(d *schema.ResourceData, meta interface{}) error userCidr1Raw, _ := jsonpath.Get("$.UserCidrs.UserCidr", objectRaw) d.Set("user_cidrs", userCidr1Raw) - objectRaw, err = vpcServiceV2.DescribeDescribeRouteTableList(d.Id()) - if err != nil { + objectRaw, err = vpcServiceV2.DescribeVpcDescribeRouteTableList(d.Id()) + if err != nil && !NotFoundError(err) { return WrapError(err) } @@ -375,6 +406,81 @@ func resourceAliCloudVpcVpcUpdate(d *schema.ResourceData, meta interface{}) erro var query map[string]interface{} update := false d.Partial(true) + + if d.HasChange("classic_link_enabled") { + vpcServiceV2 := VpcServiceV2{client} + object, err := vpcServiceV2.DescribeVpcVpc(d.Id()) + if err != nil { + return WrapError(err) + } + + target := d.Get("classic_link_enabled").(bool) + if _, ok := object["ClassicLinkEnabled"]; ok && object["ClassicLinkEnabled"].(bool) != target { + if target == true { + action := "EnableVpcClassicLink" + conn, err := client.NewVpcClient() + if err != nil { + return WrapError(err) + } + request = make(map[string]interface{}) + query = make(map[string]interface{}) + request["VpcId"] = d.Id() + request["RegionId"] = client.RegionId + request["ClientToken"] = buildClientToken(action) + runtime := util.RuntimeOptions{} + runtime.SetAutoretry(true) + wait := incrementalWait(3*time.Second, 5*time.Second) + err = resource.Retry(d.Timeout(schema.TimeoutUpdate), func() *resource.RetryError { + response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2016-04-28"), StringPointer("AK"), query, request, &runtime) + if err != nil { + if IsExpectedErrors(err, []string{"IncorrectVpcStatus"}) || NeedRetry(err) { + wait() + return resource.RetryableError(err) + } + return resource.NonRetryableError(err) + } + return nil + }) + addDebug(action, response, request) + if err != nil { + return WrapErrorf(err, DefaultErrorMsg, d.Id(), action, AlibabaCloudSdkGoERROR) + } + + } + if target == false { + action := "DisableVpcClassicLink" + conn, err := client.NewVpcClient() + if err != nil { + return WrapError(err) + } + request = make(map[string]interface{}) + query = make(map[string]interface{}) + request["VpcId"] = d.Id() + request["RegionId"] = client.RegionId + request["ClientToken"] = buildClientToken(action) + runtime := util.RuntimeOptions{} + runtime.SetAutoretry(true) + wait := incrementalWait(3*time.Second, 5*time.Second) + err = resource.Retry(d.Timeout(schema.TimeoutUpdate), func() *resource.RetryError { + response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2016-04-28"), StringPointer("AK"), query, request, &runtime) + if err != nil { + if IsExpectedErrors(err, []string{"InternalError", "IncorrectVpcStatus"}) || NeedRetry(err) { + wait() + return resource.RetryableError(err) + } + return resource.NonRetryableError(err) + } + return nil + }) + addDebug(action, response, request) + if err != nil { + return WrapErrorf(err, DefaultErrorMsg, d.Id(), action, AlibabaCloudSdkGoERROR) + } + + } + } + } + action := "ModifyVpcAttribute" conn, err := client.NewVpcClient() if err != nil { @@ -382,8 +488,8 @@ func resourceAliCloudVpcVpcUpdate(d *schema.ResourceData, meta interface{}) erro } request = make(map[string]interface{}) query = make(map[string]interface{}) - query["VpcId"] = d.Id() - query["RegionId"] = client.RegionId + request["VpcId"] = d.Id() + request["RegionId"] = client.RegionId if !d.IsNewResource() && d.HasChange("description") { update = true request["Description"] = d.Get("description") @@ -412,6 +518,11 @@ func resourceAliCloudVpcVpcUpdate(d *schema.ResourceData, meta interface{}) erro if v, ok := d.GetOk("ipv6_isp"); ok { request["Ipv6Isp"] = v } + if !d.IsNewResource() && d.HasChange("dns_hostname_status") { + update = true + request["EnableDnsHostname"] = convertVpcVpcEnableDnsHostnameRequest(d.Get("dns_hostname_status").(string)) + } + if update { runtime := util.RuntimeOptions{} runtime.SetAutoretry(true) @@ -425,9 +536,9 @@ func resourceAliCloudVpcVpcUpdate(d *schema.ResourceData, meta interface{}) erro } return resource.NonRetryableError(err) } - addDebug(action, response, request) return nil }) + addDebug(action, response, request) if err != nil { return WrapErrorf(err, DefaultErrorMsg, d.Id(), action, AlibabaCloudSdkGoERROR) } @@ -436,6 +547,13 @@ func resourceAliCloudVpcVpcUpdate(d *schema.ResourceData, meta interface{}) erro if _, err := stateConf.WaitForState(); err != nil { return WrapErrorf(err, IdMsg, d.Id()) } + + if d.HasChange("dns_hostname_status") { + stateConf := BuildStateConf([]string{}, []string{fmt.Sprint(d.Get("dns_hostname_status"))}, d.Timeout(schema.TimeoutUpdate), 5*time.Second, vpcServiceV2.VpcVpcStateRefreshFunc(d.Id(), "DnsHostnameStatus", []string{})) + if _, err := stateConf.WaitForState(); err != nil { + return WrapErrorf(err, IdMsg, d.Id()) + } + } } update = false action = "MoveResourceGroup" @@ -445,13 +563,12 @@ func resourceAliCloudVpcVpcUpdate(d *schema.ResourceData, meta interface{}) erro } request = make(map[string]interface{}) query = make(map[string]interface{}) - query["ResourceId"] = d.Id() - query["RegionId"] = client.RegionId + request["ResourceId"] = d.Id() + request["RegionId"] = client.RegionId if _, ok := d.GetOk("resource_group_id"); ok && !d.IsNewResource() && d.HasChange("resource_group_id") { update = true - request["NewResourceGroupId"] = d.Get("resource_group_id") } - + request["NewResourceGroupId"] = d.Get("resource_group_id") request["ResourceType"] = "VPC" if update { runtime := util.RuntimeOptions{} @@ -466,9 +583,9 @@ func resourceAliCloudVpcVpcUpdate(d *schema.ResourceData, meta interface{}) erro } return resource.NonRetryableError(err) } - addDebug(action, response, request) return nil }) + addDebug(action, response, request) if err != nil { return WrapErrorf(err, DefaultErrorMsg, d.Id(), action, AlibabaCloudSdkGoERROR) } @@ -484,7 +601,7 @@ func resourceAliCloudVpcVpcUpdate(d *schema.ResourceData, meta interface{}) erro query["RegionId"] = client.RegionId vpcServiceV2 := VpcServiceV2{client} - objectRaw, err := vpcServiceV2.DescribeDescribeRouteTableList(d.Id()) + objectRaw, err := vpcServiceV2.DescribeVpcDescribeRouteTableList(d.Id()) if err != nil { return WrapError(err) } @@ -525,89 +642,14 @@ func resourceAliCloudVpcVpcUpdate(d *schema.ResourceData, meta interface{}) erro } return resource.NonRetryableError(err) } - addDebug(action, response, request) return nil }) + addDebug(action, response, request) if err != nil { return WrapErrorf(err, DefaultErrorMsg, d.Id(), action, AlibabaCloudSdkGoERROR) } } - if d.HasChange("classic_link_enabled") { - client := meta.(*connectivity.AliyunClient) - vpcServiceV2 := VpcServiceV2{client} - object, err := vpcServiceV2.DescribeVpcVpc(d.Id()) - if err != nil { - return WrapError(err) - } - - target := d.Get("classic_link_enabled").(bool) - if object["ClassicLinkEnabled"].(bool) != target { - if target == true { - action = "EnableVpcClassicLink" - conn, err = client.NewVpcClient() - if err != nil { - return WrapError(err) - } - request = make(map[string]interface{}) - query = make(map[string]interface{}) - query["VpcId"] = d.Id() - query["RegionId"] = client.RegionId - request["ClientToken"] = buildClientToken(action) - runtime := util.RuntimeOptions{} - runtime.SetAutoretry(true) - wait := incrementalWait(3*time.Second, 5*time.Second) - err = resource.Retry(d.Timeout(schema.TimeoutUpdate), func() *resource.RetryError { - response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2016-04-28"), StringPointer("AK"), query, request, &runtime) - if err != nil { - if IsExpectedErrors(err, []string{"IncorrectVpcStatus"}) || NeedRetry(err) { - wait() - return resource.RetryableError(err) - } - return resource.NonRetryableError(err) - } - addDebug(action, response, request) - return nil - }) - if err != nil { - return WrapErrorf(err, DefaultErrorMsg, d.Id(), action, AlibabaCloudSdkGoERROR) - } - - } - if target == false { - action = "DisableVpcClassicLink" - conn, err = client.NewVpcClient() - if err != nil { - return WrapError(err) - } - request = make(map[string]interface{}) - query = make(map[string]interface{}) - query["VpcId"] = d.Id() - query["RegionId"] = client.RegionId - request["ClientToken"] = buildClientToken(action) - runtime := util.RuntimeOptions{} - runtime.SetAutoretry(true) - wait := incrementalWait(3*time.Second, 5*time.Second) - err = resource.Retry(d.Timeout(schema.TimeoutUpdate), func() *resource.RetryError { - response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2016-04-28"), StringPointer("AK"), query, request, &runtime) - if err != nil { - if IsExpectedErrors(err, []string{"InternalError", "IncorrectVpcStatus"}) || NeedRetry(err) { - wait() - return resource.RetryableError(err) - } - return resource.NonRetryableError(err) - } - addDebug(action, response, request) - return nil - }) - if err != nil { - return WrapErrorf(err, DefaultErrorMsg, d.Id(), action, AlibabaCloudSdkGoERROR) - } - - } - } - } - if d.HasChange("secondary_cidr_blocks") { oldEntry, newEntry := d.GetChange("secondary_cidr_blocks") removed := oldEntry @@ -624,8 +666,8 @@ func resourceAliCloudVpcVpcUpdate(d *schema.ResourceData, meta interface{}) erro } request = make(map[string]interface{}) query = make(map[string]interface{}) - query["VpcId"] = d.Id() - query["RegionId"] = client.RegionId + request["VpcId"] = d.Id() + request["RegionId"] = client.RegionId if v, ok := item.(string); ok { jsonPathResult, err := jsonpath.Get("$", v) if err != nil { @@ -645,9 +687,9 @@ func resourceAliCloudVpcVpcUpdate(d *schema.ResourceData, meta interface{}) erro } return resource.NonRetryableError(err) } - addDebug(action, response, request) return nil }) + addDebug(action, response, request) if err != nil { return WrapErrorf(err, DefaultErrorMsg, d.Id(), action, AlibabaCloudSdkGoERROR) } @@ -671,8 +713,8 @@ func resourceAliCloudVpcVpcUpdate(d *schema.ResourceData, meta interface{}) erro } request = make(map[string]interface{}) query = make(map[string]interface{}) - query["VpcId"] = d.Id() - query["RegionId"] = client.RegionId + request["VpcId"] = d.Id() + request["RegionId"] = client.RegionId if v, ok := item.(string); ok { jsonPathResult, err := jsonpath.Get("$", v) if err != nil { @@ -685,6 +727,9 @@ func resourceAliCloudVpcVpcUpdate(d *schema.ResourceData, meta interface{}) erro _request := make(map[string]interface{}) _request["IpamPoolId"] = v } + if v, ok := d.GetOkExists("secondary_cidr_mask"); ok { + request["SecondaryCidrMask"] = v + } runtime := util.RuntimeOptions{} runtime.SetAutoretry(true) wait := incrementalWait(3*time.Second, 5*time.Second) @@ -697,9 +742,9 @@ func resourceAliCloudVpcVpcUpdate(d *schema.ResourceData, meta interface{}) erro } return resource.NonRetryableError(err) } - addDebug(action, response, request) return nil }) + addDebug(action, response, request) if err != nil { return WrapErrorf(err, DefaultErrorMsg, d.Id(), action, AlibabaCloudSdkGoERROR) } @@ -734,8 +779,9 @@ func resourceAliCloudVpcVpcDelete(d *schema.ResourceData, meta interface{}) erro return WrapError(err) } request = make(map[string]interface{}) - query["VpcId"] = d.Id() - query["RegionId"] = client.RegionId + request["VpcId"] = d.Id() + request["RegionId"] = client.RegionId + request["ClientToken"] = buildClientToken(action) if v, ok := d.GetOkExists("dry_run"); ok { request["DryRun"] = v @@ -747,18 +793,18 @@ func resourceAliCloudVpcVpcDelete(d *schema.ResourceData, meta interface{}) erro response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2016-04-28"), StringPointer("AK"), query, request, &runtime) if err != nil { - if NeedRetry(err) || IsExpectedErrors(err, []string{"DependencyViolation.VSwitch", "DependencyViolation.SecurityGroup", "IncorrectVpcStatus"}) { + if IsExpectedErrors(err, []string{"DependencyViolation.VSwitch", "DependencyViolation.SecurityGroup", "IncorrectVpcStatus"}) || NeedRetry(err) { wait() return resource.RetryableError(err) } return resource.NonRetryableError(err) } - addDebug(action, response, request) return nil }) + addDebug(action, response, request) if err != nil { - if IsExpectedErrors(err, []string{"InvalidResource.NotFound"}) { + if IsExpectedErrors(err, []string{"InvalidResource.NotFound"}) || NotFoundError(err) { return nil } return WrapErrorf(err, DefaultErrorMsg, d.Id(), action, AlibabaCloudSdkGoERROR) @@ -772,3 +818,14 @@ func resourceAliCloudVpcVpcDelete(d *schema.ResourceData, meta interface{}) erro return nil } + +func convertVpcVpcEnableDnsHostnameRequest(source interface{}) interface{} { + source = fmt.Sprint(source) + switch source { + case "ENABLED": + return "true" + case "DISABLED": + return "false" + } + return source +} diff --git a/alicloud/resource_alicloud_vpc_test.go b/alicloud/resource_alicloud_vpc_test.go index a16df379977f..447c5a8f5abd 100644 --- a/alicloud/resource_alicloud_vpc_test.go +++ b/alicloud/resource_alicloud_vpc_test.go @@ -542,6 +542,7 @@ func TestAccAliCloudVPC_basic2(t *testing.T) { "resource_group_id": "${data.alicloud_resource_manager_resource_groups.default.groups.0.id}", "dry_run": "false", "user_cidrs": []string{"106.11.62.0/24"}, + // "ipv6_cidr_block": "240b:4000:95:5a00::/56", }), Check: resource.ComposeTestCheckFunc( testAccCheck(map[string]string{ @@ -1145,8 +1146,6 @@ func TestUnitAlicloudVPCdsafa(t *testing.T) { }) } -// Test Vpc Vpc. >>> Resource test cases, automatically generated. -// Case 3113 func TestAccAliCloudVpcVpc_basic3113(t *testing.T) { var v map[string]interface{} resourceId := "alicloud_vpc.default" @@ -1169,21 +1168,25 @@ func TestAccAliCloudVpcVpc_basic3113(t *testing.T) { Steps: []resource.TestStep{ { Config: testAccConfig(map[string]interface{}{ - "vpc_name": name, + "vpc_name": name, + "dns_hostname_status": "ENABLED", }), Check: resource.ComposeTestCheckFunc( testAccCheck(map[string]string{ - "vpc_name": name, + "vpc_name": name, + "dns_hostname_status": "ENABLED", }), ), }, { Config: testAccConfig(map[string]interface{}{ - "description": "test", + "description": "test", + "dns_hostname_status": "DISABLED", }), Check: resource.ComposeTestCheckFunc( testAccCheck(map[string]string{ - "description": "test", + "description": "test", + "dns_hostname_status": "DISABLED", }), ), }, @@ -1229,11 +1232,11 @@ func TestAccAliCloudVpcVpc_basic3113(t *testing.T) { }, { Config: testAccConfig(map[string]interface{}{ - "classic_link_enabled": "true", + "classic_link_enabled": "false", }), Check: resource.ComposeTestCheckFunc( testAccCheck(map[string]string{ - "classic_link_enabled": "true", + "classic_link_enabled": "false", }), ), }, @@ -1363,7 +1366,7 @@ func TestAccAliCloudVpcVpc_basic3113_twin(t *testing.T) { "description": "test-update", "cidr_block": "172.16.0.0/12", "vpc_name": name, - "classic_link_enabled": "true", + "classic_link_enabled": "false", "secondary_cidr_blocks": []string{ "192.168.0.0/16"}, "tags": map[string]string{ @@ -1377,7 +1380,7 @@ func TestAccAliCloudVpcVpc_basic3113_twin(t *testing.T) { "description": "test-update", "cidr_block": "172.16.0.0/12", "vpc_name": name, - "classic_link_enabled": "true", + "classic_link_enabled": "false", "secondary_cidr_blocks.#": "1", "tags.%": "2", "tags.Created": "TF", @@ -1395,4 +1398,134 @@ func TestAccAliCloudVpcVpc_basic3113_twin(t *testing.T) { }) } +// Test Vpc Vpc. >>> Resource test cases, automatically generated. +// Case 从IPAM地址池创建VPC及添加附加网段 9656 +func TestAccAliCloudVpcVpc_basic9656(t *testing.T) { + var v map[string]interface{} + resourceId := "alicloud_vpc.default" + ra := resourceAttrInit(resourceId, AlicloudVpcVpcMap9656) + rc := resourceCheckInitWithDescribeMethod(resourceId, &v, func() interface{} { + return &VpcServiceV2{testAccProvider.Meta().(*connectivity.AliyunClient)} + }, "DescribeVpcVpc") + rac := resourceAttrCheckInit(rc, ra) + testAccCheck := rac.resourceAttrMapUpdateSet() + rand := acctest.RandIntRange(10000, 99999) + name := fmt.Sprintf("tf-testacc%svpcvpc%d", defaultRegionToTest, rand) + testAccConfig := resourceTestAccConfigFunc(resourceId, name, AlicloudVpcVpcBasicDependence9656) + resource.Test(t, resource.TestCase{ + PreCheck: func() { + testAccPreCheckWithRegions(t, true, []connectivity.Region{"cn-hangzhou"}) + testAccPreCheck(t) + }, + IDRefreshName: resourceId, + Providers: testAccProviders, + CheckDestroy: rac.checkResourceDestroy(), + Steps: []resource.TestStep{ + { + Config: testAccConfig(map[string]interface{}{ + "ipv4_cidr_mask": "12", + "ipv4_ipam_pool_id": "${alicloud_vpc_ipam_ipam_pool_cidr.defaultIpamPoolCidr.ipam_pool_id}", + }), + Check: resource.ComposeTestCheckFunc( + testAccCheck(map[string]string{ + "ipv4_cidr_mask": "12", + "ipv4_ipam_pool_id": CHECKSET, + }), + ), + }, + { + Config: testAccConfig(map[string]interface{}{ + "secondary_cidr_mask": "24", + "description": "desc", + }), + Check: resource.ComposeTestCheckFunc( + testAccCheck(map[string]string{ + "secondary_cidr_mask": "24", + "description": "desc", + }), + ), + }, + { + Config: testAccConfig(map[string]interface{}{ + "tags": map[string]string{ + "Created": "TF", + "For": "Test", + }, + }), + Check: resource.ComposeTestCheckFunc( + testAccCheck(map[string]string{ + "tags.%": "2", + "tags.Created": "TF", + "tags.For": "Test", + }), + ), + }, + { + Config: testAccConfig(map[string]interface{}{ + "tags": map[string]string{ + "Created": "TF-update", + "For": "Test-update", + }, + }), + Check: resource.ComposeTestCheckFunc( + testAccCheck(map[string]string{ + "tags.%": "2", + "tags.Created": "TF-update", + "tags.For": "Test-update", + }), + ), + }, + { + Config: testAccConfig(map[string]interface{}{ + "tags": REMOVEKEY, + }), + Check: resource.ComposeTestCheckFunc( + testAccCheck(map[string]string{ + "tags.%": "0", + "tags.Created": REMOVEKEY, + "tags.For": REMOVEKEY, + }), + ), + }, + { + ResourceName: resourceId, + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"dry_run", "ipv4_cidr_mask", "ipv4_ipam_pool_id", "ipv6_isp", "secondary_cidr_mask"}, + }, + }, + }) +} + +var AlicloudVpcVpcMap9656 = map[string]string{ + "router_id": CHECKSET, + "status": CHECKSET, + "create_time": CHECKSET, + "region_id": CHECKSET, +} + +func AlicloudVpcVpcBasicDependence9656(name string) string { + return fmt.Sprintf(` +variable "name" { + default = "%s" +} + +resource "alicloud_vpc_ipam_ipam" "defaultIpam" { + operating_region_list = ["cn-hangzhou"] +} + +resource "alicloud_vpc_ipam_ipam_pool" "defaultIpamPool" { + ipam_scope_id = alicloud_vpc_ipam_ipam.defaultIpam.private_default_scope_id + pool_region_id = "cn-hangzhou" +} + +resource "alicloud_vpc_ipam_ipam_pool_cidr" "defaultIpamPoolCidr" { + cidr = "10.0.0.0/8" + ipam_pool_id = alicloud_vpc_ipam_ipam_pool.defaultIpamPool.id +} + + +`, name) +} + // Test Vpc Vpc. <<< Resource test cases, automatically generated. diff --git a/alicloud/service_alicloud_vpc_v2.go b/alicloud/service_alicloud_vpc_v2.go index f3fcfc4c2766..249e8d2ac21c 100644 --- a/alicloud/service_alicloud_vpc_v2.go +++ b/alicloud/service_alicloud_vpc_v2.go @@ -120,12 +120,12 @@ func (s *VpcServiceV2) SetResourceTags(d *schema.ResourceData, resourceType stri request = make(map[string]interface{}) query = make(map[string]interface{}) request["ResourceId.1"] = d.Id() - query["RegionId"] = client.RegionId + request["RegionId"] = client.RegionId + request["ResourceType"] = resourceType for i, key := range removedTagKeys { request[fmt.Sprintf("TagKey.%d", i+1)] = key } - request["ResourceType"] = resourceType runtime := util.RuntimeOptions{} runtime.SetAutoretry(true) wait := incrementalWait(3*time.Second, 5*time.Second) @@ -138,9 +138,9 @@ func (s *VpcServiceV2) SetResourceTags(d *schema.ResourceData, resourceType stri } return resource.NonRetryableError(err) } - addDebug(action, response, request) return nil }) + addDebug(action, response, request) if err != nil { return WrapErrorf(err, DefaultErrorMsg, d.Id(), action, AlibabaCloudSdkGoERROR) } @@ -156,7 +156,8 @@ func (s *VpcServiceV2) SetResourceTags(d *schema.ResourceData, resourceType stri request = make(map[string]interface{}) query = make(map[string]interface{}) request["ResourceId.1"] = d.Id() - query["RegionId"] = client.RegionId + request["RegionId"] = client.RegionId + request["ResourceType"] = resourceType count := 1 for key, value := range added { request[fmt.Sprintf("Tag.%d.Key", count)] = key @@ -164,7 +165,6 @@ func (s *VpcServiceV2) SetResourceTags(d *schema.ResourceData, resourceType stri count++ } - request["ResourceType"] = resourceType runtime := util.RuntimeOptions{} runtime.SetAutoretry(true) wait := incrementalWait(3*time.Second, 5*time.Second) @@ -177,9 +177,9 @@ func (s *VpcServiceV2) SetResourceTags(d *schema.ResourceData, resourceType stri } return resource.NonRetryableError(err) } - addDebug(action, response, request) return nil }) + addDebug(action, response, request) if err != nil { return WrapErrorf(err, DefaultErrorMsg, d.Id(), action, AlibabaCloudSdkGoERROR) } @@ -890,11 +890,10 @@ func (s *VpcServiceV2) DescribeVpcVpc(id string) (object map[string]interface{}, } return resource.NonRetryableError(err) } - addDebug(action, response, request) return nil }) + addDebug(action, response, request) if err != nil { - addDebug(action, response, request) return object, WrapErrorf(err, DefaultErrorMsg, id, action, AlibabaCloudSdkGoERROR) } @@ -905,7 +904,7 @@ func (s *VpcServiceV2) DescribeVpcVpc(id string) (object map[string]interface{}, return response, nil } -func (s *VpcServiceV2) DescribeDescribeRouteTableList(id string) (object map[string]interface{}, err error) { +func (s *VpcServiceV2) DescribeVpcDescribeRouteTableList(id string) (object map[string]interface{}, err error) { client := s.client var request map[string]interface{} var response map[string]interface{} @@ -926,11 +925,10 @@ func (s *VpcServiceV2) DescribeDescribeRouteTableList(id string) (object map[str } return resource.NonRetryableError(err) } - addDebug(action, response, request) return nil }) + addDebug(action, response, request) if err != nil { - addDebug(action, response, request) return object, WrapErrorf(err, DefaultErrorMsg, id, action, AlibabaCloudSdkGoERROR) } @@ -946,57 +944,15 @@ func (s *VpcServiceV2) DescribeDescribeRouteTableList(id string) (object map[str result, _ := v.([]interface{}) for _, v := range result { item := v.(map[string]interface{}) - if item["RouteTableType"] != "System" { + if fmt.Sprint(item["RouteTableType"]) != "System" { continue } - if item["VpcId"] != id { + if fmt.Sprint(item["VpcId"]) != id { continue } return item, nil } - - return v.([]interface{})[0].(map[string]interface{}), nil -} -func (s *VpcServiceV2) DescribeListTagResources(id string) (object map[string]interface{}, err error) { - - client := s.client - var request map[string]interface{} - var response map[string]interface{} - var query map[string]interface{} - action := "ListTagResources" - conn, err := client.NewVpcClient() - if err != nil { - return object, WrapError(err) - } - request = make(map[string]interface{}) - query = make(map[string]interface{}) - - request["ResourceId.1"] = id - request["RegionId"] = client.RegionId - - request["ResourceType"] = "VPC" - wait := incrementalWait(3*time.Second, 5*time.Second) - err = resource.Retry(1*time.Minute, func() *resource.RetryError { - response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2016-04-28"), StringPointer("AK"), query, request, &util.RuntimeOptions{}) - - if err != nil { - if NeedRetry(err) { - wait() - return resource.RetryableError(err) - } - return resource.NonRetryableError(err) - } - addDebug(action, response, request) - return nil - }) - if err != nil { - if IsExpectedErrors(err, []string{}) { - return object, WrapErrorf(Error(GetNotFoundMessage("Vpc", id)), NotFoundMsg, ProviderERROR, fmt.Sprint(response["RequestId"])) - } - return object, WrapErrorf(err, DefaultErrorMsg, id, action, AlibabaCloudSdkGoERROR) - } - - return response, nil + return object, WrapErrorf(Error(GetNotFoundMessage("Vpc", id)), NotFoundMsg, response) } func (s *VpcServiceV2) VpcVpcStateRefreshFunc(id string, field string, failStates []string) resource.StateRefreshFunc { @@ -1012,6 +968,13 @@ func (s *VpcServiceV2) VpcVpcStateRefreshFunc(id string, field string, failState v, err := jsonpath.Get(field, object) currentStatus := fmt.Sprint(v) + if strings.HasPrefix(field, "#") { + v, _ := jsonpath.Get(strings.TrimPrefix(field, "#"), object) + if v != nil { + currentStatus = "#CHECKSET" + } + } + for _, failState := range failStates { if currentStatus == failState { return object, currentStatus, WrapError(Error(FailedToReachTargetStatus, currentStatus)) diff --git a/website/docs/r/vpc.html.markdown b/website/docs/r/vpc.html.markdown index 9800dd9a2b7d..3e4fa07af5e9 100644 --- a/website/docs/r/vpc.html.markdown +++ b/website/docs/r/vpc.html.markdown @@ -3,12 +3,12 @@ subcategory: "VPC" layout: "alicloud" page_title: "Alicloud: alicloud_vpc" description: |- - Provides a Alicloud VPC Vpc resource. + Provides a Alicloud VPC VPC resource. --- # alicloud_vpc -Provides a VPC Vpc resource. +Provides a VPC VPC resource. A VPC instance creates a VPC. You can fully control your own VPC, such as selecting IP address ranges, configuring routing tables, and gateways. You can use Alibaba cloud resources such as cloud servers, apsaradb for RDS, and load balancer in your own VPC. @@ -27,12 +27,6 @@ For information about VPC Vpc and how to use it, see [What is Vpc](https://www.a Basic Usage -
- ```terraform variable "name" { default = "terraform-example" @@ -58,16 +52,22 @@ The following arguments are supported: * `cidr_block` - (Optional, Computed) The CIDR block of the VPC. - You can specify one of the following CIDR blocks or their subsets as the primary IPv4 CIDR block of the VPC: 192.168.0.0/16, 172.16.0.0/12, and 10.0.0.0/8. These CIDR blocks are standard private CIDR blocks as defined by Request for Comments (RFC) documents. The subnet mask must be 8 to 28 bits in length. - You can also use a custom CIDR block other than 100.64.0.0/10, 224.0.0.0/4, 127.0.0.0/8, 169.254.0.0/16, and their subnets as the primary IPv4 CIDR block of the VPC. - * `classic_link_enabled` - (Optional) The status of ClassicLink function. -* `description` - (Optional) The new description of the VPC. The description must be 1 to 256 characters in length, and cannot start with `http://` or `https://`. -* `dry_run` - (Optional) Specifies whether to perform a dry run. Valid values: +* `description` - (Optional) The new description of the VPC. + + The description must be 1 to 256 characters in length, and cannot start with `http://` or `https://`. +* `dns_hostname_status` - (Optional, Available since v1.240.0) The status of VPC DNS Hostname. Valid values: `ENABLED`, `DISABLED`. +* `dry_run` - (Optional, Available since v1.119.0) Specifies whether to perform a dry run. Valid values: - `true`: performs a dry run. The system checks the required parameters, request syntax, and limits. If the request fails the dry run, an error message is returned. If the request passes the dry run, the `DryRunOperation` error code is returned. - `false` (default): performs a dry run and sends the request. If the request passes the dry run, a 2xx HTTP status code is returned and the operation is performed. -* `enable_ipv6` - (Optional) Specifies whether to enable IPv6. Valid values: +* `enable_ipv6` - (Optional, Available since v1.119.0) Specifies whether to enable IPv6. Valid values: - `false` (default) - `true` -* `ipv4_ipam_pool_id` - (Optional) The ID of the IP Address Manager (IPAM) pool that contains IPv4 addresses. +* `ipv4_cidr_mask` - (Optional, Int, Available since v1.240.0) Allocate VPC from The IPAM address pool by entering a mask. + +-> **NOTE:** when you specify the IPAM address pool to create a VPC, enter at least one of the CidrBlock or Ipv4CidrMask parameters. + +* `ipv4_ipam_pool_id` - (Optional) The ID of the IP Address Manager (IPAM) pool that contains IPv4 addresses. * `ipv6_cidr_block` - (Optional, ForceNew, Computed) The IPv6 CIDR block of the default VPC. -> **NOTE:** When `EnableIpv6` is set to `true`, this parameter is required. @@ -79,17 +79,28 @@ The following arguments are supported: - `ChinaTelecom`: China Telecom (single line). -> **NOTE:** If a single-line bandwidth whitelist is enabled, this field can be set to `ChinaTelecom` (China Telecom), `ChinaUnicom` (China Unicom), or `ChinaMobile` (China Mobile). -* `resource_group_id` - (Optional, Computed) The ID of the resource group to which you want to move the resource. + +* `resource_group_id` - (Optional, Computed, Available since v1.115) The ID of the resource group to which you want to move the resource. -> **NOTE:** You can use resource groups to facilitate resource grouping and permission management for an Alibaba Cloud. For more information, see [What is resource management?](https://www.alibabacloud.com/help/en/doc-detail/94475.html) -* `route_table_id` - (Computed) The ID of the route table that you want to query. -* `secondary_cidr_blocks` - (Optional, Computed, Deprecated since v1.185.0) Field 'secondary_cidr_blocks' has been deprecated from provider version 1.185.0 and it will be removed in the future version. Please use the new resource 'alicloud_vpc_ipv4_cidr_block'. `secondary_cidr_blocks` attributes and `alicloud_vpc_ipv4_cidr_block` resource cannot be used at the same time. -* `system_route_table_description` - (Optional) The description of the route table. The description must be 1 to 256 characters in length, and cannot start with `http://` or `https://`. -* `system_route_table_name` - (Optional) The name of the route table. The name must be 1 to 128 characters in length and cannot start with `http://` or `https://`. -* `tags` - (Optional, Map) The tags of Vpc. -* `user_cidrs` - (Optional, ForceNew, Computed) A list of user CIDRs. -* `vpc_name` - (Optional) The new name of the VPC. The name must be 1 to 128 characters in length and cannot start with `http://` or `https://`. +* `route_table_id` - (Optional, ForceNew, Computed) The ID of the route table that you want to query. +* `secondary_cidr_blocks` - (Optional, Computed, List, Deprecated since v1.185.0) Field 'secondary_cidr_blocks' has been deprecated from provider version 1.185.0 and it will be removed in the future version. Please use the new resource 'alicloud_vpc_ipv4_cidr_block'. `secondary_cidr_blocks` attributes and `alicloud_vpc_ipv4_cidr_block` resource cannot be used at the same time. +* `secondary_cidr_mask` - (Optional, Int, Available since v1.240.0) Add an additional CIDR block from the IPAM address pool to the VPC by entering a mask. + +-> **NOTE:** Specify the IPAM address pool to add an additional CIDR block to the VPC. Enter at least one of the SecondaryCidrBlock or SecondaryCidrMask parameters. + +* `system_route_table_description` - (Optional) The description of the route table. + + The description must be 1 to 256 characters in length, and cannot start with `http://` or `https://`. +* `system_route_table_name` - (Optional) The name of the route table. + + The name must be 1 to 128 characters in length and cannot start with `http://` or `https://`. +* `tags` - (Optional, Map, Available since v1.55.3) The tags of Vpc. +* `user_cidrs` - (Optional, ForceNew, Computed, List, Available since v1.119.0) A list of user CIDRs. +* `vpc_name` - (Optional, Available since v1.119.0) The new name of the VPC. + + The name must be 1 to 128 characters in length and cannot start with `http://` or `https://`. The following arguments will be discarded. Please use new fields as soon as possible: * `name` - (Deprecated since v1.119.0). Field 'name' has been deprecated from provider version 1.119.0. New field 'vpc_name' instead. @@ -106,19 +117,20 @@ The following attributes are exported: - `ChinaMobile`: China Mobile (single line). - `ChinaUnicom`: China Unicom (single line). - `ChinaTelecom`: China Telecom (single line). -* `router_id` - The region ID of the VPC to which the route table belongs. You can call the [DescribeRegions](https://www.alibabacloud.com/help/en/doc-detail/36063.html) operation to query the most recent region list. -* `status` - The status of the VPC. `Pending`: The VPC is being configured. `Available`: The VPC is available. +* `region_id` - (Available since v1.240.0) The region ID of the VPC to which the route table belongs. +* `router_id` - The router ID of the VPC. +* `status` - The status of the VPC. ## Timeouts The `timeouts` block allows you to specify [timeouts](https://www.terraform.io/docs/configuration-0-11/resources.html#timeouts) for certain actions: -* `create` - (Defaults to 5 mins) Used when create the Vpc. -* `delete` - (Defaults to 5 mins) Used when delete the Vpc. -* `update` - (Defaults to 5 mins) Used when update the Vpc. +* `create` - (Defaults to 5 mins) Used when create the VPC. +* `delete` - (Defaults to 5 mins) Used when delete the VPC. +* `update` - (Defaults to 5 mins) Used when update the VPC. ## Import -VPC Vpc can be imported using the id, e.g. +VPC VPC can be imported using the id, e.g. ```shell $ terraform import alicloud_vpc.example