From b33f08bc8c2ed7d709b5d25b250867c9ac868497 Mon Sep 17 00:00:00 2001 From: Christian Lechner Date: Sun, 9 Jul 2023 19:47:13 +0200 Subject: [PATCH] fix: update subaccount with missing fields (#279) --- internal/btpcli/facade_accounts_subaccount.go | 27 +++++++++++++++---- .../btpcli/facade_accounts_subaccount_test.go | 16 ++++++++--- internal/provider/resource_subaccount.go | 19 +++++++++---- 3 files changed, 49 insertions(+), 13 deletions(-) diff --git a/internal/btpcli/facade_accounts_subaccount.go b/internal/btpcli/facade_accounts_subaccount.go index 7f9b33fb..3fcce459 100644 --- a/internal/btpcli/facade_accounts_subaccount.go +++ b/internal/btpcli/facade_accounts_subaccount.go @@ -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() @@ -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) { diff --git a/internal/btpcli/facade_accounts_subaccount_test.go b/internal/btpcli/facade_accounts_subaccount_test.go index 193eedc9..d70b1ce6 100644 --- a/internal/btpcli/facade_accounts_subaccount_test.go +++ b/internal/btpcli/facade_accounts_subaccount_test.go @@ -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 @@ -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) diff --git a/internal/provider/resource_subaccount.go b/internal/provider/resource_subaccount.go index 03724c44..bc9ebeea 100644 --- a/internal/provider/resource_subaccount.go +++ b/internal/provider/resource_subaccount.go @@ -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