-
-
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
Along with docs.
- Loading branch information
Showing
9 changed files
with
392 additions
and
3 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,39 @@ | ||
package bitbucket | ||
|
||
import ( | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
) | ||
|
||
func dataSourceBitbucketUserPermission() *schema.Resource { | ||
return &schema.Resource{ | ||
ReadContext: resourceBitbucketUserPermissionRead, | ||
Schema: map[string]*schema.Schema{ | ||
"id": { | ||
Description: "The ID of the user permission.", | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"workspace": { | ||
Description: "The UUID (including the enclosing `{}`) of the workspace.", | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
"repository": { | ||
Description: "The slug of the repository (must consist of only lowercase ASCII letters, numbers, underscores and hyphens).", | ||
Type: schema.TypeString, | ||
Required: true, | ||
ValidateDiagFunc: validateRepositoryName, | ||
}, | ||
"user": { | ||
Description: "The UUID (including the enclosing `{}`) of the user.", | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
"permission": { | ||
Description: "The permission this user will have. Must be one of 'read', 'write', 'admin'.", | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
}, | ||
} | ||
} |
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,69 @@ | ||
package bitbucket | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
) | ||
|
||
func TestAccBitbucketUserPermissionDataSource_basic(t *testing.T) { | ||
workspaceSlug := os.Getenv("BITBUCKET_USERNAME") | ||
projectName := "tf-acc-test-" + acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum) | ||
projectKey := strings.ToUpper(acctest.RandStringFromCharSet(3, acctest.CharSetAlpha)) | ||
repoName := "tf-acc-test-" + acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum) | ||
user := os.Getenv("BITBUCKET_MEMBER_ACCOUNT_UUID") | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
ProviderFactories: testAccProviders, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: fmt.Sprintf(` | ||
data "bitbucket_workspace" "testacc" { | ||
id = "%s" | ||
} | ||
resource "bitbucket_project" "testacc" { | ||
workspace = data.bitbucket_workspace.testacc.id | ||
name = "%s" | ||
key = "%s" | ||
} | ||
resource "bitbucket_repository" "testacc" { | ||
workspace = data.bitbucket_workspace.testacc.id | ||
project_key = bitbucket_project.testacc.key | ||
name = "%s" | ||
} | ||
data "bitbucket_user" "testacc" { | ||
id = "%s" | ||
} | ||
resource "bitbucket_user_permission" "testacc" { | ||
workspace = data.bitbucket_workspace.testacc.uuid | ||
repository = bitbucket_repository.testacc.name | ||
user = data.bitbucket_user.testacc.id | ||
permission = "read" | ||
} | ||
data "bitbucket_user_permission" "testacc" { | ||
workspace = data.bitbucket_workspace.testacc.uuid | ||
repository = bitbucket_repository.testacc.name | ||
user = data.bitbucket_user.testacc.id | ||
}`, workspaceSlug, projectName, projectKey, repoName, user), | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttr("data.bitbucket_user_permission.testacc", "repository", repoName), | ||
resource.TestCheckResourceAttr("data.bitbucket_user_permission.testacc", "user", user), | ||
|
||
resource.TestCheckResourceAttrSet("data.bitbucket_user_permission.testacc", "id"), | ||
resource.TestCheckResourceAttrSet("data.bitbucket_user_permission.testacc", "workspace"), | ||
resource.TestCheckResourceAttrSet("data.bitbucket_user_permission.testacc", "permission"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} |
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,137 @@ | ||
package bitbucket | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" | ||
"github.com/ktrysmt/go-bitbucket" | ||
) | ||
|
||
func resourceBitbucketUserPermission() *schema.Resource { | ||
return &schema.Resource{ | ||
CreateContext: resourceBitbucketUserPermissionCreate, | ||
ReadContext: resourceBitbucketUserPermissionRead, | ||
DeleteContext: resourceBitbucketUserPermissionDelete, | ||
Importer: &schema.ResourceImporter{ | ||
StateContext: resourceBitbucketUserPermissionImport, | ||
}, | ||
Schema: map[string]*schema.Schema{ | ||
"id": { | ||
Description: "The ID of the user permission.", | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"workspace": { | ||
Description: "The UUID (including the enclosing `{}`) of the workspace.", | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
"repository": { | ||
Description: "The slug of the repository (must consist of only lowercase ASCII letters, numbers, underscores and hyphens).", | ||
Type: schema.TypeString, | ||
Required: true, | ||
ValidateDiagFunc: validateRepositoryName, | ||
ForceNew: true, | ||
}, | ||
"user": { | ||
Description: "The UUID (including the enclosing `{}`) of the user.", | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
"permission": { | ||
Description: "The permission this user will have. Must be one of 'read', 'write', 'admin'.", | ||
Type: schema.TypeString, | ||
Required: true, | ||
ValidateFunc: validation.StringInSlice([]string{"read", "write", "admin"}, false), | ||
ForceNew: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceBitbucketUserPermissionCreate(ctx context.Context, resourceData *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
client := meta.(*Clients).V2 | ||
|
||
_, err := client.Repositories.Repository.SetUserPermissions(&bitbucket.RepositoryUserPermissionsOptions{ | ||
Owner: resourceData.Get("workspace").(string), | ||
RepoSlug: resourceData.Get("repository").(string), | ||
User: resourceData.Get("user").(string), | ||
Permission: resourceData.Get("permission").(string), | ||
}) | ||
|
||
if err != nil { | ||
return diag.FromErr(fmt.Errorf("unable to create user permission with error: %s", err)) | ||
} | ||
|
||
return resourceBitbucketUserPermissionRead(ctx, resourceData, meta) | ||
} | ||
|
||
func resourceBitbucketUserPermissionRead(ctx context.Context, resourceData *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
client := meta.(*Clients).V2 | ||
|
||
workspace := resourceData.Get("workspace").(string) | ||
repository := resourceData.Get("repository").(string) | ||
|
||
userPermission, err := client.Repositories.Repository.GetUserPermissions(&bitbucket.RepositoryUserPermissionsOptions{ | ||
Owner: workspace, | ||
RepoSlug: repository, | ||
User: resourceData.Get("user").(string), | ||
Permission: resourceData.Get("permission").(string), | ||
}) | ||
if err != nil { | ||
return diag.FromErr(fmt.Errorf("unable to get user permission with error: %s", err)) | ||
} | ||
|
||
_ = resourceData.Set("user", userPermission.User.Uuid) | ||
_ = resourceData.Set("permission", userPermission.Permission) | ||
|
||
resourceData.SetId(generateUserPermissionResourceId(workspace, repository, userPermission.User.Uuid)) | ||
|
||
return nil | ||
} | ||
|
||
func resourceBitbucketUserPermissionDelete(ctx context.Context, resourceData *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
client := meta.(*Clients).V2 | ||
|
||
_, err := client.Repositories.Repository.DeleteUserPermissions(&bitbucket.RepositoryUserPermissionsOptions{ | ||
Owner: resourceData.Get("workspace").(string), | ||
RepoSlug: resourceData.Get("repository").(string), | ||
User: resourceData.Get("user").(string), | ||
Permission: resourceData.Get("permission").(string), | ||
}) | ||
|
||
if err != nil { | ||
return diag.FromErr(fmt.Errorf("unable to delete user permission with error: %s", err)) | ||
} | ||
|
||
resourceData.SetId("") | ||
|
||
return nil | ||
} | ||
|
||
func resourceBitbucketUserPermissionImport(ctx context.Context, resourceData *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { | ||
ret := []*schema.ResourceData{resourceData} | ||
|
||
splitID := strings.Split(resourceData.Id(), "/") | ||
if len(splitID) < 3 { | ||
return ret, fmt.Errorf("invalid import ID. It must to be in this format \"<workspace-uuid>/<repo-slug>/<user-uuid>\"") | ||
} | ||
|
||
_ = resourceData.Set("workspace", splitID[0]) | ||
_ = resourceData.Set("repository", splitID[1]) | ||
_ = resourceData.Set("user", splitID[2]) | ||
|
||
_ = resourceBitbucketUserPermissionRead(ctx, resourceData, meta) | ||
|
||
return ret, nil | ||
} | ||
|
||
func generateUserPermissionResourceId(workspace string, repo string, user string) string { | ||
return fmt.Sprintf("%s-%s-%s", workspace, repo, 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,81 @@ | ||
package bitbucket | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestAccBitbucketUserPermissionResource_basic(t *testing.T) { | ||
workspaceSlug := os.Getenv("BITBUCKET_USERNAME") | ||
projectName := "tf-acc-test-" + acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum) | ||
projectKey := strings.ToUpper(acctest.RandStringFromCharSet(3, acctest.CharSetAlpha)) | ||
repoName := "tf-acc-test-" + acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum) | ||
user := os.Getenv("BITBUCKET_MEMBER_ACCOUNT_UUID") | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
ProviderFactories: testAccProviders, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: fmt.Sprintf(` | ||
data "bitbucket_workspace" "testacc" { | ||
id = "%s" | ||
} | ||
resource "bitbucket_project" "testacc" { | ||
workspace = data.bitbucket_workspace.testacc.id | ||
name = "%s" | ||
key = "%s" | ||
} | ||
resource "bitbucket_repository" "testacc" { | ||
workspace = data.bitbucket_workspace.testacc.id | ||
project_key = bitbucket_project.testacc.key | ||
name = "%s" | ||
} | ||
data "bitbucket_user" "testacc" { | ||
id = "%s" | ||
} | ||
resource "bitbucket_user_permission" "testacc" { | ||
workspace = data.bitbucket_workspace.testacc.uuid | ||
repository = bitbucket_repository.testacc.name | ||
user = data.bitbucket_user.testacc.id | ||
permission = "read" | ||
}`, workspaceSlug, projectName, projectKey, repoName, user), | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttr("bitbucket_user_permission.testacc", "repository", repoName), | ||
resource.TestCheckResourceAttr("bitbucket_user_permission.testacc", "user", user), | ||
resource.TestCheckResourceAttr("bitbucket_user_permission.testacc", "permission", "read"), | ||
|
||
resource.TestCheckResourceAttrSet("bitbucket_user_permission.testacc", "id"), | ||
resource.TestCheckResourceAttrSet("bitbucket_user_permission.testacc", "workspace"), | ||
), | ||
}, | ||
{ | ||
ResourceName: "bitbucket_user_permission.testacc", | ||
ImportState: true, | ||
ImportStateVerify: true, | ||
ImportStateIdFunc: func(state *terraform.State) (string, error) { | ||
resources := state.Modules[0].Resources | ||
workspaceResourceAttr := resources["data.bitbucket_workspace.testacc"].Primary.Attributes | ||
return fmt.Sprintf("%s/%s/%s", workspaceResourceAttr["uuid"], repoName, user), nil | ||
}, | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func TestGenerateUserPermissionResourceId(t *testing.T) { | ||
expected := "{my-workspace-uuid}-my-test-repo-{my-user-uuid}" | ||
result := generateUserPermissionResourceId("{my-workspace-uuid}", "my-test-repo", "{my-user-uuid}") | ||
assert.Equal(t, expected, result) | ||
} |
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,22 @@ | ||
# Data Source: bitbucket_user_permission | ||
Use this data source to get the user permission resource, you can then reference its attributes without having to hardcode them. | ||
|
||
## Example Usage | ||
```hcl | ||
data "bitbucket_user_permission" "example" { | ||
workspace = "{workspace-uuid}" | ||
repository = "example-repository" | ||
user = "{user-uuid}" | ||
} | ||
``` | ||
|
||
## Argument Reference | ||
The following arguments are supported: | ||
* `workspace` - (Required) The UUID (including the enclosing `{}`) of the workspace. | ||
* `repository` - (Required) The slug of the repository. | ||
* `user` - (Required) The UUID (including the enclosing `{}`) of the user. | ||
|
||
## Attribute Reference | ||
In addition to the arguments above, the following additional attributes are exported: | ||
* `id` - The ID of the user permission. | ||
* `permission` - The permission this user will have. Is one of 'read', 'write', or 'admin'. |
Oops, something went wrong.