Skip to content

Commit

Permalink
Merge pull request #57 from matt-illion/f-workspace-users-data-source
Browse files Browse the repository at this point in the history
Adding workspace members data source
  • Loading branch information
DrFaust92 authored May 9, 2022
2 parents 99d5b9f + f986233 commit 5b161ff
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 0 deletions.
73 changes: 73 additions & 0 deletions bitbucket/data_workspace_members.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package bitbucket

import (
"encoding/json"
"fmt"

"github.com/DrFaust92/bitbucket-go-client"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func dataWorkspaceMembers() *schema.Resource {
return &schema.Resource{
Read: dataReadWorkspaceMembers,

Schema: map[string]*schema.Schema{
"workspace": {
Type: schema.TypeString,
Required: true,
},
"members": {
Type: schema.TypeSet,
Elem: &schema.Schema{Type: schema.TypeString},
Computed: true,
},
},
}
}

func dataReadWorkspaceMembers(d *schema.ResourceData, m interface{}) error {
client := m.(Clients).httpClient

workspace := d.Get("workspace").(string)
resourceURL := fmt.Sprintf("2.0/workspaces/%s/members", workspace)

_, err := client.Get(resourceURL)
if err != nil {
return err
}

var paginatedMemberships bitbucket.PaginatedWorkspaceMemberships
var members []string

for {
membersRes, err := client.Get(resourceURL)
if err != nil {
return err
}

decoder := json.NewDecoder(membersRes.Body)
err = decoder.Decode(&paginatedMemberships)
if err != nil {
return err
}

for _, member := range paginatedMemberships.Values {
members = append(members, member.User.Uuid)
}

if paginatedMemberships.Next != "" {
nextPage := paginatedMemberships.Page + 1
resourceURL = fmt.Sprintf("2.0/workspaces/%s/members?page=%d", workspace, nextPage)
paginatedMemberships = bitbucket.PaginatedWorkspaceMemberships{}
} else {
break
}
}

d.SetId(workspace)
d.Set("workspace", workspace)
d.Set("members", members)

return nil
}
34 changes: 34 additions & 0 deletions bitbucket/data_workspace_members_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package bitbucket

import (
"fmt"
"os"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

func TestAccWorkspaceMembers_basic(t *testing.T) {
dataSourceName := "data.bitbucket_workspace_members.test"
workspace := os.Getenv("BITBUCKET_TEAM")
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccBitbucketWorkspaceMembersConfig(workspace),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet(dataSourceName, "members.#"),
),
},
},
})
}

func testAccBitbucketWorkspaceMembersConfig(workspace string) string {
return fmt.Sprintf(`
data "bitbucket_workspace_members" "test" {
workspace = %[1]q
}
`, workspace)
}
1 change: 1 addition & 0 deletions bitbucket/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ func Provider() *schema.Provider {
"bitbucket_user": dataUser(),
"bitbucket_current_user": dataCurrentUser(),
"bitbucket_workspace": dataWorkspace(),
"bitbucket_workspace_members": dataWorkspaceMembers(),
},
}
}
Expand Down
30 changes: 30 additions & 0 deletions docs/data-sources/workspace_members.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
---
layout: "bitbucket"
page_title: "Bitbucket: bitbucket_workspace_members"
sidebar_current: "docs-bitbucket-data-workspace-members"
description: |-
Provides a data for a Bitbucket workspace members
---

# bitbucket\_workspace\_members

Provides a way to fetch data on a the members of a workspace.

## Example Usage

```hcl
data "bitbucket_workspace_members" "example" {
workspace = "gob"
}
```

## Argument Reference

The following arguments are supported:

* `workspace` - (Required) This can either be the workspace ID (slug) or the workspace UUID surrounded by curly-braces.

## Attributes Reference

* `members` - A set of string containing the member UUIDs.
* `id` - The workspace's immutable id.

0 comments on commit 5b161ff

Please sign in to comment.