Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: regenerate SDK #242

Merged
merged 4 commits into from
Nov 17, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@


default: build

.PHONY: init-examples
init-examples:
@echo "==> Creating symlinks for example/ projects to terraform-provider-castai binary"; \
TF_PROVIDER_FILENAME=terraform-provider-castai; \
Expand All @@ -16,29 +15,35 @@ init-examples:
done \
done

.PHONY: generate-sdk
generate-sdk:
@echo "==> Generating castai sdk client"
@API_TAGS=ExternalClusterAPI,PoliciesAPI,NodeConfigurationAPI,NodeTemplatesAPI,AuthTokenAPI,ScheduledRebalancingAPI,InventoryAPI,UsersAPI,OperationsAPI,EvictorAPI,SSOAPI go generate castai/sdk/generate.go

# The following command also rewrites existing documentation
.PHONY: generate-docs
generate-docs:
go install github.com/hashicorp/terraform-plugin-docs/cmd/[email protected]
tfplugindocs generate --rendered-provider-name "CAST AI" --ignore-deprecated

.PHONY: build
build: init-examples
build: generate-sdk
build:
@echo "==> Building terraform-provider-castai"
go build

.PHONY: test
test:
@echo "==> Running tests"
go test $$(go list ./... | grep -v vendor/ | grep -v e2e) -timeout=1m -parallel=4

.PHONY: testacc
testacc:
@echo "==> Running acceptance tests"
TF_ACC=1 go test ./castai/... '-run=^TestAcc' -v -timeout 10m

.PHONY: validate-terraform-examples
validate-terraform-examples:
for examples in examples/eks examples/gke examples/aks ; do \
for tfproject in $$examples/* ; do \
Expand Down
2 changes: 1 addition & 1 deletion castai/data_source_organization.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
func dataSourceOrganizationRead(ctx context.Context, data *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*ProviderConfig).api

resp, err := client.ListOrganizationsWithResponse(ctx)
resp, err := client.UsersAPIListOrganizationsWithResponse(ctx, &sdk.UsersAPIListOrganizationsParams{})

Check failure on line 35 in castai/data_source_organization.go

View workflow job for this annotation

GitHub Actions / Build

client.UsersAPIListOrganizationsWithResponse undefined (type *sdk.ClientWithResponses has no field or method UsersAPIListOrganizationsWithResponse)

Check failure on line 35 in castai/data_source_organization.go

View workflow job for this annotation

GitHub Actions / Build

undefined: sdk.UsersAPIListOrganizationsParams
if err := sdk.CheckOKResponse(resp, err); err != nil {
return diag.FromErr(fmt.Errorf("retrieving organizations: %w", err))
}
Expand Down
4 changes: 2 additions & 2 deletions castai/data_source_organization_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func TestOrganizationDataSourceRead(t *testing.T) {
}`)))

mockClient.EXPECT().
ListOrganizations(gomock.Any()).
UsersAPIListOrganizations(gomock.Any(), gomock.Any()).
Return(&http.Response{StatusCode: 200, Body: body, Header: map[string][]string{"Content-Type": {"json"}}}, nil)

state := terraform.NewInstanceStateShimmedFromValue(cty.ObjectVal(map[string]cty.Value{}), 0)
Expand Down Expand Up @@ -97,7 +97,7 @@ func TestOrganizationDataSourceReadError(t *testing.T) {
}`)))

mockClient.EXPECT().
ListOrganizations(gomock.Any()).
UsersAPIListOrganizations(gomock.Any(), gomock.Any()).
Return(&http.Response{StatusCode: 200, Body: body, Header: map[string][]string{"Content-Type": {"json"}}}, nil)

state := terraform.NewInstanceStateShimmedFromValue(cty.ObjectVal(map[string]cty.Value{}), 0)
Expand Down
68 changes: 34 additions & 34 deletions castai/resource_organization_members.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"github.com/castai/terraform-provider-castai/castai/sdk"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/samber/lo"
)

const (
Expand Down Expand Up @@ -76,12 +77,12 @@
func resourceOrganizationMembersCreate(ctx context.Context, data *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*ProviderConfig).api

currentUserResp, err := client.CurrentUserProfileWithResponse(ctx)
currentUserResp, err := client.UsersAPICurrentUserProfileWithResponse(ctx)

Check failure on line 80 in castai/resource_organization_members.go

View workflow job for this annotation

GitHub Actions / Build

client.UsersAPICurrentUserProfileWithResponse undefined (type *sdk.ClientWithResponses has no field or method UsersAPICurrentUserProfileWithResponse)
if err := sdk.CheckOKResponse(currentUserResp, err); err != nil {
return diag.FromErr(fmt.Errorf("retrieving current user profile: %w", err))
}

var newMemberships []sdk.NewMembershipByEmail
var newMemberships []sdk.CastaiUsersV1beta1NewMembershipByEmail

Check failure on line 85 in castai/resource_organization_members.go

View workflow job for this annotation

GitHub Actions / Build

undefined: sdk.CastaiUsersV1beta1NewMembershipByEmail

if owners, ok := data.GetOk(FieldOrganizationMembersOwners); ok {
emails := toStringList(owners.([]interface{}))
Expand All @@ -90,11 +91,11 @@
// Person that creates a new organization is automatically the owner
// of it. That's why when creating this resource we would like to skip
// re-creating that user because it would fail.
if currentUserResp.JSON200.Email == email {
if lo.FromPtr(currentUserResp.JSON200.Email) == email {
continue
}

newMemberships = append(newMemberships, sdk.NewMembershipByEmail{
newMemberships = append(newMemberships, sdk.CastaiUsersV1beta1NewMembershipByEmail{

Check failure on line 98 in castai/resource_organization_members.go

View workflow job for this annotation

GitHub Actions / Build

undefined: sdk.CastaiUsersV1beta1NewMembershipByEmail
Role: ownerRole,
UserEmail: email,
})
Expand All @@ -105,7 +106,7 @@
emails := toStringList(viewers.([]interface{}))

for _, email := range emails {
newMemberships = append(newMemberships, sdk.NewMembershipByEmail{
newMemberships = append(newMemberships, sdk.CastaiUsersV1beta1NewMembershipByEmail{

Check failure on line 109 in castai/resource_organization_members.go

View workflow job for this annotation

GitHub Actions / Build

undefined: sdk.CastaiUsersV1beta1NewMembershipByEmail
Role: viewerRole,
UserEmail: email,
})
Expand All @@ -116,7 +117,7 @@
emails := toStringList(members.([]interface{}))

for _, email := range emails {
newMemberships = append(newMemberships, sdk.NewMembershipByEmail{
newMemberships = append(newMemberships, sdk.CastaiUsersV1beta1NewMembershipByEmail{

Check failure on line 120 in castai/resource_organization_members.go

View workflow job for this annotation

GitHub Actions / Build

undefined: sdk.CastaiUsersV1beta1NewMembershipByEmail
Role: memberRole,
UserEmail: email,
})
Expand All @@ -125,9 +126,8 @@

organizationID := data.Get(FieldOrganizationMembersOrganizationID).(string)

resp, err := client.CreateInvitationWithResponse(ctx, sdk.CreateInvitationJSONRequestBody{
OrganizationId: organizationID,
Members: newMemberships,
resp, err := client.UsersAPICreateInvitationsWithResponse(ctx, sdk.UsersAPICreateInvitationsJSONRequestBody{

Check failure on line 129 in castai/resource_organization_members.go

View workflow job for this annotation

GitHub Actions / Build

client.UsersAPICreateInvitationsWithResponse undefined (type *sdk.ClientWithResponses has no field or method UsersAPICreateInvitationsWithResponse)

Check failure on line 129 in castai/resource_organization_members.go

View workflow job for this annotation

GitHub Actions / Build

undefined: sdk.UsersAPICreateInvitationsJSONRequestBody
Members: &newMemberships,
})
if err := sdk.CheckOKResponse(resp, err); err != nil {
return diag.FromErr(fmt.Errorf("creating invitations: %w", err))
Expand All @@ -142,7 +142,7 @@
client := meta.(*ProviderConfig).api

organizationID := data.Id()
usersResp, err := client.GetOrganizationUsersWithResponse(ctx, organizationID)
usersResp, err := client.UsersAPIListOrganizationUsersWithResponse(ctx, organizationID)

Check failure on line 145 in castai/resource_organization_members.go

View workflow job for this annotation

GitHub Actions / Build

client.UsersAPIListOrganizationUsersWithResponse undefined (type *sdk.ClientWithResponses has no field or method UsersAPIListOrganizationUsersWithResponse)
if err := sdk.CheckOKResponse(usersResp, err); err != nil {
return diag.FromErr(fmt.Errorf("retrieving users: %w", err))
}
Expand All @@ -161,7 +161,7 @@

var nextCursor string
for {
invitationsResp, err := client.ListInvitationsWithResponse(ctx, &sdk.ListInvitationsParams{
invitationsResp, err := client.UsersAPIListInvitationsWithResponse(ctx, &sdk.UsersAPIListInvitationsParams{
PageCursor: &nextCursor,
})
if err := sdk.CheckOKResponse(usersResp, err); err != nil {
Expand Down Expand Up @@ -208,29 +208,29 @@
client := meta.(*ProviderConfig).api
organizationID := data.Id()

usersResp, err := client.GetOrganizationUsersWithResponse(ctx, organizationID)
usersResp, err := client.UsersAPIListOrganizationUsersWithResponse(ctx, organizationID)
if err := sdk.CheckOKResponse(usersResp, err); err != nil {
return diag.FromErr(fmt.Errorf("retrieving users: %w", err))
}

userIDByEmail := make(map[string]string)
for _, user := range *usersResp.JSON200.Users {
userIDByEmail[user.User.Email] = user.User.Id
userIDByEmail[user.User.Email] = lo.FromPtr(user.User.Id)
}

invitationIDByEmail := make(map[string]string)

var nextCursor string
for {
invitationsResp, err := client.ListInvitationsWithResponse(ctx, &sdk.ListInvitationsParams{
invitationsResp, err := client.UsersAPIListInvitationsWithResponse(ctx, &sdk.UsersAPIListInvitationsParams{
PageCursor: &nextCursor,
})
if err := sdk.CheckOKResponse(usersResp, err); err != nil {
return diag.FromErr(fmt.Errorf("retrieving pending invitations: %w", err))
}

for _, invitation := range invitationsResp.JSON200.Invitations {
invitationIDByEmail[invitation.InviteEmail] = invitation.Id
invitationIDByEmail[invitation.InviteEmail] = lo.FromPtr(invitation.Id)
}

nextCursor = invitationsResp.JSON200.NextCursor
Expand All @@ -247,50 +247,50 @@

manipulations := getPendingManipulations(diff, userIDByEmail, invitationIDByEmail)

currentUserResp, err := client.CurrentUserProfileWithResponse(ctx)
currentUserResp, err := client.UsersAPICurrentUserProfileWithResponse(ctx)
if err := sdk.CheckOKResponse(currentUserResp, err); err != nil {
return diag.FromErr(fmt.Errorf("retrieving current user profile: %w", err))
}
if contains(manipulations.membersToDelete, *currentUserResp.JSON200.Id) {
return diag.FromErr(
fmt.Errorf("can't delete user that is currently managing this organization: %s", currentUserResp.JSON200.Email),
fmt.Errorf("can't delete user that is currently managing this organization: %s", lo.FromPtr(currentUserResp.JSON200.Email)),
)
}

for userID, role := range manipulations.membersToUpdate {
resp, err := client.UpdateOrganizationUserWithResponse(ctx, organizationID, userID, sdk.UpdateOrganizationUser{
Role: sdk.OrganizationRole(role),
role := role
resp, err := client.UsersAPIUpdateOrganizationUserWithResponse(ctx, organizationID, userID, sdk.UsersAPIUpdateOrganizationUserJSONRequestBody{
Role: &role,
})
if err := sdk.CheckOKResponse(resp, err); err != nil {
return diag.FromErr(fmt.Errorf("updating user: %w", err))
}
}

for _, userID := range manipulations.membersToDelete {
resp, err := client.DeleteOrganizationUserWithResponse(ctx, organizationID, userID)
resp, err := client.UsersAPIRemoveUserFromOrganizationWithResponse(ctx, organizationID, userID)
if err := sdk.CheckOKResponse(resp, err); err != nil {
return diag.FromErr(fmt.Errorf("deleting user: %w", err))
}
}

for _, invitationID := range manipulations.invitationsToDelete {
resp, err := client.DeleteInvitationWithResponse(ctx, invitationID)
resp, err := client.UsersAPIDeleteInvitationWithResponse(ctx, invitationID)
if err := sdk.CheckOKResponse(resp, err); err != nil {
return diag.FromErr(fmt.Errorf("deleting invitation: %w", err))
}
}

newMemberships := make([]sdk.NewMembershipByEmail, 0, len(manipulations.membersToAdd))
newMemberships := make([]sdk.CastaiUsersV1beta1NewMembershipByEmail, 0, len(manipulations.membersToAdd))
for user, role := range manipulations.membersToAdd {
newMemberships = append(newMemberships, sdk.NewMembershipByEmail{
newMemberships = append(newMemberships, sdk.CastaiUsersV1beta1NewMembershipByEmail{
Role: role,
UserEmail: user,
})
}

resp, err := client.CreateInvitationWithResponse(ctx, sdk.CreateInvitationJSONBody{
Members: newMemberships,
OrganizationId: organizationID,
resp, err := client.UsersAPICreateInvitationsWithResponse(ctx, sdk.UsersAPICreateInvitationsJSONBody{
Members: &newMemberships,
})
if err := sdk.CheckOKResponse(resp, err); err != nil {
return diag.FromErr(fmt.Errorf("creating invitations: %w", err))
Expand All @@ -303,12 +303,12 @@
client := meta.(*ProviderConfig).api
organizationID := data.Id()

currentUserResp, err := client.CurrentUserProfileWithResponse(ctx)
currentUserResp, err := client.UsersAPICurrentUserProfileWithResponse(ctx)
if err := sdk.CheckOKResponse(currentUserResp, err); err != nil {
return diag.FromErr(fmt.Errorf("retrieving current user profile: %w", err))
}

usersResp, err := client.GetOrganizationUsersWithResponse(ctx, organizationID)
usersResp, err := client.UsersAPIListOrganizationUsersWithResponse(ctx, organizationID)
if err := sdk.CheckOKResponse(usersResp, err); err != nil {
return diag.FromErr(fmt.Errorf("retrieving users: %w", err))
}
Expand All @@ -319,25 +319,25 @@
// user that is currently managing this organization.
// Otherwise, if we have deleted all the members, the organization would
// get deleted too.
if user.User.Id == *currentUserResp.JSON200.Id {
if lo.FromPtr(user.User.Id) == lo.FromPtr(currentUserResp.JSON200.Id) {
continue
}

usersIDsToDelete = append(usersIDsToDelete, user.User.Id)
usersIDsToDelete = append(usersIDsToDelete, lo.FromPtr(user.User.Id))
}

var invitationIDsToDelete []string
var nextCursor string
for {
invitationsResp, err := client.ListInvitationsWithResponse(ctx, &sdk.ListInvitationsParams{
invitationsResp, err := client.UsersAPIListInvitationsWithResponse(ctx, &sdk.UsersAPIListInvitationsParams{
PageCursor: &nextCursor,
})
if err != nil {
return diag.FromErr(fmt.Errorf("retrieving invitations: %w", err))
}

for _, invitation := range invitationsResp.JSON200.Invitations {
invitationIDsToDelete = append(invitationIDsToDelete, invitation.Id)
invitationIDsToDelete = append(invitationIDsToDelete, lo.FromPtr(invitation.Id))
}

nextCursor = invitationsResp.JSON200.NextCursor
Expand All @@ -347,14 +347,14 @@
}

for _, invitationID := range invitationIDsToDelete {
resp, err := client.DeleteInvitationWithResponse(ctx, invitationID)
resp, err := client.UsersAPIDeleteInvitationWithResponse(ctx, invitationID)
if err := sdk.CheckOKResponse(resp, err); err != nil {
return diag.FromErr(fmt.Errorf("deleting invitation: %w", err))
}
}

for _, userID := range usersIDsToDelete {
resp, err := client.DeleteOrganizationUserWithResponse(ctx, organizationID, userID)
resp, err := client.UsersAPIRemoveUserFromOrganizationWithResponse(ctx, organizationID, userID)
if err := sdk.CheckOKResponse(resp, err); err != nil {
return diag.FromErr(fmt.Errorf("deleting user: %w", err))
}
Expand Down
4 changes: 2 additions & 2 deletions castai/resource_organization_members_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,11 @@ func TestOrganizationResourceReadContext(t *testing.T) {
"invitations": []
}`)))
mockClient.EXPECT().
GetOrganizationUsers(gomock.Any(), organizationID).
UsersAPIListOrganizationUsers(gomock.Any(), organizationID).
Return(&http.Response{StatusCode: 200, Body: body, Header: map[string][]string{"Content-Type": {"json"}}}, nil)

mockClient.EXPECT().
ListInvitations(gomock.Any(), gomock.Any()).Return(&http.Response{StatusCode: 200, Body: listInvitationsBody, Header: map[string][]string{"Content-Type": {"json"}}}, nil)
UsersAPIListInvitations(gomock.Any(), gomock.Any()).Return(&http.Response{StatusCode: 200, Body: listInvitationsBody, Header: map[string][]string{"Content-Type": {"json"}}}, nil)

state := terraform.NewInstanceStateShimmedFromValue(cty.ObjectVal(map[string]cty.Value{}), 0)
state.ID = organizationID
Expand Down
9 changes: 5 additions & 4 deletions castai/resource_reservations.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@ import (
"context"
"encoding/csv"
"fmt"
"log"
"strings"
"time"

"github.com/castai/terraform-provider-castai/castai/reservations"
"github.com/castai/terraform-provider-castai/castai/sdk"
"github.com/google/uuid"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/samber/lo"
"log"
"strings"
"time"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
Expand Down Expand Up @@ -273,7 +274,7 @@ func getOrganizationId(ctx context.Context, d *schema.ResourceData, meta any) (s
return organizationId.String(), nil
}

response, err := client.ListOrganizationsWithResponse(ctx)
response, err := client.UsersAPIListOrganizationsWithResponse(ctx, &sdk.UsersAPIListOrganizationsParams{})
if checkErr := sdk.CheckOKResponse(response, err); checkErr != nil {
return "", fmt.Errorf("fetching organizations: %w", checkErr)
}
Expand Down
Loading
Loading