Skip to content

Commit

Permalink
fix(dws): fix some resources cannot trigger the check checkdeleted logic
Browse files Browse the repository at this point in the history
  • Loading branch information
wuzhuanhong committed Aug 15, 2024
1 parent 4ad1b8b commit 8af304c
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,10 @@ resource "huaweicloud_dws_logical_cluster" "test" {
}
resource "huaweicloud_dws_logical_cluster" "test2" {
// It does not support creating multiple logical clusters at the same time.
// The next logical cluster can be created only after the previous logical cluster is created successfully.
depends_on = [huaweicloud_dws_logical_cluster.test]
cluster_id = huaweicloud_dws_cluster.test.id
logical_cluster_name = "%s_test2"
Expand Down
51 changes: 23 additions & 28 deletions huaweicloud/services/dws/resource_huaweicloud_dws_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ const (
PublicBindTypeBindExisting = "bind_existing"
)

const ClusterIdIllegalErrCode = "DWS.0001"

// @API DWS POST /v1.0/{project_id}/clusters
// @API DWS GET /v1.0/{project_id}/clusters/{cluster_id}
// @API DWS POST /v1.0/{project_id}/clusters/{cluster_id}/expand-instance-storage
Expand Down Expand Up @@ -1149,11 +1151,6 @@ func deleteClusterWaitingForCompleted(ctx context.Context, d *schema.ResourceDat

deleteDwsClusterWaitingResp, err := deleteDwsClusterWaitingClient.Request("GET", deleteDwsClusterWaitingPath, &deleteDwsClusterWaitingOpt)
if err != nil {
if _, ok := err.(golangsdk.ErrDefault404); ok {
// When the error code is 404, the value of respBody is nil, and a non-null value is returned to avoid continuing the loop check.
return "Resource Not Found", "COMPLETED", nil
}

err = parseClusterNotFoundError(err)
if _, ok := err.(golangsdk.ErrDefault404); ok {
// When the error code is 404, the value of respBody is nil, and a non-null value is returned to avoid continuing the loop check.
Expand Down Expand Up @@ -1199,29 +1196,6 @@ func deleteClusterWaitingForCompleted(ctx context.Context, d *schema.ResourceDat
return err
}

func parseClusterNotFoundError(respErr error) error {
var apiErr interface{}
if errCode, ok := respErr.(golangsdk.ErrDefault401); ok {
pErr := json.Unmarshal(errCode.Body, &apiErr)
if pErr != nil {
return pErr
}
errCode, err := jmespath.Search(`errCode`, apiErr)
if err != nil {
return fmt.Errorf("error parse errorCode from response body: %s", err.Error())
}

if errCode == `DWS.0047` {
return golangsdk.ErrDefault404{
ErrUnexpectedResponseCode: golangsdk.ErrUnexpectedResponseCode{
Body: []byte("the DWS cluster does not exist"),
},
}
}
}
return respErr
}

func addClusterTags(client *golangsdk.ServiceClient, clusterId string, rawTags []tags.ResourceTag) error {
var (
addTagsHttpUrl = "v1.0/{project_id}/clusters/{cluster_id}/tags/batch-create"
Expand Down Expand Up @@ -1477,3 +1451,24 @@ func parseLtsError(err error) error {
}
return err
}

func parseClusterNotFoundError(err error) error {
// "DWS.0001": The cluster ID does not exist (non-standard UUID format). Status code is 400.
parsedErr := common.ConvertExpected400ErrInto404Err(err, "error_code", ClusterIdIllegalErrCode)
if _, ok := parsedErr.(golangsdk.ErrDefault404); ok {
return parsedErr
}

parsedErr = common.ConvertExpected401ErrInto404Err(err, "error_code", "DWS.0047")
if _, ok := parsedErr.(golangsdk.ErrDefault404); ok {
return parsedErr
}

// "DWS.0015": The cluster ID does not exist (standard UUID format). Status code is 403.
parsedErr = common.ConvertExpected403ErrInto404Err(err, "error_code", "DWS.0015")
// "DWS.3027": The cluster was deleted after it was created. Status code is 404.
if _, ok := parsedErr.(golangsdk.ErrDefault404); ok {
return parsedErr
}
return err
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
// @API DWS GET /v1.0/{project_id}/clusters/{cluster_id}/ext-data-sources
// @API DWS POST /v1.0/{project_id}/clusters/{cluster_id}/ext-data-sources
// @API DWS DELETE /v1.0/{project_id}/clusters/{cluster_id}/ext-data-sources/{ext_data_source_id}
// @API AWS PUT /v1.0/{project_id}/clusters/{cluster_id}/ext-data-sources/{ext_data_source_id}
// @API DWS PUT /v1.0/{project_id}/clusters/{cluster_id}/ext-data-sources/{ext_data_source_id}
func ResourceDwsExtDataSource() *schema.Resource {
return &schema.Resource{
CreateContext: resourceDwsExtDataSourceCreate,
Expand Down Expand Up @@ -205,7 +205,11 @@ func resourceDwsExtDataSourceRead(_ context.Context, d *schema.ResourceData, met
// getDwsExtDataSource: Query the DWS external data source.
extDataSource, err := GetExtDataSource(cfg, region, d, d.Get("type").(string))
if err != nil {
return common.CheckDeletedDiag(d, err, "error retrieving DWS external data source")
// The cluster ID does not exist.
// "DWS.0001": The cluster ID is a non-standard UUID, the status code is 400.
// "DWS.0047": The cluster ID is a standard UUID, the status code is 404.
return common.CheckDeletedDiag(d, common.ConvertExpected400ErrInto404Err(err, "error_code", ClusterIdIllegalErrCode),
"error retrieving DWS external data source")
}

if extDataSource == nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -337,9 +337,11 @@ func resourceLogicalClusterRead(_ context.Context, d *schema.ResourceData, meta
}

clusterRespBody, err := readLogicalClusters(client, d)
// The list API response status code is `404` when the cluster does not exist.
// The list API response status code is `404` when the cluster does not exist (standard UUID format).
// "DWS.0001": The cluster ID is a non-standard UUID, the status code is 400.
if err != nil {
return common.CheckDeletedDiag(d, err, "error retrieving DWS logical cluster")
return common.CheckDeletedDiag(d, common.ConvertExpected400ErrInto404Err(err, "error_code", ClusterIdIllegalErrCode),
"error retrieving DWS logical cluster")
}

expression := fmt.Sprintf("logical_clusters[?logical_cluster_id=='%s']|[0]", d.Id())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,11 @@ func resourceDwsSnapshotPolicyRead(_ context.Context, d *schema.ResourceData, me
getDwsSnapshotPolicyResp, err := getDwsSnapshotPolicyClient.Request("GET", getDwsSnapshotPolicyPath, &getDwsSnapshotPolicyOpt)

if err != nil {
return common.CheckDeletedDiag(d, err, "error retrieving DwsSnapshotPolicy")
// The cluster ID does not exist.
// "DWS.0001": The cluster ID is a non-standard UUID, the status code is 400.
// "DWS.0047": The cluster ID is a standard UUID, the status code is 404.
return common.CheckDeletedDiag(d, common.ConvertExpected400ErrInto404Err(err, "error_code", ClusterIdIllegalErrCode),
"error retrieving DwsSnapshotPolicy")
}

getDwsSnapshotPolicyRespBody, err := utils.FlattenResponse(getDwsSnapshotPolicyResp)
Expand Down

0 comments on commit 8af304c

Please sign in to comment.