-
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
8 changed files
with
172 additions
and
23 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
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,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")) | ||
} |
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,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), | ||
), | ||
}, | ||
}, | ||
}) | ||
} |
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 @@ | ||
# 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. |
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
Oops, something went wrong.