Skip to content

Commit f4564bc

Browse files
Fix Id management by using a two part id
1 parent ee2810b commit f4564bc

File tree

2 files changed

+35
-4
lines changed

2 files changed

+35
-4
lines changed

gitlab/resource_gitlab_project_variable.go

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ func resourceGitlabProjectVariable() *schema.Resource {
1313
Read: resourceGitlabProjectVariableRead,
1414
Update: resourceGitlabProjectVariableUpdate,
1515
Delete: resourceGitlabProjectVariableDelete,
16+
Importer: &schema.ResourceImporter{
17+
State: schema.ImportStatePassthrough,
18+
},
1619

1720
Schema: map[string]*schema.Schema{
1821
"project": {
@@ -49,27 +52,37 @@ func resourceGitlabProjectVariableCreate(d *schema.ResourceData, meta interface{
4952
}
5053
log.Printf("[DEBUG] create gitlab project variable %s/%s", project, key)
5154

52-
_, _, err := client.BuildVariables.CreateBuildVariable(project, options)
55+
v, _, err := client.BuildVariables.CreateBuildVariable(project, options)
5356
if err != nil {
5457
return err
5558
}
5659

60+
d.SetId(buildTwoPartID(&project, &v.Key))
61+
5762
return resourceGitlabProjectVariableRead(d, meta)
5863
}
5964

6065
func resourceGitlabProjectVariableRead(d *schema.ResourceData, meta interface{}) error {
6166
client := meta.(*gitlab.Client)
62-
project := d.Get("project").(string)
63-
key := d.Get("key").(string)
67+
68+
project, key, err := parseTwoPartID(d.Id())
69+
if err != nil {
70+
return err
71+
}
72+
6473
log.Printf("[DEBUG] read gitlab project variable %s/%s", project, key)
6574

6675
v, _, err := client.BuildVariables.GetBuildVariable(project, key)
6776
if err != nil {
6877
return err
6978
}
7079

80+
d.Set("key", v.Key)
7181
d.Set("value", v.Value)
82+
d.Set("project", project)
7283
d.Set("protected", v.Protected)
84+
d.SetId(buildTwoPartID(&project, &v.Key))
85+
7386
return nil
7487
}
7588

@@ -84,11 +97,13 @@ func resourceGitlabProjectVariableUpdate(d *schema.ResourceData, meta interface{
8497
}
8598
log.Printf("[DEBUG] update gitlab project variable %s/%s", project, key)
8699

87-
_, _, err := client.BuildVariables.UpdateBuildVariable(project, key, options)
100+
v, _, err := client.BuildVariables.UpdateBuildVariable(project, key, options)
88101
if err != nil {
89102
return err
90103
}
91104

105+
d.SetId(buildTwoPartID(&project, &v.Key))
106+
92107
return resourceGitlabProjectVariableRead(d, meta)
93108
}
94109

gitlab/util.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"github.com/hashicorp/terraform/helper/schema"
77
"github.com/xanzy/go-gitlab"
88
"regexp"
9+
"strings"
910
)
1011

1112
// copied from ../github/util.go
@@ -59,3 +60,18 @@ func StringIsGitlabVariableName() schema.SchemaValidateFunc {
5960
return
6061
}
6162
}
63+
64+
// return the pieces of id `a:b` as a, b
65+
func parseTwoPartID(id string) (string, string, error) {
66+
parts := strings.SplitN(id, ":", 2)
67+
if len(parts) != 2 {
68+
return "", "", fmt.Errorf("Unexpected ID format (%q). Expected project:key", id)
69+
}
70+
71+
return parts[0], parts[1], nil
72+
}
73+
74+
// format the strings into an id `a:b`
75+
func buildTwoPartID(a, b *string) string {
76+
return fmt.Sprintf("%s:%s", *a, *b)
77+
}

0 commit comments

Comments
 (0)