Skip to content

Commit e1c2d47

Browse files
committed
resource/gitlab_instance_variable: better error message for invalid masked variable values. Closes #371
1 parent f7cb0ea commit e1c2d47

File tree

2 files changed

+68
-4
lines changed

2 files changed

+68
-4
lines changed

internal/provider/resource_gitlab_instance_variable.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ func resourceGitlabInstanceVariableCreate(ctx context.Context, d *schema.Resourc
8181

8282
_, _, err := client.InstanceVariables.CreateVariable(&options, gitlab.WithContext(ctx))
8383
if err != nil {
84-
return diag.FromErr(err)
84+
return augmentVariableClientError(d, err)
8585
}
8686

8787
d.SetId(key)
@@ -103,7 +103,7 @@ func resourceGitlabInstanceVariableRead(ctx context.Context, d *schema.ResourceD
103103
d.SetId("")
104104
return nil
105105
}
106-
return diag.FromErr(err)
106+
return augmentVariableClientError(d, err)
107107
}
108108

109109
d.Set("key", v.Key)
@@ -133,7 +133,7 @@ func resourceGitlabInstanceVariableUpdate(ctx context.Context, d *schema.Resourc
133133

134134
_, _, err := client.InstanceVariables.UpdateVariable(key, options, gitlab.WithContext(ctx))
135135
if err != nil {
136-
return diag.FromErr(err)
136+
return augmentVariableClientError(d, err)
137137
}
138138
return resourceGitlabInstanceVariableRead(ctx, d, meta)
139139
}
@@ -145,7 +145,7 @@ func resourceGitlabInstanceVariableDelete(ctx context.Context, d *schema.Resourc
145145

146146
_, err := client.InstanceVariables.RemoveVariable(key, gitlab.WithContext(ctx))
147147
if err != nil {
148-
return diag.FromErr(err)
148+
return augmentVariableClientError(d, err)
149149
}
150150

151151
return nil

internal/provider/resource_gitlab_instance_variable_test.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package provider
22

33
import (
44
"fmt"
5+
"regexp"
56
"testing"
67

78
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
@@ -54,6 +55,46 @@ func TestAccGitlabInstanceVariable_basic(t *testing.T) {
5455
}),
5556
),
5657
},
58+
// Update the instance variable to enable "masked" for a value that does not meet masking requirements, and expect an error with no state change.
59+
// ref: https://docs.gitlab.com/ce/ci/variables/README.html#masked-variable-requirements
60+
{
61+
Config: testAccGitlabInstanceVariableUpdateConfigMaskedBad(rString),
62+
Check: resource.ComposeTestCheckFunc(
63+
testAccCheckGitlabInstanceVariableExists("gitlab_instance_variable.foo", &instanceVariable),
64+
testAccCheckGitlabInstanceVariableAttributes(&instanceVariable, &testAccGitlabInstanceVariableExpectedAttributes{
65+
Key: fmt.Sprintf("key_%s", rString),
66+
Value: fmt.Sprintf("value-%s", rString),
67+
}),
68+
),
69+
ExpectError: regexp.MustCompile(regexp.QuoteMeta(
70+
"Invalid value for a masked variable. Check the masked variable requirements: https://docs.gitlab.com/ee/ci/variables/#masked-variable-requirements",
71+
)),
72+
},
73+
// Update the instance variable to to enable "masked" and meet masking requirements
74+
// ref: https://docs.gitlab.com/ce/ci/variables/README.html#masked-variable-requirements
75+
{
76+
Config: testAccGitlabInstanceVariableUpdateConfigMaskedGood(rString),
77+
Check: resource.ComposeTestCheckFunc(
78+
testAccCheckGitlabInstanceVariableExists("gitlab_instance_variable.foo", &instanceVariable),
79+
testAccCheckGitlabInstanceVariableAttributes(&instanceVariable, &testAccGitlabInstanceVariableExpectedAttributes{
80+
Key: fmt.Sprintf("key_%s", rString),
81+
Value: fmt.Sprintf("value-%s", rString),
82+
Masked: true,
83+
}),
84+
),
85+
},
86+
// Update the instance variable to toggle the options back
87+
{
88+
Config: testAccGitlabInstanceVariableConfig(rString),
89+
Check: resource.ComposeTestCheckFunc(
90+
testAccCheckGitlabInstanceVariableExists("gitlab_instance_variable.foo", &instanceVariable),
91+
testAccCheckGitlabInstanceVariableAttributes(&instanceVariable, &testAccGitlabInstanceVariableExpectedAttributes{
92+
Key: fmt.Sprintf("key_%s", rString),
93+
Value: fmt.Sprintf("value-%s", rString),
94+
Protected: false,
95+
}),
96+
),
97+
},
5798
},
5899
})
59100
}
@@ -129,3 +170,26 @@ resource "gitlab_instance_variable" "foo" {
129170
}
130171
`, rString, rString)
131172
}
173+
174+
func testAccGitlabInstanceVariableUpdateConfigMaskedBad(rString string) string {
175+
return fmt.Sprintf(`
176+
resource "gitlab_instance_variable" "foo" {
177+
key = "key_%s"
178+
value = <<EOF
179+
value-%s"
180+
i am multiline
181+
EOF
182+
masked = true
183+
}
184+
`, rString, rString)
185+
}
186+
187+
func testAccGitlabInstanceVariableUpdateConfigMaskedGood(rString string) string {
188+
return fmt.Sprintf(`
189+
resource "gitlab_instance_variable" "foo" {
190+
key = "key_%s"
191+
value = "value-%s"
192+
masked = true
193+
}
194+
`, rString, rString)
195+
}

0 commit comments

Comments
 (0)