From 021f930f9fa9ac7541954404cb64e4bf25d93271 Mon Sep 17 00:00:00 2001 From: profoundwu Date: Wed, 18 Sep 2024 16:42:34 +0800 Subject: [PATCH] chore(cc): add CheckDeleted to CC resource deletion (#5563) --- .../cc_central_network_policy_apply.md | 4 +-- ...ud_cc_central_network_policy_apply_test.go | 19 ++++++++++++ .../resource_huaweicloud_cc_authorization.go | 2 +- ...source_huaweicloud_cc_bandwidth_package.go | 2 +- ...resource_huaweicloud_cc_central_network.go | 2 +- ...aweicloud_cc_central_network_attachment.go | 2 +- ..._network_connection_bandwidth_associate.go | 4 ++- ...e_huaweicloud_cc_central_network_policy.go | 2 +- ...eicloud_cc_central_network_policy_apply.go | 29 +++++++++++++++---- .../cc/resource_huaweicloud_cc_connection.go | 2 +- ...weicloud_cc_global_connection_bandwidth.go | 2 +- ...c_global_connection_bandwidth_associate.go | 15 +++++----- ...e_huaweicloud_cc_inter_region_bandwidth.go | 2 +- ...esource_huaweicloud_cc_network_instance.go | 4 ++- 14 files changed, 67 insertions(+), 24 deletions(-) diff --git a/docs/resources/cc_central_network_policy_apply.md b/docs/resources/cc_central_network_policy_apply.md index 5e29a15fbc..e9527f9daa 100644 --- a/docs/resources/cc_central_network_policy_apply.md +++ b/docs/resources/cc_central_network_policy_apply.md @@ -52,8 +52,8 @@ This resource provides the following timeouts configuration options: ## Import -The central network policy apply can be imported using `id`, separated by a slash, e.g. +The central network policy apply can be imported using `central_network_id` and `policy_id`, separated by a slash, e.g. ```bash -$ terraform import huaweicloud_cc_central_network_policy_apply.test 0ce123456a00f2591fabc00385ff1234 +$ terraform import huaweicloud_cc_central_network_policy_apply.test / ``` diff --git a/huaweicloud/services/acceptance/cc/resource_huaweicloud_cc_central_network_policy_apply_test.go b/huaweicloud/services/acceptance/cc/resource_huaweicloud_cc_central_network_policy_apply_test.go index eaaa3055b9..1793bce79d 100644 --- a/huaweicloud/services/acceptance/cc/resource_huaweicloud_cc_central_network_policy_apply_test.go +++ b/huaweicloud/services/acceptance/cc/resource_huaweicloud_cc_central_network_policy_apply_test.go @@ -92,6 +92,7 @@ func TestAccCentralNetworkPolicyApply_basic(t *testing.T) { ResourceName: rName, ImportState: true, ImportStateVerify: true, + ImportStateIdFunc: testCentralNetworkPolicyApplyImportState(rName), }, }, }) @@ -109,3 +110,21 @@ resource "huaweicloud_cc_central_network_policy_apply" "test" { } `, policyConfig) } + +func testCentralNetworkPolicyApplyImportState(name string) resource.ImportStateIdFunc { + return func(s *terraform.State) (string, error) { + rs, ok := s.RootModule().Resources[name] + if !ok { + return "", fmt.Errorf("resource (%s) not found: %s", name, rs) + } + if rs.Primary.ID == "" { + return "", fmt.Errorf("attribute (ID) of resource (%s) not found: %s", name, rs) + } + + if rs.Primary.Attributes["policy_id"] == "" { + return "", fmt.Errorf("attribute (policy_id) of resource (%s) not found: %s", name, rs) + } + + return rs.Primary.ID + "/" + rs.Primary.Attributes["policy_id"], nil + } +} diff --git a/huaweicloud/services/cc/resource_huaweicloud_cc_authorization.go b/huaweicloud/services/cc/resource_huaweicloud_cc_authorization.go index d07c77cabf..2d639b7ade 100644 --- a/huaweicloud/services/cc/resource_huaweicloud_cc_authorization.go +++ b/huaweicloud/services/cc/resource_huaweicloud_cc_authorization.go @@ -279,7 +279,7 @@ func resourceAuthorizationDelete(_ context.Context, d *schema.ResourceData, meta _, err = deleteAuthorizationClient.Request("DELETE", deleteAuthorizationPath, &deleteAuthorizationOpt) if err != nil { - return diag.Errorf("error deleting authorization: %s", err) + return common.CheckDeletedDiag(d, err, "error deleting authorization") } return nil diff --git a/huaweicloud/services/cc/resource_huaweicloud_cc_bandwidth_package.go b/huaweicloud/services/cc/resource_huaweicloud_cc_bandwidth_package.go index b65d278aa4..0f7b42573b 100644 --- a/huaweicloud/services/cc/resource_huaweicloud_cc_bandwidth_package.go +++ b/huaweicloud/services/cc/resource_huaweicloud_cc_bandwidth_package.go @@ -538,7 +538,7 @@ func resourceBandwidthPackageDelete(_ context.Context, d *schema.ResourceData, m _, err = deleteBandwidthPackageClient.Request("DELETE", deleteBandwidthPackagePath, &deleteBandwidthPackageOpt) if err != nil { - return diag.Errorf("error deleting bandwidth package: %s", err) + return common.CheckDeletedDiag(d, err, "error deleting bandwidth package") } return nil diff --git a/huaweicloud/services/cc/resource_huaweicloud_cc_central_network.go b/huaweicloud/services/cc/resource_huaweicloud_cc_central_network.go index 4b419f644d..f5c23f8ef2 100644 --- a/huaweicloud/services/cc/resource_huaweicloud_cc_central_network.go +++ b/huaweicloud/services/cc/resource_huaweicloud_cc_central_network.go @@ -345,7 +345,7 @@ func resourceCentralNetworkDelete(ctx context.Context, d *schema.ResourceData, m _, err = deleteCentralNetworkClient.Request("DELETE", deleteCentralNetworkPath, &deleteCentralNetworkOpt) if err != nil { - return diag.Errorf("error deleting central network: %s", err) + return common.CheckDeletedDiag(d, err, "error deleting central network") } err = deleteCentralNetworkWaitingForStateCompleted(ctx, d, meta, d.Timeout(schema.TimeoutDelete)) diff --git a/huaweicloud/services/cc/resource_huaweicloud_cc_central_network_attachment.go b/huaweicloud/services/cc/resource_huaweicloud_cc_central_network_attachment.go index fd99ea06e3..0f739a84f8 100644 --- a/huaweicloud/services/cc/resource_huaweicloud_cc_central_network_attachment.go +++ b/huaweicloud/services/cc/resource_huaweicloud_cc_central_network_attachment.go @@ -408,7 +408,7 @@ func resourceCentralNetworkAttachmentDelete(ctx context.Context, d *schema.Resou _, err = deleteCentralNetworkAttachmentClient.Request("DELETE", deleteCentralNetworkAttachmentPath, &deleteCentralNetworkAttachmentOpt) if err != nil { - return diag.Errorf("error deleting central network attachment: %s", err) + return common.CheckDeletedDiag(d, err, "error deleting central network attachment") } err = centralNetworkAttachmentDeleteWaitingForStateCompleted(ctx, d, meta, d.Timeout(schema.TimeoutDelete)) diff --git a/huaweicloud/services/cc/resource_huaweicloud_cc_central_network_connection_bandwidth_associate.go b/huaweicloud/services/cc/resource_huaweicloud_cc_central_network_connection_bandwidth_associate.go index 26e06fff18..465e399972 100644 --- a/huaweicloud/services/cc/resource_huaweicloud_cc_central_network_connection_bandwidth_associate.go +++ b/huaweicloud/services/cc/resource_huaweicloud_cc_central_network_connection_bandwidth_associate.go @@ -252,7 +252,9 @@ func resourceCentralNetworkConnectionBandwidthAssociateDelete(ctx context.Contex _, err = client.Request("PUT", path, &opt) if err != nil { - return diag.Errorf("error deleting central network connection bandwidth associate: %s", err) + return common.CheckDeletedDiag(d, + common.ConvertExpected400ErrInto404Err(err, "error_code", "GCN.101505"), + "error deleting central network connection bandwidth associate") } err = centralNetworkConnectionBandwidthAssociateWaitingForStateCompleted(ctx, d, meta, d.Timeout(schema.TimeoutDelete)) diff --git a/huaweicloud/services/cc/resource_huaweicloud_cc_central_network_policy.go b/huaweicloud/services/cc/resource_huaweicloud_cc_central_network_policy.go index f6805a6607..11c18afc34 100644 --- a/huaweicloud/services/cc/resource_huaweicloud_cc_central_network_policy.go +++ b/huaweicloud/services/cc/resource_huaweicloud_cc_central_network_policy.go @@ -487,7 +487,7 @@ func resourceCentralNetworkPolicyDelete(_ context.Context, d *schema.ResourceDat _, err = deleteCentralNetworkPolicyClient.Request("DELETE", deleteCentralNetworkPolicyPath, &deleteCentralNetworkPolicyOpt) if err != nil { - return diag.Errorf("error deleting CentralNetworkPolicy: %s", err) + return common.CheckDeletedDiag(d, err, "error deleting CentralNetworkPolicy") } return nil diff --git a/huaweicloud/services/cc/resource_huaweicloud_cc_central_network_policy_apply.go b/huaweicloud/services/cc/resource_huaweicloud_cc_central_network_policy_apply.go index 43b1cdfbfa..ebf8c23799 100644 --- a/huaweicloud/services/cc/resource_huaweicloud_cc_central_network_policy_apply.go +++ b/huaweicloud/services/cc/resource_huaweicloud_cc_central_network_policy_apply.go @@ -33,8 +33,9 @@ func ResourceCentralNetworkPolicyApply() *schema.Resource { ReadContext: resourceCentralNetworkPolicyApplyRead, DeleteContext: resourceCentralNetworkPolicyApplyDelete, Importer: &schema.ResourceImporter{ - StateContext: schema.ImportStatePassthroughContext, + StateContext: resourceCentralNetworkPolicyApplyImportState, }, + Timeouts: &schema.ResourceTimeout{ Create: schema.DefaultTimeout(10 * time.Minute), Update: schema.DefaultTimeout(10 * time.Minute), @@ -220,10 +221,10 @@ func resourceCentralNetworkPolicyApplyRead(_ context.Context, d *schema.Resource return diag.FromErr(err) } - getCentralNetworkPolicyApplyRespBody = utils.PathSearch("central_network_policies[?is_applied]|[0]", - getCentralNetworkPolicyApplyRespBody, nil) + jsonPath := fmt.Sprintf("central_network_policies[?id =='%s' && is_applied == `true`]|[0]", d.Get("policy_id").(string)) + getCentralNetworkPolicyApplyRespBody = utils.PathSearch(jsonPath, getCentralNetworkPolicyApplyRespBody, nil) if getCentralNetworkPolicyApplyRespBody == nil { - return common.CheckDeletedDiag(d, golangsdk.ErrDefault404{}, "no data found") + return common.CheckDeletedDiag(d, golangsdk.ErrDefault404{}, "no policy found") } mErr = multierror.Append( @@ -275,9 +276,15 @@ func resourceCentralNetworkPolicyApplyDelete(ctx context.Context, d *schema.Reso return diag.FromErr(err) } + jsonPath := fmt.Sprintf("central_network_policies[?id =='%s' && is_applied == `true`]|[0]", d.Get("policy_id").(string)) + appliedID := utils.PathSearch(jsonPath, getCentralNetworkPolicyApplyRespBody, nil) + if appliedID == nil { + return common.CheckDeletedDiag(d, golangsdk.ErrDefault404{}, "no policy found") + } + defaultId := utils.PathSearch("central_network_policies[?version == `1`]|[0].id", getCentralNetworkPolicyApplyRespBody, nil) if defaultId == nil { - return diag.Errorf("error applying central network policy to none: %s", err) + return diag.Errorf("error applying central network policy to none: no default policy found") } // apply default policy @@ -287,3 +294,15 @@ func resourceCentralNetworkPolicyApplyDelete(ctx context.Context, d *schema.Reso } return nil } + +func resourceCentralNetworkPolicyApplyImportState(_ context.Context, d *schema.ResourceData, _ interface{}) ([]*schema.ResourceData, error) { + parts := strings.Split(d.Id(), "/") + if len(parts) != 2 { + return nil, fmt.Errorf("invalid format specified for import ID, must be /") + } + + d.SetId(parts[0]) + d.Set("policy_id", parts[1]) + + return []*schema.ResourceData{d}, nil +} diff --git a/huaweicloud/services/cc/resource_huaweicloud_cc_connection.go b/huaweicloud/services/cc/resource_huaweicloud_cc_connection.go index f522f23605..f698900413 100644 --- a/huaweicloud/services/cc/resource_huaweicloud_cc_connection.go +++ b/huaweicloud/services/cc/resource_huaweicloud_cc_connection.go @@ -379,7 +379,7 @@ func resourceCloudConnectionDelete(_ context.Context, d *schema.ResourceData, me } _, err = deleteCloudConnectionClient.Request("DELETE", deleteCloudConnectionPath, &deleteCloudConnectionOpt) if err != nil { - return diag.Errorf("error deleting CloudConnection: %s", err) + return common.CheckDeletedDiag(d, err, "error deleting CloudConnection") } return nil diff --git a/huaweicloud/services/cc/resource_huaweicloud_cc_global_connection_bandwidth.go b/huaweicloud/services/cc/resource_huaweicloud_cc_global_connection_bandwidth.go index 34e6467c85..970e7fc809 100644 --- a/huaweicloud/services/cc/resource_huaweicloud_cc_global_connection_bandwidth.go +++ b/huaweicloud/services/cc/resource_huaweicloud_cc_global_connection_bandwidth.go @@ -397,7 +397,7 @@ func resourceGlobalConnectionBandwidthDelete(_ context.Context, d *schema.Resour _, err = client.Request("DELETE", deleteGCBPath, &deleteGCBOpt) if err != nil { - return diag.Errorf("error deleting global connection bandwidth: %s", err) + return common.CheckDeletedDiag(d, err, "error deleting global connection bandwidth") } return nil diff --git a/huaweicloud/services/cc/resource_huaweicloud_cc_global_connection_bandwidth_associate.go b/huaweicloud/services/cc/resource_huaweicloud_cc_global_connection_bandwidth_associate.go index ea608d0330..ad64d38f05 100644 --- a/huaweicloud/services/cc/resource_huaweicloud_cc_global_connection_bandwidth_associate.go +++ b/huaweicloud/services/cc/resource_huaweicloud_cc_global_connection_bandwidth_associate.go @@ -233,7 +233,7 @@ func resourceGlobalConnectionBandwidthAssociateUpdate(ctx context.Context, d *sc disassociateResources := oldResources.(*schema.Set).Difference(newResources.(*schema.Set)) if disassociateResources.Len() > 0 { - err := disassociateGlobalConnectionBandwidth(client, gcbID, cfg.DomainID, disassociateResources) + err := disassociateGlobalConnectionBandwidth(client, d, cfg.DomainID, disassociateResources) if err != nil { return diag.FromErr(err) } @@ -249,11 +249,11 @@ func resourceGlobalConnectionBandwidthAssociateUpdate(ctx context.Context, d *sc return resourceGlobalConnectionBandwidthAssociateRead(ctx, d, meta) } -func disassociateGlobalConnectionBandwidth(client *golangsdk.ServiceClient, gcbID, domainID string, resources *schema.Set) error { +func disassociateGlobalConnectionBandwidth(client *golangsdk.ServiceClient, d *schema.ResourceData, domainID string, resources *schema.Set) error { httpUrl := "v3/{domain_id}/gcb/gcbandwidths/{id}/disassociate-instance" path := client.Endpoint + httpUrl path = strings.ReplaceAll(path, "{domain_id}", domainID) - path = strings.ReplaceAll(path, "{id}", gcbID) + path = strings.ReplaceAll(path, "{id}", d.Id()) opt := golangsdk.RequestOpts{ KeepResponseBody: true, @@ -262,7 +262,7 @@ func disassociateGlobalConnectionBandwidth(client *golangsdk.ServiceClient, gcbI opt.JSONBody = utils.RemoveNil(buildGlobalConnectionBandwidthAssociateBodyParams(resources)) resp, err := client.Request("POST", path, &opt) if err != nil { - return fmt.Errorf("error disassociating the resource instance from the global connection bandwidth: %s", err) + return err } respBody, err := utils.FlattenResponse(resp) @@ -287,7 +287,6 @@ func resourceGlobalConnectionBandwidthAssociateDelete(_ context.Context, d *sche var ( cfg = meta.(*config.Config) region = cfg.GetRegion(d) - gcbID = d.Id() ) client, err := cfg.NewServiceClient("cc", region) @@ -296,9 +295,11 @@ func resourceGlobalConnectionBandwidthAssociateDelete(_ context.Context, d *sche } disassociateResources, _ := d.GetChange("gcb_binding_resources") - err = disassociateGlobalConnectionBandwidth(client, gcbID, cfg.DomainID, disassociateResources.(*schema.Set)) + err = disassociateGlobalConnectionBandwidth(client, d, cfg.DomainID, disassociateResources.(*schema.Set)) if err != nil { - return diag.FromErr(err) + return common.CheckDeletedDiag(d, + common.ConvertExpected400ErrInto404Err(err, "error_code", "GCB.0001"), + "error disassociating the resource instance from the global connection bandwidth") } return nil } diff --git a/huaweicloud/services/cc/resource_huaweicloud_cc_inter_region_bandwidth.go b/huaweicloud/services/cc/resource_huaweicloud_cc_inter_region_bandwidth.go index 437fd735fe..a4b1d47c57 100644 --- a/huaweicloud/services/cc/resource_huaweicloud_cc_inter_region_bandwidth.go +++ b/huaweicloud/services/cc/resource_huaweicloud_cc_inter_region_bandwidth.go @@ -299,7 +299,7 @@ func resourceInterRegionBandwidthDelete(_ context.Context, d *schema.ResourceDat _, err = deleteInterRegionBandwidthClient.Request("DELETE", deleteInterRegionBandwidthPath, &deleteInterRegionBandwidthOpt) if err != nil { - return diag.Errorf("error deleting inter-region bandwidth: %s", err) + return common.CheckDeletedDiag(d, err, "error deleting inter-region bandwidth") } return nil diff --git a/huaweicloud/services/cc/resource_huaweicloud_cc_network_instance.go b/huaweicloud/services/cc/resource_huaweicloud_cc_network_instance.go index d2658da623..ec558757d3 100644 --- a/huaweicloud/services/cc/resource_huaweicloud_cc_network_instance.go +++ b/huaweicloud/services/cc/resource_huaweicloud_cc_network_instance.go @@ -318,7 +318,9 @@ func resourceNetworkInstanceDelete(_ context.Context, d *schema.ResourceData, me } _, err = deleteNetworkInstanceClient.Request("DELETE", deleteNetworkInstancePath, &deleteNetworkInstanceOpt) if err != nil { - return diag.Errorf("error deleting NetworkInstance: %s", err) + return common.CheckDeletedDiag(d, + common.ConvertExpected400ErrInto404Err(err, "error_code", "CC.1002"), + "error deleting NetworkInstance") } return nil