From 12a2489bcd5d13850baaca3a9f3a170a36a08167 Mon Sep 17 00:00:00 2001 From: Nathan McKinley Date: Tue, 16 Jan 2018 19:24:11 +0000 Subject: [PATCH] Add disable_on_destroy option to google_project_service. --- google/resource_google_project_service.go | 19 ++++++++++ .../resource_google_project_service_test.go | 36 +++++++++++++++++++ .../r/google_project_service.html.markdown | 2 ++ 3 files changed, 57 insertions(+) diff --git a/google/resource_google_project_service.go b/google/resource_google_project_service.go index 73b5e21175d..8ab54065002 100644 --- a/google/resource_google_project_service.go +++ b/google/resource_google_project_service.go @@ -2,6 +2,7 @@ package google import ( "fmt" + "log" "strings" "github.com/hashicorp/terraform/helper/schema" @@ -12,6 +13,7 @@ func resourceGoogleProjectService() *schema.Resource { Create: resourceGoogleProjectServiceCreate, Read: resourceGoogleProjectServiceRead, Delete: resourceGoogleProjectServiceDelete, + Update: resourceGoogleProjectServiceUpdate, Schema: map[string]*schema.Schema{ "service": { @@ -25,6 +27,11 @@ func resourceGoogleProjectService() *schema.Resource { Computed: true, ForceNew: true, }, + "disable_on_destroy": &schema.Schema{ + Type: schema.TypeBool, + Optional: true, + Default: true, + }, }, } } @@ -82,6 +89,11 @@ func resourceGoogleProjectServiceRead(d *schema.ResourceData, meta interface{}) func resourceGoogleProjectServiceDelete(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) + if disable := d.Get("disable_on_destroy"); !(disable.(bool)) { + log.Printf("Not disabling service '%s', because disable_on_destroy is false.", d.Id()) + d.SetId("") + return nil + } project, err := getProject(d, config) if err != nil { return err @@ -100,6 +112,13 @@ func resourceGoogleProjectServiceDelete(d *schema.ResourceData, meta interface{} return nil } +func resourceGoogleProjectServiceUpdate(d *schema.ResourceData, meta interface{}) error { + // The only thing that can be updated without a ForceNew is whether to disable the service on resource delete. + // This doesn't require any calls to any APIs since it's all internal state. + // This update is a no-op. + return nil +} + // Parts that make up the id of a `google_project_service` resource. // Project is included in order to allow multiple projects to enable the same service within the same Terraform state type projectServiceId struct { diff --git a/google/resource_google_project_service_test.go b/google/resource_google_project_service_test.go index 708138d9114..4e2ee066dac 100644 --- a/google/resource_google_project_service_test.go +++ b/google/resource_google_project_service_test.go @@ -33,6 +33,20 @@ func TestAccGoogleProjectService_basic(t *testing.T) { testAccCheckProjectService(services, pid, false), ), }, + // Create services with disabling turned off. + resource.TestStep{ + Config: testAccGoogleProjectService_noDisable(services, pid, pname, org), + Check: resource.ComposeTestCheckFunc( + testAccCheckProjectService(services, pid, true), + ), + }, + // Check that services are still enabled even after the resources are deleted. + resource.TestStep{ + Config: testAccGoogleProject_create(pid, pname, org), + Check: resource.ComposeTestCheckFunc( + testAccCheckProjectService(services, pid, true), + ), + }, }, }) } @@ -84,3 +98,25 @@ resource "google_project_service" "test2" { } `, pid, name, org, services[0], services[1]) } + +func testAccGoogleProjectService_noDisable(services []string, pid, name, org 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" + disable_on_destroy = false +} + +resource "google_project_service" "test2" { + project = "${google_project.acceptance.project_id}" + service = "%s" + disable_on_destroy = false +} +`, pid, name, org, services[0], services[1]) +} diff --git a/website/docs/r/google_project_service.html.markdown b/website/docs/r/google_project_service.html.markdown index 11b8698a213..ea97319314a 100644 --- a/website/docs/r/google_project_service.html.markdown +++ b/website/docs/r/google_project_service.html.markdown @@ -32,3 +32,5 @@ The following arguments are supported: * `service` - (Required) The service to enable. * `project` - (Optional) The project ID. If not provided, the provider project is used. + +* `disable_on_destroy` - (Optional) If true, disable the service when the terraform resource is destroyed. Defaults to true. May be useful in the event that a project is long-lived but the infrastructure running in that project changes frequently.