Skip to content

Commit

Permalink
GH-67 Add new bitbucket_user_workspace data source (#94)
Browse files Browse the repository at this point in the history
* GH-67 Add new `bitbucket_user_workspace` data source

This allows for finding users by just their Bitbucket nickname - currently experimental, needs more validation before it can be promoted to stable.

And update `go-bitbucket` module.
  • Loading branch information
zahiar authored Apr 25, 2022
1 parent 3cf27b0 commit 30dc16c
Show file tree
Hide file tree
Showing 8 changed files with 172 additions and 23 deletions.
1 change: 1 addition & 0 deletions bitbucket/data_source_bitbucket_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
)

func dataSourceBitbucketUser() *schema.Resource {
// Any changes made here must be made to `dataSourceBitbucketUserWorkspace`
return &schema.Resource{
ReadContext: dataSourceBitbucketUserRead,
Schema: map[string]*schema.Schema{
Expand Down
4 changes: 2 additions & 2 deletions bitbucket/data_source_bitbucket_user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ func TestAccBitbucketUserDataSource_basic(t *testing.T) {
id = "%s"
}`, user.Uuid),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.bitbucket_user.testacc", "id", user.Uuid),
resource.TestCheckResourceAttr("data.bitbucket_user.testacc", "nickname", user.Nickname),
resource.TestCheckResourceAttr("data.bitbucket_user.testacc", "display_name", user.DisplayName),
resource.TestCheckResourceAttr("data.bitbucket_user.testacc", "account_status", user.AccountStatus),
resource.TestCheckResourceAttrSet("data.bitbucket_user.testacc", "id"),
resource.TestCheckResourceAttrSet("data.bitbucket_user.testacc", "account_id"),
resource.TestCheckResourceAttr("data.bitbucket_user.testacc", "account_id", user.AccountId),
),
},
},
Expand Down
67 changes: 67 additions & 0 deletions bitbucket/data_source_bitbucket_user_workspace.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package bitbucket

import (
"context"
"fmt"

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

func dataSourceBitbucketUserWorkspace() *schema.Resource {
// Any changes made here must be made to `dataSourceBitbucketUser`
return &schema.Resource{
ReadContext: dataSourceBitbucketUserWorkspaceRead,
Schema: map[string]*schema.Schema{
"id": {
Description: "The User's UUID.",
Type: schema.TypeString,
Computed: true,
},
"workspace": {
Description: "The slug or UUID (including the enclosing `{}`) of the workspace this user belongs to.",
Type: schema.TypeString,
Required: true,
},
"nickname": {
Description: "The User's nickname.",
Type: schema.TypeString,
Required: true,
},
"display_name": {
Description: "The User's display name.",
Type: schema.TypeString,
Computed: true,
},
"account_id": {
Description: "The User's account ID.",
Type: schema.TypeString,
Computed: true,
},
"account_status": {
Description: "The User's account status. Will be one of active, inactive or closed.",
Type: schema.TypeString,
Computed: true,
},
},
}
}

func dataSourceBitbucketUserWorkspaceRead(ctx context.Context, resourceData *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*Clients).V2

workspaceUsers, err := client.Workspaces.Members(resourceData.Get("workspace").(string))
if err != nil {
return diag.FromErr(fmt.Errorf("unable to get users with error: %s", err))
}

nickname := resourceData.Get("nickname").(string)
for _, user := range workspaceUsers.Members {
if nickname == user.Nickname {
resourceData.SetId(user.Uuid)
return dataSourceBitbucketUserRead(ctx, resourceData, meta)
}
}

return diag.FromErr(fmt.Errorf("unable to find user"))
}
40 changes: 40 additions & 0 deletions bitbucket/data_source_bitbucket_user_workspace_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package bitbucket

import (
"fmt"
"os"
"testing"

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

func TestAccBitbucketUserWorkspaceDataSource_basic(t *testing.T) {
workspace := os.Getenv("BITBUCKET_USERNAME")
user, _ := getCurrentUser()

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: testAccProviders,
Steps: []resource.TestStep{
{
Config: fmt.Sprintf(`
data "bitbucket_workspace" "testacc" {
id = "%s"
}
data "bitbucket_user_workspace" "testacc" {
nickname = "%s"
workspace = data.bitbucket_workspace.testacc.id
}`, workspace, user.Nickname),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.bitbucket_user_workspace.testacc", "id", user.Uuid),
resource.TestCheckResourceAttr("data.bitbucket_user_workspace.testacc", "nickname", user.Nickname),
resource.TestCheckResourceAttr("data.bitbucket_user_workspace.testacc", "display_name", user.DisplayName),
resource.TestCheckResourceAttr("data.bitbucket_user_workspace.testacc", "account_status", user.AccountStatus),
resource.TestCheckResourceAttr("data.bitbucket_user_workspace.testacc", "account_id", user.AccountId),
resource.TestCheckResourceAttr("data.bitbucket_user_workspace.testacc", "workspace", workspace),
),
},
},
})
}
3 changes: 3 additions & 0 deletions bitbucket/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ func Provider() *schema.Provider {
"bitbucket_project": dataSourceBitbucketProject(),
"bitbucket_repository": dataSourceBitbucketRepository(),
"bitbucket_user": dataSourceBitbucketUser(),
"bitbucket_user_workspace": dataSourceBitbucketUserWorkspace(),
"bitbucket_webhook": dataSourceBitbucketWebhook(),
"bitbucket_workspace": dataSourceBitbucketWorkspace(),
},
Expand Down Expand Up @@ -72,6 +73,8 @@ func configureProvider(ctx context.Context, resourceData *schema.ResourceData) (
resourceData.Get("username").(string),
resourceData.Get("password").(string),
)
client.Pagelen = 100
client.MaxDepth = 10

v1Client := v1.NewClient(
&v1.Auth{
Expand Down
30 changes: 30 additions & 0 deletions docs/data-sources/bitbucket_user_workspace.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Data Source: bitbucket_user_workspace

Note: this is an **experimental** data source - please raise tickets if you encounter issues.


Use this data source to get the user resource, you can then reference its attributes without having to hardcode them.

This data source is very similar to `bitbucket_user` - only difference being, this allows you to find users in a given
workspace by their Bitbucket nickname. Be mindful on very large teams, this resource may need to do multiple requests
to Bitbucket's API in order to find them.

## Example Usage
```hcl
data "bitbucket_user_workspace" "example" {
workspace = "{workspace-uuid}"
nickname = "EXAMPLE"
}
```

## Argument Reference
The following arguments are supported:
* `workspace` - (Required) The slug or UUID (including the enclosing `{}`) of the workspace the user belongs to.
* `nickname` - (Required) The User's nickname.

## Attribute Reference
In addition to the arguments above, the following additional attributes are exported:
* `id` - The User's UUID.
* `display_name` - The User's display name.
* `account_id` - The User's account ID.
* `account_status` - The User's account status. Will be one of active, inactive or closed.
16 changes: 8 additions & 8 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.17
require (
github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320
github.com/hashicorp/terraform-plugin-sdk/v2 v2.14.0
github.com/ktrysmt/go-bitbucket v0.9.42
github.com/ktrysmt/go-bitbucket v0.9.44
github.com/stretchr/testify v1.7.1
)

Expand All @@ -26,21 +26,21 @@ require (
github.com/hashicorp/go-uuid v1.0.3 // indirect
github.com/hashicorp/go-version v1.4.0 // indirect
github.com/hashicorp/hc-install v0.3.1 // indirect
github.com/hashicorp/hcl/v2 v2.11.1 // indirect
github.com/hashicorp/hcl/v2 v2.12.0 // indirect
github.com/hashicorp/logutils v1.0.0 // indirect
github.com/hashicorp/terraform-exec v0.16.1 // indirect
github.com/hashicorp/terraform-json v0.13.0 // indirect
github.com/hashicorp/terraform-plugin-go v0.9.0 // indirect
github.com/hashicorp/terraform-plugin-log v0.3.0 // indirect
github.com/hashicorp/terraform-registry-address v0.0.0-20220131103327-5c1c5e123275 // indirect
github.com/hashicorp/terraform-registry-address v0.0.0-20220422185603-6772e136ec01 // indirect
github.com/hashicorp/terraform-svchost v0.0.0-20200729002733-f050f53b9734 // indirect
github.com/hashicorp/yamux v0.0.0-20211028200310-0bc27b27de87 // indirect
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/mitchellh/copystructure v1.2.0 // indirect
github.com/mitchellh/go-testing-interface v1.14.1 // indirect
github.com/mitchellh/go-wordwrap v1.0.1 // indirect
github.com/mitchellh/mapstructure v1.4.3 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/oklog/run v1.1.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
Expand All @@ -49,13 +49,13 @@ require (
github.com/vmihailenco/tagparser v0.1.2 // indirect
github.com/zclconf/go-cty v1.10.0 // indirect
golang.org/x/crypto v0.0.0-20210616213533-5ff15b29337e // indirect
golang.org/x/net v0.0.0-20220412020605-290c469a71a5 // indirect
golang.org/x/net v0.0.0-20220421235706-1d1ef9303861 // indirect
golang.org/x/oauth2 v0.0.0-20220411215720-9780585627b5 // indirect
golang.org/x/sys v0.0.0-20220412211240-33da011f77ad // indirect
golang.org/x/sys v0.0.0-20220422013727-9388b58f7150 // indirect
golang.org/x/text v0.3.7 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20220414192740-2d67ff6cf2b4 // indirect
google.golang.org/grpc v1.45.0 // indirect
google.golang.org/genproto v0.0.0-20220422154200-b37d22cd5731 // indirect
google.golang.org/grpc v1.46.0 // indirect
google.golang.org/protobuf v1.28.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
)
Loading

0 comments on commit 30dc16c

Please sign in to comment.