Skip to content

Commit

Permalink
Generate google_vpn_gateway using Magic Module
Browse files Browse the repository at this point in the history
  • Loading branch information
rosbo authored and modular-magician committed May 1, 2018
1 parent b5ec48c commit af7858c
Show file tree
Hide file tree
Showing 3 changed files with 241 additions and 75 deletions.
1 change: 1 addition & 0 deletions google/provider_compute_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ var GeneratedComputeResourcesMap = map[string]*schema.Resource{
"google_compute_https_health_check": resourceComputeHttpsHealthCheck(),
"google_compute_target_http_proxy": resourceComputeTargetHttpProxy(),
"google_compute_target_ssl_proxy": resourceComputeTargetSslProxy(),
"google_compute_vpn_gateway": resourceComputeVpnGateway(),
}
237 changes: 179 additions & 58 deletions google/resource_compute_vpn_gateway.go
Original file line number Diff line number Diff line change
@@ -1,56 +1,78 @@
// ----------------------------------------------------------------------------
//
// *** AUTO GENERATED CODE *** AUTO GENERATED CODE ***
//
// ----------------------------------------------------------------------------
//
// This file is automatically generated by Magic Modules and manual
// changes will be clobbered when the file is regenerated.
//
// Please read more about how to change this file in
// .github/CONTRIBUTING.md.
//
// ----------------------------------------------------------------------------

package google

import (
"fmt"
"log"
"time"

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

"google.golang.org/api/compute/v1"
compute "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,

Importer: &schema.ResourceImporter{
State: resourceComputeVpnGatewayImport,
},

Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(240 * time.Second),
Delete: schema.DefaultTimeout(240 * time.Second),
},

Schema: map[string]*schema.Schema{
"name": &schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},

"network": &schema.Schema{
"network": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
DiffSuppressFunc: compareSelfLinkOrResourceName,
},

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

"project": &schema.Schema{
"region": {
Type: schema.TypeString,
Computed: true,
Optional: true,
ForceNew: true,
DiffSuppressFunc: compareSelfLinkOrResourceName,
},
"creation_timestamp": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},

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

"self_link": &schema.Schema{
"self_link": {
Type: schema.TypeString,
Computed: true,
},
Expand All @@ -60,42 +82,68 @@ func resourceComputeVpnGateway() *schema.Resource {

func resourceComputeVpnGatewayCreate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
network, err := ParseNetworkFieldValue(d.Get("network").(string), d, config)

project, err := getProject(d, config)
if err != nil {
return err
}

region, err := getRegion(d, config)
descriptionProp, err := expandComputeVpnGatewayDescription(d.Get("description"), d, config)
if err != nil {
return err
}

project, err := getProject(d, config)
nameProp, err := expandComputeVpnGatewayName(d.Get("name"), d, config)
if err != nil {
return err
}
networkProp, err := expandComputeVpnGatewayNetwork(d.Get("network"), d, config)
if err != nil {
return err
}
regionProp, err := expandComputeVpnGatewayRegion(d.Get("region"), d, config)
if err != nil {
return err
}

name := d.Get("name").(string)

vpnGatewaysService := compute.NewTargetVpnGatewaysService(config.clientCompute)
obj := map[string]interface{}{
"description": descriptionProp,
"name": nameProp,
"network": networkProp,
"region": regionProp,
}

vpnGateway := &compute.TargetVpnGateway{
Name: name,
Network: network.RelativeLink(),
url, err := replaceVars(d, config, "https://www.googleapis.com/compute/v1/projects/{{project}}/regions/{{region}}/targetVpnGateways")
if err != nil {
return err
}

if v, ok := d.GetOk("description"); ok {
vpnGateway.Description = v.(string)
log.Printf("[DEBUG] Creating new VpnGateway: %#v", obj)
res, err := Post(config, url, obj)
if err != nil {
return fmt.Errorf("Error creating VpnGateway: %s", err)
}

op, err := vpnGatewaysService.Insert(project, region, vpnGateway).Do()
// Store the ID now
id, err := replaceVars(d, config, "{{name}}")
if err != nil {
return fmt.Errorf("Error Inserting VPN Gateway %s into network %s: %s", name, network.Name, err)
return fmt.Errorf("Error constructing id: %s", err)
}
d.SetId(id)

err = computeOperationWait(config.clientCompute, op, project, "Inserting VPN Gateway")
op := &compute.Operation{}
err = Convert(res, op)
if err != nil {
return fmt.Errorf("Error Waiting to Insert VPN Gateway %s into network %s: %s", name, network.Name, err)
return err
}

waitErr := computeOperationWaitTime(
config.clientCompute, op, project, "Creating VpnGateway",
int(d.Timeout(schema.TimeoutCreate).Minutes()))

if waitErr != nil {
// The resource didn't actually create
d.SetId("")
return waitErr
}

return resourceComputeVpnGatewayRead(d, meta)
Expand All @@ -104,62 +152,135 @@ func resourceComputeVpnGatewayCreate(d *schema.ResourceData, meta interface{}) e
func resourceComputeVpnGatewayRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)

region, err := getRegion(d, config)
project, err := getProject(d, config)
if err != nil {
return err
}

project, err := getProject(d, config)
url, err := replaceVars(d, config, "https://www.googleapis.com/compute/v1/projects/{{project}}/regions/{{region}}/targetVpnGateways/{{name}}")
if err != nil {
return err
}

name := d.Get("name").(string)

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

res, err := Get(config, url)
if err != nil {
return handleNotFoundError(err, d, fmt.Sprintf("VPN Gateway %q", d.Get("name").(string)))
return handleNotFoundError(err, d, fmt.Sprintf("ComputeVpnGateway %q", d.Id()))
}
if err := d.Set("creation_timestamp", flattenComputeVpnGatewayCreationTimestamp(res["creationTimestamp"])); err != nil {
return fmt.Errorf("Error reading VpnGateway: %s", err)
}
if err := d.Set("description", flattenComputeVpnGatewayDescription(res["description"])); err != nil {
return fmt.Errorf("Error reading VpnGateway: %s", err)
}
if err := d.Set("name", flattenComputeVpnGatewayName(res["name"])); err != nil {
return fmt.Errorf("Error reading VpnGateway: %s", err)
}
if err := d.Set("network", flattenComputeVpnGatewayNetwork(res["network"])); err != nil {
return fmt.Errorf("Error reading VpnGateway: %s", err)
}
if err := d.Set("region", flattenComputeVpnGatewayRegion(res["region"])); err != nil {
return fmt.Errorf("Error reading VpnGateway: %s", err)
}
if err := d.Set("self_link", res["selfLink"]); err != nil {
return fmt.Errorf("Error reading VpnGateway: %s", err)
}
if err := d.Set("project", project); err != nil {
return fmt.Errorf("Error reading VpnGateway: %s", err)
}

d.Set("name", vpnGateway.Name)
d.Set("description", vpnGateway.Description)
d.Set("network", vpnGateway.Network)
d.Set("project", project)
d.Set("region", region)
d.Set("self_link", vpnGateway.SelfLink)
d.SetId(name)

return nil
}

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

region, err := getRegion(d, config)
project, err := getProject(d, config)
if err != nil {
return err
}

project, err := getProject(d, config)
url, err := replaceVars(d, config, "https://www.googleapis.com/compute/v1/projects/{{project}}/regions/{{region}}/targetVpnGateways/{{name}}")
if err != nil {
return err
}

name := d.Get("name").(string)

vpnGatewaysService := compute.NewTargetVpnGatewaysService(config.clientCompute)
log.Printf("[DEBUG] Deleting VpnGateway %q", d.Id())
res, err := Delete(config, url)
if err != nil {
return fmt.Errorf("Error deleting VpnGateway %q: %s", d.Id(), err)
}

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

err = computeOperationWait(config.clientCompute, op, project, "Deleting VPN Gateway")
err = computeOperationWaitTime(
config.clientCompute, op, project, "Deleting VpnGateway",
int(d.Timeout(schema.TimeoutDelete).Minutes()))

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

return nil
}

func resourceComputeVpnGatewayImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
config := meta.(*Config)
parseImportId([]string{"projects/(?P<project>[^/]+)/regions/(?P<region>[^/]+)/targetVpnGateways/(?P<name>[^/]+)", "(?P<project>[^/]+)/(?P<region>[^/]+)/(?P<name>[^/]+)", "(?P<name>[^/]+)"}, d, config)

// Replace import id for the resource id
id, err := replaceVars(d, config, "{{name}}")
if err != nil {
return nil, fmt.Errorf("Error constructing id: %s", err)
}
d.SetId(id)

return []*schema.ResourceData{d}, nil
}

func flattenComputeVpnGatewayCreationTimestamp(v interface{}) interface{} {
return v
}

func flattenComputeVpnGatewayDescription(v interface{}) interface{} {
return v
}

func flattenComputeVpnGatewayName(v interface{}) interface{} {
return v
}

func flattenComputeVpnGatewayNetwork(v interface{}) interface{} {
return v
}

func flattenComputeVpnGatewayRegion(v interface{}) interface{} {
return v
}

func expandComputeVpnGatewayDescription(v interface{}, d *schema.ResourceData, config *Config) (interface{}, error) {
return v, nil
}

func expandComputeVpnGatewayName(v interface{}, d *schema.ResourceData, config *Config) (interface{}, error) {
return v, nil
}

func expandComputeVpnGatewayNetwork(v interface{}, d *schema.ResourceData, config *Config) (interface{}, error) {
f, err := parseGlobalFieldValue("networks", v.(string), "project", d, config, true)
if err != nil {
return nil, fmt.Errorf("Invalid value for network: %s", err)
}
return f.RelativeLink(), nil
}

func expandComputeVpnGatewayRegion(v interface{}, d *schema.ResourceData, config *Config) (interface{}, error) {
f, err := parseGlobalFieldValue("regions", v.(string), "project", d, config, true)
if err != nil {
return nil, fmt.Errorf("Invalid value for region: %s", err)
}
return f.RelativeLink(), nil
}
Loading

0 comments on commit af7858c

Please sign in to comment.