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
Changes from 1 commit
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
Next Next commit
Delete the default network created by the project.
  • Loading branch information
nat-henderson committed Apr 10, 2018
commit acb806c18053570cdf323e31539b5389eb362bfc
36 changes: 34 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,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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is not only for ImportStateVerify but also to ensure people don't see a diff after importing this resource.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great point, I'll modify that.

d.Set("auto_create_network", true)
return []*schema.ResourceData{d}, nil
}