Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…into feat_cloud_cost_credentials-#218
  • Loading branch information
samuel-br committed Mar 29, 2022
2 parents b149231 + b0607a2 commit 96c66c5
Show file tree
Hide file tree
Showing 39 changed files with 1,271 additions and 200 deletions.
19 changes: 13 additions & 6 deletions client/api_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ type ApiClientInterface interface {
SshKeys() ([]SshKey, error)
SshKeyCreate(payload SshKeyCreatePayload) (SshKey, error)
SshKeyDelete(id string) error
CloudCredentials(id string) (ApiKey, error)
CloudCredentialsList() ([]ApiKey, error)
AwsCredentialsCreate(request AwsCredentialsCreatePayload) (ApiKey, error)
CloudCredentials(id string) (Credentials, error)
CloudCredentialsList() ([]Credentials, error)
AwsCredentialsCreate(request AwsCredentialsCreatePayload) (Credentials, error)
CloudCredentialsDelete(id string) error
GcpCredentialsCreate(request GcpCredentialsCreatePayload) (ApiKey, error)
GoogleCostCredentialsCreate(request GoogleCostCredentialsCreatePayload) (ApiKey, error)
AzureCredentialsCreate(request AzureCredentialsCreatePayload) (ApiKey, error)
GcpCredentialsCreate(request GcpCredentialsCreatePayload) (Credentials, error)
GoogleCostCredentialsCreate(request GoogleCostCredentialsCreatePayload) (Credentials, error)
AzureCredentialsCreate(request AzureCredentialsCreatePayload) (Credentials, error)
AssignCloudCredentialsToProject(projectId string, credentialId string) (CloudCredentialsProjectAssignment, error)
RemoveCloudCredentialsFromProject(projectId string, credentialId string) error
CloudCredentialIdsInProject(projectId string) ([]string, error)
Expand Down Expand Up @@ -84,6 +84,13 @@ type ApiClientInterface interface {
ModuleDelete(id string) error
ModuleUpdate(id string, payload ModuleUpdatePayload) (*Module, error)
Modules() ([]Module, error)
GitToken(id string) (*GitToken, error)
GitTokens() ([]GitToken, error)
GitTokenCreate(payload GitTokenCreatePayload) (*GitToken, error)
GitTokenDelete(id string) error
ApiKeyCreate(payload ApiKeyCreatePayload) (*ApiKey, error)
ApiKeyDelete(id string) error
ApiKeys() ([]ApiKey, error)
}

func NewApiClient(client http.HttpClientInterface) ApiClientInterface {
Expand Down
127 changes: 115 additions & 12 deletions client/api_client_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 43 additions & 0 deletions client/api_key.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package client

type ApiKeyCreatePayloadWith struct {
ApiKeyCreatePayload
OrganizationId string `json:"organizationId"`
}

func (ac *ApiClient) ApiKeyCreate(payload ApiKeyCreatePayload) (*ApiKey, error) {
organizationId, err := ac.organizationId()
if err != nil {
return nil, err
}

payloadWith := ApiKeyCreatePayloadWith{
ApiKeyCreatePayload: payload,
OrganizationId: organizationId,
}

var result ApiKey
if err := ac.http.Post("/api-keys", payloadWith, &result); err != nil {
return nil, err
}

return &result, nil
}

func (ac *ApiClient) ApiKeyDelete(id string) error {
return ac.http.Delete("/api-keys/" + id)
}

func (ac *ApiClient) ApiKeys() ([]ApiKey, error) {
organizationId, err := ac.organizationId()
if err != nil {
return nil, err
}

var result []ApiKey
if err := ac.http.Get("/api-keys", map[string]string{"organizationId": organizationId}, &result); err != nil {
return nil, err
}

return result, err
}
97 changes: 97 additions & 0 deletions client/api_key_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package client_test

import (
. "github.com/env0/terraform-provider-env0/client"
"github.com/golang/mock/gomock"
"github.com/jinzhu/copier"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)

var _ = Describe("ApiKey Client", func() {
mockApiKey := ApiKey{
Id: "id",
Name: "name",
ApiKeyId: "keyid",
ApiKeySecret: "keysecret",
}

Describe("Get All ApiKeys", func() {
var returnedApiKeys []ApiKey
mockApiKeys := []ApiKey{mockApiKey}

BeforeEach(func() {
mockOrganizationIdCall(organizationId)
httpCall = mockHttpClient.EXPECT().
Get("/api-keys", map[string]string{"organizationId": organizationId}, gomock.Any()).
Do(func(path string, request interface{}, response *[]ApiKey) {
*response = mockApiKeys
})
returnedApiKeys, _ = apiClient.ApiKeys()
})

It("Should get organization id", func() {
organizationIdCall.Times(1)
})

It("Should send GET request", func() {
httpCall.Times(1)
})

It("Should return ApiKeys", func() {
Expect(returnedApiKeys).To(Equal(mockApiKeys))
})
})

Describe("Create ApiKeys", func() {
var createdApiKey *ApiKey
var err error

BeforeEach(func() {
mockOrganizationIdCall(organizationId)

createApiKeyPayload := ApiKeyCreatePayload{}
copier.Copy(&createApiKeyPayload, &mockApiKey)

expectedCreateRequest := ApiKeyCreatePayloadWith{
ApiKeyCreatePayload: createApiKeyPayload,
OrganizationId: organizationId,
}

httpCall = mockHttpClient.EXPECT().
Post("/api-keys", expectedCreateRequest, gomock.Any()).
Do(func(path string, request interface{}, response *ApiKey) {
*response = mockApiKey
})

createdApiKey, err = apiClient.ApiKeyCreate(createApiKeyPayload)
})

It("Should get organization id", func() {
organizationIdCall.Times(1)
})

It("Should send POST request with params", func() {
httpCall.Times(1)
})

It("Should not return error", func() {
Expect(err).To(BeNil())
})

It("Should return created ApiKey", func() {
Expect(*createdApiKey).To(Equal(mockApiKey))
})
})

Describe("Delete ApiKey", func() {
BeforeEach(func() {
httpCall = mockHttpClient.EXPECT().Delete("/api-keys/" + mockApiKey.Id)
apiClient.ApiKeyDelete(mockApiKey.Id)
})

It("Should send DELETE request with ApiKey id", func() {
httpCall.Times(1)
})
})
})
Loading

0 comments on commit 96c66c5

Please sign in to comment.