Skip to content

Commit

Permalink
Ensure association resources perform operations through mutex
Browse files Browse the repository at this point in the history
  • Loading branch information
sergiught committed Jan 4, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 5c5219f commit 704cd91
Showing 3 changed files with 33 additions and 2 deletions.
8 changes: 8 additions & 0 deletions internal/provider/resource_auth0_connection_client.go
Original file line number Diff line number Diff line change
@@ -87,6 +87,10 @@ func createConnectionClient(ctx context.Context, data *schema.ResourceData, meta
api := meta.(*management.Management)

connectionID := data.Get("connection_id").(string)

globalMutex.Lock(connectionID)
defer globalMutex.Unlock(connectionID)

connection, err := api.Connection.Read(connectionID)
if err != nil {
return diag.FromErr(err)
@@ -145,6 +149,10 @@ func deleteConnectionClient(_ context.Context, data *schema.ResourceData, meta i
api := meta.(*management.Management)

connectionID := data.Get("connection_id").(string)

globalMutex.Lock(connectionID)
defer globalMutex.Unlock(connectionID)

connection, err := api.Connection.Read(connectionID)
if err != nil {
if mErr, ok := err.(management.Error); ok && mErr.Status() == http.StatusNotFound {
12 changes: 12 additions & 0 deletions internal/provider/resource_auth0_organization_connection.go
Original file line number Diff line number Diff line change
@@ -94,6 +94,10 @@ func createOrganizationConnection(ctx context.Context, data *schema.ResourceData
api := meta.(*management.Management)

organizationID := data.Get("organization_id").(string)

globalMutex.Lock(organizationID)
defer globalMutex.Unlock(organizationID)

connectionID := data.Get("connection_id").(string)
assignMembershipOnLogin := data.Get("assign_membership_on_login").(bool)

@@ -139,6 +143,10 @@ func updateOrganizationConnection(ctx context.Context, data *schema.ResourceData
api := meta.(*management.Management)

organizationID := data.Get("organization_id").(string)

globalMutex.Lock(organizationID)
defer globalMutex.Unlock(organizationID)

connectionID := data.Get("connection_id").(string)
assignMembershipOnLogin := data.Get("assign_membership_on_login").(bool)

@@ -157,6 +165,10 @@ func deleteOrganizationConnection(ctx context.Context, data *schema.ResourceData
api := meta.(*management.Management)

organizationID := data.Get("organization_id").(string)

globalMutex.Lock(organizationID)
defer globalMutex.Unlock(organizationID)

connectionID := data.Get("connection_id").(string)

if err := api.Organization.DeleteConnection(organizationID, connectionID); err != nil {
15 changes: 13 additions & 2 deletions internal/provider/resource_auth0_organization_member.go
Original file line number Diff line number Diff line change
@@ -86,6 +86,9 @@ func createOrganizationMember(ctx context.Context, d *schema.ResourceData, m int
userID := d.Get("user_id").(string)
orgID := d.Get("organization_id").(string)

globalMutex.Lock(orgID)
defer globalMutex.Unlock(orgID)

if err := api.Organization.AddMembers(orgID, []string{userID}); err != nil {
return diag.FromErr(err)
}
@@ -104,8 +107,8 @@ func assignRoles(d *schema.ResourceData, api *management.Management) error {
return nil
}

orgID := d.Get("organization_id").(string)
userID := d.Get("user_id").(string)
orgID := d.Get("organization_id").(string)

toAdd, toRemove := value.Difference(d, "roles")

@@ -179,6 +182,11 @@ func readOrganizationMember(ctx context.Context, d *schema.ResourceData, m inter
func updateOrganizationMember(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
api := m.(*management.Management)

orgID := d.Get("organization_id").(string)

globalMutex.Lock(orgID)
defer globalMutex.Unlock(orgID)

if err := assignRoles(d, api); err != nil {
return diag.FromErr(fmt.Errorf("failed to assign members to organization. %w", err))
}
@@ -189,8 +197,11 @@ func updateOrganizationMember(ctx context.Context, d *schema.ResourceData, m int
func deleteOrganizationMember(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
api := m.(*management.Management)

orgID := d.Get("organization_id").(string)
userID := d.Get("user_id").(string)
orgID := d.Get("organization_id").(string)

globalMutex.Lock(orgID)
defer globalMutex.Unlock(orgID)

if err := api.Organization.DeleteMember(orgID, []string{userID}); err != nil {
if err, ok := err.(management.Error); ok && err.Status() == http.StatusNotFound {

0 comments on commit 704cd91

Please sign in to comment.