Skip to content

Commit

Permalink
Delete the default network created by the project. (#1316)
Browse files Browse the repository at this point in the history
  • Loading branch information
nat-henderson authored Apr 10, 2018
1 parent 7a84ea8 commit 98dbf7a
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
37 changes: 35 additions & 2 deletions google/resource_google_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func resourceGoogleProject() *schema.Resource {
Delete: resourceGoogleProjectDelete,

Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
State: resourceProjectImportState,
},
MigrateState: resourceGoogleProjectMigrateState,

Expand All @@ -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,
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
5 changes: 5 additions & 0 deletions website/docs/r/google_project.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 98dbf7a

Please sign in to comment.