Skip to content

Commit

Permalink
feature(resource): support directory update (#284)
Browse files Browse the repository at this point in the history
Add support for update operation to directory facade and resource

Also: add Globalaccount property to DirectoryCreateInput

Resolves #88
  • Loading branch information
kuntzed authored Jul 10, 2023
1 parent 539ee6a commit 05c0365
Show file tree
Hide file tree
Showing 6 changed files with 1,679 additions and 892 deletions.
36 changes: 29 additions & 7 deletions internal/btpcli/facade_accounts_directory.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,25 +27,47 @@ func (f *accountsDirectoryFacade) Get(ctx context.Context, directoryId string) (
}

type DirectoryCreateInput struct {
DisplayName string `btpcli:"displayName"`
Description *string `btpcli:"description"`
ParentID *string `btpcli:"parentID"`
Subdomain *string `btpcli:"subdomain"`
Labels map[string][]string `btpcli:"labels"`
DisplayName string `btpcli:"displayName"`
Description *string `btpcli:"description"`
ParentID *string `btpcli:"parentID"`
Subdomain *string `btpcli:"subdomain"`
Labels map[string][]string `btpcli:"labels"`
Globalaccount string `btpcli:"globalAccount"`
//DirectoryAdmins string `btpcli:"directoryAdmins"`
}

type DirectoryUpdateInput struct {
DirectoryId string `btpcli:"directoryID"`
Globalaccount string `btpcli:"globalAccount"`
DisplayName *string `btpcli:"displayName"`
Description *string `btpcli:"description"`
Labels map[string][]string `btpcli:"labels"`
}

func (f *accountsDirectoryFacade) Create(ctx context.Context, args *DirectoryCreateInput) (cis.DirectoryResponseObject, CommandResponse, error) {
args.Globalaccount = f.cliClient.GetGlobalAccountSubdomain()

params, err := tfutils.ToBTPCLIParamsMap(args)

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

params["globalAccount"] = f.cliClient.GetGlobalAccountSubdomain()

return doExecute[cis.DirectoryResponseObject](f.cliClient, ctx, NewCreateRequest(f.getCommand(), params))
}

func (f *accountsDirectoryFacade) Update(ctx context.Context, args *DirectoryUpdateInput) (cis.DirectoryResponseObject, CommandResponse, error) {
args.Globalaccount = f.cliClient.GetGlobalAccountSubdomain()

params, err := tfutils.ToBTPCLIParamsMap(args)

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

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

func (f *accountsDirectoryFacade) Delete(ctx context.Context, directoryId string) (cis.DirectoryResponseObject, CommandResponse, error) {
return doExecute[cis.DirectoryResponseObject](f.cliClient, ctx, NewDeleteRequest(f.getCommand(), map[string]string{
"globalAccount": f.cliClient.GetGlobalAccountSubdomain(),
Expand Down
49 changes: 45 additions & 4 deletions internal/btpcli/facade_accounts_directory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func TestAccountsDirectoryFacade_Get(t *testing.T) {

func TestAccountsDirectoryFacade_Create(t *testing.T) {
command := "accounts/directory"
globalAccount := "795b53bb-a3f0-4769-adf0-26173282a975"

displayName := "my-directory"
description := "a description"
Expand All @@ -47,14 +48,14 @@ func TestAccountsDirectoryFacade_Create(t *testing.T) {
srvCalled = true

assertCall(t, r, command, ActionCreate, map[string]string{
"globalAccount": "795b53bb-a3f0-4769-adf0-26173282a975",
"displayName": "my-directory",
"globalAccount": globalAccount,
"displayName": displayName,
})
}))
defer srv.Close()

_, res, err := uut.Accounts.Directory.Create(context.TODO(), &DirectoryCreateInput{
DisplayName: "my-directory",
DisplayName: displayName,
})

if assert.True(t, srvCalled) && assert.NoError(t, err) {
Expand All @@ -68,11 +69,12 @@ func TestAccountsDirectoryFacade_Create(t *testing.T) {
srvCalled = true

assertCall(t, r, command, ActionCreate, map[string]string{
"globalAccount": "795b53bb-a3f0-4769-adf0-26173282a975",
"globalAccount": globalAccount,
"displayName": displayName,
"description": description,
"subdomain": subdomain,
"parentID": parentId,
"labels": "{}",
})
}))
defer srv.Close()
Expand All @@ -82,6 +84,45 @@ func TestAccountsDirectoryFacade_Create(t *testing.T) {
Description: &description,
Subdomain: &subdomain,
ParentID: &parentId,
Labels: map[string][]string{},
})

if assert.True(t, srvCalled) && assert.NoError(t, err) {
assert.Equal(t, 200, res.StatusCode)
}
})
}

func TestAccountsDirectoryFacade_Update(t *testing.T) {
command := "accounts/directory"
globalAccount := "795b53bb-a3f0-4769-adf0-26173282a975"

directoryId := "6aa64c2f-38c1-49a9-b2e8-cf9fea769b7f"
displayName := "my-directory"
description := "a description"

t.Run("constructs the CLI params correctly", func(t *testing.T) {
var srvCalled bool

uut, srv := prepareClientFacadeForTest(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
srvCalled = true

assertCall(t, r, command, ActionUpdate, map[string]string{
"globalAccount": globalAccount,
"directoryID": directoryId,
"displayName": displayName,
"description": description,
"labels": "{}",
})

}))
defer srv.Close()

_, res, err := uut.Accounts.Directory.Update(context.TODO(), &DirectoryUpdateInput{
DirectoryId: directoryId,
DisplayName: &displayName,
Description: &description,
Labels: map[string][]string{},
})

if assert.True(t, srvCalled) && assert.NoError(t, err) {
Expand Down
Loading

0 comments on commit 05c0365

Please sign in to comment.