Skip to content

Commit

Permalink
fix: update subaccount with missing fields (#279)
Browse files Browse the repository at this point in the history
  • Loading branch information
lechnerc77 authored Jul 9, 2023
1 parent 684d353 commit b33f08b
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 13 deletions.
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 @@ -116,8 +116,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 @@ -126,14 +129,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
19 changes: 14 additions & 5 deletions internal/provider/resource_subaccount.go
Original file line number Diff line number Diff line change
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

0 comments on commit b33f08b

Please sign in to comment.