Skip to content

Commit

Permalink
Feat: add resource aws_cloud_configuration (#923)
Browse files Browse the repository at this point in the history
* Feat: add resource aws_cloud_configuration

* added import and added tests. fixed errors handling.

* added examples

* updated description

* updated description
  • Loading branch information
TomerHeber authored Aug 7, 2024
1 parent c7ba92b commit ed2b945
Show file tree
Hide file tree
Showing 14 changed files with 924 additions and 5 deletions.
5 changes: 5 additions & 0 deletions client/api_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,11 @@ type ApiClientInterface interface {
AssignConfigurationSets(scope string, scopeId string, sets []string) error
UnassignConfigurationSets(scope string, scopeId string, sets []string) error
ConfigurationSetsAssignments(scope string, scopeId string) ([]ConfigurationSet, error)
CloudAccountCreate(payload *CloudAccountCreatePayload) (*CloudAccount, error)
CloudAccountUpdate(id string, payload *CloudAccountUpdatePayload) (*CloudAccount, error)
CloudAccountDelete(id string) error
CloudAccount(id string) (*CloudAccount, error)
CloudAccounts() ([]CloudAccount, error)
}

func NewApiClient(client http.HttpClientInterface, defaultOrganizationId string) ApiClientInterface {
Expand Down
74 changes: 74 additions & 0 deletions client/api_client_mock.go

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

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

import "fmt"

type CloudAccountCreatePayload struct {
Provider string `json:"provider"`
Name string `json:"name"`
Configuration interface{} `json:"configuration" tfschema:"-"`
}

type CloudAccountUpdatePayload struct {
Name string `json:"name"`
Configuration interface{} `json:"configuration" tfschema:"-"`
}

type AWSCloudAccountConfiguration struct {
AccountId string `json:"accountId"`
BucketName string `json:"bucketName"`
Prefix string `json:"prefix,omitempty"`
Regions []string `json:"regions"`
}

type CloudAccount struct {
Id string `json:"id"`
Provider string `json:"provider"`
Name string `json:"name"`
Health bool `json:"health"`
Configuration interface{} `json:"configuration" tfschema:"-"`
}

func (client *ApiClient) CloudAccountCreate(payload *CloudAccountCreatePayload) (*CloudAccount, error) {
organizationId, err := client.OrganizationId()
if err != nil {
return nil, fmt.Errorf("failed to get organization id: %w", err)
}

payloadWithOrganizationId := struct {
*CloudAccountCreatePayload
OrganizationId string `json:"organizationId"`
}{
payload,
organizationId,
}

var cloudAccount CloudAccount
if err := client.http.Post("/cloud/configurations", &payloadWithOrganizationId, &cloudAccount); err != nil {
return nil, err
}

return &cloudAccount, nil
}

func (client *ApiClient) CloudAccountUpdate(id string, payload *CloudAccountUpdatePayload) (*CloudAccount, error) {
var cloudAccount CloudAccount
if err := client.http.Put("/cloud/configurations/"+id, payload, &cloudAccount); err != nil {
return nil, err
}

return &cloudAccount, nil
}

func (client *ApiClient) CloudAccountDelete(id string) error {
return client.http.Delete("/cloud/configurations/"+id, nil)
}

func (client *ApiClient) CloudAccount(id string) (*CloudAccount, error) {
var cloudAccount CloudAccount

if err := client.http.Get("/cloud/configurations/"+id, nil, &cloudAccount); err != nil {
return nil, err
}

return &cloudAccount, nil
}

func (client *ApiClient) CloudAccounts() ([]CloudAccount, error) {
organizationId, err := client.OrganizationId()
if err != nil {
return nil, fmt.Errorf("failed to get organization id: %w", err)
}

var cloudAccounts []CloudAccount
if err := client.http.Get("/cloud/configurations", map[string]string{"organizationId": organizationId}, &cloudAccounts); err != nil {
return nil, err
}

return cloudAccounts, nil
}
173 changes: 173 additions & 0 deletions client/cloud_account_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
package client_test

import (
. "github.com/env0/terraform-provider-env0/client"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"go.uber.org/mock/gomock"
)

var _ = Describe("CloudAccount", func() {
var account *CloudAccount
var accounts []CloudAccount
var err error

awsConfiguration := AWSCloudAccountConfiguration{
AccountId: "a",
BucketName: "b",
Prefix: "prefix",
Regions: []string{"us-east-10", "us-west-24"},
}

awsConfigurationUpdated := AWSCloudAccountConfiguration{
AccountId: "c",
BucketName: "d",
Prefix: "prefix2",
Regions: []string{"us-east-10"},
}

account1 := CloudAccount{
Id: "id1",
Provider: "AWS",
Name: "name1",
Health: true,
Configuration: &awsConfiguration,
}

account1Updated := account1
account1Updated.Name = "updatedname1"
account1Updated.Configuration = awsConfigurationUpdated

account2 := CloudAccount{
Id: "id2",
Provider: "GCP",
Name: "name2",
Configuration: []string{"some random configuration"},
}

Describe("create", func() {
BeforeEach(func() {
mockOrganizationIdCall(organizationId)

payload := CloudAccountCreatePayload{
Provider: account1.Provider,
Name: account1.Name,
Configuration: account1.Configuration,
}

payloadWithOrganizationId := struct {
*CloudAccountCreatePayload
OrganizationId string `json:"organizationId"`
}{
&payload,
organizationId,
}

httpCall = mockHttpClient.EXPECT().
Post("/cloud/configurations", &payloadWithOrganizationId, gomock.Any()).
Do(func(path string, request interface{}, response *CloudAccount) {
*response = account1
}).Times(1)

account, err = apiClient.CloudAccountCreate(&payload)
})

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

It("should return account", func() {
Expect(*account).To(Equal(account1))
})

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

Describe("update", func() {
BeforeEach(func() {
payload := CloudAccountUpdatePayload{
Name: account1Updated.Name,
Configuration: account1Updated.Configuration,
}

httpCall = mockHttpClient.EXPECT().
Put("/cloud/configurations/"+account.Id, &payload, gomock.Any()).
Do(func(path string, request interface{}, response *CloudAccount) {
*response = account1Updated
}).Times(1)

account, err = apiClient.CloudAccountUpdate(account.Id, &payload)
})

It("should return updated account", func() {
Expect(*account).To(Equal(account1Updated))
})

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

Describe("delete", func() {
BeforeEach(func() {
httpCall = mockHttpClient.EXPECT().
Delete("/cloud/configurations/"+account.Id, nil).
Do(func(path string, request interface{}) {}).Times(1)

err = apiClient.CloudAccountDelete(account.Id)
})

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

Describe("get", func() {
BeforeEach(func() {
httpCall = mockHttpClient.EXPECT().
Get("/cloud/configurations/"+account.Id, nil, gomock.Any()).
Do(func(path string, request interface{}, response *CloudAccount) {
*response = account1
}).Times(1)

account, err = apiClient.CloudAccount(account.Id)
})

It("should return account", func() {
Expect(*account).To(Equal(account1))
})

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

Describe("list", func() {
mockedAccounts := []CloudAccount{
account1,
account2,
}

BeforeEach(func() {
mockOrganizationIdCall(organizationId)

httpCall = mockHttpClient.EXPECT().
Get("/cloud/configurations", map[string]string{"organizationId": organizationId}, gomock.Any()).
Do(func(path string, request interface{}, response *[]CloudAccount) {
*response = mockedAccounts
}).Times(1)

accounts, err = apiClient.CloudAccounts()
})

It("should return accounts", func() {
Expect(accounts).To(Equal(mockedAccounts))
})

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

0 comments on commit ed2b945

Please sign in to comment.