-
Notifications
You must be signed in to change notification settings - Fork 59
fix: implement Read/Update/Delete for stub resources #285
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -47,16 +47,19 @@ func resourceCloudStackAccount() *schema.Resource { | |
| Required: true, | ||
| }, | ||
| "password": { | ||
| Type: schema.TypeString, | ||
| Required: true, | ||
| Type: schema.TypeString, | ||
| Required: true, | ||
| Sensitive: true, | ||
| }, | ||
| "username": { | ||
| Type: schema.TypeString, | ||
| Required: true, | ||
| ForceNew: true, | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Username is the username of the user created inside the account. and the username can be updated for the user. We shouldn't be recreating the account in this case. |
||
| }, | ||
| "account_type": { | ||
| Type: schema.TypeInt, | ||
| Required: true, | ||
| ForceNew: true, | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. AFAIK, this is updated as per the role of the account. |
||
| }, | ||
| "role_id": { | ||
| Type: schema.TypeString, | ||
|
|
@@ -110,9 +113,107 @@ func resourceCloudStackAccountCreate(d *schema.ResourceData, meta interface{}) e | |
| return resourceCloudStackAccountRead(d, meta) | ||
| } | ||
|
|
||
| func resourceCloudStackAccountRead(d *schema.ResourceData, meta interface{}) error { return nil } | ||
| func resourceCloudStackAccountRead(d *schema.ResourceData, meta interface{}) error { | ||
| cs := meta.(*cloudstack.CloudStackClient) | ||
|
|
||
| log.Printf("[DEBUG] Reading Account %s", d.Id()) | ||
|
|
||
|
Comment on lines
+116
to
120
|
||
| func resourceCloudStackAccountUpdate(d *schema.ResourceData, meta interface{}) error { return nil } | ||
| p := cs.Account.NewListAccountsParams() | ||
| p.SetId(d.Id()) | ||
|
|
||
| accounts, err := cs.Account.ListAccounts(p) | ||
| if err != nil { | ||
| return fmt.Errorf("Error retrieving Account %s: %s", d.Id(), err) | ||
| } | ||
|
|
||
| if accounts.Count == 0 { | ||
| log.Printf("[DEBUG] Account %s does no longer exist", d.Id()) | ||
| d.SetId("") | ||
| return nil | ||
| } | ||
|
|
||
| account := accounts.Accounts[0] | ||
|
|
||
| d.Set("account_type", account.Accounttype) | ||
| d.Set("role_id", account.Roleid) | ||
| d.Set("account", account.Name) | ||
| d.Set("domain_id", account.Domainid) | ||
|
|
||
| if len(account.User) > 0 { | ||
| user := account.User[0] | ||
| d.Set("email", user.Email) | ||
| d.Set("first_name", user.Firstname) | ||
| d.Set("last_name", user.Lastname) | ||
| d.Set("username", user.Username) | ||
| } | ||
|
Comment on lines
+142
to
+148
|
||
|
|
||
| log.Printf("[DEBUG] Account %s successfully read", d.Id()) | ||
| return nil | ||
| } | ||
|
|
||
| func resourceCloudStackAccountUpdate(d *schema.ResourceData, meta interface{}) error { | ||
| cs := meta.(*cloudstack.CloudStackClient) | ||
|
|
||
| log.Printf("[DEBUG] Updating Account %s", d.Id()) | ||
|
|
||
| // Handle account-level changes | ||
| if d.HasChange("role_id") || d.HasChange("account") || d.HasChange("domain_id") { | ||
| p := cs.Account.NewUpdateAccountParams() | ||
| p.SetId(d.Id()) | ||
|
|
||
| if d.HasChange("role_id") { | ||
| p.SetRoleid(d.Get("role_id").(string)) | ||
| } | ||
| if d.HasChange("account") { | ||
| p.SetNewname(d.Get("account").(string)) | ||
| } | ||
| if d.HasChange("domain_id") { | ||
| p.SetDomainid(d.Get("domain_id").(string)) | ||
| } | ||
|
|
||
| _, err := cs.Account.UpdateAccount(p) | ||
| if err != nil { | ||
| return fmt.Errorf("Error updating Account %s: %s", d.Id(), err) | ||
| } | ||
| } | ||
|
|
||
| // Handle user-level changes via updateUser API | ||
| if d.HasChange("email") || d.HasChange("first_name") || d.HasChange("last_name") || d.HasChange("password") { | ||
| lp := cs.Account.NewListAccountsParams() | ||
| lp.SetId(d.Id()) | ||
| accounts, err := cs.Account.ListAccounts(lp) | ||
| if err != nil { | ||
| return fmt.Errorf("Error retrieving Account %s for user update: %s", d.Id(), err) | ||
| } | ||
| if accounts.Count == 0 || len(accounts.Accounts[0].User) == 0 { | ||
| return fmt.Errorf("Account %s has no users to update", d.Id()) | ||
| } | ||
|
|
||
| userID := accounts.Accounts[0].User[0].Id | ||
|
|
||
| up := cs.User.NewUpdateUserParams(userID) | ||
|
|
||
| if d.HasChange("email") { | ||
| up.SetEmail(d.Get("email").(string)) | ||
| } | ||
| if d.HasChange("first_name") { | ||
| up.SetFirstname(d.Get("first_name").(string)) | ||
| } | ||
| if d.HasChange("last_name") { | ||
| up.SetLastname(d.Get("last_name").(string)) | ||
| } | ||
| if d.HasChange("password") { | ||
| up.SetPassword(d.Get("password").(string)) | ||
| } | ||
|
|
||
| _, err = cs.User.UpdateUser(up) | ||
| if err != nil { | ||
| return fmt.Errorf("Error updating user for Account %s: %s", d.Id(), err) | ||
| } | ||
| } | ||
|
|
||
| log.Printf("[DEBUG] Account %s successfully updated", d.Id()) | ||
| return resourceCloudStackAccountRead(d, meta) | ||
| } | ||
|
|
||
| func resourceCloudStackAccountDelete(d *schema.ResourceData, meta interface{}) error { | ||
| cs := meta.(*cloudstack.CloudStackClient) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -37,6 +37,7 @@ func resourceCloudStackUser() *schema.Resource { | |
| "account": { | ||
| Type: schema.TypeString, | ||
| Optional: true, | ||
| ForceNew: true, | ||
| }, | ||
| "email": { | ||
| Type: schema.TypeString, | ||
|
|
@@ -51,12 +52,14 @@ func resourceCloudStackUser() *schema.Resource { | |
| Required: true, | ||
| }, | ||
| "password": { | ||
| Type: schema.TypeString, | ||
| Required: true, | ||
| Type: schema.TypeString, | ||
| Required: true, | ||
| Sensitive: true, | ||
| }, | ||
| "username": { | ||
| Type: schema.TypeString, | ||
| Required: true, | ||
| ForceNew: true, | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. force new shouldn't be required here. |
||
| }, | ||
| }, | ||
| } | ||
|
|
@@ -88,6 +91,28 @@ func resourceCloudStackUserCreate(d *schema.ResourceData, meta interface{}) erro | |
| } | ||
|
|
||
| func resourceCloudStackUserUpdate(d *schema.ResourceData, meta interface{}) error { | ||
| cs := meta.(*cloudstack.CloudStackClient) | ||
|
|
||
| p := cs.User.NewUpdateUserParams(d.Id()) | ||
|
|
||
| if d.HasChange("email") { | ||
| p.SetEmail(d.Get("email").(string)) | ||
| } | ||
| if d.HasChange("first_name") { | ||
| p.SetFirstname(d.Get("first_name").(string)) | ||
| } | ||
| if d.HasChange("last_name") { | ||
| p.SetLastname(d.Get("last_name").(string)) | ||
| } | ||
| if d.HasChange("password") { | ||
| p.SetPassword(d.Get("password").(string)) | ||
| } | ||
|
kristofer-atlas marked this conversation as resolved.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add changes for username here |
||
|
|
||
| _, err := cs.User.UpdateUser(p) | ||
| if err != nil { | ||
| return fmt.Errorf("Error updating User %s: %s", d.Id(), err) | ||
| } | ||
|
kristofer-atlas marked this conversation as resolved.
|
||
|
|
||
| return resourceCloudStackUserRead(d, meta) | ||
| } | ||
|
|
||
|
|
@@ -106,5 +131,22 @@ func resourceCloudStackUserDelete(d *schema.ResourceData, meta interface{}) erro | |
| } | ||
|
|
||
| func resourceCloudStackUserRead(d *schema.ResourceData, meta interface{}) error { | ||
| cs := meta.(*cloudstack.CloudStackClient) | ||
|
|
||
| user, count, err := cs.User.GetUserByID(d.Id()) | ||
| if err != nil { | ||
| if count == 0 { | ||
| d.SetId("") | ||
| return nil | ||
| } | ||
| return fmt.Errorf("Error reading User %s: %s", d.Id(), err) | ||
| } | ||
|
|
||
| d.Set("account", user.Account) | ||
| d.Set("email", user.Email) | ||
| d.Set("first_name", user.Firstname) | ||
| d.Set("last_name", user.Lastname) | ||
| d.Set("username", user.Username) | ||
|
|
||
| return nil | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.