terraform/builtin/providers/google/resource_compute_network.go

110 lines
2.4 KiB
Go
Raw Normal View History

2014-08-26 06:41:15 +02:00
package google
import (
"fmt"
"log"
"github.com/hashicorp/terraform/helper/schema"
"google.golang.org/api/compute/v1"
"google.golang.org/api/googleapi"
2014-08-26 06:41:15 +02:00
)
func resourceComputeNetwork() *schema.Resource {
return &schema.Resource{
Create: resourceComputeNetworkCreate,
Read: resourceComputeNetworkRead,
Delete: resourceComputeNetworkDelete,
Schema: map[string]*schema.Schema{
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"ipv4_range": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"gateway_ipv4": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"self_link": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
2014-08-26 06:41:15 +02:00
},
}
}
func resourceComputeNetworkCreate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
// Build the network parameter
network := &compute.Network{
Name: d.Get("name").(string),
IPv4Range: d.Get("ipv4_range").(string),
}
log.Printf("[DEBUG] Network insert request: %#v", network)
op, err := config.clientCompute.Networks.Insert(
config.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 = computeOperationWaitGlobal(config, op, "Creating Network")
if err != nil {
return err
}
2014-08-26 06:41:15 +02:00
return resourceComputeNetworkRead(d, meta)
}
func resourceComputeNetworkRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
network, err := config.clientCompute.Networks.Get(
config.Project, d.Id()).Do()
if err != nil {
if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 {
// The resource doesn't exist anymore
d.SetId("")
return nil
}
2014-08-26 07:09:38 +02:00
return fmt.Errorf("Error reading network: %s", err)
2014-08-26 06:41:15 +02:00
}
d.Set("gateway_ipv4", network.GatewayIPv4)
d.Set("self_link", network.SelfLink)
2014-08-26 06:41:15 +02:00
return nil
}
func resourceComputeNetworkDelete(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
// Delete the network
2014-08-26 06:41:15 +02:00
op, err := config.clientCompute.Networks.Delete(
config.Project, d.Id()).Do()
if err != nil {
return fmt.Errorf("Error deleting network: %s", err)
}
err = computeOperationWaitGlobal(config, op, "Deleting Network")
if err != nil {
return err
}
2014-08-26 06:41:15 +02:00
d.SetId("")
return nil
}