Skip to content

Commit dc8953e

Browse files
committed
Add validation to operator attributes
1 parent 7147526 commit dc8953e

3 files changed

Lines changed: 23 additions & 17 deletions

File tree

github/resource_github_organization_ruleset.go

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -339,9 +339,10 @@ func resourceGithubOrganizationRuleset() *schema.Resource {
339339
Description: "If true, the rule will fail if the pattern matches.",
340340
},
341341
"operator": {
342-
Type: schema.TypeString,
343-
Required: true,
344-
Description: "The operator to use for matching. Can be one of: `starts_with`, `ends_with`, `contains`, `regex`.",
342+
Type: schema.TypeString,
343+
ValidateDiagFunc: operatorValidation,
344+
Required: true,
345+
Description: "The operator to use for matching. Can be one of: `starts_with`, `ends_with`, `contains`, `regex`.",
345346
},
346347
"pattern": {
347348
Type: schema.TypeString,
@@ -369,9 +370,10 @@ func resourceGithubOrganizationRuleset() *schema.Resource {
369370
Description: "If true, the rule will fail if the pattern matches.",
370371
},
371372
"operator": {
372-
Type: schema.TypeString,
373-
Required: true,
374-
Description: "The operator to use for matching. Can be one of: `starts_with`, `ends_with`, `contains`, `regex`.",
373+
Type: schema.TypeString,
374+
ValidateDiagFunc: operatorValidation,
375+
Required: true,
376+
Description: "The operator to use for matching. Can be one of: `starts_with`, `ends_with`, `contains`, `regex`.",
375377
},
376378
"pattern": {
377379
Type: schema.TypeString,
@@ -399,9 +401,10 @@ func resourceGithubOrganizationRuleset() *schema.Resource {
399401
Description: "If true, the rule will fail if the pattern matches.",
400402
},
401403
"operator": {
402-
Type: schema.TypeString,
403-
Required: true,
404-
Description: "The operator to use for matching. Can be one of: `starts_with`, `ends_with`, `contains`, `regex`.",
404+
Type: schema.TypeString,
405+
ValidateDiagFunc: operatorValidation,
406+
Required: true,
407+
Description: "The operator to use for matching. Can be one of: `starts_with`, `ends_with`, `contains`, `regex`.",
405408
},
406409
"pattern": {
407410
Type: schema.TypeString,
@@ -430,9 +433,10 @@ func resourceGithubOrganizationRuleset() *schema.Resource {
430433
Description: "If true, the rule will fail if the pattern matches.",
431434
},
432435
"operator": {
433-
Type: schema.TypeString,
434-
Required: true,
435-
Description: "The operator to use for matching. Can be one of: `starts_with`, `ends_with`, `contains`, `regex`.",
436+
Type: schema.TypeString,
437+
ValidateDiagFunc: operatorValidation,
438+
Required: true,
439+
Description: "The operator to use for matching. Can be one of: `starts_with`, `ends_with`, `contains`, `regex`.",
436440
},
437441
"pattern": {
438442
Type: schema.TypeString,
@@ -461,9 +465,10 @@ func resourceGithubOrganizationRuleset() *schema.Resource {
461465
Description: "If true, the rule will fail if the pattern matches.",
462466
},
463467
"operator": {
464-
Type: schema.TypeString,
465-
Required: true,
466-
Description: "The operator to use for matching. Can be one of: `starts_with`, `ends_with`, `contains`, `regex`.",
468+
Type: schema.TypeString,
469+
ValidateDiagFunc: operatorValidation,
470+
Required: true,
471+
Description: "The operator to use for matching. Can be one of: `starts_with`, `ends_with`, `contains`, `regex`.",
467472
},
468473
"pattern": {
469474
Type: schema.TypeString,

github/resource_github_repository_ruleset.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ import (
1818

1919
var supportedRulesetTargetTypes = []string{string(github.RulesetTargetBranch), string(github.RulesetTargetPush), string(github.RulesetTargetTag)}
2020

21-
var operatorValidation = validation.ToDiagFunc(validation.StringInSlice([]string{"starts_with", "ends_with", "contains", "regex"}, false))
22-
2321
func resourceGithubRepositoryRuleset() *schema.Resource {
2422
return &schema.Resource{
2523
CreateContext: resourceGithubRepositoryRulesetCreate,

github/util_ruleset_validation.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@ import (
88
"github.com/google/go-github/v82/github"
99
"github.com/hashicorp/terraform-plugin-log/tflog"
1010
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
11+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
1112
)
1213

14+
var operatorValidation = validation.ToDiagFunc(validation.StringInSlice([]string{"starts_with", "ends_with", "contains", "regex"}, false))
15+
1316
// branchTagOnlyRules contains rules that are only valid for branch and tag targets.
1417
//
1518
// These rules apply to ref-based operations (branches and tags) and are not supported

0 commit comments

Comments
 (0)