Skip to content

Commit 385c892

Browse files
committed
Use SDK validation function in favor of home brew utils
1 parent 0cbcd3d commit 385c892

10 files changed

+20
-65
lines changed

gitlab/data_source_gitlab_group_membership.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99

1010
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1111
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
12+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
1213
"github.com/xanzy/go-gitlab"
1314
)
1415

@@ -42,7 +43,7 @@ func dataSourceGitlabGroupMembership() *schema.Resource {
4243
Type: schema.TypeString,
4344
Computed: true,
4445
Optional: true,
45-
ValidateDiagFunc: validateValueFunc(validGroupAccessLevelNames),
46+
ValidateDiagFunc: validation.ToDiagFunc(validation.StringInSlice(validGroupAccessLevelNames, false)),
4647
},
4748
"members": {
4849
Description: "The list of group members.",

gitlab/resource_gitlab_branch_protection.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1010
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
11+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
1112
gitlab "github.com/xanzy/go-gitlab"
1213
)
1314

@@ -66,14 +67,14 @@ func resourceGitlabBranchProtection() *schema.Resource {
6667
"merge_access_level": {
6768
Description: fmt.Sprintf("Access levels allowed to merge. Valid values are: %s.", renderValueListForDocs(validProtectedBranchTagAccessLevelNames)),
6869
Type: schema.TypeString,
69-
ValidateDiagFunc: validateValueFunc(validProtectedBranchTagAccessLevelNames),
70+
ValidateDiagFunc: validation.ToDiagFunc(validation.StringInSlice(validProtectedBranchTagAccessLevelNames, false)),
7071
Required: true,
7172
ForceNew: true,
7273
},
7374
"push_access_level": {
7475
Description: fmt.Sprintf("Access levels allowed to push. Valid values are: %s.", renderValueListForDocs(validProtectedBranchTagAccessLevelNames)),
7576
Type: schema.TypeString,
76-
ValidateDiagFunc: validateValueFunc(validProtectedBranchTagAccessLevelNames),
77+
ValidateDiagFunc: validation.ToDiagFunc(validation.StringInSlice(validProtectedBranchTagAccessLevelNames, false)),
7778
Required: true,
7879
ForceNew: true,
7980
},

gitlab/resource_gitlab_group_ldap_link.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1010
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
11+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
1112
gitlab "github.com/xanzy/go-gitlab"
1213
)
1314

@@ -36,7 +37,7 @@ func resourceGitlabGroupLdapLink() *schema.Resource {
3637
"access_level": {
3738
Description: fmt.Sprintf("Minimum access level for members of the LDAP group. Valid values are: %s", renderValueListForDocs(validGroupAccessLevelNames)),
3839
Type: schema.TypeString,
39-
ValidateDiagFunc: validateValueFunc(validGroupAccessLevelNames),
40+
ValidateDiagFunc: validation.ToDiagFunc(validation.StringInSlice(validGroupAccessLevelNames, false)),
4041
Optional: true,
4142
ForceNew: true,
4243
Deprecated: "Use `group_access` instead of the `access_level` attribute.",
@@ -45,7 +46,7 @@ func resourceGitlabGroupLdapLink() *schema.Resource {
4546
"group_access": {
4647
Description: fmt.Sprintf("Minimum access level for members of the LDAP group. Valid values are: %s", renderValueListForDocs(validGroupAccessLevelNames)),
4748
Type: schema.TypeString,
48-
ValidateDiagFunc: validateValueFunc(validGroupAccessLevelNames),
49+
ValidateDiagFunc: validation.ToDiagFunc(validation.StringInSlice(validGroupAccessLevelNames, false)),
4950
Optional: true,
5051
ForceNew: true,
5152
ExactlyOneOf: []string{"access_level", "group_access"},

gitlab/resource_gitlab_group_membership.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99

1010
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1111
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
12+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
1213
"github.com/xanzy/go-gitlab"
1314
)
1415

@@ -40,7 +41,7 @@ func resourceGitlabGroupMembership() *schema.Resource {
4041
"access_level": {
4142
Description: fmt.Sprintf("Access level for the member. Valid values are: %s.", renderValueListForDocs(validGroupAccessLevelNames)),
4243
Type: schema.TypeString,
43-
ValidateDiagFunc: validateValueFunc(validGroupAccessLevelNames),
44+
ValidateDiagFunc: validation.ToDiagFunc(validation.StringInSlice(validGroupAccessLevelNames, false)),
4445
Required: true,
4546
},
4647
"expires_at": {

gitlab/resource_gitlab_group_share_group.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99

1010
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1111
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
12+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
1213
gitlab "github.com/xanzy/go-gitlab"
1314
)
1415

@@ -40,7 +41,7 @@ func resourceGitlabGroupShareGroup() *schema.Resource {
4041
"group_access": {
4142
Description: fmt.Sprintf("The access level to grant the group. Valid values are: %s", renderValueListForDocs(validGroupAccessLevelNames)),
4243
Type: schema.TypeString,
43-
ValidateDiagFunc: validateValueFunc(validGroupAccessLevelNames),
44+
ValidateDiagFunc: validation.ToDiagFunc(validation.StringInSlice(validGroupAccessLevelNames, false)),
4445
ForceNew: true,
4546
Required: true,
4647
},

gitlab/resource_gitlab_project_membership.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010

1111
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1212
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
13+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
1314
"github.com/xanzy/go-gitlab"
1415
)
1516

@@ -41,7 +42,7 @@ func resourceGitlabProjectMembership() *schema.Resource {
4142
"access_level": {
4243
Description: fmt.Sprintf("The access level for the member. Valid values are: %s", renderValueListForDocs(validProjectAccessLevelNames)),
4344
Type: schema.TypeString,
44-
ValidateDiagFunc: validateValueFunc(validProjectAccessLevelNames),
45+
ValidateDiagFunc: validation.ToDiagFunc(validation.StringInSlice(validProjectAccessLevelNames, false)),
4546
Required: true,
4647
},
4748
},

gitlab/resource_gitlab_project_share_group.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1010
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
11+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
1112
"github.com/xanzy/go-gitlab"
1213
)
1314

@@ -38,15 +39,15 @@ func resourceGitlabProjectShareGroup() *schema.Resource {
3839
"group_access": {
3940
Description: fmt.Sprintf("The access level to grant the group for the project. Valid values are: %s", renderValueListForDocs(validProjectAccessLevelNames)),
4041
Type: schema.TypeString,
41-
ValidateDiagFunc: validateValueFunc(validProjectAccessLevelNames),
42+
ValidateDiagFunc: validation.ToDiagFunc(validation.StringInSlice(validProjectAccessLevelNames, false)),
4243
ForceNew: true,
4344
Optional: true,
4445
ExactlyOneOf: []string{"access_level", "group_access"},
4546
},
4647
"access_level": {
4748
Description: fmt.Sprintf("The access level to grant the group for the project. Valid values are: %s", renderValueListForDocs(validProjectAccessLevelNames)),
4849
Type: schema.TypeString,
49-
ValidateDiagFunc: validateValueFunc(validProjectAccessLevelNames),
50+
ValidateDiagFunc: validation.ToDiagFunc(validation.StringInSlice(validProjectAccessLevelNames, false)),
5051
ForceNew: true,
5152
Optional: true,
5253
Deprecated: "Use `group_access` instead of the `access_level` attribute.",
@@ -192,7 +193,7 @@ func resourceGitlabProjectShareGroupResourceV0() *schema.Resource {
192193
"access_level": {
193194
Description: fmt.Sprintf("The access level to grant the group for the project. Valid values are: %s", renderValueListForDocs(validProjectAccessLevelNames)),
194195
Type: schema.TypeString,
195-
ValidateDiagFunc: validateValueFunc(validProjectAccessLevelNames),
196+
ValidateDiagFunc: validation.ToDiagFunc(validation.StringInSlice(validProjectAccessLevelNames, false)),
196197
ForceNew: true,
197198
Required: true,
198199
},

gitlab/resource_gitlab_tag_protection.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77

88
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
99
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
10+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
1011
gitlab "github.com/xanzy/go-gitlab"
1112
)
1213

@@ -37,7 +38,7 @@ func resourceGitlabTagProtection() *schema.Resource {
3738
"create_access_level": {
3839
Description: fmt.Sprintf("Access levels which are allowed to create. Valid values are: %s.", renderValueListForDocs(validProtectedBranchTagAccessLevelNames)),
3940
Type: schema.TypeString,
40-
ValidateDiagFunc: validateValueFunc(validProtectedBranchTagAccessLevelNames),
41+
ValidateDiagFunc: validation.ToDiagFunc(validation.StringInSlice(validProtectedBranchTagAccessLevelNames, false)),
4142
Required: true,
4243
ForceNew: true,
4344
},

gitlab/util.go

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ import (
88
"strings"
99
"time"
1010

11-
"github.com/hashicorp/go-cty/cty"
12-
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1311
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1412
gitlab "github.com/xanzy/go-gitlab"
1513
)
@@ -22,26 +20,6 @@ func renderValueListForDocs(values []string) string {
2220
return strings.Join(inlineCodeValues, ", ")
2321
}
2422

25-
// copied from ../github/util.go
26-
func validateValueFunc(values []string) schema.SchemaValidateDiagFunc {
27-
return func(v interface{}, k cty.Path) diag.Diagnostics {
28-
value := v.(string)
29-
valid := false
30-
for _, role := range values {
31-
if value == role {
32-
valid = true
33-
break
34-
}
35-
}
36-
37-
if !valid {
38-
return diag.Errorf("%s is an invalid value for argument %s acceptable values are: %v", value, k, values)
39-
}
40-
41-
return nil
42-
}
43-
}
44-
4523
var validateDateFunc = func(v interface{}, k string) (we []string, errors []error) {
4624
value := v.(string)
4725
//add zero hours and let time figure out correctness

gitlab/util_test.go

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,40 +3,9 @@ package gitlab
33
import (
44
"testing"
55

6-
"github.com/hashicorp/go-cty/cty"
76
gitlab "github.com/xanzy/go-gitlab"
87
)
98

10-
func TestGitlab_validation(t *testing.T) {
11-
cases := []struct {
12-
Value string
13-
ErrCount int
14-
}{
15-
{
16-
Value: "invalid",
17-
ErrCount: 1,
18-
},
19-
{
20-
Value: "valid_one",
21-
ErrCount: 0,
22-
},
23-
{
24-
Value: "valid_two",
25-
ErrCount: 0,
26-
},
27-
}
28-
29-
validationFunc := validateValueFunc([]string{"valid_one", "valid_two"})
30-
31-
for _, tc := range cases {
32-
diags := validationFunc(tc.Value, cty.Path{cty.IndexStep{Key: cty.StringVal("test_arg")}})
33-
34-
if len(diags) != tc.ErrCount {
35-
t.Fatalf("Expected 1 validation error")
36-
}
37-
}
38-
}
39-
409
func TestGitlab_visbilityHelpers(t *testing.T) {
4110
cases := []struct {
4211
String string

0 commit comments

Comments
 (0)