Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SCALRCORE-21783 Add scalr_service_account_token resource
[API_BRANCH]
- Loading branch information
1 parent
c81e4fc
commit 76f6889
Showing
11 changed files
with
290 additions
and
24 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
|
||
# Resource `scalr_service_account_token` | ||
|
||
Manage the state of service account's tokens in Scalr. Create, update and destroy. | ||
|
||
## Example Usage | ||
|
||
Basic usage: | ||
|
||
```hcl | ||
resource "scalr_service_account_token" "default" { | ||
service_account_id = "sa-xxxxxxx" | ||
description = "Some description" | ||
} | ||
``` | ||
|
||
## Argument Reference | ||
|
||
* `service_account_id` - (Required) ID of the service account. | ||
* `description` - (Optional) Description of the token. | ||
|
||
## Attribute Reference | ||
|
||
All arguments plus: | ||
|
||
* `id` - The ID of the token. | ||
* `token` - (Sensitive) The token of the service account. |
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
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,146 @@ | ||
package scalr | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
"log" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
"github.com/scalr/go-scalr" | ||
) | ||
|
||
func resourceScalrServiceAccountToken() *schema.Resource { | ||
return &schema.Resource{ | ||
CreateContext: resourceScalrServiceAccountTokenCreate, | ||
ReadContext: resourceScalrServiceAccountTokenRead, | ||
UpdateContext: resourceScalrServiceAccountTokenUpdate, | ||
DeleteContext: resourceScalrServiceAccountTokenDelete, | ||
Schema: map[string]*schema.Schema{ | ||
"service_account_id": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
"description": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
}, | ||
"token": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Sensitive: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceScalrServiceAccountTokenCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
scalrClient := meta.(*scalr.Client) | ||
|
||
saID := d.Get("service_account_id").(string) | ||
|
||
options := scalr.AccessTokenCreateOptions{} | ||
if desc, ok := d.GetOk("description"); ok { | ||
options.Description = scalr.String(desc.(string)) | ||
} | ||
|
||
log.Printf("[DEBUG] Create access token for service account: %s", saID) | ||
at, err := scalrClient.ServiceAccountTokens.Create(ctx, saID, options) | ||
if err != nil { | ||
return diag.Errorf( | ||
"Error creating access token for service account %s: %v", saID, err) | ||
} | ||
|
||
// the token is returned from API only while creating | ||
_ = d.Set("token", at.Token) | ||
|
||
d.SetId(at.ID) | ||
|
||
return resourceScalrServiceAccountTokenRead(ctx, d, meta) | ||
} | ||
|
||
func resourceScalrServiceAccountTokenRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
scalrClient := meta.(*scalr.Client) | ||
id := d.Id() | ||
saID := d.Get("service_account_id").(string) | ||
|
||
if saID == "" { | ||
return diag.Errorf("This resource does not support import") | ||
} | ||
|
||
log.Printf("[DEBUG] Read service account token: %s", id) | ||
options := scalr.AccessTokenListOptions{} | ||
|
||
for { | ||
atl, err := scalrClient.ServiceAccountTokens.List(ctx, saID, options) | ||
|
||
if err != nil { | ||
if errors.Is(err, scalr.ErrResourceNotFound) { | ||
log.Printf("[DEBUG] service account %s not found", saID) | ||
d.SetId("") | ||
return nil | ||
} | ||
return diag.Errorf("Error reading service account token %s: %v", id, err) | ||
} | ||
|
||
for _, at := range atl.Items { | ||
if at.ID == id { | ||
_ = d.Set("description", at.Description) | ||
return nil | ||
} | ||
} | ||
|
||
// Exit the loop when we've seen all pages. | ||
if atl.CurrentPage >= atl.TotalPages { | ||
break | ||
} | ||
|
||
// Update the page number to get the next page. | ||
options.PageNumber = atl.NextPage | ||
} | ||
|
||
// the token has been deleted | ||
d.SetId("") | ||
return nil | ||
} | ||
|
||
func resourceScalrServiceAccountTokenUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
scalrClient := meta.(*scalr.Client) | ||
|
||
id := d.Id() | ||
|
||
if d.HasChange("description") { | ||
desc := d.Get("description").(string) | ||
|
||
options := scalr.AccessTokenUpdateOptions{ | ||
Description: scalr.String(desc), | ||
} | ||
|
||
log.Printf("[DEBUG] Update service account access token %s", id) | ||
_, err := scalrClient.AccessTokens.Update(ctx, id, options) | ||
if err != nil { | ||
return diag.Errorf( | ||
"Error updating service account access token %s: %v", id, err) | ||
} | ||
} | ||
|
||
return resourceScalrServiceAccountTokenRead(ctx, d, meta) | ||
} | ||
|
||
func resourceScalrServiceAccountTokenDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
scalrClient := meta.(*scalr.Client) | ||
id := d.Id() | ||
|
||
log.Printf("[DEBUG] Delete service account access token %s", id) | ||
err := scalrClient.AccessTokens.Delete(ctx, id) | ||
if err != nil { | ||
if errors.Is(err, scalr.ErrResourceNotFound) { | ||
return nil | ||
} | ||
return diag.Errorf( | ||
"Error deleting service account access token %s: %v", id, err) | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.