Skip to content
This repository has been archived by the owner on Mar 20, 2023. It is now read-only.

Commit

Permalink
Better handle invitation status for team_user_association during stat…
Browse files Browse the repository at this point in the history
…e refresh & deletion. (#64)
  • Loading branch information
davidji99 authored Jun 2, 2021
1 parent 7728036 commit 9268edf
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
3 changes: 3 additions & 0 deletions docs/resources/team_user_association.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ If the specified `email` belongs to a new user:
* For resource deletion, if the invitation has been accepted, and the user has joined the team,
the user will be removed from the team. If the invitation has not been accepted, the invitation
will be revoked.
* For resource state refresh, if the invitation was either cancelled or rejected, a new invitation
will be sent out on the next resource creation unless the associated Terraform code is removed
from your configuration.

If the specified `email` belongs to an existing Rollbar user:

Expand Down
18 changes: 15 additions & 3 deletions rollbar/resource_rollbar_team_user_association.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ import (
const (
TeamUserAddedStatus = "added"
TeamUserInvitedStatus = "invited"

InviteStatusPending = "pending"
InviteStatusAccepted = "accepted"
InviteStatusRejected = "rejected"
InviteStatusCancelled = "canceled"
)

func resourceRollbarTeamUserAssociation() *schema.Resource {
Expand Down Expand Up @@ -199,7 +204,7 @@ func resourceRollbarTeamUserAssociationRead(ctx context.Context, d *schema.Resou
d.Set("email", email)
d.Set("invitation_status", "")

if invitedOrAdded == TeamUserAddedStatus || d.Get("invitation_status").(string) == "accepted" {
if invitedOrAdded == TeamUserAddedStatus || d.Get("invitation_status").(string) == InviteStatusAccepted {
user, _, userFindErr := findUserByEmail(client, email)
if userFindErr != nil {
diags = append(diags, diag.Diagnostic{
Expand All @@ -223,6 +228,13 @@ func resourceRollbarTeamUserAssociationRead(ctx context.Context, d *schema.Resou
})
return diags
}

// Remove resource from state to trigger recreation if invitation is cancelled or rejected.
if inviteStatus.GetResult().GetStatus() == InviteStatusRejected || inviteStatus.GetResult().GetStatus() == InviteStatusCancelled {
d.SetId("")
return nil
}

d.Set("invitation_status", inviteStatus.GetResult().GetStatus())
}

Expand All @@ -247,7 +259,7 @@ func resourceRollbarTeamUserAssociationDelete(ctx context.Context, d *schema.Res
email := result[1]
invitedOrAdded := d.Get("invited_or_added").(string)

if invitedOrAdded == TeamUserInvitedStatus {
if invitedOrAdded == TeamUserInvitedStatus && d.Get("invitation_status").(string) == InviteStatusPending {
inviteID := d.Get("invitation_id").(int)

log.Printf("[DEBUG] Cancelling invitation %d", inviteID)
Expand All @@ -261,7 +273,7 @@ func resourceRollbarTeamUserAssociationDelete(ctx context.Context, d *schema.Res
log.Printf("[DEBUG] Cancelled invitation %d", inviteID)
}

if invitedOrAdded == TeamUserAddedStatus {
if invitedOrAdded == TeamUserAddedStatus || d.Get("invitation_status").(string) == InviteStatusAccepted {
log.Printf("[DEBUG] Removing %s from team %d", email, teamID)

_, _, removeErr := client.Teams.RemoveUser(teamID, d.Get("user_id").(int))
Expand Down

0 comments on commit 9268edf

Please sign in to comment.