Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…into feat_api_client_for_cost_cred-#218
  • Loading branch information
samuel-br committed Mar 23, 2022
2 parents 2dd4e4e + 13cba39 commit a0aa9e7
Show file tree
Hide file tree
Showing 11 changed files with 108 additions and 83 deletions.
4 changes: 2 additions & 2 deletions client/api_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ type ApiClientInterface interface {
EnvironmentUpdateDriftDetection(environmentId string, payload EnvironmentSchedulingExpression) (EnvironmentSchedulingExpression, error)
EnvironmentStopDriftDetection(environmentId string) error
Notifications() ([]Notification, error)
NotificationCreate(payload NotificationCreate) (*Notification, error)
NotificationCreate(payload NotificationCreatePayload) (*Notification, error)
NotificationDelete(id string) error
NotificationUpdate(id string, payload NotificationUpdate) (*Notification, error)
NotificationUpdate(id string, payload NotificationUpdatePayload) (*Notification, error)
ModuleCreate(payload ModuleCreatePayload) (*Module, error)
Module(id string) (*Module, error)
ModuleDelete(id string) error
Expand Down
4 changes: 2 additions & 2 deletions client/api_client_mock.go

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

9 changes: 2 additions & 7 deletions client/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -493,18 +493,13 @@ type Notification struct {
Value string `json:"value"`
}

type NotificationCreate struct {
type NotificationCreatePayload struct {
Name string `json:"name"`
Type NotificationType `json:"type"`
Value string `json:"value"`
}

type NotificationCreateWithOrganizationId struct {
NotificationCreate
OrganizationId string `json:"organizationId"`
}

type NotificationUpdate struct {
type NotificationUpdatePayload struct {
Name string `json:"name,omitempty"`
Type NotificationType `json:"type,omitempty"`
Value string `json:"value,omitempty"`
Expand Down
15 changes: 10 additions & 5 deletions client/notification.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package client

type NotificationCreatePayloadWith struct {
NotificationCreatePayload
OrganizationId string `json:"organizationId"`
}

func (ac *ApiClient) Notifications() ([]Notification, error) {
organizationId, err := ac.organizationId()
if err != nil {
Expand All @@ -13,17 +18,17 @@ func (ac *ApiClient) Notifications() ([]Notification, error) {
return result, nil
}

func (ac *ApiClient) NotificationCreate(payload NotificationCreate) (*Notification, error) {
func (ac *ApiClient) NotificationCreate(payload NotificationCreatePayload) (*Notification, error) {
var result Notification

organizationId, err := ac.organizationId()
if err != nil {
return nil, err
}

payloadWithOrganizationId := NotificationCreateWithOrganizationId{
NotificationCreate: payload,
OrganizationId: organizationId,
payloadWithOrganizationId := NotificationCreatePayloadWith{
NotificationCreatePayload: payload,
OrganizationId: organizationId,
}

if err = ac.http.Post("/notifications/endpoints", payloadWithOrganizationId, &result); err != nil {
Expand All @@ -39,7 +44,7 @@ func (ac *ApiClient) NotificationDelete(id string) error {
return nil
}

func (ac *ApiClient) NotificationUpdate(id string, payload NotificationUpdate) (*Notification, error) {
func (ac *ApiClient) NotificationUpdate(id string, payload NotificationUpdatePayload) (*Notification, error) {
var result Notification

if err := ac.http.Patch("/notifications/endpoints/"+id, payload, &result); err != nil {
Expand Down
10 changes: 5 additions & 5 deletions client/notification_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ var _ = Describe("Notification Client", func() {
BeforeEach(func() {
mockOrganizationIdCall(organizationId)

createNotificationPayload := NotificationCreate{}
createNotificationPayload := NotificationCreatePayload{}
copier.Copy(&createNotificationPayload, &mockNotification)

expectedCreateRequest := NotificationCreateWithOrganizationId{
NotificationCreate: createNotificationPayload,
OrganizationId: organizationId,
expectedCreateRequest := NotificationCreatePayloadWith{
NotificationCreatePayload: createNotificationPayload,
OrganizationId: organizationId,
}

httpCall = mockHttpClient.EXPECT().
Expand Down Expand Up @@ -106,7 +106,7 @@ var _ = Describe("Notification Client", func() {
var err error

BeforeEach(func() {
updateNotificationPayload := NotificationUpdate{Name: "updated-name"}
updateNotificationPayload := NotificationUpdatePayload{Name: "updated-name"}

httpCall = mockHttpClient.EXPECT().
Patch("/notifications/endpoints/"+mockNotification.Id, updateNotificationPayload, gomock.Any()).
Expand Down
5 changes: 3 additions & 2 deletions env0/data_notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,9 @@ func dataNotificationRead(ctx context.Context, d *schema.ResourceData, meta inte
return diag.Errorf("could not read notification: %v", err)
}

d.SetId(notification.Id)
setNotificationSchema(d, notification)
if err := writeResourceData(notification, d); err != nil {
diag.Errorf("schema resource data serialization failed: %v", err)
}

return nil
}
4 changes: 2 additions & 2 deletions env0/resource_module.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func resourceModuleCreate(ctx context.Context, d *schema.ResourceData, meta inte

var payload client.ModuleCreatePayload
if err := readResourceData(&payload, d); err != nil {
diag.Errorf("schema resource data deserialization failed: %v", err)
return diag.Errorf("schema resource data deserialization failed: %v", err)
}

if len(payload.TokenId) > 0 {
Expand Down Expand Up @@ -136,7 +136,7 @@ func resourceModuleUpdate(ctx context.Context, d *schema.ResourceData, meta inte

var payload client.ModuleUpdatePayload
if err := readResourceData(&payload, d); err != nil {
diag.Errorf("schema resource data deserialization failed: %v", err)
return diag.Errorf("schema resource data deserialization failed: %v", err)
}

if _, err := apiClient.ModuleUpdate(d.Id(), payload); err != nil {
Expand Down
37 changes: 13 additions & 24 deletions env0/resource_notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,6 @@ func resourceNotification() *schema.Resource {
}
}

func setNotificationSchema(d *schema.ResourceData, notification *client.Notification) {
d.Set("name", notification.Name)
d.Set("type", notification.Type)
d.Set("value", notification.Value)
}

func getNotificationById(id string, meta interface{}) (*client.Notification, error) {
apiClient := meta.(client.ApiClientInterface)

Expand Down Expand Up @@ -99,10 +93,10 @@ func getNotificationByName(name string, meta interface{}) (*client.Notification,

func resourceNotificationCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
apiClient := meta.(client.ApiClientInterface)
payload := client.NotificationCreate{
Name: d.Get("name").(string),
Type: client.NotificationType(d.Get("type").(string)),
Value: d.Get("value").(string),

var payload client.NotificationCreatePayload
if err := readResourceData(&payload, d); err != nil {
return diag.Errorf("schema resource data deserialization failed: %v", err)
}

notification, err := apiClient.NotificationCreate(payload)
Expand All @@ -126,26 +120,20 @@ func resourceNotificationRead(ctx context.Context, d *schema.ResourceData, meta
return nil
}

setNotificationSchema(d, notification)
if err := writeResourceData(notification, d); err != nil {
diag.Errorf("schema resource data serialization failed: %v", err)
}

return nil
}

func resourceNotificationUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
apiClient := meta.(client.ApiClientInterface)

payload := client.NotificationUpdate{}

if name, ok := d.GetOk("name"); ok {
payload.Name = name.(string)
}
payload := client.NotificationUpdatePayload{}

if notificationType, ok := d.GetOk("type"); ok {
payload.Type = client.NotificationType(notificationType.(string))
}

if value, ok := d.GetOk("value"); ok {
payload.Value = value.(string)
if err := readResourceData(&payload, d); err != nil {
return diag.Errorf("schema resource data deserialization failed: %v", err)
}

_, err := apiClient.NotificationUpdate(d.Id(), payload)
Expand Down Expand Up @@ -183,8 +171,9 @@ func resourceNotificationImport(ctx context.Context, d *schema.ResourceData, met
return nil, err
}

d.SetId(notification.Id)
setNotificationSchema(d, notification)
if err := writeResourceData(notification, d); err != nil {
return nil, err
}

return []*schema.ResourceData{d}, nil
}
14 changes: 7 additions & 7 deletions env0/resource_notification_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,13 @@ func TestUnitNotificationResource(t *testing.T) {
}

runUnitTest(t, testCase, func(mock *client.MockApiClientInterface) {
mock.EXPECT().NotificationCreate(client.NotificationCreate{
mock.EXPECT().NotificationCreate(client.NotificationCreatePayload{
Name: notification.Name,
Type: notification.Type,
Value: notification.Value,
}).Times(1).Return(&notification, nil)

mock.EXPECT().NotificationUpdate(updatedNotification.Id, client.NotificationUpdate{
mock.EXPECT().NotificationUpdate(updatedNotification.Id, client.NotificationUpdatePayload{
Name: updatedNotification.Name,
Type: updatedNotification.Type,
Value: updatedNotification.Value,
Expand Down Expand Up @@ -171,7 +171,7 @@ func TestUnitNotificationResource(t *testing.T) {
}

runUnitTest(t, testCase, func(mock *client.MockApiClientInterface) {
mock.EXPECT().NotificationCreate(client.NotificationCreate{
mock.EXPECT().NotificationCreate(client.NotificationCreatePayload{
Name: notification.Name,
Type: notification.Type,
Value: notification.Value,
Expand Down Expand Up @@ -201,13 +201,13 @@ func TestUnitNotificationResource(t *testing.T) {
}

runUnitTest(t, testCase, func(mock *client.MockApiClientInterface) {
mock.EXPECT().NotificationCreate(client.NotificationCreate{
mock.EXPECT().NotificationCreate(client.NotificationCreatePayload{
Name: notification.Name,
Type: notification.Type,
Value: notification.Value,
}).Times(1).Return(&notification, nil)

mock.EXPECT().NotificationUpdate(updatedNotification.Id, client.NotificationUpdate{
mock.EXPECT().NotificationUpdate(updatedNotification.Id, client.NotificationUpdatePayload{
Name: updatedNotification.Name,
Type: updatedNotification.Type,
Value: updatedNotification.Value,
Expand Down Expand Up @@ -238,7 +238,7 @@ func TestUnitNotificationResource(t *testing.T) {
}

runUnitTest(t, testCase, func(mock *client.MockApiClientInterface) {
mock.EXPECT().NotificationCreate(client.NotificationCreate{
mock.EXPECT().NotificationCreate(client.NotificationCreatePayload{
Name: notification.Name,
Type: notification.Type,
Value: notification.Value,
Expand Down Expand Up @@ -268,7 +268,7 @@ func TestUnitNotificationResource(t *testing.T) {
}

runUnitTest(t, testCase, func(mock *client.MockApiClientInterface) {
mock.EXPECT().NotificationCreate(client.NotificationCreate{
mock.EXPECT().NotificationCreate(client.NotificationCreatePayload{
Name: notificationById.Name,
Type: notificationById.Type,
Value: notificationById.Value,
Expand Down
2 changes: 1 addition & 1 deletion env0/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func readResourceData(i interface{}, d *schema.ResourceData) error {
field.Set(reflect.ValueOf(sshKeys))
}
case reflect.String, reflect.Bool, reflect.Int:
field.Set(reflect.ValueOf(dval))
field.Set(reflect.ValueOf(dval).Convert(fieldType))
default:
return fmt.Errorf("internal error - unhandled field kind %v", fieldType.Kind())
}
Expand Down
87 changes: 61 additions & 26 deletions env0/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,35 +9,33 @@ import (
)

func TestReadResourceDataModule(t *testing.T) {
t.Run("match", func(t *testing.T) {
d := schema.TestResourceDataRaw(t, resourceModule().Schema, map[string]interface{}{
"module_name": "module_name",
"module_provider": "module_provider",
"github_installation_id": 1000,
"ssh_keys": []interface{}{
map[string]interface{}{"id": "id1", "name": "name1"},
},
})

githubInstallationId := 1000

expectedPayload := client.ModuleCreatePayload{
ModuleName: "module_name",
ModuleProvider: "module_provider",
GithubInstallationId: &githubInstallationId,
SshKeys: []client.ModuleSshKey{
{
Id: "id1",
Name: "name1",
},
d := schema.TestResourceDataRaw(t, resourceModule().Schema, map[string]interface{}{
"module_name": "module_name",
"module_provider": "module_provider",
"github_installation_id": 1000,
"ssh_keys": []interface{}{
map[string]interface{}{"id": "id1", "name": "name1"},
},
})

githubInstallationId := 1000

expectedPayload := client.ModuleCreatePayload{
ModuleName: "module_name",
ModuleProvider: "module_provider",
GithubInstallationId: &githubInstallationId,
SshKeys: []client.ModuleSshKey{
{
Id: "id1",
Name: "name1",
},
}
},
}

var payload client.ModuleCreatePayload
var payload client.ModuleCreatePayload

assert.Nil(t, readResourceData(&payload, d))
assert.Equal(t, expectedPayload, payload)
})
assert.Nil(t, readResourceData(&payload, d))
assert.Equal(t, expectedPayload, payload)
}

func TestWriteResourceDataModule(t *testing.T) {
Expand Down Expand Up @@ -73,3 +71,40 @@ func TestWriteResourceDataModule(t *testing.T) {
rawSshKeys = append(rawSshKeys, map[string]interface{}{"id": "id1", "name": "name1"})
assert.Equal(t, rawSshKeys, d.Get("ssh_keys"))
}

func TestReadResourceDataNotification(t *testing.T) {
d := schema.TestResourceDataRaw(t, resourceNotification().Schema, map[string]interface{}{
"name": "name",
"type": "Slack",
"value": "value",
})

expectedPayload := client.NotificationCreatePayload{
Name: "name",
Type: "Slack",
Value: "value",
}

var payload client.NotificationCreatePayload

assert.Nil(t, readResourceData(&payload, d))
assert.Equal(t, expectedPayload, payload)
}

func TestWriteResourceDataNotification(t *testing.T) {
d := schema.TestResourceDataRaw(t, resourceNotification().Schema, map[string]interface{}{})

n := client.Notification{
Id: "id",
Name: "name",
Type: "Teams",
Value: "value",
}

assert.Nil(t, writeResourceData(&n, d))

assert.Equal(t, "id", d.Id())
assert.Equal(t, "name", d.Get("name"))
assert.Equal(t, "Teams", d.Get("type"))
assert.Equal(t, "value", d.Get("value"))
}

0 comments on commit a0aa9e7

Please sign in to comment.