2014-08-26 06:41:15 +02:00
|
|
|
package google
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
2015-03-18 18:10:39 +01:00
|
|
|
"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,
|
|
|
|
},
|
2015-02-10 12:14:37 +01:00
|
|
|
|
|
|
|
"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)
|
|
|
|
|
2015-09-24 22:30:12 +02:00
|
|
|
err = computeOperationWaitGlobal(config, op, "Creating Network")
|
2014-08-26 07:44:27 +02:00
|
|
|
if err != nil {
|
2015-09-24 22:30:12 +02:00
|
|
|
return err
|
2014-08-26 07:44:27 +02:00
|
|
|
}
|
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 {
|
2014-08-26 07:49:14 +02:00
|
|
|
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)
|
2015-02-10 12:14:37 +01:00
|
|
|
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)
|
|
|
|
|
2014-08-26 07:13:37 +02:00
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
2015-09-24 22:30:12 +02:00
|
|
|
err = computeOperationWaitGlobal(config, op, "Deleting Network")
|
2014-08-26 07:44:27 +02:00
|
|
|
if err != nil {
|
2015-09-24 22:30:12 +02:00
|
|
|
return err
|
2014-08-26 07:44:27 +02:00
|
|
|
}
|
2014-08-26 06:41:15 +02:00
|
|
|
|
|
|
|
d.SetId("")
|
|
|
|
return nil
|
|
|
|
}
|