Skip to content

Commit e3f028b

Browse files
author
Thibault Drevon
committed
Added validation on Variable Key.
1 parent 65e1bb4 commit e3f028b

File tree

4 files changed

+29
-10
lines changed

4 files changed

+29
-10
lines changed

gitlab/provider.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func Provider() terraform.ResourceProvider {
5454
"gitlab_deploy_key": resourceGitlabDeployKey(),
5555
"gitlab_user": resourceGitlabUser(),
5656
"gitlab_project_membership": resourceGitlabProjectMembership(),
57-
"gitlab_project_variable": resourceGitlabProjectVariable(),
57+
"gitlab_project_variable": resourceGitlabProjectVariable(),
5858
},
5959

6060
ConfigureFunc: providerConfigure,

gitlab/resource_gitlab_project_variable.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ func resourceGitlabProjectVariable() *schema.Resource {
2020
Required: true,
2121
},
2222
"key": {
23-
Type: schema.TypeString,
24-
Required: true,
23+
Type: schema.TypeString,
24+
Required: true,
25+
ValidateFunc: StringIsGitlabVariableName(),
2526
},
2627
"value": {
2728
Type: schema.TypeString,

gitlab/resource_gitlab_project_variable_test.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,15 @@ func TestAccGitlabProjectVariable_basic(t *testing.T) {
2424
Config: testAccGitlabProjectVariableConfig(rString),
2525
Check: resource.ComposeTestCheckFunc(
2626
testAccCheckGitlabProjectVariableExists("gitlab_project_variable.foo", &projectVariable),
27-
testAccCheckGitlabProjectVariableAttributes(&projectVariable, &testAccGitlabProjectVariableExpectedAttributes{
28-
Key: fmt.Sprintf("key-%s", rString),
29-
Value: fmt.Sprintf("value-%s", rString),
30-
}),
27+
resource.TestCheckResourceAttr("gitlab_project_variable.foo", "Key", fmt.Sprintf("key-%s", rString)),
28+
resource.TestCheckResourceAttr("gitlab_project_variable.foo", "Value", fmt.Sprintf("value-%s", rString)),
3129
),
3230
},
3331
// Update the project variable to toggle all the values to their inverse
3432
{
3533
Config: testAccGitlabProjectVariableUpdateConfig(rString),
3634
Check: resource.ComposeTestCheckFunc(
37-
testAccCheckGitlabProjectVariableExists("gitlab_project_hook.foo", &projectVariable),
35+
testAccCheckGitlabProjectVariableExists("gitlab_project_variable.foo", &projectVariable),
3836
testAccCheckGitlabProjectVariableAttributes(&projectVariable, &testAccGitlabProjectVariableExpectedAttributes{
3937
Key: fmt.Sprintf("key-%s", rString),
4038
Value: fmt.Sprintf("value-inverse-%s", rString),
@@ -46,7 +44,7 @@ func TestAccGitlabProjectVariable_basic(t *testing.T) {
4644
{
4745
Config: testAccGitlabProjectVariableConfig(rString),
4846
Check: resource.ComposeTestCheckFunc(
49-
testAccCheckGitlabProjectVariableExists("gitlab_project_hook.foo", &projectVariable),
47+
testAccCheckGitlabProjectVariableExists("gitlab_project_variable.foo", &projectVariable),
5048
testAccCheckGitlabProjectVariableAttributes(&projectVariable, &testAccGitlabProjectVariableExpectedAttributes{
5149
Key: fmt.Sprintf("key-%s", rString),
5250
Value: fmt.Sprintf("value-%s", rString),

gitlab/util.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ import (
44
"fmt"
55

66
"github.com/hashicorp/terraform/helper/schema"
7-
gitlab "github.com/xanzy/go-gitlab"
7+
"github.com/xanzy/go-gitlab"
8+
"regexp"
89
)
910

1011
// copied from ../github/util.go
@@ -39,3 +40,22 @@ func stringToVisibilityLevel(s string) *gitlab.VisibilityValue {
3940
}
4041
return &value
4142
}
43+
44+
func StringIsGitlabVariableName() schema.SchemaValidateFunc {
45+
return func(v interface{}, k string) (s []string, es []error) {
46+
value, ok := v.(string)
47+
if !ok {
48+
es = append(es, fmt.Errorf("expected type of %s to be string", k))
49+
return
50+
}
51+
if len(value) < 1 || len(value) > 255 {
52+
es = append(es, fmt.Errorf("expected length of %s to be in the range (%d - %d), got %s", k, 1, 255, v))
53+
}
54+
55+
match, _ := regexp.MatchString("[a-zA-Z0-9_]+", value)
56+
if !match {
57+
es = append(es, fmt.Errorf("%s is an invalid value for argument %s. Only A-Z, a-z, 0-9, and _ are allowed", value, k))
58+
}
59+
return
60+
}
61+
}

0 commit comments

Comments
 (0)