From e61f12d1bfb03c72ab34f73c404a271ce2f9aee8 Mon Sep 17 00:00:00 2001 From: MrWolong Date: Tue, 10 Dec 2024 11:18:40 +0800 Subject: [PATCH] resource/alicloud_cen_private_zone: Improved alicloud_cen_private_zone testcase --- alicloud/provider.go | 2 +- .../resource_alicloud_cen_private_zone.go | 77 ++++++++++++------- ...resource_alicloud_cen_private_zone_test.go | 66 +++++++++------- alicloud/service_alicloud_cbn.go | 15 ++-- website/docs/r/cen_private_zone.html.markdown | 62 +++++++++------ 5 files changed, 136 insertions(+), 86 deletions(-) diff --git a/alicloud/provider.go b/alicloud/provider.go index fd31342eed5a..e0a8c8b6e690 100644 --- a/alicloud/provider.go +++ b/alicloud/provider.go @@ -1312,7 +1312,7 @@ func Provider() terraform.ResourceProvider { "alicloud_resource_manager_resource_group": resourceAliCloudResourceManagerResourceGroup(), "alicloud_resource_manager_folder": resourceAlicloudResourceManagerFolder(), "alicloud_resource_manager_handshake": resourceAlicloudResourceManagerHandshake(), - "alicloud_cen_private_zone": resourceAlicloudCenPrivateZone(), + "alicloud_cen_private_zone": resourceAliCloudCenPrivateZone(), "alicloud_resource_manager_policy": resourceAlicloudResourceManagerPolicy(), "alicloud_resource_manager_account": resourceAlicloudResourceManagerAccount(), "alicloud_waf_instance": resourceAlicloudWafInstance(), diff --git a/alicloud/resource_alicloud_cen_private_zone.go b/alicloud/resource_alicloud_cen_private_zone.go index 3b9b12e99523..90e413f2bb07 100644 --- a/alicloud/resource_alicloud_cen_private_zone.go +++ b/alicloud/resource_alicloud_cen_private_zone.go @@ -1,6 +1,7 @@ package alicloud import ( + "fmt" "time" "github.com/aliyun/alibaba-cloud-sdk-go/services/cbn" @@ -9,34 +10,35 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/helper/schema" ) -func resourceAlicloudCenPrivateZone() *schema.Resource { +func resourceAliCloudCenPrivateZone() *schema.Resource { return &schema.Resource{ - Create: resourceAlicloudCenPrivateZoneCreate, - Read: resourceAlicloudCenPrivateZoneRead, - Delete: resourceAlicloudCenPrivateZoneDelete, + Create: resourceAliCloudCenPrivateZoneCreate, + Read: resourceAliCloudCenPrivateZoneRead, + Delete: resourceAliCloudCenPrivateZoneDelete, Importer: &schema.ResourceImporter{ State: schema.ImportStatePassthrough, }, Timeouts: &schema.ResourceTimeout{ Create: schema.DefaultTimeout(6 * time.Minute), + Delete: schema.DefaultTimeout(5 * time.Minute), }, Schema: map[string]*schema.Schema{ - "access_region_id": { + "cen_id": { Type: schema.TypeString, Required: true, ForceNew: true, }, - "cen_id": { + "access_region_id": { Type: schema.TypeString, Required: true, ForceNew: true, }, - "host_region_id": { + "host_vpc_id": { Type: schema.TypeString, Required: true, ForceNew: true, }, - "host_vpc_id": { + "host_region_id": { Type: schema.TypeString, Required: true, ForceNew: true, @@ -49,87 +51,108 @@ func resourceAlicloudCenPrivateZone() *schema.Resource { } } -func resourceAlicloudCenPrivateZoneCreate(d *schema.ResourceData, meta interface{}) error { +func resourceAliCloudCenPrivateZoneCreate(d *schema.ResourceData, meta interface{}) error { client := meta.(*connectivity.AliyunClient) cbnService := CbnService{client} request := cbn.CreateRoutePrivateZoneInCenToVpcRequest() - request.AccessRegionId = d.Get("access_region_id").(string) request.CenId = d.Get("cen_id").(string) - request.HostRegionId = d.Get("host_region_id").(string) + request.AccessRegionId = d.Get("access_region_id").(string) request.HostVpcId = d.Get("host_vpc_id").(string) + request.HostRegionId = d.Get("host_region_id").(string) + + var raw interface{} + var err error wait := incrementalWait(3*time.Second, 5*time.Second) - err := resource.Retry(d.Timeout(schema.TimeoutCreate), func() *resource.RetryError { - raw, err := client.WithCbnClient(func(cbnClient *cbn.Client) (interface{}, error) { + err = resource.Retry(client.GetRetryTimeout(d.Timeout(schema.TimeoutCreate)), func() *resource.RetryError { + raw, err = client.WithCbnClient(func(cbnClient *cbn.Client) (interface{}, error) { return cbnClient.RoutePrivateZoneInCenToVpc(request) }) if err != nil { - if IsExpectedErrors(err, []string{"Operation.Blocking", "InvalidOperation.CenInstanceStatus", "InvalidOperation.NoChildInstanceEitherRegion"}) { + if IsExpectedErrors(err, []string{"Operation.Blocking", "InvalidOperation.CenInstanceStatus", "InvalidOperation.NoChildInstanceEitherRegion"}) || NeedRetry(err) { wait() return resource.RetryableError(err) } return resource.NonRetryableError(err) } - addDebug(request.GetActionName(), raw) - d.SetId(d.Get("cen_id").(string) + ":" + d.Get("access_region_id").(string)) return nil }) + addDebug(request.GetActionName(), raw, request.RpcRequest, request) + if err != nil { return WrapErrorf(err, DefaultErrorMsg, "alicloud_cen_private_zone", request.GetActionName(), AlibabaCloudSdkGoERROR) } + + d.SetId(fmt.Sprintf("%v:%v", request.CenId, request.AccessRegionId)) + stateConf := BuildStateConf([]string{}, []string{"Active"}, d.Timeout(schema.TimeoutCreate), 5*time.Second, cbnService.CenPrivateZoneStateRefreshFunc(d.Id(), []string{})) if _, err := stateConf.WaitForState(); err != nil { return WrapErrorf(err, IdMsg, d.Id()) } - return resourceAlicloudCenPrivateZoneRead(d, meta) + return resourceAliCloudCenPrivateZoneRead(d, meta) } -func resourceAlicloudCenPrivateZoneRead(d *schema.ResourceData, meta interface{}) error { + +func resourceAliCloudCenPrivateZoneRead(d *schema.ResourceData, meta interface{}) error { client := meta.(*connectivity.AliyunClient) cbnService := CbnService{client} + object, err := cbnService.DescribeCenPrivateZone(d.Id()) if err != nil { - if NotFoundError(err) { + if !d.IsNewResource() && NotFoundError(err) { d.SetId("") return nil } return WrapError(err) } + parts, err := ParseResourceId(d.Id(), 2) if err != nil { return WrapError(err) } - d.Set("access_region_id", parts[1]) + d.Set("cen_id", parts[0]) - d.Set("host_region_id", object.HostRegionId) + d.Set("access_region_id", object.AccessRegionId) d.Set("host_vpc_id", object.HostVpcId) + d.Set("host_region_id", object.HostRegionId) d.Set("status", object.Status) + return nil } -func resourceAlicloudCenPrivateZoneDelete(d *schema.ResourceData, meta interface{}) error { + +func resourceAliCloudCenPrivateZoneDelete(d *schema.ResourceData, meta interface{}) error { client := meta.(*connectivity.AliyunClient) + parts, err := ParseResourceId(d.Id(), 2) if err != nil { return WrapError(err) } + request := cbn.CreateUnroutePrivateZoneInCenToVpcRequest() - request.AccessRegionId = parts[1] request.CenId = parts[0] - err = resource.Retry(300*time.Second, func() *resource.RetryError { - raw, err := client.WithCbnClient(func(cbnClient *cbn.Client) (interface{}, error) { + request.AccessRegionId = parts[1] + + var raw interface{} + err = resource.Retry(client.GetRetryTimeout(d.Timeout(schema.TimeoutDelete)), func() *resource.RetryError { + raw, err = client.WithCbnClient(func(cbnClient *cbn.Client) (interface{}, error) { return cbnClient.UnroutePrivateZoneInCenToVpc(request) }) if err != nil { - if IsExpectedErrors(err, []string{"Operation.Blocking", "InvalidOperation.CenInstanceStatus"}) { + if IsExpectedErrors(err, []string{"Operation.Blocking", "InvalidOperation.CenInstanceStatus"}) || NeedRetry(err) { return resource.RetryableError(err) } return resource.NonRetryableError(err) } - addDebug(request.GetActionName(), raw) return nil }) + addDebug(request.GetActionName(), raw, request.RpcRequest, request) + if err != nil { + if NotFoundError(err) { + return nil + } return WrapErrorf(err, DefaultErrorMsg, d.Id(), request.GetActionName(), AlibabaCloudSdkGoERROR) } + return nil } diff --git a/alicloud/resource_alicloud_cen_private_zone_test.go b/alicloud/resource_alicloud_cen_private_zone_test.go index 26e504d4fd7c..5e0f5a9f9a01 100644 --- a/alicloud/resource_alicloud_cen_private_zone_test.go +++ b/alicloud/resource_alicloud_cen_private_zone_test.go @@ -4,46 +4,45 @@ import ( "fmt" "testing" - "github.com/aliyun/alibaba-cloud-sdk-go/services/cbn" "github.com/aliyun/terraform-provider-alicloud/alicloud/connectivity" "github.com/hashicorp/terraform-plugin-sdk/helper/acctest" "github.com/hashicorp/terraform-plugin-sdk/helper/resource" ) -func TestAccAlicloudCenPrivateZone_basic(t *testing.T) { - var v cbn.PrivateZoneInfo +func TestAccAliCloudCenPrivateZone_basic0(t *testing.T) { + var v map[string]interface{} + checkoutSupportedRegions(t, true, connectivity.CenSupportRegions) resourceId := "alicloud_cen_private_zone.default" - ra := resourceAttrInit(resourceId, CenPrivateZoneMap) + ra := resourceAttrInit(resourceId, AliCloudCenPrivateZoneMap0) rc := resourceCheckInitWithDescribeMethod(resourceId, &v, func() interface{} { return &CbnService{testAccProvider.Meta().(*connectivity.AliyunClient)} }, "DescribeCenPrivateZone") rac := resourceAttrCheckInit(rc, ra) testAccCheck := rac.resourceAttrMapUpdateSet() - rand := acctest.RandIntRange(1000000, 9999999) + rand := acctest.RandIntRange(10000, 99999) name := fmt.Sprintf("tf-testAccCenPrivateZone%d", rand) - testAccConfig := resourceTestAccConfigFunc(resourceId, name, CenPrivateZoneBasicdependence) + testAccConfig := resourceTestAccConfigFunc(resourceId, name, AliCloudCenPrivateZoneBasicDependence0) resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, - IDRefreshName: resourceId, Providers: testAccProviders, CheckDestroy: rac.checkResourceDestroy(), Steps: []resource.TestStep{ { Config: testAccConfig(map[string]interface{}{ - "access_region_id": defaultRegionToTest, "cen_id": "${alicloud_cen_instance_attachment.default.instance_id}", - "host_region_id": defaultRegionToTest, + "access_region_id": "${data.alicloud_regions.default.regions.0.id}", "host_vpc_id": "${alicloud_cen_instance_attachment.default.child_instance_id}", + "host_region_id": "${data.alicloud_regions.default.regions.0.id}", }), Check: resource.ComposeTestCheckFunc( testAccCheck(map[string]string{ - "access_region_id": defaultRegionToTest, "cen_id": CHECKSET, - "host_region_id": defaultRegionToTest, + "access_region_id": CHECKSET, "host_vpc_id": CHECKSET, + "host_region_id": CHECKSET, }), ), }, @@ -56,26 +55,35 @@ func TestAccAlicloudCenPrivateZone_basic(t *testing.T) { }) } -var CenPrivateZoneMap = map[string]string{ +var AliCloudCenPrivateZoneMap0 = map[string]string{ "status": CHECKSET, } -func CenPrivateZoneBasicdependence(name string) string { +func AliCloudCenPrivateZoneBasicDependence0(name string) string { return fmt.Sprintf(` -variable "name" { - default = "%s" -} -resource "alicloud_cen_instance" "default" { - name = "${var.name}" -} -data "alicloud_vpcs" "default" { - name_regex = "^default-NODELETING$" -} -resource "alicloud_cen_instance_attachment" "default" { - instance_id = "${alicloud_cen_instance.default.id}" - child_instance_id = "${data.alicloud_vpcs.default.ids.0}" - child_instance_type = "VPC" - child_instance_region_id = "%s" -} -`, name, defaultRegionToTest) + variable "name" { + default = "%s" + } + + data "alicloud_regions" "default" { + current = true + } + + resource "alicloud_vpc" "default" { + vpc_name = var.name + cidr_block = "172.17.3.0/24" + } + + resource "alicloud_cen_instance" "default" { + cen_instance_name = var.name + description = var.name + } + + resource "alicloud_cen_instance_attachment" "default" { + instance_id = alicloud_cen_instance.default.id + child_instance_id = alicloud_vpc.default.id + child_instance_type = "VPC" + child_instance_region_id = data.alicloud_regions.default.regions.0.id + } +`, name) } diff --git a/alicloud/service_alicloud_cbn.go b/alicloud/service_alicloud_cbn.go index 728ef4b1eccf..f0af340bbdb7 100644 --- a/alicloud/service_alicloud_cbn.go +++ b/alicloud/service_alicloud_cbn.go @@ -285,9 +285,8 @@ func (s *CbnService) DescribeCenPrivateZone(id string) (object cbn.PrivateZoneIn return } request := cbn.CreateDescribeCenPrivateZoneRoutesRequest() - request.RegionId = s.client.RegionId - request.AccessRegionId = parts[1] request.CenId = parts[0] + request.AccessRegionId = parts[1] var raw interface{} wait := incrementalWait(3*time.Second, 3*time.Second) @@ -302,20 +301,24 @@ func (s *CbnService) DescribeCenPrivateZone(id string) (object cbn.PrivateZoneIn } return resource.NonRetryableError(err) } - addDebug(request.GetActionName(), raw, request.RpcRequest, request) return nil }) + addDebug(request.GetActionName(), raw, request.RpcRequest, request) if err != nil { - err = WrapErrorf(err, DefaultErrorMsg, id, request.GetActionName(), AlibabaCloudSdkGoERROR) - return + if IsExpectedErrors(err, []string{"ParameterIllegal.CenInstanceId"}) { + return object, WrapErrorf(Error(GetNotFoundMessage("Cen:PrivateZone", id)), NotFoundMsg, ProviderERROR) + } + return object, WrapErrorf(err, DefaultErrorMsg, id, request.GetActionName(), AlibabaCloudSdkGoERROR) } + response, _ := raw.(*cbn.DescribeCenPrivateZoneRoutesResponse) if len(response.PrivateZoneInfos.PrivateZoneInfo) < 1 { - err = WrapErrorf(Error(GetNotFoundMessage("CenPrivateZone", id)), NotFoundMsg, ProviderERROR) + err = WrapErrorf(Error(GetNotFoundMessage("Cen:PrivateZone", id)), NotFoundMsg, ProviderERROR) return } + return response.PrivateZoneInfos.PrivateZoneInfo[0], nil } diff --git a/website/docs/r/cen_private_zone.html.markdown b/website/docs/r/cen_private_zone.html.markdown index 8c553a23bf5c..4b051d47b5b3 100644 --- a/website/docs/r/cen_private_zone.html.markdown +++ b/website/docs/r/cen_private_zone.html.markdown @@ -4,16 +4,14 @@ layout: "alicloud" page_title: "Alicloud: alicloud_cen_private_zone" sidebar_current: "docs-alicloud-resource-cen-private-zone" description: |- - Provides a Alicloud CEN private zone resource. + Provides a Alicloud Cloud Enterprise Network (CEN) Private Zone resource. --- # alicloud_cen_private_zone -This topic describes how to configure PrivateZone access. -PrivateZone is a VPC-based resolution and management service for private domain names. -After you set a PrivateZone access, the Cloud Connect Network (CCN) and Virtual Border Router (VBR) attached to a CEN instance can access the PrivateZone service through CEN. +Provides a Cloud Enterprise Network (CEN) Private Zone resource. -For information about CEN Private Zone and how to use it, see [Manage CEN Private Zone](https://www.alibabacloud.com/help/en/cloud-enterprise-network/latest/api-cbn-2017-09-12-routeprivatezoneincentovpc). +For information about Cloud Enterprise Network (CEN) Private Zone and how to use it, see [What is Private Zone](https://www.alibabacloud.com/help/en/cloud-enterprise-network/latest/api-cbn-2017-09-12-routeprivatezoneincentovpc). -> **NOTE:** Available since v1.83.0. @@ -28,56 +26,74 @@ Basic Usage ```terraform +variable "name" { + default = "terraform-example" +} + +provider "alicloud" { + region = "cn-hangzhou" +} + data "alicloud_regions" "default" { current = true } -resource "alicloud_vpc" "example" { - vpc_name = "tf_example" +resource "alicloud_vpc" "default" { + vpc_name = var.name cidr_block = "172.17.3.0/24" } -resource "alicloud_cen_instance" "example" { - cen_instance_name = "tf_example" - description = "an example for cen" +resource "alicloud_cen_instance" "default" { + cen_instance_name = var.name + description = var.name } -resource "alicloud_cen_instance_attachment" "example" { - instance_id = alicloud_cen_instance.example.id - child_instance_id = alicloud_vpc.example.id +resource "alicloud_cen_instance_attachment" "default" { + instance_id = alicloud_cen_instance.default.id + child_instance_id = alicloud_vpc.default.id child_instance_type = "VPC" child_instance_region_id = data.alicloud_regions.default.regions.0.id } resource "alicloud_cen_private_zone" "default" { + cen_id = alicloud_cen_instance_attachment.default.instance_id access_region_id = data.alicloud_regions.default.regions.0.id - cen_id = alicloud_cen_instance_attachment.example.instance_id + host_vpc_id = alicloud_vpc.default.id host_region_id = data.alicloud_regions.default.regions.0.id - host_vpc_id = alicloud_vpc.example.id } ``` + ## Argument Reference The following arguments are supported: * `cen_id` - (Required, ForceNew) The ID of the CEN instance. -* `access_region_id` - (Required, ForceNew) The access region. The access region is the region of the cloud resource that accesses the PrivateZone service through CEN. -* `host_region_id` - (Required, ForceNew) The service region. The service region is the target region of the PrivateZone service to be accessed through CEN. -* `host_vpc_id` - (Required, ForceNew) The VPC that belongs to the service region. +* `access_region_id` - (Required, ForceNew) The ID of the region where PrivateZone is accessed. This region refers to the region in which PrivateZone is accessed by clients. +* `host_vpc_id` - (Required, ForceNew) The ID of the VPC that is associated with PrivateZone. +* `host_region_id` - (Required, ForceNew) The ID of the region where PrivateZone is deployed. -->**NOTE:** The "alicloud_cen_private_zone" resource depends on the related "alicloud_cen_instance_attachment" resource. +->**NOTE:** The resource `alicloud_cen_private_zone` depends on the resource `alicloud_cen_instance_attachment`. ## Attributes Reference The following attributes are exported: -* `id` - ID of the resource, formatted as `:`. -* `status` - The status of the PrivateZone service. Valid values: ["Creating", "Active", "Deleting"]. +* `id` - The resource ID in terraform of Private Zone. It formats as `:`. +* `status` - The status of the Private Zone. + +## Timeouts + +-> **NOTE:** Available since v1.238.0. + +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 6 mins) Used when create the Private Zone. +* `delete` - (Defaults to 5 mins) Used when delete the Private Zone. ## Import -CEN Private Zone can be imported using the id, e.g. +Cloud Enterprise Network (CEN) Private Zone can be imported using the id, e.g. ```shell -$ terraform import alicloud_cen_private_zone.example cen-abc123456:cn-hangzhou +$ terraform import alicloud_cen_private_zone.example : ```