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

azuread_group: remediate replication delays when adding/removing group members #283

Merged
merged 2 commits into from
Jun 25, 2020
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
19 changes: 17 additions & 2 deletions azuread/helpers/graph/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"log"
"time"

"github.com/Azure/azure-sdk-for-go/services/graphrbac/1.6/graphrbac"
)
Expand Down Expand Up @@ -121,8 +122,22 @@ func GroupAddMember(client graphrbac.GroupsClient, ctx context.Context, groupId
}

log.Printf("[DEBUG] Adding member with id %q to Azure AD group with id %q", member, groupId)
if _, err := client.AddMember(ctx, groupId, properties); err != nil {
return fmt.Errorf("Error adding group member %q to Azure AD Group with ID %q: %+v", member, groupId, err)
var err error
attempts := 10
for i := 0; i <= attempts; i++ {
if _, err = client.AddMember(ctx, groupId, properties); err == nil {
break
}
if i == attempts {
return fmt.Errorf("Error adding group member %q to Azure AD Group with ID %q: %+v", member, groupId, err)
}
time.Sleep(time.Second * 2)
}

if _, err := WaitForListAdd(member, func() ([]string, error) {
return GroupAllMembers(client, ctx, groupId)
}); err != nil {
return fmt.Errorf("Error waiting for group membership: %+v", err)
}

return nil
Expand Down
50 changes: 50 additions & 0 deletions azuread/helpers/graph/replication.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,53 @@ func WaitForCreationReplication(f func() (interface{}, error)) (interface{}, err
},
}).WaitForState()
}

func WaitForListAdd(item string, f func() ([]string, error)) (interface{}, error) {
return (&resource.StateChangeConf{
Pending: []string{"404"},
Target: []string{"Found"},
Timeout: 5 * time.Minute,
MinTimeout: 1 * time.Second,
ContinuousTargetOccurence: 10,
Refresh: func() (interface{}, string, error) {
listItems, err := f()

if err != nil {
return listItems, "Error", err
}

for _, v := range listItems {
if v == item {
return listItems, "Found", nil
}
}

return listItems, "404", nil
},
}).WaitForState()
}

func WaitForListRemove(item string, f func() ([]string, error)) (interface{}, error) {
return (&resource.StateChangeConf{
Pending: []string{"Found"},
Target: []string{"404"},
Timeout: 5 * time.Minute,
MinTimeout: 1 * time.Second,
ContinuousTargetOccurence: 10,
Refresh: func() (interface{}, string, error) {
listItems, err := f()

if err != nil {
return listItems, "Error", err
}

for _, v := range listItems {
if v == item {
return listItems, "Found", nil
}
}

return listItems, "404", nil
},
}).WaitForState()
}
21 changes: 19 additions & 2 deletions azuread/resource_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package azuread
import (
"fmt"
"log"
"time"

"github.com/Azure/azure-sdk-for-go/services/graphrbac/1.6/graphrbac"
"github.com/Azure/go-autorest/autorest"
"github.com/google/uuid"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
Expand Down Expand Up @@ -186,11 +188,26 @@ func resourceGroupUpdate(d *schema.ResourceData, meta interface{}) error {
membersToAdd := slices.Difference(desiredMembers, existingMembers)

for _, existingMember := range membersForRemoval {
var err error
var resp autorest.Response
log.Printf("[DEBUG] Removing member with id %q from Azure AD group with id %q", existingMember, d.Id())
if resp, err := client.RemoveMember(ctx, d.Id(), existingMember); err != nil {
if !ar.ResponseWasNotFound(resp) {
attempts := 10
for i := 0; i <= attempts; i++ {
if resp, err = client.RemoveMember(ctx, d.Id(), existingMember); err != nil {
if ar.ResponseWasNotFound(resp) {
break
}
}
if i == attempts {
return fmt.Errorf("Error Deleting group member %q from Azure AD Group with ID %q: %+v", existingMember, d.Id(), err)
}
time.Sleep(time.Second * 2)
}

if _, err := graph.WaitForListRemove(existingMember, func() ([]string, error) {
return graph.GroupAllMembers(client, ctx, d.Id())
}); err != nil {
return fmt.Errorf("Error waiting for group membership removal: %+v", err)
}
}

Expand Down