Add disable_on_destroy option to google_project_service. (#965)

This commit is contained in:
Nathan McKinley 2018-01-16 14:39:27 -08:00 committed by GitHub
parent bc266979d4
commit ea50226ade
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 57 additions and 0 deletions

View File

@ -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 {

View File

@ -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])
}

View File

@ -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.