|
| 1 | +package codefresh |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + cfClient "github.com/codefresh-io/terraform-provider-codefresh/client" |
| 8 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/customdiff" |
| 9 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 10 | +) |
| 11 | + |
| 12 | +func resourceAccountUserAssociation() *schema.Resource { |
| 13 | + return &schema.Resource{ |
| 14 | + Description: ` |
| 15 | + Associates a user with the account which the provider is authenticated against. If the user is not present in the system, an invitation will be sent to the specified email address. |
| 16 | + `, |
| 17 | + Create: resourceAccountUserAssociationCreate, |
| 18 | + Read: resourceAccountUserAssociationRead, |
| 19 | + Update: resourceAccountUserAssociationUpdate, |
| 20 | + Delete: resourceAccountUserAssociationDelete, |
| 21 | + Importer: &schema.ResourceImporter{ |
| 22 | + State: schema.ImportStatePassthrough, |
| 23 | + }, |
| 24 | + Schema: map[string]*schema.Schema{ |
| 25 | + "email": { |
| 26 | + Description: ` |
| 27 | + The email of the user to associate with the specified account. |
| 28 | + If the user is not present in the system, an invitation will be sent to this email. |
| 29 | + This field can only be changed when 'status' is 'pending'. |
| 30 | + `, |
| 31 | + Type: schema.TypeString, |
| 32 | + Required: true, |
| 33 | + }, |
| 34 | + "admin": { |
| 35 | + Description: "Whether to make this user an account admin.", |
| 36 | + Type: schema.TypeBool, |
| 37 | + Optional: true, |
| 38 | + Default: false, |
| 39 | + }, |
| 40 | + "username": { |
| 41 | + Computed: true, |
| 42 | + Type: schema.TypeString, |
| 43 | + Description: "The username of the associated user.", |
| 44 | + }, |
| 45 | + "status": { |
| 46 | + Computed: true, |
| 47 | + Type: schema.TypeString, |
| 48 | + Description: "The status of the association.", |
| 49 | + }, |
| 50 | + }, |
| 51 | + CustomizeDiff: customdiff.All( |
| 52 | + // The email field is immutable, except for users with status "pending". |
| 53 | + customdiff.ForceNewIf("email", func(_ context.Context, d *schema.ResourceDiff, _ any) bool { |
| 54 | + return d.Get("status").(string) != "pending" && d.HasChange("email") |
| 55 | + }), |
| 56 | + ), |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +func resourceAccountUserAssociationCreate(d *schema.ResourceData, meta interface{}) error { |
| 61 | + client := meta.(*cfClient.Client) |
| 62 | + currentAccount, err := client.GetCurrentAccount() |
| 63 | + if err != nil { |
| 64 | + return err |
| 65 | + } |
| 66 | + |
| 67 | + user, err := client.AddNewUserToAccount(currentAccount.ID, "", d.Get("email").(string)) |
| 68 | + if err != nil { |
| 69 | + return err |
| 70 | + } |
| 71 | + |
| 72 | + d.SetId(user.ID) |
| 73 | + |
| 74 | + if d.Get("admin").(bool) { |
| 75 | + err = client.SetUserAsAccountAdmin(currentAccount.ID, d.Id()) |
| 76 | + if err != nil { |
| 77 | + return err |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + d.Set("status", user.Status) |
| 82 | + |
| 83 | + return nil |
| 84 | +} |
| 85 | + |
| 86 | +func resourceAccountUserAssociationRead(d *schema.ResourceData, meta interface{}) error { |
| 87 | + client := meta.(*cfClient.Client) |
| 88 | + currentAccount, err := client.GetCurrentAccount() |
| 89 | + if err != nil { |
| 90 | + return err |
| 91 | + } |
| 92 | + |
| 93 | + userID := d.Id() |
| 94 | + if userID == "" { |
| 95 | + d.SetId("") |
| 96 | + return nil |
| 97 | + } |
| 98 | + |
| 99 | + for _, user := range currentAccount.Users { |
| 100 | + if user.ID == userID { |
| 101 | + d.Set("email", user.Email) |
| 102 | + d.Set("username", user.UserName) |
| 103 | + d.Set("status", user.Status) |
| 104 | + d.Set("admin", false) // avoid missing attributes after import |
| 105 | + for _, admin := range currentAccount.Admins { |
| 106 | + if admin.ID == userID { |
| 107 | + d.Set("admin", true) |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + if d.Id() == "" { |
| 114 | + return fmt.Errorf("a user with ID %s was not found", userID) |
| 115 | + } |
| 116 | + |
| 117 | + return nil |
| 118 | +} |
| 119 | + |
| 120 | +func resourceAccountUserAssociationUpdate(d *schema.ResourceData, meta interface{}) error { |
| 121 | + client := meta.(*cfClient.Client) |
| 122 | + |
| 123 | + currentAccount, err := client.GetCurrentAccount() |
| 124 | + if err != nil { |
| 125 | + return err |
| 126 | + } |
| 127 | + |
| 128 | + if d.HasChange("email") { |
| 129 | + user, err := client.UpdateUserDetails(currentAccount.ID, d.Id(), d.Get("username").(string), d.Get("email").(string)) |
| 130 | + if err != nil { |
| 131 | + return err |
| 132 | + } |
| 133 | + if user.Email != d.Get("email").(string) { |
| 134 | + return fmt.Errorf("failed to update user email, despite successful API response") |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + if d.HasChange("admin") { |
| 139 | + if d.Get("admin").(bool) { |
| 140 | + err = client.SetUserAsAccountAdmin(currentAccount.ID, d.Id()) |
| 141 | + if err != nil { |
| 142 | + return err |
| 143 | + } |
| 144 | + } else { |
| 145 | + err = client.DeleteUserAsAccountAdmin(currentAccount.ID, d.Id()) |
| 146 | + if err != nil { |
| 147 | + return err |
| 148 | + } |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + return nil |
| 153 | +} |
| 154 | + |
| 155 | +func resourceAccountUserAssociationDelete(d *schema.ResourceData, meta interface{}) error { |
| 156 | + client := meta.(*cfClient.Client) |
| 157 | + |
| 158 | + currentAccount, err := client.GetCurrentAccount() |
| 159 | + if err != nil { |
| 160 | + return err |
| 161 | + } |
| 162 | + |
| 163 | + err = client.DeleteUserFromAccount(currentAccount.ID, d.Id()) |
| 164 | + if err != nil { |
| 165 | + return err |
| 166 | + } |
| 167 | + |
| 168 | + return nil |
| 169 | +} |
0 commit comments