Skip to content

Commit

Permalink
allow importing repo vars
Browse files Browse the repository at this point in the history
  • Loading branch information
DrFaust92 committed Nov 24, 2023
1 parent 4598e3b commit a5f37ac
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 4 deletions.
17 changes: 17 additions & 0 deletions bitbucket/resource_repository_variable.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,18 @@ func resourceRepositoryVariable() *schema.Resource {
UpdateWithoutTimeout: resourceRepositoryVariableUpdate,
ReadWithoutTimeout: resourceRepositoryVariableRead,
DeleteWithoutTimeout: resourceRepositoryVariableDelete,
Importer: &schema.ResourceImporter{
State: func(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
idParts := strings.Split(d.Id(), "/")
if len(idParts) != 4 || idParts[0] == "" || idParts[1] == "" || idParts[2] == "" || idParts[3] == "" {
return nil, fmt.Errorf("unexpected format of ID (%q), expected REPOSITORY/KEY/UUID", d.Id())
}
d.SetId(idParts[2])
d.Set("uuid", idParts[3])
d.Set("repository", fmt.Sprintf("%s/%s", idParts[0], idParts[1]))
return []*schema.ResourceData{d}, nil
},
},

Schema: map[string]*schema.Schema{
"uuid": {
Expand All @@ -42,6 +54,10 @@ func resourceRepositoryVariable() *schema.Resource {
Type: schema.TypeString,
Required: true,
},
"workspace": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
Expand Down Expand Up @@ -102,6 +118,7 @@ func resourceRepositoryVariableRead(ctx context.Context, d *schema.ResourceData,
d.Set("uuid", rvRes.Uuid)
d.Set("key", rvRes.Key)
d.Set("secured", rvRes.Secured)
d.Set("workspace", workspace)

if !rvRes.Secured {
d.Set("value", rvRes.Value)
Expand Down
18 changes: 18 additions & 0 deletions bitbucket/resource_repository_variable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,15 @@ func TestAccBitbucketRepositoryVariable_basic(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "key", "test"),
resource.TestCheckResourceAttr(resourceName, "value", "test-val"),
resource.TestCheckResourceAttr(resourceName, "secured", "false"),
resource.TestCheckResourceAttr(resourceName, "workspace", owner),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateIdFunc: testAccBitbucketRepoVariableImportStateIdFunc(resourceName),
ImportStateVerify: true,
},
{
Config: testAccBitbucketRepositoryVariableConfig(owner, rName, "test-val-2"),
Check: resource.ComposeTestCheckFunc(
Expand Down Expand Up @@ -99,3 +106,14 @@ resource "bitbucket_repository_variable" "test" {
}
`, team, rName, val)
}

func testAccBitbucketRepoVariableImportStateIdFunc(resourceName string) resource.ImportStateIdFunc {
return func(s *terraform.State) (string, error) {
rs, ok := s.RootModule().Resources[resourceName]
if !ok {
return "", fmt.Errorf("Not found: %s", resourceName)
}

return fmt.Sprintf("%s/%s/%s", rs.Primary.Attributes["repository"], rs.Primary.ID, rs.Primary.Attributes["uuid"]), nil
}
}
19 changes: 15 additions & 4 deletions docs/resources/repository_variable.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,27 @@ resource "bitbucket_repository" "monorepo" {
resource "bitbucket_repository_variable" "debug" {
key = "DEBUG"
value = "true"
repository = "${bitbucket_repository.monorepo.id}"
repository = bitbucket_repository.monorepo.id
secured = false
}
```

## Argument Reference

* `key` - (Required) The key of the key value pair
* `value` - (Required) The value of the key
* `repository` - (Required) The repository ID you want to put this variable onto.
* `value` - (Required) The value of the key. This will not be returned if `secured` is set to true from API and wont be drift detected by provider.
* `repository` - (Required) The repository ID you want to put this variable onto. (of form workspace-id/repository-id)
* `secured` - (Optional) If you want to make this viewable in the UI.

* `uuid` - (Computed) The UUID of the variable
## Attributes Reference

* `uuid` - (Computed) The UUID identifying the variable.
* `workspace` - (Computed) The workspace the variable is created in.

## Import

Repository Variables can be imported using their `workspace/repository/key/uuid` ID, e.g.

```sh
terraform import bitbucket_repository_variable.example workspace/repository/key/uuid
```

0 comments on commit a5f37ac

Please sign in to comment.