2016-02-14 23:27:17 +01:00
|
|
|
package google
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
|
2016-04-10 18:59:57 +02:00
|
|
|
"strings"
|
|
|
|
|
2016-02-14 23:27:17 +01:00
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
|
|
"google.golang.org/api/compute/v1"
|
|
|
|
)
|
|
|
|
|
|
|
|
func resourceComputeSubnetwork() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
|
|
|
Create: resourceComputeSubnetworkCreate,
|
|
|
|
Read: resourceComputeSubnetworkRead,
|
2017-06-07 15:52:14 +02:00
|
|
|
Update: resourceComputeSubnetworkUpdate,
|
2016-02-14 23:27:17 +01:00
|
|
|
Delete: resourceComputeSubnetworkDelete,
|
|
|
|
|
|
|
|
Schema: map[string]*schema.Schema{
|
2016-04-10 23:34:15 +02:00
|
|
|
"ip_cidr_range": &schema.Schema{
|
2016-02-14 23:27:17 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
2016-04-10 23:34:15 +02:00
|
|
|
"name": &schema.Schema{
|
2016-02-14 23:27:17 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"network": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"description": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"gateway_address": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
|
2016-04-10 23:34:15 +02:00
|
|
|
"project": &schema.Schema{
|
2016-02-14 23:27:17 +01:00
|
|
|
Type: schema.TypeString,
|
2016-04-10 23:34:15 +02:00
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
2016-02-14 23:27:17 +01:00
|
|
|
},
|
2016-04-10 18:59:57 +02:00
|
|
|
|
2016-04-10 23:34:15 +02:00
|
|
|
"region": &schema.Schema{
|
2016-04-10 18:59:57 +02:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
2016-04-10 23:34:15 +02:00
|
|
|
|
2017-05-18 22:35:02 +02:00
|
|
|
"private_ip_google_access": &schema.Schema{
|
|
|
|
Type: schema.TypeBool,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
|
2016-04-10 23:34:15 +02:00
|
|
|
"self_link": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
2016-02-14 23:27:17 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func createSubnetID(s *compute.Subnetwork) string {
|
|
|
|
return fmt.Sprintf("%s/%s", s.Region, s.Name)
|
|
|
|
}
|
|
|
|
|
2016-02-16 04:04:44 +01:00
|
|
|
func splitSubnetID(id string) (region string, name string) {
|
|
|
|
parts := strings.Split(id, "/")
|
|
|
|
region = parts[0]
|
|
|
|
name = parts[1]
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-02-14 23:27:17 +01:00
|
|
|
func resourceComputeSubnetworkCreate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
config := meta.(*Config)
|
|
|
|
|
2016-04-10 23:34:15 +02:00
|
|
|
region, err := getRegion(d, config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-04-10 18:59:57 +02:00
|
|
|
project, err := getProject(d, config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-09-03 11:51:20 +02:00
|
|
|
network, err := getNetworkLink(d, config, "network")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-02-14 23:27:17 +01:00
|
|
|
// Build the subnetwork parameters
|
|
|
|
subnetwork := &compute.Subnetwork{
|
2017-05-18 22:35:02 +02:00
|
|
|
Name: d.Get("name").(string),
|
|
|
|
Description: d.Get("description").(string),
|
|
|
|
IpCidrRange: d.Get("ip_cidr_range").(string),
|
|
|
|
PrivateIpGoogleAccess: d.Get("private_ip_google_access").(bool),
|
|
|
|
Network: network,
|
2016-02-14 23:27:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] Subnetwork insert request: %#v", subnetwork)
|
|
|
|
op, err := config.clientCompute.Subnetworks.Insert(
|
2016-04-10 18:59:57 +02:00
|
|
|
project, region, subnetwork).Do()
|
2016-02-14 23:27:17 +01:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error creating subnetwork: %s", err)
|
|
|
|
}
|
|
|
|
|
2016-02-16 04:04:44 +01:00
|
|
|
// It probably maybe worked, so store the ID now. ID is a combination of region + subnetwork
|
|
|
|
// name because subnetwork names are not unique in a project, per the Google docs:
|
|
|
|
// "When creating a new subnetwork, its name has to be unique in that project for that region, even across networks.
|
|
|
|
// The same name can appear twice in a project, as long as each one is in a different region."
|
|
|
|
// https://cloud.google.com/compute/docs/subnetworks
|
2016-02-14 23:27:17 +01:00
|
|
|
subnetwork.Region = region
|
|
|
|
d.SetId(createSubnetID(subnetwork))
|
|
|
|
|
2016-06-06 19:35:13 +02:00
|
|
|
err = computeOperationWaitRegion(config, op, project, region, "Creating Subnetwork")
|
2016-02-14 23:27:17 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return resourceComputeSubnetworkRead(d, meta)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceComputeSubnetworkRead(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
config := meta.(*Config)
|
2016-04-10 18:59:57 +02:00
|
|
|
|
2016-04-10 23:34:15 +02:00
|
|
|
region, err := getRegion(d, config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-04-10 18:59:57 +02:00
|
|
|
project, err := getProject(d, config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-02-14 23:27:17 +01:00
|
|
|
name := d.Get("name").(string)
|
|
|
|
|
|
|
|
subnetwork, err := config.clientCompute.Subnetworks.Get(
|
2016-04-10 18:59:57 +02:00
|
|
|
project, region, name).Do()
|
2016-02-14 23:27:17 +01:00
|
|
|
if err != nil {
|
2017-05-10 01:00:47 +02:00
|
|
|
return handleNotFoundError(err, d, fmt.Sprintf("Subnetwork %q", name))
|
2016-02-14 23:27:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
d.Set("gateway_address", subnetwork.GatewayAddress)
|
|
|
|
d.Set("self_link", subnetwork.SelfLink)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-06-07 15:52:14 +02:00
|
|
|
func resourceComputeSubnetworkUpdate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
config := meta.(*Config)
|
|
|
|
|
|
|
|
region, err := getRegion(d, config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
project, err := getProject(d, config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
d.Partial(true)
|
|
|
|
|
|
|
|
if d.HasChange("private_ip_google_access") {
|
|
|
|
subnetworksSetPrivateIpGoogleAccessRequest := &compute.SubnetworksSetPrivateIpGoogleAccessRequest{
|
|
|
|
PrivateIpGoogleAccess: d.Get("private_ip_google_access").(bool),
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] Updating Subnetwork PrivateIpGoogleAccess %q: %#v", d.Id(), subnetworksSetPrivateIpGoogleAccessRequest)
|
|
|
|
op, err := config.clientCompute.Subnetworks.SetPrivateIpGoogleAccess(
|
|
|
|
project, region, d.Get("name").(string), subnetworksSetPrivateIpGoogleAccessRequest).Do()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error updating subnetwork PrivateIpGoogleAccess: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = computeOperationWaitRegion(config, op, project, region, "Updating Subnetwork PrivateIpGoogleAccess")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
d.SetPartial("private_ip_google_access")
|
|
|
|
}
|
|
|
|
|
|
|
|
d.Partial(false)
|
|
|
|
|
|
|
|
return resourceComputeSubnetworkRead(d, meta)
|
|
|
|
}
|
|
|
|
|
2016-02-14 23:27:17 +01:00
|
|
|
func resourceComputeSubnetworkDelete(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
config := meta.(*Config)
|
2016-04-10 18:59:57 +02:00
|
|
|
|
2016-04-10 23:34:15 +02:00
|
|
|
region, err := getRegion(d, config)
|
2016-04-10 18:59:57 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-04-10 23:34:15 +02:00
|
|
|
project, err := getProject(d, config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-02-14 23:27:17 +01:00
|
|
|
|
2016-02-16 04:04:44 +01:00
|
|
|
// Delete the subnetwork
|
2016-02-14 23:27:17 +01:00
|
|
|
op, err := config.clientCompute.Subnetworks.Delete(
|
2016-04-10 18:59:57 +02:00
|
|
|
project, region, d.Get("name").(string)).Do()
|
2016-02-14 23:27:17 +01:00
|
|
|
if err != nil {
|
2016-02-16 04:04:44 +01:00
|
|
|
return fmt.Errorf("Error deleting subnetwork: %s", err)
|
2016-02-14 23:27:17 +01:00
|
|
|
}
|
|
|
|
|
2016-06-06 19:35:13 +02:00
|
|
|
err = computeOperationWaitRegion(config, op, project, region, "Deleting Subnetwork")
|
2016-02-14 23:27:17 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
d.SetId("")
|
|
|
|
return nil
|
|
|
|
}
|