Skip to content

Commit

Permalink
develop-ram-group-resource (#6)
Browse files Browse the repository at this point in the history
* Enhance st-alicloud_ram_user_group_attachment code.

---------

Co-authored-by: xiaotongwong <[email protected]>
  • Loading branch information
WongXiaoTong and xiaotongwong authored Mar 28, 2023
1 parent f03d9cf commit 76733f3
Showing 1 changed file with 13 additions and 21 deletions.
34 changes: 13 additions & 21 deletions alicloud/resource_ram_user_group_attachment.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,9 @@ func (r *ramUserGroupAttachmentResource) Create(ctx context.Context, req resourc
return
}

addUserToGroupRequest := &alicloudRamClient.AddUserToGroupRequest{
UserName: tea.String(plan.UserName.ValueString()),
GroupName: tea.String(plan.GroupName.ValueString()),
}

err := r.addUserToGroup(addUserToGroupRequest)
if err != nil {
if err := r.addUserToGroup(plan); err != nil {
resp.Diagnostics.AddError(
"[API ERROR] Failed to Add User to Group",
"[API ERROR] Failed to Add User to Group.",
err.Error(),
)
return
Expand Down Expand Up @@ -155,15 +149,9 @@ func (r *ramUserGroupAttachmentResource) Update(ctx context.Context, req resourc
return
}

updateUserGroupRequest := &alicloudRamClient.AddUserToGroupRequest{
UserName: tea.String(plan.UserName.ValueString()),
GroupName: tea.String(plan.GroupName.ValueString()),
}

err := r.addUserToGroup(updateUserGroupRequest)
if err != nil {
if err := r.addUserToGroup(plan); err != nil {
resp.Diagnostics.AddError(
"[API ERROR] Failed to Add User to Group",
"[API ERROR] Failed to Add User to Group.",
err.Error(),
)
return
Expand Down Expand Up @@ -195,21 +183,25 @@ func (r *ramUserGroupAttachmentResource) Delete(ctx context.Context, req resourc

runtime := &util.RuntimeOptions{}

_, err := r.client.RemoveUserFromGroupWithOptions(removeUserFromGroupRequest, runtime)
if err != nil {
if _, err := r.client.RemoveUserFromGroupWithOptions(removeUserFromGroupRequest, runtime); err != nil {
resp.Diagnostics.AddError(
"[API ERROR] Failed to Remove User from Group",
err.Error(),
)
return
}
}

func (r *ramUserGroupAttachmentResource) addUserToGroup(req *alicloudRamClient.AddUserToGroupRequest) (err error) {
func (r *ramUserGroupAttachmentResource) addUserToGroup(plan *ramUserGroupAttachmentResourceModel) (err error) {
addUserToGroupRequest := &alicloudRamClient.AddUserToGroupRequest{
UserName: tea.String(plan.UserName.ValueString()),
GroupName: tea.String(plan.GroupName.ValueString()),
}

addUserToGroup := func() error {
runtime := &util.RuntimeOptions{}

_, err := r.client.AddUserToGroupWithOptions(req, runtime)
if err != nil {
if _, err := r.client.AddUserToGroupWithOptions(addUserToGroupRequest, runtime); err != nil {
if _t, ok := err.(*tea.SDKError); ok {
if isAbleToRetry(*_t.Code) {
return err
Expand Down

0 comments on commit 76733f3

Please sign in to comment.