Skip to content

Commit

Permalink
Merge branch 'main' into chore-refactor-credentials-#394
Browse files Browse the repository at this point in the history
  • Loading branch information
TomerHeber committed Jun 12, 2022
2 parents f852426 + 84193fb commit 1c82b4d
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 10 deletions.
7 changes: 6 additions & 1 deletion client/api_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,13 @@ type ApiKey struct {
CreatedByUser User `json:"createdByUser"`
}

type ApiKeyPermissions struct {
OrganizationRole string `json:"organizationRole"`
}

type ApiKeyCreatePayload struct {
Name string `json:"name"`
Name string `json:"name"`
Permissions ApiKeyPermissions `json:"permissions"`
}

type ApiKeyCreatePayloadWith struct {
Expand Down
2 changes: 1 addition & 1 deletion docs/resources/api_key.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ resource "env0_api_key" "api_key_sample" {
### Optional

- **id** (String) The ID of this resource.
- **organization_role** (String) the api key type. 'Admin' or 'User'. If not set defaults to 'Admin'. For more details check https://docs.env0.com/docs/api-keys
- **organization_role** (String) the api key type. 'Admin' or 'User'. Defaults to 'Admin'. For more details check https://docs.env0.com/docs/api-keys

## Import

Expand Down
5 changes: 4 additions & 1 deletion env0/resource_api_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func resourceApiKey() *schema.Resource {
},
"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",
Description: "the api key type. 'Admin' or 'User'. Defaults to 'Admin'. For more details check https://docs.env0.com/docs/api-keys",
Default: "Admin",
Optional: true,
ForceNew: true,
Expand All @@ -46,6 +46,9 @@ func resourceApiKeyCreate(ctx context.Context, d *schema.ResourceData, meta inte
return diag.Errorf("schema resource data deserialization failed: %v", err)
}

organizationRole := d.Get("organization_role").(string)
payload.Permissions.OrganizationRole = organizationRole

apiKey, err := apiClient.ApiKeyCreate(payload)
if err != nil {
return diag.Errorf("could not create api key: %v", err)
Expand Down
60 changes: 53 additions & 7 deletions env0/resource_api_key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ func TestUnitApiKeyResource(t *testing.T) {
OrganizationRole: "Admin",
}

apiKeyUser := client.ApiKey{
Id: uuid.NewString(),
Name: "name-user",
ApiKeyId: "keyid",
ApiKeySecret: "keysecret",
OrganizationId: "org",
OrganizationRole: "User",
}

updatedApiKey := client.ApiKey{
Id: "id2",
Name: "name2",
Expand All @@ -36,7 +45,7 @@ func TestUnitApiKeyResource(t *testing.T) {
OrganizationRole: "Admin",
}

t.Run("Success", func(t *testing.T) {
t.Run("Success - Admin", func(t *testing.T) {
testCase := resource.TestCase{
Steps: []resource.TestStep{
{
Expand Down Expand Up @@ -65,19 +74,52 @@ func TestUnitApiKeyResource(t *testing.T) {
runUnitTest(t, testCase, func(mock *client.MockApiClientInterface) {
gomock.InOrder(
mock.EXPECT().ApiKeyCreate(client.ApiKeyCreatePayload{
Name: apiKey.Name,
Name: apiKey.Name,
Permissions: client.ApiKeyPermissions{OrganizationRole: "Admin"},
}).Times(1).Return(&apiKey, nil),
mock.EXPECT().ApiKeys().Times(2).Return([]client.ApiKey{apiKey}, nil),
mock.EXPECT().ApiKeyDelete(apiKey.Id).Times(1),
mock.EXPECT().ApiKeyCreate(client.ApiKeyCreatePayload{
Name: updatedApiKey.Name,
Name: updatedApiKey.Name,
Permissions: client.ApiKeyPermissions{OrganizationRole: "Admin"},
}).Times(1).Return(&updatedApiKey, nil),
mock.EXPECT().ApiKeys().Times(1).Return([]client.ApiKey{updatedApiKey}, nil),
mock.EXPECT().ApiKeyDelete(updatedApiKey.Id).Times(1),
)
})
})

t.Run("Success - User", func(t *testing.T) {
testCase := resource.TestCase{
Steps: []resource.TestStep{
{
Config: resourceConfigCreate(resourceType, resourceName, map[string]interface{}{
"name": apiKeyUser.Name,
"organization_role": apiKeyUser.OrganizationRole,
}),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(accessor, "id", apiKeyUser.Id),
resource.TestCheckResourceAttr(accessor, "name", apiKeyUser.Name),
resource.TestCheckResourceAttr(accessor, "organization_role", apiKeyUser.OrganizationRole),
),
},
},
}

runUnitTest(t, testCase, func(mock *client.MockApiClientInterface) {
gomock.InOrder(
mock.EXPECT().ApiKeyCreate(client.ApiKeyCreatePayload{
Name: apiKeyUser.Name,
Permissions: client.ApiKeyPermissions{
OrganizationRole: apiKeyUser.OrganizationRole,
},
}).Times(1).Return(&apiKeyUser, nil),
mock.EXPECT().ApiKeys().Times(1).Return([]client.ApiKey{apiKeyUser}, nil),
mock.EXPECT().ApiKeyDelete(apiKeyUser.Id).Times(1),
)
})
})

t.Run("Create Failure", func(t *testing.T) {
testCase := resource.TestCase{
Steps: []resource.TestStep{
Expand All @@ -92,7 +134,8 @@ func TestUnitApiKeyResource(t *testing.T) {

runUnitTest(t, testCase, func(mock *client.MockApiClientInterface) {
mock.EXPECT().ApiKeyCreate(client.ApiKeyCreatePayload{
Name: apiKey.Name,
Name: apiKey.Name,
Permissions: client.ApiKeyPermissions{OrganizationRole: "Admin"},
}).Times(1).Return(nil, errors.New("error"))
})
})
Expand All @@ -116,7 +159,8 @@ func TestUnitApiKeyResource(t *testing.T) {

runUnitTest(t, testCase, func(mock *client.MockApiClientInterface) {
mock.EXPECT().ApiKeyCreate(client.ApiKeyCreatePayload{
Name: apiKey.Name,
Name: apiKey.Name,
Permissions: client.ApiKeyPermissions{OrganizationRole: "Admin"},
}).Times(1).Return(&apiKey, nil)
mock.EXPECT().ApiKeys().Times(3).Return([]client.ApiKey{apiKey}, nil)
mock.EXPECT().ApiKeyDelete(apiKey.Id).Times(1)
Expand All @@ -142,7 +186,8 @@ func TestUnitApiKeyResource(t *testing.T) {

runUnitTest(t, testCase, func(mock *client.MockApiClientInterface) {
mock.EXPECT().ApiKeyCreate(client.ApiKeyCreatePayload{
Name: apiKey.Name,
Name: apiKey.Name,
Permissions: client.ApiKeyPermissions{OrganizationRole: "Admin"},
}).Times(1).Return(&apiKey, nil)
mock.EXPECT().ApiKeys().Times(3).Return([]client.ApiKey{apiKey}, nil)
mock.EXPECT().ApiKeyDelete(apiKey.Id).Times(1)
Expand All @@ -169,7 +214,8 @@ func TestUnitApiKeyResource(t *testing.T) {

runUnitTest(t, testCase, func(mock *client.MockApiClientInterface) {
mock.EXPECT().ApiKeyCreate(client.ApiKeyCreatePayload{
Name: updatedApiKey.Name,
Name: updatedApiKey.Name,
Permissions: client.ApiKeyPermissions{OrganizationRole: "Admin"},
}).Times(1).Return(&updatedApiKey, nil)
mock.EXPECT().ApiKeys().Times(2).Return([]client.ApiKey{updatedApiKey}, nil)
mock.EXPECT().ApiKeyDelete(updatedApiKey.Id).Times(1)
Expand Down
5 changes: 5 additions & 0 deletions tests/integration/020_api_key/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ resource "env0_api_key" "test_api_key" {
name = "my-little-api-key-${random_string.random.result}"
}

resource "env0_api_key" "test_user_api_key" {
name = "my-little-user-api-key-${random_string.random.result}"
organization_role = "User"
}

data "env0_api_key" "test_api_key1" {
name = env0_api_key.test_api_key.name
depends_on = [env0_api_key.test_api_key]
Expand Down

0 comments on commit 1c82b4d

Please sign in to comment.