Skip to content

Commit

Permalink
fix(CodeArts/Deploy): fix error info for permission management
Browse files Browse the repository at this point in the history
  • Loading branch information
saf3dfsa committed Jan 8, 2025
1 parent 1f52fb7 commit f44f409
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package codeartsdeploy
import (
"context"
"fmt"
"log"
"strings"

"github.com/hashicorp/go-multierror"
Expand Down Expand Up @@ -437,7 +438,7 @@ func resourceDeployEnvironmentRead(_ context.Context, d *schema.ResourceData, me

permissionMatrix, err := getDeployEnvironmentPermissionMatrix(client, d.Get("application_id").(string), d.Id())
if err != nil {
return diag.FromErr(err)
log.Printf("[WARN] failed to retrieve environment permission matrix: %s", err)
}

mErr := multierror.Append(nil,
Expand All @@ -450,7 +451,7 @@ func resourceDeployEnvironmentRead(_ context.Context, d *schema.ResourceData, me
d.Set("instance_count", utils.PathSearch("instance_count", environment, nil)),
d.Set("created_by", flattenDeployEnvironmentCreatedBy(environment)),
d.Set("permission", flattenDeployEnvironmentPermission(environment)),
d.Set("permission_matrix", flattenDeployEnvironmentPermissionMatrix(permissionMatrix.([]interface{}))),
d.Set("permission_matrix", flattenDeployEnvironmentPermissionMatrix(permissionMatrix)),
d.Set("hosts", hosts),
d.Set("proxies", proxies),
)
Expand Down Expand Up @@ -575,25 +576,28 @@ func flattenDeployEnvironmentPermission(resp interface{}) []interface{} {
}
}

func flattenDeployEnvironmentPermissionMatrix(resp []interface{}) []interface{} {
rst := make([]interface{}, 0, len(resp))
for _, v := range resp {
rst = append(rst, map[string]interface{}{
"permission_id": utils.PathSearch("id", v, nil),
"role_id": utils.PathSearch("role_id", v, nil),
"role_name": utils.PathSearch("name", v, nil),
"role_type": utils.PathSearch("role_type", v, nil),
"can_view": utils.PathSearch("can_view", v, nil),
"can_edit": utils.PathSearch("can_edit", v, nil),
"can_delete": utils.PathSearch("can_delete", v, nil),
"can_manage": utils.PathSearch("can_manage", v, nil),
"can_deploy": utils.PathSearch("can_deploy", v, nil),
"created_at": utils.PathSearch("create_time", v, nil),
"updated_at": utils.PathSearch("update_time", v, nil),
})
}

return rst
func flattenDeployEnvironmentPermissionMatrix(respBody interface{}) []interface{} {
if resp, isList := respBody.([]interface{}); isList {
rst := make([]interface{}, 0, len(resp))
for _, v := range resp {
rst = append(rst, map[string]interface{}{
"permission_id": utils.PathSearch("id", v, nil),
"role_id": utils.PathSearch("role_id", v, nil),
"role_name": utils.PathSearch("name", v, nil),
"role_type": utils.PathSearch("role_type", v, nil),
"can_view": utils.PathSearch("can_view", v, nil),
"can_edit": utils.PathSearch("can_edit", v, nil),
"can_delete": utils.PathSearch("can_delete", v, nil),
"can_manage": utils.PathSearch("can_manage", v, nil),
"can_deploy": utils.PathSearch("can_deploy", v, nil),
"created_at": utils.PathSearch("create_time", v, nil),
"updated_at": utils.PathSearch("update_time", v, nil),
})
}
return rst
}

return nil
}

func resourceDeployEnvironmentUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ func resourceDeployGroupRead(_ context.Context, d *schema.ResourceData, meta int

permissionMatrix, err := getDeployGroupPermissionMatrix(client, d.Id())
if err != nil {
return diag.FromErr(err)
log.Printf("[WARN] failed to retrieve cluster group permission matrix: %s", err)
}

mErr = multierror.Append(
Expand All @@ -322,7 +322,7 @@ func resourceDeployGroupRead(_ context.Context, d *schema.ResourceData, meta int
d.Set("updated_at", utils.PathSearch("updated_time", resultRespBody, nil)),
d.Set("created_by", flattenDeployGroupCreatedBy(resultRespBody)),
d.Set("permission", flattenDeployGroupPermission(resultRespBody)),
d.Set("permission_matrix", flattenDeployGroupPermissionMatrix(permissionMatrix.([]interface{}))),
d.Set("permission_matrix", flattenDeployGroupPermissionMatrix(permissionMatrix)),
)

return diag.FromErr(mErr.ErrorOrNil())
Expand Down Expand Up @@ -390,25 +390,28 @@ func flattenDeployGroupPermission(resp interface{}) []interface{} {
}
}

func flattenDeployGroupPermissionMatrix(resp []interface{}) []interface{} {
rst := make([]interface{}, 0, len(resp))
for _, v := range resp {
rst = append(rst, map[string]interface{}{
"role_id": utils.PathSearch("role_id", v, nil),
"role_name": utils.PathSearch("name", v, nil),
"role_type": utils.PathSearch("role_type", v, nil),
"can_view": utils.PathSearch("can_view", v, nil),
"can_edit": utils.PathSearch("can_edit", v, nil),
"can_delete": utils.PathSearch("can_delete", v, nil),
"can_add_host": utils.PathSearch("can_add_host", v, nil),
"can_manage": utils.PathSearch("can_manage", v, nil),
"can_copy": utils.PathSearch("can_copy", v, nil),
"created_at": utils.PathSearch("create_time", v, nil),
"updated_at": utils.PathSearch("update_time", v, nil),
})
}

return rst
func flattenDeployGroupPermissionMatrix(respBody interface{}) []interface{} {
if resp, isList := respBody.([]interface{}); isList {
rst := make([]interface{}, 0, len(resp))
for _, v := range resp {
rst = append(rst, map[string]interface{}{
"role_id": utils.PathSearch("role_id", v, nil),
"role_name": utils.PathSearch("name", v, nil),
"role_type": utils.PathSearch("role_type", v, nil),
"can_view": utils.PathSearch("can_view", v, nil),
"can_edit": utils.PathSearch("can_edit", v, nil),
"can_delete": utils.PathSearch("can_delete", v, nil),
"can_add_host": utils.PathSearch("can_add_host", v, nil),
"can_manage": utils.PathSearch("can_manage", v, nil),
"can_copy": utils.PathSearch("can_copy", v, nil),
"created_at": utils.PathSearch("create_time", v, nil),
"updated_at": utils.PathSearch("update_time", v, nil),
})
}
return rst
}

return nil
}

func resourceDeployGroupUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func resourceDeployGroupPermissionRead(_ context.Context, d *schema.ResourceData
expression := fmt.Sprintf("[?role_id=='%s']|[0]", roleId)
role := utils.PathSearch(expression, permissionMatrix, nil)
if role == nil {
return diag.Errorf("unable to find role (%s) from API response", roleId)
return common.CheckDeletedDiag(d, golangsdk.ErrDefault404{}, "unable to find role from API response")
}

mErr := multierror.Append(nil,
Expand Down

0 comments on commit f44f409

Please sign in to comment.