Skip to content

Commit e469656

Browse files
authored
Merge pull request #189 from DrFaust92/repo-var-import
allow importing repo vars
2 parents 4598e3b + a5f37ac commit e469656

File tree

3 files changed

+50
-4
lines changed

3 files changed

+50
-4
lines changed

Diff for: bitbucket/resource_repository_variable.go

+17
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,18 @@ func resourceRepositoryVariable() *schema.Resource {
1818
UpdateWithoutTimeout: resourceRepositoryVariableUpdate,
1919
ReadWithoutTimeout: resourceRepositoryVariableRead,
2020
DeleteWithoutTimeout: resourceRepositoryVariableDelete,
21+
Importer: &schema.ResourceImporter{
22+
State: func(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
23+
idParts := strings.Split(d.Id(), "/")
24+
if len(idParts) != 4 || idParts[0] == "" || idParts[1] == "" || idParts[2] == "" || idParts[3] == "" {
25+
return nil, fmt.Errorf("unexpected format of ID (%q), expected REPOSITORY/KEY/UUID", d.Id())
26+
}
27+
d.SetId(idParts[2])
28+
d.Set("uuid", idParts[3])
29+
d.Set("repository", fmt.Sprintf("%s/%s", idParts[0], idParts[1]))
30+
return []*schema.ResourceData{d}, nil
31+
},
32+
},
2133

2234
Schema: map[string]*schema.Schema{
2335
"uuid": {
@@ -42,6 +54,10 @@ func resourceRepositoryVariable() *schema.Resource {
4254
Type: schema.TypeString,
4355
Required: true,
4456
},
57+
"workspace": {
58+
Type: schema.TypeString,
59+
Computed: true,
60+
},
4561
},
4662
}
4763
}
@@ -102,6 +118,7 @@ func resourceRepositoryVariableRead(ctx context.Context, d *schema.ResourceData,
102118
d.Set("uuid", rvRes.Uuid)
103119
d.Set("key", rvRes.Key)
104120
d.Set("secured", rvRes.Secured)
121+
d.Set("workspace", workspace)
105122

106123
if !rvRes.Secured {
107124
d.Set("value", rvRes.Value)

Diff for: bitbucket/resource_repository_variable_test.go

+18
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,15 @@ func TestAccBitbucketRepositoryVariable_basic(t *testing.T) {
3030
resource.TestCheckResourceAttr(resourceName, "key", "test"),
3131
resource.TestCheckResourceAttr(resourceName, "value", "test-val"),
3232
resource.TestCheckResourceAttr(resourceName, "secured", "false"),
33+
resource.TestCheckResourceAttr(resourceName, "workspace", owner),
3334
),
3435
},
36+
{
37+
ResourceName: resourceName,
38+
ImportState: true,
39+
ImportStateIdFunc: testAccBitbucketRepoVariableImportStateIdFunc(resourceName),
40+
ImportStateVerify: true,
41+
},
3542
{
3643
Config: testAccBitbucketRepositoryVariableConfig(owner, rName, "test-val-2"),
3744
Check: resource.ComposeTestCheckFunc(
@@ -99,3 +106,14 @@ resource "bitbucket_repository_variable" "test" {
99106
}
100107
`, team, rName, val)
101108
}
109+
110+
func testAccBitbucketRepoVariableImportStateIdFunc(resourceName string) resource.ImportStateIdFunc {
111+
return func(s *terraform.State) (string, error) {
112+
rs, ok := s.RootModule().Resources[resourceName]
113+
if !ok {
114+
return "", fmt.Errorf("Not found: %s", resourceName)
115+
}
116+
117+
return fmt.Sprintf("%s/%s/%s", rs.Primary.Attributes["repository"], rs.Primary.ID, rs.Primary.Attributes["uuid"]), nil
118+
}
119+
}

Diff for: docs/resources/repository_variable.md

+15-4
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,27 @@ resource "bitbucket_repository" "monorepo" {
2525
resource "bitbucket_repository_variable" "debug" {
2626
key = "DEBUG"
2727
value = "true"
28-
repository = "${bitbucket_repository.monorepo.id}"
28+
repository = bitbucket_repository.monorepo.id
2929
secured = false
3030
}
3131
```
3232

3333
## Argument Reference
3434

3535
* `key` - (Required) The key of the key value pair
36-
* `value` - (Required) The value of the key
37-
* `repository` - (Required) The repository ID you want to put this variable onto.
36+
* `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.
37+
* `repository` - (Required) The repository ID you want to put this variable onto. (of form workspace-id/repository-id)
3838
* `secured` - (Optional) If you want to make this viewable in the UI.
3939

40-
* `uuid` - (Computed) The UUID of the variable
40+
## Attributes Reference
41+
42+
* `uuid` - (Computed) The UUID identifying the variable.
43+
* `workspace` - (Computed) The workspace the variable is created in.
44+
45+
## Import
46+
47+
Repository Variables can be imported using their `workspace/repository/key/uuid` ID, e.g.
48+
49+
```sh
50+
terraform import bitbucket_repository_variable.example workspace/repository/key/uuid
51+
```

0 commit comments

Comments
 (0)