-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add service_account_token resource
* adds a new resource `doppler_service_account_token` * added example usage * autogenerated docs for newly added resource
- Loading branch information
1 parent
1d5e488
commit c11e379
Showing
7 changed files
with
255 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
--- | ||
page_title: "doppler_service_account_token Resource - terraform-provider-doppler" | ||
subcategory: "" | ||
description: |- | ||
Manage a Doppler service_account_token. | ||
--- | ||
|
||
# doppler_service_account_token (Resource) | ||
|
||
Manage a Doppler service account token. | ||
|
||
## Example Usage | ||
|
||
```terraform | ||
resource "doppler_service_account_token" "builder_ci_token" { | ||
service_account = "builder" | ||
name = "Builder CI Token" | ||
expires_at = "2024-05-30T11:00:00.000Z" | ||
} | ||
# Service token key available as `doppler_service_account_token.builder_ci_token.api_key` | ||
``` | ||
|
||
<!-- schema generated by tfplugindocs --> | ||
## Schema | ||
|
||
### Required | ||
|
||
- `name` (String) The display name of the API token | ||
- `service_account_slug` (String) Slug of the service account | ||
|
||
### Optional | ||
|
||
- `expires_at` (String) The datetime at which the API token should expire. If not provided, the API token will remain valid indefinitely unless manually revoked | ||
|
||
### Read-Only | ||
|
||
- `api_key` (String, Sensitive) The api key used to authenticate the service account | ||
- `created_at` (String) The datetime that the token was created. | ||
- `id` (String) The ID of this resource. | ||
- `slug` (String) Slug of the service account token |
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,129 @@ | ||
package doppler | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
) | ||
|
||
func resourceServiceAccountToken() *schema.Resource { | ||
return &schema.Resource{ | ||
CreateContext: resourceServiceAccountTokenCreate, | ||
ReadContext: resourceServiceAccountTokenRead, | ||
DeleteContext: resourceServiceAccountTokenDelete, | ||
// ForceNew is specified for all user-specified fields | ||
// Service account tokens cannot be moved, renamed, or edited to change their access | ||
Schema: map[string]*schema.Schema{ | ||
"service_account_slug": { | ||
Description: "Slug of the service account", | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
"name": { | ||
Description: "The display name of the API token", | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
"slug": { | ||
Description: "Slug of the service account token", | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"expires_at": { | ||
Description: "The datetime at which the API token should expire. " + | ||
"If not provided, the API token will remain valid indefinitely unless manually revoked", | ||
Type: schema.TypeString, | ||
Optional: true, | ||
ForceNew: true, | ||
}, | ||
"created_at": { | ||
Description: "The datetime that the token was created.", | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"api_key": { | ||
Description: "The api key used to authenticate the service account", | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Sensitive: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceServiceAccountTokenCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { | ||
client := m.(APIClient) | ||
|
||
var diags diag.Diagnostics | ||
serviceAccount := d.Get("service_account_slug").(string) | ||
name := d.Get("name").(string) | ||
expiresAt, ok := d.Get("expires_at").(string) | ||
if !ok { | ||
expiresAt = "" | ||
} | ||
|
||
token, err := client.CreateServiceAccountToken(ctx, serviceAccount, name, expiresAt) | ||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
|
||
d.SetId(token.ServiceAccountToken.getResourceId()) | ||
|
||
if err = d.Set("expires_at", token.ServiceAccountToken.ExpiresAt); err != nil { | ||
return diag.FromErr(err) | ||
} | ||
if err = d.Set("created_at", token.ServiceAccountToken.CreatedAt); err != nil { | ||
return diag.FromErr(err) | ||
} | ||
if err = d.Set("api_key", token.ApiKey); err != nil { | ||
return diag.FromErr(err) | ||
} | ||
if err = d.Set("slug", token.ServiceAccountToken.Slug); err != nil { | ||
return diag.FromErr(err) | ||
} | ||
|
||
return diags | ||
} | ||
|
||
func resourceServiceAccountTokenRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { | ||
client := m.(APIClient) | ||
|
||
var diags diag.Diagnostics | ||
serviceAccount := d.Get("service_account_slug").(string) | ||
slug := d.Id() | ||
|
||
token, err := client.GetServiceAccountToken(ctx, serviceAccount, slug) | ||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
|
||
if err = d.Set("name", token.ServiceAccountToken.Name); err != nil { | ||
return diag.FromErr(err) | ||
} | ||
if err = d.Set("expires_at", token.ServiceAccountToken.ExpiresAt); err != nil { | ||
return diag.FromErr(err) | ||
} | ||
if err = d.Set("created_at", token.ServiceAccountToken.CreatedAt); err != nil { | ||
return diag.FromErr(err) | ||
} | ||
if err = d.Set("slug", token.ServiceAccountToken.Slug); err != nil { | ||
return diag.FromErr(err) | ||
} | ||
return diags | ||
} | ||
|
||
func resourceServiceAccountTokenDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { | ||
client := m.(APIClient) | ||
|
||
var diags diag.Diagnostics | ||
serviceAccount := d.Get("service_account_slug").(string) | ||
slug := d.Id() | ||
|
||
if err := client.DeleteServiceAccountToken(ctx, serviceAccount, slug); err != nil { | ||
return diag.FromErr(err) | ||
} | ||
return diags | ||
} |
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,7 @@ | ||
resource "doppler_service_account_token" "builder_ci_token" { | ||
service_account = "builder" | ||
name = "Builder CI Token" | ||
expires_at = "2024-05-30T11:00:00.000Z" | ||
} | ||
|
||
# Service token key available as `doppler_service_account_token.builder_ci_token.api_key` |
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,16 @@ | ||
--- | ||
page_title: "doppler_service_account_token Resource - terraform-provider-doppler" | ||
subcategory: "" | ||
description: |- | ||
Manage a Doppler service_account_token. | ||
--- | ||
|
||
# doppler_service_account_token (Resource) | ||
|
||
Manage a Doppler service account token. | ||
|
||
## Example Usage | ||
|
||
{{tffile "examples/resources/service_account_token.tf"}} | ||
|
||
{{ .SchemaMarkdown | trimspace }} |