Skip to content

Commit

Permalink
Feat: support Non-Admin API key creation (#411)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomerHeber authored Jun 7, 2022
1 parent 79568b0 commit 53228db
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 19 deletions.
19 changes: 10 additions & 9 deletions client/api_key.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package client

type ApiKey struct {
Id string `json:"id"`
Name string `json:"name"`
ApiKeyId string `json:"apiKeyId"`
ApiKeySecret string `json:"apiKeySecret"`
LastUsedAt string `json:"lastUsedAt"`
OrganizationId string `json:"organizationId"`
CreatedAt string `json:"createdAt"`
CreatedBy string `json:"createdBy"`
CreatedByUser User `json:"createdByUser"`
Id string `json:"id"`
Name string `json:"name"`
ApiKeyId string `json:"apiKeyId"`
ApiKeySecret string `json:"apiKeySecret"`
LastUsedAt string `json:"lastUsedAt"`
OrganizationId string `json:"organizationId"`
OrganizationRole string `json:"organizationRole"`
CreatedAt string `json:"createdAt"`
CreatedBy string `json:"createdBy"`
CreatedByUser User `json:"createdByUser"`
}

type ApiKeyCreatePayload struct {
Expand Down
8 changes: 8 additions & 0 deletions env0/resource_api_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ func resourceApiKey() *schema.Resource {
Required: true,
ForceNew: true,
},
"organization_role": {
Type: schema.TypeString,
Description: "the api key type. 'Admin' or 'User'. If not set defaults to 'Admin'. For more details check https://docs.env0.com/docs/api-keys",
Default: "Admin",
Optional: true,
ForceNew: true,
ValidateDiagFunc: NewStringInValidator([]string{"Admin", "User"}),
},
},
}
}
Expand Down
24 changes: 14 additions & 10 deletions env0/resource_api_key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,21 @@ func TestUnitApiKeyResource(t *testing.T) {
accessor := resourceAccessor(resourceType, resourceName)

apiKey := client.ApiKey{
Id: uuid.NewString(),
Name: "name",
ApiKeyId: "keyid",
ApiKeySecret: "keysecret",
OrganizationId: "org",
Id: uuid.NewString(),
Name: "name",
ApiKeyId: "keyid",
ApiKeySecret: "keysecret",
OrganizationId: "org",
OrganizationRole: "Admin",
}

updatedApiKey := client.ApiKey{
Id: "id2",
Name: "name2",
ApiKeyId: "keyid2",
ApiKeySecret: "keysecret2",
OrganizationId: "org",
Id: "id2",
Name: "name2",
ApiKeyId: "keyid2",
ApiKeySecret: "keysecret2",
OrganizationId: "org",
OrganizationRole: "Admin",
}

t.Run("Success", func(t *testing.T) {
Expand All @@ -44,6 +46,7 @@ func TestUnitApiKeyResource(t *testing.T) {
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(accessor, "id", apiKey.Id),
resource.TestCheckResourceAttr(accessor, "name", apiKey.Name),
resource.TestCheckResourceAttr(accessor, "organization_role", apiKey.OrganizationRole),
),
},
{
Expand All @@ -53,6 +56,7 @@ func TestUnitApiKeyResource(t *testing.T) {
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(accessor, "id", updatedApiKey.Id),
resource.TestCheckResourceAttr(accessor, "name", updatedApiKey.Name),
resource.TestCheckResourceAttr(accessor, "organization_role", updatedApiKey.OrganizationRole),
),
},
},
Expand Down

0 comments on commit 53228db

Please sign in to comment.