diff --git a/google/resource_google_project.go b/google/resource_google_project.go index 28a8142765c..506e3d87f2c 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,10 @@ 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, and so that users + // don't see a diff immediately after import. + d.Set("auto_create_network", true) + return []*schema.ResourceData{d}, nil +} 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