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

Expand results for group members + move to use sdk #198

Merged
merged 1 commit into from
Feb 18, 2024
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
29 changes: 26 additions & 3 deletions bitbucket/data_group_members.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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,
},
},
},
},
},
}
Expand All @@ -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 {
Expand All @@ -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
}
1 change: 1 addition & 0 deletions bitbucket/data_group_members_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
),
},
},
Expand Down
83 changes: 59 additions & 24 deletions bitbucket/data_workspace_members.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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
}
Expand All @@ -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
}
4 changes: 4 additions & 0 deletions bitbucket/data_workspace_members_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.#"),
),
},
},
Expand All @@ -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
}
Expand Down
7 changes: 7 additions & 0 deletions docs/data-sources/group_members.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
7 changes: 7 additions & 0 deletions docs/data-sources/workspace_members.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Loading