-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add
codefresh_account_user_association
resource (#123)
## What * Add `codefresh_account_user_association` resource ## Why * Allow account user associations to be made (aka account collaborators) ## Notes <!-- Add any notes here --> ## Checklist * [x] _I have read [CONTRIBUTING.md](https://github.com/codefresh-io/terraform-provider-codefresh/blob/master/CONTRIBUTING.md)._ * [x] _I have [allowed changes to my fork to be made](https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/working-with-forks/allowing-changes-to-a-pull-request-branch-created-from-a-fork)._ * [x] _I have added tests, assuming new tests are warranted_. * [x] _I understand that the `/test` comment will be ignored by the CI trigger [unless it is made by a repo admin or collaborator](https://codefresh.io/docs/docs/pipelines/triggers/git-triggers/#support-for-building-pull-requests-from-forks)._
- Loading branch information
Showing
15 changed files
with
635 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
package codefresh | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
cfClient "github.com/codefresh-io/terraform-provider-codefresh/client" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/customdiff" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
) | ||
|
||
func resourceAccountUserAssociation() *schema.Resource { | ||
return &schema.Resource{ | ||
Description: ` | ||
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. | ||
`, | ||
Create: resourceAccountUserAssociationCreate, | ||
Read: resourceAccountUserAssociationRead, | ||
Update: resourceAccountUserAssociationUpdate, | ||
Delete: resourceAccountUserAssociationDelete, | ||
Importer: &schema.ResourceImporter{ | ||
State: schema.ImportStatePassthrough, | ||
}, | ||
Schema: map[string]*schema.Schema{ | ||
"email": { | ||
Description: ` | ||
The email of the user to associate with the specified account. | ||
If the user is not present in the system, an invitation will be sent to this email. | ||
This field can only be changed when 'status' is 'pending'. | ||
`, | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
"admin": { | ||
Description: "Whether to make this user an account admin.", | ||
Type: schema.TypeBool, | ||
Optional: true, | ||
Default: false, | ||
}, | ||
"username": { | ||
Computed: true, | ||
Type: schema.TypeString, | ||
Description: "The username of the associated user.", | ||
}, | ||
"status": { | ||
Computed: true, | ||
Type: schema.TypeString, | ||
Description: "The status of the association.", | ||
}, | ||
}, | ||
CustomizeDiff: customdiff.All( | ||
// The email field is immutable, except for users with status "pending". | ||
customdiff.ForceNewIf("email", func(_ context.Context, d *schema.ResourceDiff, _ any) bool { | ||
return d.Get("status").(string) != "pending" && d.HasChange("email") | ||
}), | ||
), | ||
} | ||
} | ||
|
||
func resourceAccountUserAssociationCreate(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*cfClient.Client) | ||
currentAccount, err := client.GetCurrentAccount() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
user, err := client.AddNewUserToAccount(currentAccount.ID, "", d.Get("email").(string)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
d.SetId(user.ID) | ||
|
||
if d.Get("admin").(bool) { | ||
err = client.SetUserAsAccountAdmin(currentAccount.ID, d.Id()) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
d.Set("status", user.Status) | ||
|
||
return nil | ||
} | ||
|
||
func resourceAccountUserAssociationRead(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*cfClient.Client) | ||
currentAccount, err := client.GetCurrentAccount() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
userID := d.Id() | ||
if userID == "" { | ||
d.SetId("") | ||
return nil | ||
} | ||
|
||
for _, user := range currentAccount.Users { | ||
if user.ID == userID { | ||
d.Set("email", user.Email) | ||
d.Set("username", user.UserName) | ||
d.Set("status", user.Status) | ||
d.Set("admin", false) // avoid missing attributes after import | ||
for _, admin := range currentAccount.Admins { | ||
if admin.ID == userID { | ||
d.Set("admin", true) | ||
} | ||
} | ||
} | ||
} | ||
|
||
if d.Id() == "" { | ||
return fmt.Errorf("a user with ID %s was not found", userID) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func resourceAccountUserAssociationUpdate(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*cfClient.Client) | ||
|
||
currentAccount, err := client.GetCurrentAccount() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if d.HasChange("email") { | ||
user, err := client.UpdateUserDetails(currentAccount.ID, d.Id(), d.Get("username").(string), d.Get("email").(string)) | ||
if err != nil { | ||
return err | ||
} | ||
if user.Email != d.Get("email").(string) { | ||
return fmt.Errorf("failed to update user email, despite successful API response") | ||
} | ||
} | ||
|
||
if d.HasChange("admin") { | ||
if d.Get("admin").(bool) { | ||
err = client.SetUserAsAccountAdmin(currentAccount.ID, d.Id()) | ||
if err != nil { | ||
return err | ||
} | ||
} else { | ||
err = client.DeleteUserAsAccountAdmin(currentAccount.ID, d.Id()) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func resourceAccountUserAssociationDelete(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*cfClient.Client) | ||
|
||
currentAccount, err := client.GetCurrentAccount() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = client.DeleteUserFromAccount(currentAccount.ID, d.Id()) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.