Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add check_if_service_has_usage_on_destroy field to google_project_service resource #7745

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
3 changes: 3 additions & 0 deletions .changelog/10973.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
resourcemanager: added `check_if_service_has_usage_on_destroy` field to `google_project_service` resource
```
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,11 @@ func ResourceGoogleProjectService() *schema.Resource {
Optional: true,
Default: true,
},
"check_if_service_has_usage_on_destroy": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},
},
UseJSONNumber: true,
}
Expand Down Expand Up @@ -258,7 +263,9 @@ func resourceGoogleProjectServiceDelete(d *schema.ResourceData, meta interface{}

service := d.Get("service").(string)
disableDependencies := d.Get("disable_dependent_services").(bool)
if err = disableServiceUsageProjectService(service, project, d, config, disableDependencies); err != nil {
checkUsage := d.Get("check_if_service_has_usage_on_destroy").(bool)
err = disableServiceUsageProjectService(service, project, d, config, disableDependencies, checkUsage)
if err != nil {
return transport_tpg.HandleNotFoundError(err, d, fmt.Sprintf("Project Service %s", d.Id()))
}

Expand All @@ -273,7 +280,7 @@ func resourceGoogleProjectServiceUpdate(d *schema.ResourceData, meta interface{}
}

// Disables a project service.
func disableServiceUsageProjectService(service, project string, d *schema.ResourceData, config *transport_tpg.Config, disableDependentServices bool) error {
func disableServiceUsageProjectService(service, project string, d *schema.ResourceData, config *transport_tpg.Config, disableDependentServices bool, checkUsage bool) error {
err := transport_tpg.Retry(transport_tpg.RetryOptions{
RetryFunc: func() error {
billingProject := project
Expand All @@ -282,8 +289,13 @@ func disableServiceUsageProjectService(service, project string, d *schema.Resour
return err
}
name := fmt.Sprintf("projects/%s/services/%s", project, service)
checkIfServiceHasUsage := "SKIP"
if checkUsage {
checkIfServiceHasUsage = "CHECK"
}
servicesDisableCall := config.NewServiceUsageClient(userAgent).Services.Disable(name, &serviceusage.DisableServiceRequest{
DisableDependentServices: disableDependentServices,
CheckIfServiceHasUsage: checkIfServiceHasUsage,
})
if config.UserProjectOverride {
// err == nil indicates that the billing_project value was found
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func TestAccProjectService_disableDependentServices(t *testing.T) {
ResourceName: "google_project_service.test",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"disable_on_destroy"},
ImportStateVerifyIgnore: []string{"disable_on_destroy", "check_if_service_has_usage_on_destroy"},
},
{
Config: testAccProjectService_dependencyRemoved(services, pid, org, billingId),
Expand Down Expand Up @@ -166,7 +166,7 @@ func TestAccProjectService_renamedService(t *testing.T) {
ResourceName: "google_project_service.test",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"disable_on_destroy", "disable_dependent_services"},
ImportStateVerifyIgnore: []string{"disable_on_destroy", "check_if_service_has_usage_on_destroy", "disable_dependent_services"},
},
},
})
Expand Down Expand Up @@ -199,6 +199,35 @@ func testAccCheckProjectService(t *testing.T, services []string, pid string, exp
}
}

func TestAccProjectService_checkUsageOfServices(t *testing.T) {
// Multiple fine-grained resources
acctest.SkipIfVcr(t)
t.Parallel()

org := envvar.GetTestOrgFromEnv(t)
pid := fmt.Sprintf("tf-test-%d", acctest.RandInt(t))
services := "bigquerystorage.googleapis.com"

acctest.VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderBetaFactories(t),
Steps: []resource.TestStep{
{
Config: testAccProjectService_checkUsage(services, pid, org, "false"),
},
{
ResourceName: "google_project_service.test",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"disable_on_destroy", "disable_dependent_services"},
},
{
Config: testAccProjectService_checkUsage(services, pid, org, "true"),
},
},
})
}

func testAccProjectService_basic(services []string, pid, org string) string {
return fmt.Sprintf(`
resource "google_project" "acceptance" {
Expand Down Expand Up @@ -325,3 +354,20 @@ resource "google_project_service" "test" {
}
`, pid, pid, org, service)
}

func testAccProjectService_checkUsage(service string, pid, org string, checkIfServiceHasUsage string) string {
return fmt.Sprintf(`
resource "google_project" "acceptance" {
project_id = "%s"
name = "%s"
org_id = "%s"
}

resource "google_project_service" "test" {
project = google_project.acceptance.project_id
service = "%s"

check_if_service_has_usage_on_destroy = %s
}
`, pid, pid, org, service, checkIfServiceHasUsage)
}
6 changes: 6 additions & 0 deletions website/docs/r/google_project_service.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ and which depend on this service should also be disabled when this service is
destroyed. If `false` or unset, an error will be returned if any enabled
services depend on this service when attempting to destroy it.

* `check_if_service_has_usage_on_destroy` - (Optional)
[Beta](https://terraform.io/docs/providers/google/guides/provider_versions.html)
If `true`, the usage of the service to be disabled will be checked and an error
will be returned if the service to be disabled has usage in last 30 days.
Defaults to `false`.

## Attributes Reference

In addition to the arguments listed above, the following computed attributes are
Expand Down