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{
|
2016-02-15 04:17:55 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
2016-02-16 04:04:44 +01:00
|
|
|
Deprecated: "Please use google_compute_subnetwork resources instead.",
|
2014-08-26 06:41:15 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
"gateway_ipv4": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
2015-02-10 12:14:37 +01:00
|
|
|
|
2016-02-14 23:27:17 +01:00
|
|
|
"auto_create_subnetworks": &schema.Schema{
|
|
|
|
Type: schema.TypeBool,
|
|
|
|
Optional: true,
|
2016-02-16 04:04:44 +01:00
|
|
|
ForceNew: true,
|
|
|
|
Default: false, // TODO: ideally should be true to match Google's default behaviour, but this causes backward
|
|
|
|
// compatibility issue with existing terraform configs
|
2016-02-14 23:27:17 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
"description": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
2016-02-16 04:04:44 +01:00
|
|
|
ForceNew: true,
|
2016-02-14 23:27:17 +01:00
|
|
|
},
|
|
|
|
|
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)
|
|
|
|
|
2016-02-14 23:27:17 +01:00
|
|
|
//
|
|
|
|
// Possible modes:
|
2016-02-16 04:04:44 +01:00
|
|
|
// - 1 Legacy mode - Create a network in the legacy mode. ipv4_range is set. auto_create_subnetworks must be false
|
|
|
|
// and not sent in request
|
|
|
|
// - 2 Distributed Mode - Create a new generation network that supports subnetworks:
|
|
|
|
// - 2.a - Auto subnet mode - auto_create_subnetworks = true, Google will generate 1 subnetwork per region
|
|
|
|
// - 2.b - Custom subnet mode - auto_create_subnetworks = false & ipv4_range not set,
|
2016-02-14 23:27:17 +01:00
|
|
|
//
|
|
|
|
ipv4range := d.Get("ipv4_range").(string)
|
|
|
|
autoCreateSubnetworks := d.Get("auto_create_subnetworks").(bool)
|
|
|
|
|
|
|
|
if ipv4range != "" && autoCreateSubnetworks {
|
|
|
|
return fmt.Errorf("Error: cannot define ipv4_range with auto_create_subnetworks = true.")
|
|
|
|
}
|
|
|
|
|
2014-08-26 06:41:15 +02:00
|
|
|
// Build the network parameter
|
|
|
|
network := &compute.Network{
|
2016-02-14 23:27:17 +01:00
|
|
|
Name: d.Get("name").(string),
|
|
|
|
AutoCreateSubnetworks: autoCreateSubnetworks,
|
|
|
|
Description: d.Get("description").(string),
|
2014-08-26 06:41:15 +02:00
|
|
|
}
|
2016-02-14 23:27:17 +01:00
|
|
|
|
|
|
|
if v, ok := d.GetOk("ipv4_range"); ok {
|
|
|
|
log.Printf("[DEBUG] Setting IPv4Range (%#V) for legacy network mode", v.(string))
|
|
|
|
network.IPv4Range = v.(string)
|
|
|
|
} else {
|
2016-02-16 04:04:44 +01:00
|
|
|
// custom subnet mode, so make sure AutoCreateSubnetworks field is included in request otherwise
|
|
|
|
// google will create a network in legacy mode.
|
2016-02-14 23:27:17 +01:00
|
|
|
network.ForceSendFields = []string{"AutoCreateSubnetworks"}
|
|
|
|
}
|
|
|
|
|
2014-08-26 06:41:15 +02:00
|
|
|
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
|
|
|
|
2016-02-16 04:04:44 +01:00
|
|
|
return resourceComputeNetworkRead(d, meta)
|
2014-08-26 06:41:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2015-11-13 21:36:03 +01:00
|
|
|
log.Printf("[WARN] Removing Network %q because it's gone", d.Get("name").(string))
|
2014-08-26 07:49:14 +02:00
|
|
|
// 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
|
|
|
|
}
|