Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

provider/gce: VPN resources, documentation, tests and example #3213

Merged
merged 1 commit into from
Sep 15, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ FEATURES:
* **New resource: `google_compute_project_metadata`** [GH-3065]
* **New resources: `aws_ami`, `aws_ami_copy`, `aws_ami_from_instance`** [GH-2874]
* **New resource: `google_storage_bucket_object`** [GH-3192]
* **New resources: `google_compute_vpn_gateway`, `google_compute_vpn_tunnel`** [GH-3213]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI: The changelog is something maintainers update via github web iface when they're merging the PR to avoid any potential conflicts.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I was hoping I was saving you the trouble of updating that, sorry!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't know you could do it when merging, I used to do a separate commit after the merge. How do you do it?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I usually just do it via github web iface. AFAIK others do as well.


IMPROVEMENTS:

Expand Down
2 changes: 2 additions & 0 deletions builtin/providers/google/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ func Provider() terraform.ResourceProvider {
"google_compute_project_metadata": resourceComputeProjectMetadata(),
"google_compute_route": resourceComputeRoute(),
"google_compute_target_pool": resourceComputeTargetPool(),
"google_compute_vpn_gateway": resourceComputeVpnGateway(),
"google_compute_vpn_tunnel": resourceComputeVpnTunnel(),
"google_container_cluster": resourceContainerCluster(),
"google_dns_managed_zone": resourceDnsManagedZone(),
"google_dns_record_set": resourceDnsRecordSet(),
Expand Down
29 changes: 23 additions & 6 deletions builtin/providers/google/resource_compute_address.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,32 @@ func resourceComputeAddress() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},

"region": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
},
}
}

func getOptionalRegion(d *schema.ResourceData, config *Config) string {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wish we could just use this as DefaultFunc, but because of that dependency (*Config), it is effectively impossible. :-/

if res, ok := d.GetOk("region"); !ok {
return config.Region
} else {
return res.(string)
}
}

func resourceComputeAddressCreate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
region := getOptionalRegion(d, config)

// Build the address parameter
addr := &compute.Address{Name: d.Get("name").(string)}
log.Printf("[DEBUG] Address insert request: %#v", addr)
op, err := config.clientCompute.Addresses.Insert(
config.Project, config.Region, addr).Do()
config.Project, region, addr).Do()
if err != nil {
return fmt.Errorf("Error creating address: %s", err)
}
Expand All @@ -56,7 +70,7 @@ func resourceComputeAddressCreate(d *schema.ResourceData, meta interface{}) erro
Service: config.clientCompute,
Op: op,
Project: config.Project,
Region: config.Region,
Region: region,
Type: OperationWaitRegion,
}
state := w.Conf()
Expand All @@ -81,8 +95,10 @@ func resourceComputeAddressCreate(d *schema.ResourceData, meta interface{}) erro
func resourceComputeAddressRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)

region := getOptionalRegion(d, config)

addr, err := config.clientCompute.Addresses.Get(
config.Project, config.Region, d.Id()).Do()
config.Project, region, d.Id()).Do()
if err != nil {
if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 {
// The resource doesn't exist anymore
Expand All @@ -103,10 +119,11 @@ func resourceComputeAddressRead(d *schema.ResourceData, meta interface{}) error
func resourceComputeAddressDelete(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)

region := getOptionalRegion(d, config)
// Delete the address
log.Printf("[DEBUG] address delete request")
op, err := config.clientCompute.Addresses.Delete(
config.Project, config.Region, d.Id()).Do()
config.Project, region, d.Id()).Do()
if err != nil {
return fmt.Errorf("Error deleting address: %s", err)
}
Expand All @@ -116,7 +133,7 @@ func resourceComputeAddressDelete(d *schema.ResourceData, meta interface{}) erro
Service: config.clientCompute,
Op: op,
Project: config.Project,
Region: config.Region,
Region: region,
Type: OperationWaitRegion,
}
state := w.Conf()
Expand Down
28 changes: 21 additions & 7 deletions builtin/providers/google/resource_compute_forwarding_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ func resourceComputeForwardingRule() *schema.Resource {
ForceNew: true,
},

"region": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},

"self_link": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Expand All @@ -67,6 +73,8 @@ func resourceComputeForwardingRule() *schema.Resource {
func resourceComputeForwardingRuleCreate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)

region := getOptionalRegion(d, config)

frule := &compute.ForwardingRule{
IPAddress: d.Get("ip_address").(string),
IPProtocol: d.Get("ip_protocol").(string),
Expand All @@ -78,7 +86,7 @@ func resourceComputeForwardingRuleCreate(d *schema.ResourceData, meta interface{

log.Printf("[DEBUG] ForwardingRule insert request: %#v", frule)
op, err := config.clientCompute.ForwardingRules.Insert(
config.Project, config.Region, frule).Do()
config.Project, region, frule).Do()
if err != nil {
return fmt.Errorf("Error creating ForwardingRule: %s", err)
}
Expand All @@ -90,7 +98,7 @@ func resourceComputeForwardingRuleCreate(d *schema.ResourceData, meta interface{
w := &OperationWaiter{
Service: config.clientCompute,
Op: op,
Region: config.Region,
Region: region,
Project: config.Project,
Type: OperationWaitRegion,
}
Expand All @@ -116,13 +124,15 @@ func resourceComputeForwardingRuleCreate(d *schema.ResourceData, meta interface{
func resourceComputeForwardingRuleUpdate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)

region := getOptionalRegion(d, config)

d.Partial(true)

if d.HasChange("target") {
target_name := d.Get("target").(string)
target_ref := &compute.TargetReference{Target: target_name}
op, err := config.clientCompute.ForwardingRules.SetTarget(
config.Project, config.Region, d.Id(), target_ref).Do()
config.Project, region, d.Id(), target_ref).Do()
if err != nil {
return fmt.Errorf("Error updating target: %s", err)
}
Expand All @@ -131,7 +141,7 @@ func resourceComputeForwardingRuleUpdate(d *schema.ResourceData, meta interface{
w := &OperationWaiter{
Service: config.clientCompute,
Op: op,
Region: config.Region,
Region: region,
Project: config.Project,
Type: OperationWaitRegion,
}
Expand Down Expand Up @@ -161,8 +171,10 @@ func resourceComputeForwardingRuleUpdate(d *schema.ResourceData, meta interface{
func resourceComputeForwardingRuleRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)

region := getOptionalRegion(d, config)

frule, err := config.clientCompute.ForwardingRules.Get(
config.Project, config.Region, d.Id()).Do()
config.Project, region, d.Id()).Do()
if err != nil {
if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 {
// The resource doesn't exist anymore
Expand All @@ -184,10 +196,12 @@ func resourceComputeForwardingRuleRead(d *schema.ResourceData, meta interface{})
func resourceComputeForwardingRuleDelete(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)

region := getOptionalRegion(d, config)

// Delete the ForwardingRule
log.Printf("[DEBUG] ForwardingRule delete request")
op, err := config.clientCompute.ForwardingRules.Delete(
config.Project, config.Region, d.Id()).Do()
config.Project, region, d.Id()).Do()
if err != nil {
return fmt.Errorf("Error deleting ForwardingRule: %s", err)
}
Expand All @@ -196,7 +210,7 @@ func resourceComputeForwardingRuleDelete(d *schema.ResourceData, meta interface{
w := &OperationWaiter{
Service: config.clientCompute,
Op: op,
Region: config.Region,
Region: region,
Project: config.Project,
Type: OperationWaitRegion,
}
Expand Down
31 changes: 21 additions & 10 deletions builtin/providers/google/resource_compute_route.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ func resourceComputeRoute() *schema.Resource {
ForceNew: true,
},

"next_hop_vpn_tunnel": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},

"priority": &schema.Schema{
Type: schema.TypeInt,
Required: true,
Expand Down Expand Up @@ -101,13 +107,17 @@ func resourceComputeRouteCreate(d *schema.ResourceData, meta interface{}) error
}

// Next hop data
var nextHopInstance, nextHopIp, nextHopNetwork, nextHopGateway string
var nextHopInstance, nextHopIp, nextHopNetwork, nextHopGateway,
nextHopVpnTunnel string
if v, ok := d.GetOk("next_hop_ip"); ok {
nextHopIp = v.(string)
}
if v, ok := d.GetOk("next_hop_gateway"); ok {
nextHopGateway = v.(string)
}
if v, ok := d.GetOk("next_hop_vpn_tunnel"); ok {
nextHopVpnTunnel = v.(string)
}
if v, ok := d.GetOk("next_hop_instance"); ok {
nextInstance, err := config.clientCompute.Instances.Get(
config.Project,
Expand Down Expand Up @@ -140,15 +150,16 @@ func resourceComputeRouteCreate(d *schema.ResourceData, meta interface{}) error

// Build the route parameter
route := &compute.Route{
Name: d.Get("name").(string),
DestRange: d.Get("dest_range").(string),
Network: network.SelfLink,
NextHopInstance: nextHopInstance,
NextHopIp: nextHopIp,
NextHopNetwork: nextHopNetwork,
NextHopGateway: nextHopGateway,
Priority: int64(d.Get("priority").(int)),
Tags: tags,
Name: d.Get("name").(string),
DestRange: d.Get("dest_range").(string),
Network: network.SelfLink,
NextHopInstance: nextHopInstance,
NextHopVpnTunnel: nextHopVpnTunnel,
NextHopIp: nextHopIp,
NextHopNetwork: nextHopNetwork,
NextHopGateway: nextHopGateway,
Priority: int64(d.Get("priority").(int)),
Tags: tags,
}
log.Printf("[DEBUG] Route insert request: %#v", route)
op, err := config.clientCompute.Routes.Insert(
Expand Down
120 changes: 120 additions & 0 deletions builtin/providers/google/resource_compute_vpn_gateway.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package google

import (
"fmt"

"github.com/hashicorp/terraform/helper/schema"

"google.golang.org/api/compute/v1"
)

func resourceComputeVpnGateway() *schema.Resource {
return &schema.Resource{
// Unfortunately, the VPNGatewayService does not support update
// operations. This is why everything is marked forcenew
Create: resourceComputeVpnGatewayCreate,
Read: resourceComputeVpnGatewayRead,
Delete: resourceComputeVpnGatewayDelete,

Schema: map[string]*schema.Schema{
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"description": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"network": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"region": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"self_link": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
},
}
}

func resourceComputeVpnGatewayCreate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)

name := d.Get("name").(string)
network := d.Get("network").(string)
region := getOptionalRegion(d, config)
project := config.Project

vpnGatewaysService := compute.NewTargetVpnGatewaysService(config.clientCompute)

vpnGateway := &compute.TargetVpnGateway{
Name: name,
Network: network,
}

if v, ok := d.GetOk("description"); ok {
vpnGateway.Description = v.(string)
}

op, err := vpnGatewaysService.Insert(project, region, vpnGateway).Do()
if err != nil {
return fmt.Errorf("Error Inserting VPN Gateway %s into network %s: %s", name, network, err)
}

err = resourceOperationWaitRegion(config, op, region, "Inserting VPN Gateway")
if err != nil {
return fmt.Errorf("Error Waiting to Insert VPN Gateway %s into network %s: %s", name, network, err)
}

return resourceComputeVpnGatewayRead(d, meta)
}

func resourceComputeVpnGatewayRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)

name := d.Get("name").(string)
region := d.Get("region").(string)
project := config.Project

vpnGatewaysService := compute.NewTargetVpnGatewaysService(config.clientCompute)
vpnGateway, err := vpnGatewaysService.Get(project, region, name).Do()

if err != nil {
return fmt.Errorf("Error Reading VPN Gateway %s: %s", name, err)
}

d.Set("self_link", vpnGateway.SelfLink)
d.SetId(name)

return nil
}

func resourceComputeVpnGatewayDelete(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)

name := d.Get("name").(string)
region := d.Get("region").(string)
project := config.Project

vpnGatewaysService := compute.NewTargetVpnGatewaysService(config.clientCompute)

op, err := vpnGatewaysService.Delete(project, region, name).Do()
if err != nil {
return fmt.Errorf("Error Reading VPN Gateway %s: %s", name, err)
}

err = resourceOperationWaitRegion(config, op, region, "Deleting VPN Gateway")
if err != nil {
return fmt.Errorf("Error Waiting to Delete VPN Gateway %s: %s", name, err)
}

return nil
}
Loading