mirror of
https://github.com/letic/terraform-provider-google.git
synced 2024-09-06 17:16:44 +00:00
![Paddy](/assets/img/avatar_default.png)
In advance of 1.0.0, let's take the opportunity to remove the fields on resources that have been deprecated for a while.
147 lines
3.3 KiB
Go
147 lines
3.3 KiB
Go
package google
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
"google.golang.org/api/compute/v1"
|
|
)
|
|
|
|
func resourceComputeNetwork() *schema.Resource {
|
|
return &schema.Resource{
|
|
Create: resourceComputeNetworkCreate,
|
|
Read: resourceComputeNetworkRead,
|
|
Delete: resourceComputeNetworkDelete,
|
|
Importer: &schema.ResourceImporter{
|
|
State: schema.ImportStatePassthrough,
|
|
},
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
"name": &schema.Schema{
|
|
Type: schema.TypeString,
|
|
Required: true,
|
|
ForceNew: true,
|
|
},
|
|
|
|
"auto_create_subnetworks": &schema.Schema{
|
|
Type: schema.TypeBool,
|
|
Optional: true,
|
|
ForceNew: true,
|
|
Default: true,
|
|
ConflictsWith: []string{"ipv4_range"},
|
|
},
|
|
|
|
"description": &schema.Schema{
|
|
Type: schema.TypeString,
|
|
Optional: true,
|
|
ForceNew: true,
|
|
},
|
|
|
|
"gateway_ipv4": &schema.Schema{
|
|
Type: schema.TypeString,
|
|
Computed: true,
|
|
},
|
|
|
|
"ipv4_range": &schema.Schema{
|
|
Type: schema.TypeString,
|
|
Optional: true,
|
|
ForceNew: true,
|
|
Removed: "Please use google_compute_subnetwork resources instead.",
|
|
},
|
|
|
|
"project": &schema.Schema{
|
|
Type: schema.TypeString,
|
|
Optional: true,
|
|
ForceNew: true,
|
|
},
|
|
|
|
"self_link": &schema.Schema{
|
|
Type: schema.TypeString,
|
|
Computed: true,
|
|
},
|
|
},
|
|
}
|
|
}
|
|
|
|
func resourceComputeNetworkCreate(d *schema.ResourceData, meta interface{}) error {
|
|
config := meta.(*Config)
|
|
|
|
project, err := getProject(d, config)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// Build the network parameter
|
|
network := &compute.Network{
|
|
Name: d.Get("name").(string),
|
|
AutoCreateSubnetworks: d.Get("auto_create_subnetworks").(bool),
|
|
Description: d.Get("description").(string),
|
|
}
|
|
|
|
// make sure AutoCreateSubnetworks field is included in request
|
|
network.ForceSendFields = []string{"AutoCreateSubnetworks"}
|
|
log.Printf("[DEBUG] Network insert request: %#v", network)
|
|
op, err := config.clientCompute.Networks.Insert(
|
|
project, network).Do()
|
|
if err != nil {
|
|
return fmt.Errorf("Error creating network: %s", err)
|
|
}
|
|
|
|
// It probably maybe worked, so store the ID now
|
|
d.SetId(network.Name)
|
|
|
|
err = computeOperationWait(config, op, project, "Creating Network")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return resourceComputeNetworkRead(d, meta)
|
|
}
|
|
|
|
func resourceComputeNetworkRead(d *schema.ResourceData, meta interface{}) error {
|
|
config := meta.(*Config)
|
|
|
|
project, err := getProject(d, config)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
network, err := config.clientCompute.Networks.Get(
|
|
project, d.Id()).Do()
|
|
if err != nil {
|
|
return handleNotFoundError(err, d, fmt.Sprintf("Network %q", d.Get("name").(string)))
|
|
}
|
|
|
|
d.Set("gateway_ipv4", network.GatewayIPv4)
|
|
d.Set("self_link", network.SelfLink)
|
|
d.Set("name", network.Name)
|
|
d.Set("auto_create_subnetworks", network.AutoCreateSubnetworks)
|
|
|
|
return nil
|
|
}
|
|
|
|
func resourceComputeNetworkDelete(d *schema.ResourceData, meta interface{}) error {
|
|
config := meta.(*Config)
|
|
|
|
project, err := getProject(d, config)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// Delete the network
|
|
op, err := config.clientCompute.Networks.Delete(
|
|
project, d.Id()).Do()
|
|
if err != nil {
|
|
return fmt.Errorf("Error deleting network: %s", err)
|
|
}
|
|
|
|
err = computeOperationWait(config, op, project, "Deleting Network")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
d.SetId("")
|
|
return nil
|
|
}
|