From acb806c18053570cdf323e31539b5389eb362bfc Mon Sep 17 00:00:00 2001 From: Nathan McKinley Date: Tue, 10 Apr 2018 18:23:57 +0000 Subject: [PATCH 1/3] Delete the default network created by the project. --- google/resource_google_project.go | 36 +++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/google/resource_google_project.go b/google/resource_google_project.go index 28a8142765c..6782104267b 100644 --- a/google/resource_google_project.go +++ b/google/resource_google_project.go @@ -25,7 +25,7 @@ func resourceGoogleProject() *schema.Resource { Delete: resourceGoogleProjectDelete, Importer: &schema.ResourceImporter{ - State: schema.ImportStatePassthrough, + State: resourceProjectImportState, }, MigrateState: resourceGoogleProjectMigrateState, @@ -40,6 +40,11 @@ func resourceGoogleProject() *schema.Resource { Optional: true, Computed: true, }, + "auto_create_network": &schema.Schema{ + Type: schema.TypeBool, + Optional: true, + Default: true, + }, "name": &schema.Schema{ Type: schema.TypeString, Required: true, @@ -136,7 +141,28 @@ func resourceGoogleProjectCreate(d *schema.ResourceData, meta interface{}) error } } - return resourceGoogleProjectRead(d, meta) + err = resourceGoogleProjectRead(d, meta) + if err != nil { + return err + } + + // There's no such thing as "don't auto-create network", only "delete the network + // post-creation" - but that's what it's called in the UI and let's not confuse + // people if we don't have to. The GCP Console is doing the same thing - creating + // a network and deleting it in the background. + if !d.Get("auto_create_network").(bool) { + op, err := config.clientCompute.Networks.Delete( + project.Name, "default").Do() + if err != nil { + return fmt.Errorf("Error deleting network: %s", err) + } + + err = computeOperationWaitTime(config.clientCompute, op, project.Name, "Deleting Network", 10) + if err != nil { + return err + } + } + return nil } func resourceGoogleProjectRead(d *schema.ResourceData, meta interface{}) error { @@ -310,3 +336,9 @@ func resourceGoogleProjectDelete(d *schema.ResourceData, meta interface{}) error d.SetId("") return nil } + +func resourceProjectImportState(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { + // Explicitly set to default as a workaround for `ImportStateVerify` tests. + d.Set("auto_create_network", true) + return []*schema.ResourceData{d}, nil +} From 5548cceff3079934dc55070ea9ee915110892b81 Mon Sep 17 00:00:00 2001 From: Nathan McKinley Date: Tue, 10 Apr 2018 18:42:12 +0000 Subject: [PATCH 2/3] Add documentation for auto_create_network. --- website/docs/r/google_project.html.markdown | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/website/docs/r/google_project.html.markdown b/website/docs/r/google_project.html.markdown index 01ee93f04f4..0e9a77b2b4f 100755 --- a/website/docs/r/google_project.html.markdown +++ b/website/docs/r/google_project.html.markdown @@ -97,6 +97,11 @@ The following arguments are supported: * `labels` - (Optional) A set of key/value label pairs to assign to the project. +* `auto_create_network` - (Optional) Create the 'default' network automatically. Default true. + Note: this might be more accurately described as "Delete Default Network", since the network + is created automatically then deleted before project creation returns, but we choose this + name to match the GCP Console UI. + ## Attributes Reference In addition to the arguments listed above, the following computed attributes are From 8745cec8cd392103edc640c4b764691d5fe7e710 Mon Sep 17 00:00:00 2001 From: Nathan McKinley Date: Tue, 10 Apr 2018 14:30:40 -0700 Subject: [PATCH 3/3] Clarify comment --- google/resource_google_project.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/google/resource_google_project.go b/google/resource_google_project.go index 6782104267b..506e3d87f2c 100644 --- a/google/resource_google_project.go +++ b/google/resource_google_project.go @@ -338,7 +338,8 @@ func resourceGoogleProjectDelete(d *schema.ResourceData, meta interface{}) error } func resourceProjectImportState(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { - // Explicitly set to default as a workaround for `ImportStateVerify` tests. + // Explicitly set to default as a workaround for `ImportStateVerify` tests, and so that users + // don't see a diff immediately after import. d.Set("auto_create_network", true) return []*schema.ResourceData{d}, nil }