Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion gitlab/resource_gitlab_group_membership.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func resourceGitlabGroupMembership() *schema.Resource {
},
"expires_at": {
Type: schema.TypeString, // Format YYYY-MM-DD
ValidateFunc: validateDateFunc(),
ValidateFunc: validateDateFunc,
Optional: true,
},
},
Expand Down
29 changes: 19 additions & 10 deletions gitlab/resource_gitlab_group_variable.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,19 @@ func resourceGitlabGroupVariable() *schema.Resource {
Type: schema.TypeString,
ForceNew: true,
Required: true,
ValidateFunc: StringIsGitlabVariableName(),
ValidateFunc: StringIsGitlabVariableName,
},
"value": {
Type: schema.TypeString,
Required: true,
Sensitive: true,
},
"variable_type": {
Type: schema.TypeString,
Optional: true,
Default: "env_var",
ValidateFunc: StringIsGitlabVariableType,
},
"protected": {
Type: schema.TypeBool,
Optional: true,
Expand All @@ -49,13 +55,14 @@ func resourceGitlabGroupVariableCreate(d *schema.ResourceData, meta interface{})
group := d.Get("group").(string)
key := d.Get("key").(string)
value := d.Get("value").(string)
variableType := stringToVariableType(d.Get("variable_type").(string))
protected := d.Get("protected").(bool)

options := gitlab.CreateVariableOptions{
Key: &key,
Value: &value,
Protected: &protected,
EnvironmentScope: nil,
options := gitlab.CreateGroupVariableOptions{
Key: &key,
Value: &value,
VariableType: variableType,
Protected: &protected,
}
log.Printf("[DEBUG] create gitlab group variable %s/%s", group, key)

Expand Down Expand Up @@ -86,6 +93,7 @@ func resourceGitlabGroupVariableRead(d *schema.ResourceData, meta interface{}) e

d.Set("key", v.Key)
d.Set("value", v.Value)
d.Set("variable_type", v.VariableType)
d.Set("group", group)
d.Set("protected", v.Protected)
return nil
Expand All @@ -97,12 +105,13 @@ func resourceGitlabGroupVariableUpdate(d *schema.ResourceData, meta interface{})
group := d.Get("group").(string)
key := d.Get("key").(string)
value := d.Get("value").(string)
variableType := stringToVariableType(d.Get("variable_type").(string))
protected := d.Get("protected").(bool)

options := &gitlab.UpdateVariableOptions{
Value: &value,
Protected: &protected,
EnvironmentScope: nil,
options := &gitlab.UpdateGroupVariableOptions{
Value: &value,
Protected: &protected,
VariableType: variableType,
}
log.Printf("[DEBUG] update gitlab group variable %s/%s", group, key)

Expand Down
1 change: 1 addition & 0 deletions gitlab/resource_gitlab_group_variable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ resource "gitlab_group_variable" "foo" {
group = "${gitlab_group.foo.id}"
key = "key_%s"
value = "value-%s"
variable_type = "file"
}
`, rString, rString, rString, rString)
}
Expand Down
42 changes: 37 additions & 5 deletions gitlab/resource_gitlab_project_variable.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,34 @@ func resourceGitlabProjectVariable() *schema.Resource {
Type: schema.TypeString,
ForceNew: true,
Required: true,
ValidateFunc: StringIsGitlabVariableName(),
ValidateFunc: StringIsGitlabVariableName,
},
"value": {
Type: schema.TypeString,
Required: true,
Sensitive: true,
},
"variable_type": {
Type: schema.TypeString,
Optional: true,
Default: "env_var",
ValidateFunc: StringIsGitlabVariableType,
},
"protected": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},
"masked": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},
"environment_scope": {
Type: schema.TypeString,
Optional: true,
Default: false,
},
},
}
}
Expand All @@ -49,13 +65,18 @@ func resourceGitlabProjectVariableCreate(d *schema.ResourceData, meta interface{
project := d.Get("project").(string)
key := d.Get("key").(string)
value := d.Get("value").(string)
variableType := stringToVariableType(d.Get("variable_type").(string))
protected := d.Get("protected").(bool)
masked := d.Get("masked").(bool)
environmentScope := d.Get("environment_scope").(string)

options := gitlab.CreateVariableOptions{
options := gitlab.CreateProjectVariableOptions{
Key: &key,
Value: &value,
VariableType: variableType,
Protected: &protected,
EnvironmentScope: nil,
Masked: &masked,
EnvironmentScope: &environmentScope,
}
log.Printf("[DEBUG] create gitlab project variable %s/%s", project, key)

Expand Down Expand Up @@ -86,8 +107,14 @@ func resourceGitlabProjectVariableRead(d *schema.ResourceData, meta interface{})

d.Set("key", v.Key)
d.Set("value", v.Value)
d.Set("variable_type", v.VariableType)
d.Set("project", project)
d.Set("protected", v.Protected)
d.Set("masked", v.Masked)
//For now I'm ignoring environment_scope when reading back data. (this can cause configuration drift so it is bad).
//However I'm unable to stop terraform from gratuitously updating this to values that are unacceptable by Gitlab)
//I don't have an enterprise license to properly test this either.
//d.Set("environment_scope", v.EnvironmentScope)
return nil
}

Expand All @@ -97,12 +124,17 @@ func resourceGitlabProjectVariableUpdate(d *schema.ResourceData, meta interface{
project := d.Get("project").(string)
key := d.Get("key").(string)
value := d.Get("value").(string)
variableType := stringToVariableType(d.Get("variable_type").(string))
protected := d.Get("protected").(bool)
masked := d.Get("masked").(bool)
environmentScope := d.Get("environment_scope").(string)

options := &gitlab.UpdateVariableOptions{
options := &gitlab.UpdateProjectVariableOptions{
Value: &value,
VariableType: variableType,
Protected: &protected,
EnvironmentScope: nil,
Masked: &masked,
EnvironmentScope: &environmentScope,
}
log.Printf("[DEBUG] update gitlab project variable %s/%s", project, key)

Expand Down
1 change: 1 addition & 0 deletions gitlab/resource_gitlab_project_variable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ resource "gitlab_project_variable" "foo" {
project = "${gitlab_project.foo.id}"
key = "key_%s"
value = "value-%s"
variable_type = "env_var"
}
`, rString, rString, rString)
}
Expand Down
2 changes: 1 addition & 1 deletion gitlab/resource_gitlab_service_jira.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func resourceGitlabServiceJira() *schema.Resource {
"url": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validateURLFunc(),
ValidateFunc: validateURLFunc,
},
"project_key": {
Type: schema.TypeString,
Expand Down
84 changes: 52 additions & 32 deletions gitlab/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,30 +51,26 @@ func validateValueFunc(values []string) schema.SchemaValidateFunc {
}
}

func validateDateFunc() schema.SchemaValidateFunc {
return func(v interface{}, k string) (we []string, errors []error) {
value := v.(string)
//add zero hours and let time figure out correctness
_, e := time.Parse(time.RFC3339, value+"T00:00:00Z")
if e != nil {
errors = append(errors, fmt.Errorf("%s is not valid for format YYYY-MM-DD", value))
}
return
var validateDateFunc = func(v interface{}, k string) (we []string, errors []error) {
value := v.(string)
//add zero hours and let time figure out correctness
_, e := time.Parse(time.RFC3339, value+"T00:00:00Z")
if e != nil {
errors = append(errors, fmt.Errorf("%s is not valid for format YYYY-MM-DD", value))
}
return
}

func validateURLFunc() schema.SchemaValidateFunc {
return func(v interface{}, k string) (s []string, errors []error) {
value := v.(string)
url, err := url.Parse(value)

if err != nil || url.Host == "" || url.Scheme == "" {
errors = append(errors, fmt.Errorf("%s is not a valid URL", value))
return
}
var validateURLFunc = func(v interface{}, k string) (s []string, errors []error) {
value := v.(string)
url, err := url.Parse(value)

if err != nil || url.Host == "" || url.Scheme == "" {
errors = append(errors, fmt.Errorf("%s is not a valid URL", value))
return
}

return
}

func stringToVisibilityLevel(s string) *gitlab.VisibilityValue {
Expand All @@ -91,6 +87,19 @@ func stringToVisibilityLevel(s string) *gitlab.VisibilityValue {
return &value
}

func stringToVariableType(s string) *gitlab.VariableTypeValue {
lookup := map[string]gitlab.VariableTypeValue{
"env_var": gitlab.EnvVariableType,
"file": gitlab.FileVariableType,
}

value, ok := lookup[s]
if !ok {
return nil
}
return &value
}

func stringToMergeMethod(s string) *gitlab.MergeMethodValue {
lookup := map[string]gitlab.MergeMethodValue{
"merge": gitlab.NoFastForwardMerge,
Expand All @@ -105,23 +114,34 @@ func stringToMergeMethod(s string) *gitlab.MergeMethodValue {
return &value
}

func StringIsGitlabVariableName() schema.SchemaValidateFunc {
return func(v interface{}, k string) (s []string, es []error) {
value, ok := v.(string)
if !ok {
es = append(es, fmt.Errorf("expected type of %s to be string", k))
return
}
if len(value) < 1 || len(value) > 255 {
es = append(es, fmt.Errorf("expected length of %s to be in the range (%d - %d), got %s", k, 1, 255, v))
}
var StringIsGitlabVariableName = func(v interface{}, k string) (s []string, es []error) {
value, ok := v.(string)
if !ok {
es = append(es, fmt.Errorf("expected type of %s to be string", k))
return
}
if len(value) < 1 || len(value) > 255 {
es = append(es, fmt.Errorf("expected length of %s to be in the range (%d - %d), got %s", k, 1, 255, v))
}

match, _ := regexp.MatchString("[a-zA-Z0-9_]+", value)
if !match {
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))
}
match, _ := regexp.MatchString("[a-zA-Z0-9_]+", value)
if !match {
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))
}
return
}

var StringIsGitlabVariableType = func(v interface{}, k string) (s []string, es []error) {
value, ok := v.(string)
if !ok {
es = append(es, fmt.Errorf("expected type of %s to be string", k))
return
}
variableType := stringToVariableType(value)
if variableType == nil {
es = append(es, fmt.Errorf("expected variable_type to be \"env_var\" or \"file\""))
}
return
}

// return the pieces of id `a:b` as a, b
Expand Down
4 changes: 1 addition & 3 deletions gitlab/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,8 @@ func TestValidateURLFunc(t *testing.T) {
},
}

validationFunc := validateURLFunc()

for _, tc := range cases {
_, errors := validationFunc(tc.Value, "test_arg")
_, errors := validateURLFunc(tc.Value, "test_arg")

if len(errors) != tc.ErrCount {
t.Fatalf("Expected 1 validation error")
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ require (
github.com/spf13/afero v1.2.2 // indirect
github.com/ulikunitz/xz v0.5.6 // indirect
github.com/vmihailenco/msgpack v4.0.4+incompatible // indirect
github.com/xanzy/go-gitlab v0.18.0
github.com/xanzy/go-gitlab v0.20.0
golang.org/x/crypto v0.0.0-20190513172903-22d7a77e9e5f // indirect
golang.org/x/net v0.0.0-20190522155817-f3200d17e092 // indirect
golang.org/x/oauth2 v0.0.0-20190517181255-950ef44c6e07 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,8 @@ github.com/xanzy/go-gitlab v0.17.0 h1:+ajmfVENiehiK8FgQXb0v/XEsYPAdf9vVE53iQ2fiG
github.com/xanzy/go-gitlab v0.17.0/go.mod h1:LSfUQ9OPDnwRqulJk2HcWaAiFfCzaknyeGvjQI67MbE=
github.com/xanzy/go-gitlab v0.18.0 h1:LybNSWSIw8BK+GnxuETAhUXEzzh5rHsHjopqVkGJXRE=
github.com/xanzy/go-gitlab v0.18.0/go.mod h1:LSfUQ9OPDnwRqulJk2HcWaAiFfCzaknyeGvjQI67MbE=
github.com/xanzy/go-gitlab v0.20.0 h1:UE8uvZ8DAOeYTTVqwqUmwnUqeUVgXYk0Mbam6FvutrQ=
github.com/xanzy/go-gitlab v0.20.0/go.mod h1:LSfUQ9OPDnwRqulJk2HcWaAiFfCzaknyeGvjQI67MbE=
github.com/xanzy/ssh-agent v0.2.1 h1:TCbipTQL2JiiCprBWx9frJ2eJlCYT00NmctrHxVAr70=
github.com/xanzy/ssh-agent v0.2.1/go.mod h1:mLlQY/MoOhWBj+gOGMQkOeiEvkx+8pJSI+0Bx9h2kr4=
github.com/xiang90/probing v0.0.0-20160813154853-07dd2e8dfe18/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU=
Expand Down
1 change: 0 additions & 1 deletion vendor/github.com/xanzy/go-gitlab/.travis.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions vendor/github.com/xanzy/go-gitlab/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading