Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(dws): fix some resources cannot trigger the check checkdeleted logic #5395

Merged
merged 1 commit into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 DWS snapshot policy")
}

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