Skip to content

Commit

Permalink
fix(aom/dds/dms/eip): change jemspath search to utils path search and…
Browse files Browse the repository at this point in the history
… change parse error method to common error convert (huaweicloud#5678)
  • Loading branch information
saf3dfsa authored Oct 14, 2024
1 parent 03262d4 commit 5c011f0
Show file tree
Hide file tree
Showing 23 changed files with 166 additions and 267 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
"github.com/jmespath/go-jmespath"

"github.com/chnsz/golangsdk"

Expand Down Expand Up @@ -41,9 +40,9 @@ func getPromInstanceResourceFunc(conf *config.Config, state *terraform.ResourceS
return nil, fmt.Errorf("error retrieving AOM prometheus instance: %s", err)
}

curJson, err := jmespath.Search("prometheus[0]", getPrometheusInstanceRespBody)
if err != nil || curJson == nil {
return nil, fmt.Errorf("error retrieving AOM prometheus instance: %s", err)
curJson := utils.PathSearch("prometheus[0]", getPrometheusInstanceRespBody, nil)
if curJson == nil {
return nil, fmt.Errorf("unable to find the instance from the API response")
}

return curJson, nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,8 @@ func resourceAlarmGroupRuleDelete(_ context.Context, d *schema.ResourceData, met

_, err = client.Request("DELETE", deletePath, &deleteOpt)
if err != nil {
return common.CheckDeletedDiag(d, parseQueryError400(err, "AOM.08002002"), "error deleting alarm group rule")
return common.CheckDeletedDiag(d, common.ConvertExpected400ErrInto404Err(err, "error_code", "AOM.08002002"),
"error deleting alarm group rule")
}

return nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@ package aom

import (
"context"
"encoding/json"
"fmt"
"reflect"
"strings"

"github.com/hashicorp/go-multierror"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/jmespath/go-jmespath"

"github.com/chnsz/golangsdk"

Expand Down Expand Up @@ -898,28 +896,9 @@ func resourceAlarmRulesTemplateDelete(_ context.Context, d *schema.ResourceData,

_, err = client.Request("DELETE", deletePath, &deleteOpt)
if err != nil {
return common.CheckDeletedDiag(d, parseQueryError400(err, "AOM.02018001"), "error deleting alarm rules template")
return common.CheckDeletedDiag(d, common.ConvertExpected400ErrInto404Err(err, "error_code", "AOM.02018001"),
"error deleting alarm rules template")
}

return nil
}

func parseQueryError400(err error, notFoundErrorCode string) error {
if errCode, ok := err.(golangsdk.ErrDefault400); ok {
var apiError interface{}
if jsonErr := json.Unmarshal(errCode.Body, &apiError); jsonErr != nil {
return fmt.Errorf("unmarshal response body failed: %s", jsonErr)
}

errorCode, errorCodeErr := jmespath.Search("error_code", apiError)
if errorCodeErr != nil {
return fmt.Errorf("error parse errorCode from response body: %s", errorCodeErr)
}

if errorCode.(string) == notFoundErrorCode {
return golangsdk.ErrDefault404{}
}
}

return err
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,11 @@ package aom
import (
"context"
"fmt"
"log"
"strings"

"github.com/hashicorp/go-multierror"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/jmespath/go-jmespath"

"github.com/chnsz/golangsdk"

Expand Down Expand Up @@ -322,9 +320,8 @@ func resourceAlarmSilenceRuleRead(_ context.Context, d *schema.ResourceData, met

func flattenSilenceRuleSilenceTime(resp interface{}) []interface{} {
var rst []interface{}
curJson, err := jmespath.Search("mute_config", resp)
if err != nil {
log.Printf("[ERROR] error parsing silence_time from response= %#v", resp)
curJson := utils.PathSearch("mute_config", resp, nil)
if curJson == nil {
return rst
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,11 @@ package aom

import (
"context"
"encoding/json"
"log"
"strings"

"github.com/hashicorp/go-multierror"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/jmespath/go-jmespath"

"github.com/chnsz/golangsdk"

Expand Down Expand Up @@ -113,12 +110,12 @@ func resourceCmdbApplicationCreate(ctx context.Context, d *schema.ResourceData,
return diag.FromErr(err)
}

id, err := jmespath.Search("id", createApplicationRespBody)
if err != nil {
return diag.Errorf("error creating CMDB application: ID is not found in API response")
id := utils.PathSearch("id", createApplicationRespBody, "").(string)
if id == "" {
return diag.Errorf("unable to find application ID from the API response")
}

d.SetId(id.(string))
d.SetId(id)
return resourceCmdbApplicationRead(ctx, d, meta)
}

Expand All @@ -142,10 +139,8 @@ func resourceCmdbApplicationRead(_ context.Context, d *schema.ResourceData, meta

getApplicationResp, err := client.Request("GET", getApplicationPath, &getApplicationOpt)
if err != nil {
if hasErrorCode(err, AppNotExistsCode) {
err = golangsdk.ErrDefault404{}
}
return common.CheckDeletedDiag(d, err, "error retrieving CMDB application")
return common.CheckDeletedDiag(d, common.ConvertExpected400ErrInto404Err(err, "error_code", AppNotExistsCode),
"error retrieving CMDB application")
}

getApplicationRespBody, err := utils.FlattenResponse(getApplicationResp)
Expand Down Expand Up @@ -225,27 +220,10 @@ func resourceCmdbApplicationDelete(_ context.Context, d *schema.ResourceData, me
}

_, err = client.Request("DELETE", deleteApplicationPath, &deleteApplicationOpt)
if err != nil && !hasErrorCode(err, AppNotExistsCode) {
return diag.Errorf("error deleting CMDB application: %s", err)
if err != nil {
return common.CheckDeletedDiag(d, common.ConvertExpected400ErrInto404Err(err, "error_code", AppNotExistsCode),
"error deleting CMDB application")
}

return nil
}

func hasErrorCode(err error, expectCode string) bool {
if errCode, ok := err.(golangsdk.ErrDefault400); ok {
var response interface{}
if jsonErr := json.Unmarshal(errCode.Body, &response); jsonErr == nil {
errorCode, parseErr := jmespath.Search("error_code", response)
if parseErr != nil {
log.Printf("[WARN] failed to parse error_code from response body: %s", parseErr)
}

if errorCode == expectCode {
return true
}
}
}

return false
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"github.com/hashicorp/go-multierror"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/jmespath/go-jmespath"

"github.com/chnsz/golangsdk"

Expand Down Expand Up @@ -114,12 +113,12 @@ func resourceCmdbComponentCreate(ctx context.Context, d *schema.ResourceData, me
return diag.FromErr(err)
}

id, err := jmespath.Search("id", createComponentRespBody)
if err != nil {
return diag.Errorf("error creating CMDB component: ID is not found in API response")
id := utils.PathSearch("id", createComponentRespBody, "").(string)
if id == "" {
return diag.Errorf("unable to find component ID from the API response")
}

d.SetId(id.(string))
d.SetId(id)
return resourceCmdbComponentRead(ctx, d, meta)
}

Expand All @@ -143,10 +142,8 @@ func resourceCmdbComponentRead(_ context.Context, d *schema.ResourceData, meta i

getComponentResp, err := client.Request("GET", getComponentPath, &getComponentOpt)
if err != nil {
if hasErrorCode(err, ComNotExistsCode) {
err = golangsdk.ErrDefault404{}
}
return common.CheckDeletedDiag(d, err, "error retrieving CMDB component")
return common.CheckDeletedDiag(d, common.ConvertExpected400ErrInto404Err(err, "error_code", ComNotExistsCode),
"error retrieving CMDB component")
}

getComponentRespBody, err := utils.FlattenResponse(getComponentResp)
Expand Down Expand Up @@ -241,8 +238,9 @@ func resourceCmdbComponentDelete(_ context.Context, d *schema.ResourceData, meta
}

_, err = client.Request("DELETE", deleteComponentPath, &deleteComponentOpt)
if err != nil && !hasErrorCode(err, ComNotExistsCode) {
return diag.Errorf("error deleting CMDB component: %s", err)
if err != nil {
return common.CheckDeletedDiag(d, common.ConvertExpected400ErrInto404Err(err, "error_code", ComNotExistsCode),
"error deleting CMDB component")
}

return nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"github.com/hashicorp/go-multierror"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/jmespath/go-jmespath"

"github.com/chnsz/golangsdk"

Expand Down Expand Up @@ -122,12 +121,12 @@ func resourceCmdbEnvironmentCreate(ctx context.Context, d *schema.ResourceData,
return diag.FromErr(err)
}

id, err := jmespath.Search("id", createEnvironmentRespBody)
if err != nil {
return diag.Errorf("error creating CMDB environment: ID is not found in API response")
id := utils.PathSearch("id", createEnvironmentRespBody, "").(string)
if id == "" {
return diag.Errorf("unable to find environment ID from the API response")
}

d.SetId(id.(string))
d.SetId(id)
return resourceCmdbEnvironmentRead(ctx, d, meta)
}

Expand All @@ -152,10 +151,8 @@ func resourceCmdbEnvironmentRead(_ context.Context, d *schema.ResourceData, meta

getEnvironmentResp, err := client.Request("GET", getEnvironmentPath, &getEnvironmentOpt)
if err != nil {
if hasErrorCode(err, EnvNotExistsCode) {
err = golangsdk.ErrDefault404{}
}
return common.CheckDeletedDiag(d, err, "error retrieving CMDB environment")
return common.CheckDeletedDiag(d, common.ConvertExpected400ErrInto404Err(err, "error_code", EnvNotExistsCode),
"error retrieving CMDB environment")
}

getEnvironmentRespBody, err := utils.FlattenResponse(getEnvironmentResp)
Expand Down Expand Up @@ -239,8 +236,9 @@ func resourceCmdbEnvironmentDelete(_ context.Context, d *schema.ResourceData, me
}

_, err = client.Request("DELETE", deleteEnvironmentPath, &deleteEnvironmentOpt)
if err != nil && !hasErrorCode(err, EnvNotExistsCode) {
return diag.Errorf("error deleting CMDB environment: %s", err)
if err != nil {
return common.CheckDeletedDiag(d, common.ConvertExpected400ErrInto404Err(err, "error_code", EnvNotExistsCode),
"error deleting CMDB environment")
}

return nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,8 @@ func resourceMessageTemplateRead(_ context.Context, d *schema.ResourceData, meta

template, err := GetMessageTemplate(client, d.Id())
if err != nil {
return common.CheckDeletedDiag(d, parseQueryError400(err, "AOM.08025006"), "error retrieving message template")
return common.CheckDeletedDiag(d, common.ConvertExpected400ErrInto404Err(err, "error_code", "AOM.08025006"),
"error retrieving message template")
}

mErr := multierror.Append(nil,
Expand Down Expand Up @@ -287,7 +288,8 @@ func resourceMessageTemplateDelete(_ context.Context, d *schema.ResourceData, me

_, err = client.Request("DELETE", deletePath, &deleteOpt)
if err != nil {
return common.CheckDeletedDiag(d, parseQueryError400(err, "AOM.08023006"), "error deleting message template")
return common.CheckDeletedDiag(d, common.ConvertExpected400ErrInto404Err(err, "error_code", "AOM.08023006"),
"error deleting message template")
}

return nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/hashicorp/go-multierror"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/jmespath/go-jmespath"

"github.com/chnsz/golangsdk"

Expand Down Expand Up @@ -109,12 +108,12 @@ func resourcePromInstanceCreate(ctx context.Context, d *schema.ResourceData, met
return diag.FromErr(err)
}
expression := fmt.Sprintf("prometheus[?prom_name== '%s'].prom_id | [0]", d.Get("prom_name"))
id, err := jmespath.Search(expression, createPrometheusInstanceRespBody)
if err != nil || id == nil {
return diag.Errorf("error creating AOM prometheus instance: ID is not found in API response")
id := utils.PathSearch(expression, createPrometheusInstanceRespBody, "").(string)
if id == "" {
return diag.Errorf("unable to find prometheus instance ID from the API response")
}

d.SetId(id.(string))
d.SetId(id)

return resourcePromInstanceRead(ctx, d, meta)
}
Expand Down Expand Up @@ -213,8 +212,9 @@ func resourcePromInstanceDelete(_ context.Context, d *schema.ResourceData, meta
}

_, err = client.Request("DELETE", deletePrometheusInstancePath, &deletePrometheusInstanceOpt)
if err != nil && !hasErrorCode(err, prometheusInstanceNotExistsCode) {
return diag.Errorf("error deleting AOM prometheus instance: %s", err)
if err != nil {
return common.CheckDeletedDiag(d, common.ConvertExpected400ErrInto404Err(err, "error_code", prometheusInstanceNotExistsCode),
"error deleting AOM prometheus instance")
}

return nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/jmespath/go-jmespath"

"github.com/chnsz/golangsdk"
"github.com/chnsz/golangsdk/openstack/common/tags"
Expand Down Expand Up @@ -433,9 +432,8 @@ func flattenInstanceNodes(resp interface{}) []interface{} {

func flattenInstanceDatastore(resp interface{}) interface{} {
var rst []map[string]interface{}
curJson, err := jmespath.Search("datastore", resp)
if err != nil {
log.Printf("[ERROR] error parsing datastore from response= %#v", resp)
curJson := utils.PathSearch("datastore", resp, nil)
if curJson == nil {
return rst
}

Expand All @@ -451,9 +449,8 @@ func flattenInstanceDatastore(resp interface{}) interface{} {

func flattenInstanceBackupStrategy(resp interface{}) interface{} {
var rst []map[string]interface{}
curJson, err := jmespath.Search("backup_strategy", resp)
if err != nil {
log.Printf("[ERROR] error parsing backup_strategy from response= %#v", resp)
curJson := utils.PathSearch("backup_strategy", resp, nil)
if curJson == nil {
return rst
}

Expand Down
Loading

0 comments on commit 5c011f0

Please sign in to comment.