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

Draft: bugfix: azuread_group_role_management_policy - fix update rules logic #1468

Closed
wants to merge 1 commit into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,8 @@ func (r GroupRoleManagementPolicyResource) Create() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 5 * time.Minute,
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
client := metadata.Client.Policies.RoleManagementPolicyClient
clientPolicy := metadata.Client.Policies.RoleManagementPolicyClient
clientPolicyRule := metadata.Client.Policies.RoleManagementPolicyRuleClient

// Fetch the existing policy, as they already exist
id, err := getPolicyId(ctx, metadata, metadata.ResourceData.Get("group_id").(string), metadata.ResourceData.Get("role_id").(string))
Expand All @@ -358,7 +359,7 @@ func (r GroupRoleManagementPolicyResource) Create() sdk.ResourceFunc {
}
metadata.SetID(id)

policy, _, err := client.Get(ctx, id.ID())
policy, _, err := clientPolicy.Get(ctx, id.ID())
if err != nil {
return fmt.Errorf("Could not retrieve existing policy, %+v", err)
}
Expand All @@ -371,9 +372,20 @@ func (r GroupRoleManagementPolicyResource) Create() sdk.ResourceFunc {
return fmt.Errorf("Could not build update request, %+v", err)
}

_, err = client.Update(ctx, *policyUpdate)
// In the case of the policy endpoint, it does not work as expected because the associated rules are changed.
// For this reason, the endpoints for rules are used.
if policyUpdate.Rules != nil {
for _, rule := range *policyUpdate.Rules {
_, err = clientPolicyRule.Update(ctx, *policyUpdate.ID, rule)
if err != nil {
return fmt.Errorf("Could not update existing policy rule request, %+v", err)
}
}
}
policyUpdate.Rules = nil
_, err = clientPolicy.Update(ctx, *policyUpdate)
if err != nil {
return fmt.Errorf("Could not create assignment schedule request, %+v", err)
return fmt.Errorf("Could not update existing policy request, %+v", err)
}

// Update the ID as it changes on modification
Expand Down Expand Up @@ -590,15 +602,16 @@ func (r GroupRoleManagementPolicyResource) Update() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 5 * time.Minute,
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
client := metadata.Client.Policies.RoleManagementPolicyClient
clientPolicy := metadata.Client.Policies.RoleManagementPolicyClient
clientPolicyRule := metadata.Client.Policies.RoleManagementPolicyRuleClient

id, err := parse.ParseRoleManagementPolicyID(metadata.ResourceData.Id())
if err != nil {
return fmt.Errorf("Could not parse policy ID, %+v", err)
}
metadata.SetID(id)

policy, _, err := client.Get(ctx, id.ID())
policy, _, err := clientPolicy.Get(ctx, id.ID())
if err != nil {
return fmt.Errorf("Could not retrieve existing policy, %+v", err)
}
Expand All @@ -611,9 +624,20 @@ func (r GroupRoleManagementPolicyResource) Update() sdk.ResourceFunc {
return fmt.Errorf("Could not build update request, %+v", err)
}

_, err = client.Update(ctx, *policyUpdate)
// In the case of the policy endpoint, it does not work as expected because the associated rules are changed.
// For this reason, the endpoints for rules are used.
if policyUpdate.Rules != nil {
for _, rule := range *policyUpdate.Rules {
_, err = clientPolicyRule.Update(ctx, *policyUpdate.ID, rule)
if err != nil {
return fmt.Errorf("Could not update existing policy rule request, %+v", err)
}
}
}
policyUpdate.Rules = nil
_, err = clientPolicy.Update(ctx, *policyUpdate)
if err != nil {
return fmt.Errorf("Could not create assignment schedule request, %+v", err)
return fmt.Errorf("Could not update existing policy request, %+v", err)
}

// Update the ID as it changes on modification
Expand Down
Loading