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

fix: update subaccount with missing fields #279

Merged
merged 3 commits into from
Jul 9, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
27 changes: 22 additions & 5 deletions internal/btpcli/facade_accounts_subaccount.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,17 @@ type SubaccountCreateInput struct { // TODO support all options
//SubaccountAdmins string `json:"subaccountAdmins"`
}

type SubaccountUpdateInput struct {
BetaEnabled bool `btpcli:"betaEnabled"`
Description string `btpcli:"description"`
Directory string `btpcli:"directoryID"`
DisplayName string `btpcli:"displayName"`
Labels map[string][]string `btpcli:"labels"`
SubaccountId string `btpcli:"subaccount"`
Globalaccount string `btpcli:"globalAccount"`
// UsedForProduction bool `btpcli:"usedForProduction"`
}

func (f *accountsSubaccountFacade) Create(ctx context.Context, args *SubaccountCreateInput) (cis.SubaccountResponseObject, CommandResponse, error) {

args.Globalaccount = f.cliClient.GetGlobalAccountSubdomain()
Expand All @@ -65,11 +76,17 @@ func (f *accountsSubaccountFacade) Create(ctx context.Context, args *SubaccountC
return doExecute[cis.SubaccountResponseObject](f.cliClient, ctx, NewCreateRequest(f.getCommand(), params))
}

func (f *accountsSubaccountFacade) Update(ctx context.Context, subaccountId string, displayName string) (cis.SubaccountResponseObject, CommandResponse, error) { // TODO switch to object
return doExecute[cis.SubaccountResponseObject](f.cliClient, ctx, NewUpdateRequest(f.getCommand(), map[string]string{
"subaccount": subaccountId,
"displayName": displayName,
}))
func (f *accountsSubaccountFacade) Update(ctx context.Context, args *SubaccountUpdateInput) (cis.SubaccountResponseObject, CommandResponse, error) { // TODO switch to object

args.Globalaccount = f.cliClient.GetGlobalAccountSubdomain()

params, err := tfutils.ToBTPCLIParamsMap(args)

if err != nil {
return cis.SubaccountResponseObject{}, CommandResponse{}, err
}

return doExecute[cis.SubaccountResponseObject](f.cliClient, ctx, NewUpdateRequest(f.getCommand(), params))
}

func (f *accountsSubaccountFacade) Delete(ctx context.Context, subaccountId string) (cis.SubaccountResponseObject, CommandResponse, error) {
Expand Down
16 changes: 13 additions & 3 deletions internal/btpcli/facade_accounts_subaccount_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,11 @@ func TestAccountsSubaccountFacade_Create(t *testing.T) {
func TestAccountsSubaccountFacade_Update(t *testing.T) {
command := "accounts/subaccount"

globalAccount := "795b53bb-a3f0-4769-adf0-26173282a975"

subaccountId := "6aa64c2f-38c1-49a9-b2e8-cf9fea769b7f"
displayName := "my-account"
description := "My Account Description"

t.Run("constructs the CLI params correctly", func(t *testing.T) {
var srvCalled bool
Expand All @@ -127,14 +130,21 @@ func TestAccountsSubaccountFacade_Update(t *testing.T) {
srvCalled = true

assertCall(t, r, command, ActionUpdate, map[string]string{
"subaccount": subaccountId,
"displayName": displayName,
"subaccount": subaccountId,
"displayName": displayName,
"description": description,
"betaEnabled": "false",
"globalAccount": globalAccount,
})

}))
defer srv.Close()

_, res, err := uut.Accounts.Subaccount.Update(context.TODO(), subaccountId, displayName)
_, res, err := uut.Accounts.Subaccount.Update(context.TODO(), &SubaccountUpdateInput{
SubaccountId: subaccountId,
DisplayName: displayName,
Description: description,
})

if assert.True(t, srvCalled) && assert.NoError(t, err) {
assert.Equal(t, 200, res.StatusCode)
Expand Down
21 changes: 15 additions & 6 deletions internal/provider/resource_subaccount.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ func (rs *subaccountResource) Create(ctx context.Context, req resource.CreateReq
args.Directory = parentID
}

if plan.BetaEnabled.IsUnknown() {
if !plan.BetaEnabled.IsUnknown() {
betaEnabled := plan.BetaEnabled.ValueBool()
args.BetaEnabled = betaEnabled
}
Expand Down Expand Up @@ -278,19 +278,28 @@ func (rs *subaccountResource) Create(ctx context.Context, req resource.CreateReq
}

func (rs *subaccountResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
var plan, state subaccountType
var plan subaccountType

diags := req.Plan.Get(ctx, &plan)
resp.Diagnostics.Append(diags...)

diags = req.State.Get(ctx, &state)
resp.Diagnostics.Append(diags...)

if resp.Diagnostics.HasError() {
return
}

cliRes, _, err := rs.cli.Accounts.Subaccount.Update(ctx, state.ID.ValueString(), plan.Name.ValueString())
args := btpcli.SubaccountUpdateInput{
BetaEnabled: plan.BetaEnabled.ValueBool(),
Description: plan.Description.ValueString(),
Directory: plan.ParentID.ValueString(),
DisplayName: plan.Name.ValueString(),
SubaccountId: plan.ID.ValueString(),
}

var labels map[string][]string
plan.Labels.ElementsAs(ctx, &labels, false)
args.Labels = labels

cliRes, _, err := rs.cli.Accounts.Subaccount.Update(ctx, &args)
if err != nil {
resp.Diagnostics.AddError("API Error Updating Resource Subaccount", fmt.Sprintf("%s", err))
return
Expand Down
Loading