Skip to content

Commit

Permalink
Add account id update in chunk
Browse files Browse the repository at this point in the history
  • Loading branch information
daaru00 committed Aug 7, 2023
1 parent a46dfbb commit 449fd1a
Showing 1 changed file with 44 additions and 11 deletions.
55 changes: 44 additions & 11 deletions internal/document/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,21 +356,54 @@ func (d *Document) Deploy() error {

// Update permissions if needed
if len(accountsToAdd) > 0 || len(accountsToRemove) > 0 {
// Build update input
updateInput := &ssm.ModifyDocumentPermissionInput{
Name: &d.Name,
PermissionType: aws.String("Share"),
}

chunkSize := 20

// Elaborate account ids to add
if len(accountsToAdd) > 0 {
updateInput.AccountIdsToAdd = aws.StringSlice(accountsToAdd)
for i := 0; i < len(accountsToAdd); i += chunkSize {
end := i + chunkSize
if end > len(accountsToAdd) {
end = len(accountsToAdd)
}

// Build update input
updateInput := &ssm.ModifyDocumentPermissionInput{
Name: &d.Name,
PermissionType: aws.String("Share"),
AccountIdsToAdd: aws.StringSlice(accountsToAdd[i:end]),
}

// Execute update
_, err = d.clients.ssm.ModifyDocumentPermission(updateInput)
if err != nil {
return err
}
}
}

// Elaborate account ids to remove
if len(accountsToRemove) > 0 {
updateInput.AccountIdsToRemove = aws.StringSlice(accountsToRemove)
}
for i := 0; i < len(accountsToRemove); i += chunkSize {
end := i + chunkSize
if end > len(accountsToRemove) {
end = len(accountsToRemove)
}

// Execute update
_, err = d.clients.ssm.ModifyDocumentPermission(updateInput)
return err
// Build update input
updateInput := &ssm.ModifyDocumentPermissionInput{
Name: &d.Name,
PermissionType: aws.String("Share"),
AccountIdsToRemove: aws.StringSlice(accountsToRemove[i:end]),
}

// Execute update
_, err = d.clients.ssm.ModifyDocumentPermission(updateInput)
if err != nil {
return err
}
}
}
}

return nil
Expand Down

0 comments on commit 449fd1a

Please sign in to comment.