Skip to content

Commit

Permalink
Add disable_on_destroy option to google_project_service. (#965)
Browse files Browse the repository at this point in the history
  • Loading branch information
nat-henderson authored Jan 16, 2018
1 parent bc26697 commit ea50226
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 0 deletions.
19 changes: 19 additions & 0 deletions google/resource_google_project_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package google

import (
"fmt"
"log"
"strings"

"github.com/hashicorp/terraform/helper/schema"
Expand All @@ -12,6 +13,7 @@ func resourceGoogleProjectService() *schema.Resource {
Create: resourceGoogleProjectServiceCreate,
Read: resourceGoogleProjectServiceRead,
Delete: resourceGoogleProjectServiceDelete,
Update: resourceGoogleProjectServiceUpdate,

Schema: map[string]*schema.Schema{
"service": {
Expand All @@ -25,6 +27,11 @@ func resourceGoogleProjectService() *schema.Resource {
Computed: true,
ForceNew: true,
},
"disable_on_destroy": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
Default: true,
},
},
}
}
Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand Down
36 changes: 36 additions & 0 deletions google/resource_google_project_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
),
},
},
})
}
Expand Down Expand Up @@ -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])
}
2 changes: 2 additions & 0 deletions website/docs/r/google_project_service.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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.

0 comments on commit ea50226

Please sign in to comment.