Skip to content

Commit

Permalink
Made project_service.service validation reject invalid service domains (
Browse files Browse the repository at this point in the history
#4722) (#3191)

Signed-off-by: Modular Magician <magic-modules@google.com>
  • Loading branch information
modular-magician committed Apr 23, 2021
1 parent db20835 commit db7fc54
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .changelog/4722.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
serviceusage: Made `google_project_service.service` validation reject invalid service domains that don't contain a period
```
17 changes: 16 additions & 1 deletion google-beta/resource_google_project_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,21 @@ var renamedServicesByOldAndNewServiceNames = mergeStringMaps(renamedServices, re

const maxServiceUsageBatchSize = 20

func validateProjectServiceService(val interface{}, key string) (warns []string, errs []error) {
bannedServicesFunc := StringNotInSlice(append(ignoredProjectServices, bannedProjectServices...), false)
warns, errs = bannedServicesFunc(val, key)
if len(errs) > 0 {
return
}

// StringNotInSlice already validates that this is a string
v, _ := val.(string)
if !strings.Contains(v, ".") {
errs = append(errs, fmt.Errorf("expected %s to be a domain like serviceusage.googleapis.com", v))
}
return
}

func resourceGoogleProjectService() *schema.Resource {
return &schema.Resource{
Create: resourceGoogleProjectServiceCreate,
Expand All @@ -79,7 +94,7 @@ func resourceGoogleProjectService() *schema.Resource {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: StringNotInSlice(append(ignoredProjectServices, bannedProjectServices...), false),
ValidateFunc: validateProjectServiceService,
},
"project": {
Type: schema.TypeString,
Expand Down
38 changes: 38 additions & 0 deletions google-beta/resource_google_project_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,44 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
)

func TestProjectServiceServiceValidateFunc(t *testing.T) {
cases := map[string]struct {
val interface{}
ExpectValidationError bool
}{
"ignoredProjectService": {
val: "dataproc-control.googleapis.com",
ExpectValidationError: true,
},
"bannedProjectService": {
val: "bigquery-json.googleapis.com",
ExpectValidationError: true,
},
"third party API": {
val: "whatever.example.com",
ExpectValidationError: false,
},
"not a domain": {
val: "monitoring",
ExpectValidationError: true,
},
"not a string": {
val: 5,
ExpectValidationError: true,
},
}

for tn, tc := range cases {
_, errs := validateProjectServiceService(tc.val, "service")
if tc.ExpectValidationError && len(errs) == 0 {
t.Errorf("bad: %s, %q passed validation but was expected to fail", tn, tc.val)
}
if !tc.ExpectValidationError && len(errs) > 0 {
t.Errorf("bad: %s, %q failed validation but was expected to pass. errs: %q", tn, tc.val, errs)
}
}
}

// Test that services can be enabled and disabled on a project
func TestAccProjectService_basic(t *testing.T) {
t.Parallel()
Expand Down

0 comments on commit db7fc54

Please sign in to comment.