Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delete the default network created by the project. #1316

Merged
merged 3 commits into from
Apr 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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