forked from aeirola/terraform-provider-bitbucket
-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #57 from matt-illion/f-workspace-users-data-source
Adding workspace members data source
- Loading branch information
Showing
4 changed files
with
138 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |