Skip to content

Commit

Permalink
chore(cc): add CheckDeleted to CC resource deletion (#5563)
Browse files Browse the repository at this point in the history
  • Loading branch information
profoundwu authored Sep 18, 2024
1 parent 09cac5a commit 021f930
Show file tree
Hide file tree
Showing 14 changed files with 67 additions and 24 deletions.
4 changes: 2 additions & 2 deletions docs/resources/cc_central_network_policy_apply.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <central_network_id>/<policy_id>
```
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ func TestAccCentralNetworkPolicyApply_basic(t *testing.T) {
ResourceName: rName,
ImportState: true,
ImportStateVerify: true,
ImportStateIdFunc: testCentralNetworkPolicyApplyImportState(rName),
},
},
})
Expand All @@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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
Expand All @@ -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 <central_network_id>/<policy_id>")
}

d.SetId(parts[0])
d.Set("policy_id", parts[1])

return []*schema.ResourceData{d}, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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,
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 021f930

Please sign in to comment.