Skip to content

Commit

Permalink
[IF-847] Add to set and update expiration date of API keys (#45)
Browse files Browse the repository at this point in the history
* Add to set and update expiration date of API keys

* Update documentation
  • Loading branch information
sternik authored Apr 3, 2023
1 parent 22ee7c7 commit 7506a74
Show file tree
Hide file tree
Showing 14 changed files with 427 additions and 56 deletions.
4 changes: 4 additions & 0 deletions docs/data-sources/api_key.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ resource "imagefactory_role_binding" "key_binding" {

- `name` (String)

### Optional

- `expires_at` (String) API key expiration date in format: 2023-11-04

### Read-Only

- `id` (String) The ID of this resource.
Expand Down
2 changes: 1 addition & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ terraform {
required_providers {
imagefactory = {
source = "nordcloud/imagefactory"
version = "1.5.0"
version = "1.5.1"
}
}
}
Expand Down
19 changes: 18 additions & 1 deletion docs/resources/api_key.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
page_title: "imagefactory_api_key Resource - terraform-provider-imagefactory"
subcategory: ""
description: |-
---

# imagefactory_api_key (Resource)
Expand All @@ -22,6 +22,17 @@ resource "imagefactory_api_key" "api_key" {
output "api_key" {
value = imagefactory_api_key.api_key
}
# Create API key with expiration date
resource "imagefactory_api_key" "api_key_with_expiration" {
name = "IF API Key"
expires_at = "2023-04-27"
}
output "api_key_with_expiration" {
value = imagefactory_api_key.api_key_with_expiration
}
```

<!-- schema generated by tfplugindocs -->
Expand All @@ -31,6 +42,10 @@ output "api_key" {

- `name` (String)

### Optional

- `expires_at` (String) API key expiration date in format: 2023-11-04

### Read-Only

- `id` (String) The ID of this resource.
Expand All @@ -41,5 +56,7 @@ output "api_key" {
Import is supported using the following syntax:

```shell
# Copyright 2023 Nordcloud Oy or its affiliates. All Rights Reserved.

terraform import imagefactory_api_key.tf_name RESOURCE_ID
```
2 changes: 2 additions & 0 deletions docs/resources/role.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,7 @@ Required:
Import is supported using the following syntax:

```shell
# Copyright 2023 Nordcloud Oy or its affiliates. All Rights Reserved.

terraform import imagefactory_role.tf_name RESOURCE_ID
```
2 changes: 1 addition & 1 deletion examples/provider/provider.tf
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ terraform {
required_providers {
imagefactory = {
source = "nordcloud/imagefactory"
version = "1.5.0"
version = "1.5.1"
}
}
}
Expand Down
11 changes: 11 additions & 0 deletions examples/resources/imagefactory_api_key/resource.tf
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,14 @@ resource "imagefactory_api_key" "api_key" {
output "api_key" {
value = imagefactory_api_key.api_key
}

# Create API key with expiration date

resource "imagefactory_api_key" "api_key_with_expiration" {
name = "IF API Key"
expires_at = "2023-04-27"
}

output "api_key_with_expiration" {
value = imagefactory_api_key.api_key_with_expiration
}
34 changes: 33 additions & 1 deletion imagefactory/apikey/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package apikey

import (
"context"
"time"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand All @@ -17,7 +18,7 @@ func Resource() *schema.Resource {
return &schema.Resource{
CreateContext: resourceAPIKeyCreate,
ReadContext: resourceAPIKeyRead,
UpdateContext: schema.NoopContext,
UpdateContext: resourceAPIKeyUpdate,
DeleteContext: resourceAPIKeyDelete,
Schema: apiKeySchema,
Importer: &schema.ResourceImporter{
Expand All @@ -33,6 +34,12 @@ func resourceAPIKeyCreate(ctx context.Context, d *schema.ResourceData, m interfa
Name: graphql.String(d.Get("name").(string)),
}

if len(d.Get("expires_at").(string)) > 0 {
expiresAtDate, _ := time.Parse(expiresAtDateFormat, d.Get("expires_at").(string))
expiresAt := graphql.String(expiresAtDate.Format(time.RFC3339))
input.ExpiresAt = &expiresAt
}

apiKey, err := c.APIClient.CreateAPIKey(input)
if err != nil {
return diag.FromErr(err)
Expand All @@ -52,6 +59,31 @@ func resourceAPIKeyRead(ctx context.Context, d *schema.ResourceData, m interface
return setProps(d, apiKey)
}

func resourceAPIKeyUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
c := m.(*config.Config)

apiKeyID := d.Id()
name := graphql.String(d.Get("name").(string))

input := sdk.APIKeyChanges{
ID: graphql.String(apiKeyID),
Name: &name,
}

if len(d.Get("expires_at").(string)) > 0 {
expiresAtDate, _ := time.Parse(expiresAtDateFormat, d.Get("expires_at").(string))
expiresAt := graphql.String(expiresAtDate.Format(time.RFC3339))
input.ExpiresAt = &expiresAt
}

apiKey, err := c.APIClient.UpdateAPIKey(input)
if err != nil {
return diag.FromErr(err)
}

return setProps(d, apiKey)
}

func resourceAPIKeyDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
var diags diag.Diagnostics

Expand Down
51 changes: 50 additions & 1 deletion imagefactory/apikey/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@
package apikey

import (
"fmt"
"time"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/nordcloud/terraform-provider-imagefactory/pkg/sdk"
)

const expiresAtDateFormat = "2006-01-02"

var apiKeySchema = map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Expand All @@ -17,6 +22,12 @@ var apiKeySchema = map[string]*schema.Schema{
Type: schema.TypeString,
Required: true,
},
"expires_at": {
Type: schema.TypeString,
Optional: true,
Description: "API key expiration date in format: 2023-11-04",
ValidateFunc: validateExpiresAtDateFormat(),
},
"secret": {
Type: schema.TypeString,
Computed: true,
Expand All @@ -38,9 +49,47 @@ func setProps(d *schema.ResourceData, key sdk.APIKey) diag.Diagnostics {
if err := d.Set("name", key.Name); err != nil {
return diag.FromErr(err)
}
if err := d.Set("secret", key.Secret); err != nil {
keySecret := key.Secret
if keySecret == "" {
keySecret = "***"
}
if err := d.Set("secret", keySecret); err != nil {
return diag.FromErr(err)
}
if err := d.Set("expires_at", formatExpiresAtDate((*string)(key.ExpiresAt))); err != nil {
return diag.FromErr(err)
}

return diags
}

func formatExpiresAtDate(expiresAt *string) string {
if expiresAt == nil {
return ""
}

d, err := time.Parse(time.RFC3339, *expiresAt)
if err != nil {
return ""
}

return d.Format(expiresAtDateFormat)
}

func validateExpiresAtDateFormat() schema.SchemaValidateFunc { // nolint: staticcheck
return func(i interface{}, k string) (warnings []string, errors []error) {
v, ok := i.(string)
if !ok {
errors = append(errors, fmt.Errorf("expected type of %s to be string", k))
return warnings, errors
}

_, err := time.Parse(expiresAtDateFormat, v)
if err != nil {
message := "Invalid date format, an example of a valid date format: 2023-11-04"
errors = append(errors, fmt.Errorf("invalid value for %s (%s)", k, message))
}

return warnings, errors
}
}
11 changes: 11 additions & 0 deletions pkg/graphql/apikey.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ query GetApiKey($input: CustomerApiKeyIdInput!) {
apiKey(input: $input) {
id
name
expiresAt
}
}

Expand All @@ -13,6 +14,7 @@ query GetApiKeys($input: CustomerApiKeysInput!) {
results {
id
name
expiresAt
}
}
}
Expand All @@ -21,10 +23,19 @@ mutation CreateApiKey($input: NewApiKey!) {
createApiKey(input: $input) {
id
name
expiresAt
secret
}
}

mutation UpdateApiKey($input: ApiKeyChanges!) {
updateApiKey(input: $input) {
id
name
expiresAt
}
}

mutation DeleteApiKey($input: CustomerApiKeyIdInput!) {
deleteApiKey(input: $input)
}
Loading

0 comments on commit 7506a74

Please sign in to comment.