diff --git a/bitbucket/data_group_members.go b/bitbucket/data_group_members.go index 1d83d0d1..b5be3e91 100644 --- a/bitbucket/data_group_members.go +++ b/bitbucket/data_group_members.go @@ -7,6 +7,7 @@ import ( "io" "log" + "github.com/DrFaust92/bitbucket-go-client" "github.com/hashicorp/terraform-plugin-sdk/v2/diag" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" ) @@ -25,9 +26,30 @@ func dataGroupMembers() *schema.Resource { Required: true, }, "members": { + Type: schema.TypeSet, + Elem: &schema.Schema{Type: schema.TypeString}, + Computed: true, + Deprecated: "use group_members instead", + }, + "group_members": { Type: schema.TypeSet, - Elem: &schema.Schema{Type: schema.TypeString}, Computed: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "uuid": { + Type: schema.TypeString, + Computed: true, + }, + "username": { + Type: schema.TypeString, + Computed: true, + }, + "display_name": { + Type: schema.TypeString, + Computed: true, + }, + }, + }, }, }, } @@ -45,7 +67,7 @@ func dataReadGroupMembers(ctx context.Context, d *schema.ResourceData, m interfa return diag.Errorf("error reading Group (%s): empty response", d.Id()) } - var members []*UserGroupMembership + var members []bitbucket.Account body, readerr := io.ReadAll(groupsReq.Body) if readerr != nil { @@ -63,13 +85,14 @@ func dataReadGroupMembers(ctx context.Context, d *schema.ResourceData, m interfa var mems []string for _, mbr := range members { - mems = append(mems, mbr.UUID) + mems = append(mems, mbr.Uuid) } d.SetId(fmt.Sprintf("%s/%s", workspace, slug)) d.Set("workspace", workspace) d.Set("slug", slug) d.Set("members", mems) + d.Set("group_members", flattenAccounts(members)) return nil } diff --git a/bitbucket/data_group_members_test.go b/bitbucket/data_group_members_test.go index b39be6d2..7e3225c1 100644 --- a/bitbucket/data_group_members_test.go +++ b/bitbucket/data_group_members_test.go @@ -27,6 +27,7 @@ func TestAccDataSourceDataGroupMembers_basic(t *testing.T) { resource.TestCheckResourceAttrPair(dataSourceName, "slug", groupResourceName, "slug"), resource.TestCheckResourceAttr(dataSourceName, "members.#", "1"), resource.TestCheckTypeSetElemAttrPair(dataSourceName, "members.*", "data.bitbucket_current_user.test", "uuid"), + resource.TestCheckResourceAttr(dataSourceName, "group_members.#", "1"), ), }, }, diff --git a/bitbucket/data_workspace_members.go b/bitbucket/data_workspace_members.go index 7fa7a502..a7ef693c 100644 --- a/bitbucket/data_workspace_members.go +++ b/bitbucket/data_workspace_members.go @@ -2,10 +2,10 @@ package bitbucket import ( "context" - "encoding/json" - "fmt" + "log" "github.com/DrFaust92/bitbucket-go-client" + "github.com/antihax/optional" "github.com/hashicorp/terraform-plugin-sdk/v2/diag" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" ) @@ -20,48 +20,60 @@ func dataWorkspaceMembers() *schema.Resource { Required: true, }, "members": { + Type: schema.TypeSet, + Elem: &schema.Schema{Type: schema.TypeString}, + Computed: true, + Deprecated: "use workspace_members instead", + }, + "workspace_members": { Type: schema.TypeSet, - Elem: &schema.Schema{Type: schema.TypeString}, Computed: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "uuid": { + Type: schema.TypeString, + Computed: true, + }, + "username": { + Type: schema.TypeString, + Computed: true, + }, + "display_name": { + Type: schema.TypeString, + Computed: true, + }, + }, + }, }, }, } } func dataReadWorkspaceMembers(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { - client := m.(Clients).httpClient + c := m.(Clients).genClient - workspace := d.Get("workspace").(string) - resourceURL := fmt.Sprintf("2.0/workspaces/%s/members", workspace) + workspaceApi := c.ApiClient.WorkspacesApi - _, err := client.Get(resourceURL) - if err != nil { - return diag.FromErr(err) - } + workspace := d.Get("workspace").(string) - var paginatedMemberships bitbucket.PaginatedWorkspaceMemberships var members []string + var accounts []bitbucket.Account + options := bitbucket.WorkspacesApiWorkspacesWorkspaceMembersGetOpts{} for { - membersRes, err := client.Get(resourceURL) - if err != nil { + flattenAccountsReq, _, err := workspaceApi.WorkspacesWorkspaceMembersGet(c.AuthContext, workspace, &options) + if err := handleClientError(err); err != nil { return diag.FromErr(err) } - decoder := json.NewDecoder(membersRes.Body) - err = decoder.Decode(&paginatedMemberships) - if err != nil { - return diag.FromErr(err) - } - - for _, member := range paginatedMemberships.Values { + for _, member := range flattenAccountsReq.Values { members = append(members, member.User.Uuid) + accounts = append(accounts, *member.User) } - if paginatedMemberships.Next != "" { - nextPage := paginatedMemberships.Page + 1 - resourceURL = fmt.Sprintf("2.0/workspaces/%s/members?page=%d", workspace, nextPage) - paginatedMemberships = bitbucket.PaginatedWorkspaceMemberships{} + if flattenAccountsReq.Next != "" { + nextPage := flattenAccountsReq.Page + 1 + options.Page = optional.NewInt32(nextPage) } else { break } @@ -70,6 +82,29 @@ func dataReadWorkspaceMembers(ctx context.Context, d *schema.ResourceData, m int d.SetId(workspace) d.Set("workspace", workspace) d.Set("members", members) + d.Set("workspace_members", flattenAccounts(accounts)) return nil } + +func flattenAccounts(flattenAccounts []bitbucket.Account) []interface{} { + if len(flattenAccounts) == 0 { + return nil + } + + var tfList []interface{} + + for _, account := range flattenAccounts { + log.Printf("[DEBUG] Workspace Member Response: %#v", account) + + flattenAccounts := map[string]interface{}{ + "uuid": account.Uuid, + "username": account.DisplayName, + "display_name": account.Username, + } + + tfList = append(tfList, flattenAccounts) + } + + return tfList +} diff --git a/bitbucket/data_workspace_members_test.go b/bitbucket/data_workspace_members_test.go index b5d6858d..ed9967b7 100644 --- a/bitbucket/data_workspace_members_test.go +++ b/bitbucket/data_workspace_members_test.go @@ -19,6 +19,8 @@ func TestAccDataSourceWorkspaceMembers_basic(t *testing.T) { Config: testAccBitbucketWorkspaceMembersConfig(workspace), Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttrSet(dataSourceName, "members.#"), + resource.TestCheckTypeSetElemAttrPair(dataSourceName, "members.*", "data.bitbucket_current_user.test", "uuid"), + resource.TestCheckResourceAttrSet(dataSourceName, "workspace_members.#"), ), }, }, @@ -27,6 +29,8 @@ func TestAccDataSourceWorkspaceMembers_basic(t *testing.T) { func testAccBitbucketWorkspaceMembersConfig(workspace string) string { return fmt.Sprintf(` +data "bitbucket_current_user" "test" {} + data "bitbucket_workspace_members" "test" { workspace = %[1]q } diff --git a/docs/data-sources/group_members.md b/docs/data-sources/group_members.md index 8bf6cf62..5b81c466 100644 --- a/docs/data-sources/group_members.md +++ b/docs/data-sources/group_members.md @@ -29,3 +29,10 @@ The following arguments are supported: ## Attributes Reference * `members` - A list of group member uuid. +* `group_members` - A set of group member objects. See [Group Member](#group-member) below. + +### Group Member + +* `uuid` - User UUID. +* `username` - The Username. +* `display_name` - The User display name. diff --git a/docs/data-sources/workspace_members.md b/docs/data-sources/workspace_members.md index fd92aa85..f2d9bd71 100644 --- a/docs/data-sources/workspace_members.md +++ b/docs/data-sources/workspace_members.md @@ -30,3 +30,10 @@ The following arguments are supported: * `members` - A set of string containing the member UUIDs. * `id` - The workspace's immutable id. +* `workspace_members` - A set of workspace member objects. See [Workspace Members](#workspace-member) below. + +### Workspace Member + +* `uuid` - User UUID. +* `username` - The Username. +* `display_name` - The User display name.