diff --git a/google/provider_compute_gen.go b/google/provider_compute_gen.go index 8282f807b6e..4dea1c1d913 100644 --- a/google/provider_compute_gen.go +++ b/google/provider_compute_gen.go @@ -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(), } diff --git a/google/resource_compute_backend_bucket.go b/google/resource_compute_backend_bucket.go index a6b261bafff..19b18e7a118 100644 --- a/google/resource_compute_backend_bucket.go +++ b/google/resource_compute_backend_bucket.go @@ -140,7 +140,7 @@ func resourceComputeBackendBucketCreate(d *schema.ResourceData, meta interface{} if waitErr != nil { // The resource didn't actually create d.SetId("") - return waitErr + return fmt.Errorf("Error waiting to create BackendBucket: %s", waitErr) } return resourceComputeBackendBucketRead(d, meta) diff --git a/google/resource_compute_global_address.go b/google/resource_compute_global_address.go index 15d35d93449..5f7d404581e 100644 --- a/google/resource_compute_global_address.go +++ b/google/resource_compute_global_address.go @@ -136,7 +136,7 @@ func resourceComputeGlobalAddressCreate(d *schema.ResourceData, meta interface{} if waitErr != nil { // The resource didn't actually create d.SetId("") - return waitErr + return fmt.Errorf("Error waiting to create GlobalAddress: %s", waitErr) } return resourceComputeGlobalAddressRead(d, meta) diff --git a/google/resource_compute_http_health_check.go b/google/resource_compute_http_health_check.go index b0f859f45b9..83cb94c76c2 100644 --- a/google/resource_compute_http_health_check.go +++ b/google/resource_compute_http_health_check.go @@ -191,7 +191,7 @@ func resourceComputeHttpHealthCheckCreate(d *schema.ResourceData, meta interface if waitErr != nil { // The resource didn't actually create d.SetId("") - return waitErr + return fmt.Errorf("Error waiting to create HttpHealthCheck: %s", waitErr) } return resourceComputeHttpHealthCheckRead(d, meta) diff --git a/google/resource_compute_https_health_check.go b/google/resource_compute_https_health_check.go index 2b2ffd893dd..1f82451ded7 100644 --- a/google/resource_compute_https_health_check.go +++ b/google/resource_compute_https_health_check.go @@ -191,7 +191,7 @@ func resourceComputeHttpsHealthCheckCreate(d *schema.ResourceData, meta interfac if waitErr != nil { // The resource didn't actually create d.SetId("") - return waitErr + return fmt.Errorf("Error waiting to create HttpsHealthCheck: %s", waitErr) } return resourceComputeHttpsHealthCheckRead(d, meta) diff --git a/google/resource_compute_target_http_proxy.go b/google/resource_compute_target_http_proxy.go index aded0663232..07ab1bc7f66 100644 --- a/google/resource_compute_target_http_proxy.go +++ b/google/resource_compute_target_http_proxy.go @@ -137,7 +137,7 @@ func resourceComputeTargetHttpProxyCreate(d *schema.ResourceData, meta interface if waitErr != nil { // The resource didn't actually create d.SetId("") - return waitErr + return fmt.Errorf("Error waiting to create TargetHttpProxy: %s", waitErr) } return resourceComputeTargetHttpProxyRead(d, meta) diff --git a/google/resource_compute_target_ssl_proxy.go b/google/resource_compute_target_ssl_proxy.go index 46b52742914..7c1fb15bc9f 100644 --- a/google/resource_compute_target_ssl_proxy.go +++ b/google/resource_compute_target_ssl_proxy.go @@ -163,7 +163,7 @@ func resourceComputeTargetSslProxyCreate(d *schema.ResourceData, meta interface{ if waitErr != nil { // The resource didn't actually create d.SetId("") - return waitErr + return fmt.Errorf("Error waiting to create TargetSslProxy: %s", waitErr) } return resourceComputeTargetSslProxyRead(d, meta) diff --git a/google/resource_compute_vpn_gateway.go b/google/resource_compute_vpn_gateway.go index e9b2e59da55..d5ecfe02537 100644 --- a/google/resource_compute_vpn_gateway.go +++ b/google/resource_compute_vpn_gateway.go @@ -1,56 +1,79 @@ +// ---------------------------------------------------------------------------- +// +// *** 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, + StateFunc: NameFromSelfLinkStateFunc, + }, + "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, }, @@ -60,42 +83,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 fmt.Errorf("Error waiting to create VpnGateway: %s", waitErr) } return resourceComputeVpnGatewayRead(d, meta) @@ -104,32 +153,41 @@ 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 } @@ -137,29 +195,93 @@ func resourceComputeVpnGatewayRead(d *schema.ResourceData, meta interface{}) err 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[^/]+)/regions/(?P[^/]+)/targetVpnGateways/(?P[^/]+)", "(?P[^/]+)/(?P[^/]+)/(?P[^/]+)", "(?P[^/]+)"}, 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 +} diff --git a/website/docs/r/compute_vpn_gateway.html.markdown b/website/docs/r/compute_vpn_gateway.html.markdown index 18a6319f064..8e6cc757d4a 100644 --- a/website/docs/r/compute_vpn_gateway.html.markdown +++ b/website/docs/r/compute_vpn_gateway.html.markdown @@ -1,16 +1,32 @@ --- +# ---------------------------------------------------------------------------- +# +# *** 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. +# +# ---------------------------------------------------------------------------- layout: "google" page_title: "Google: google_compute_vpn_gateway" sidebar_current: "docs-google-compute-vpn-gateway" description: |- - Manages a VPN Gateway in the GCE network + Represents a VPN gateway running in GCP. --- # google\_compute\_vpn\_gateway -Manages a VPN Gateway in the GCE network. For more info, read the -[documentation](https://cloud.google.com/compute/docs/vpn). +Represents a VPN gateway running in GCP. This virtual device is managed +by Google, but used only by you. +To get more information about VpnGateway, see: + +* [API documentation](https://cloud.google.com/compute/docs/reference/rest/v1/targetVpnGateways) ## Example Usage @@ -86,27 +102,55 @@ resource "google_compute_route" "route1" { The following arguments are supported: -* `name` - (Required) A unique name for the resource, required by GCE. Changing - this forces a new resource to be created. +* `name` - + (Required) + Name of the resource. Provided by the client when the resource is +created. The name must be 1-63 characters long, and comply with +RFC1035. Specifically, the name must be 1-63 characters long and +match the regular expression `[a-z]([-a-z0-9]*[a-z0-9])?` which means +the first character must be a lowercase letter, and all following +characters must be a dash, lowercase letter, or digit, except the last +character, which cannot be a dash. +* `network` - + (Required) + A reference to Network resource -* `network` - (Required) The name or resource link to the network this VPN gateway - is accepting traffic for. Changing this forces a new resource to be created. - - - -* `description` - (Optional) A description of the resource. - Changing this forces a new resource to be created. - -* `project` - (Optional) The ID of the project in which the resource belongs. If it - is not provided, the provider project is used. +* `description` - + (Optional) + An optional description of this resource. +* `region` - + (Optional) + A reference to Region resource +* `project` (Optional) The ID of the project in which the resource belongs. + If it is not provided, the provider project is used. -* `region` - (Optional) The region this gateway should sit in. If not specified, - the project region will be used. Changing this forces a new resource to be - created. ## Attributes Reference -In addition to the arguments listed above, the following computed attributes are -exported: +In addition to the arguments listed above, the following computed attributes are exported: +* `creation_timestamp` - + Creation timestamp in RFC3339 text format. * `self_link` - The URI of the created resource. + + +## Timeouts + +This resource provides the following +[Timeouts](/docs/configuration/resources.html#timeouts) configuration options: + +- `create` - Default is 4 minutes. +- `delete` - Default is 4 minutes. + +## Import + +VpnGateway can be imported using any of these accepted formats: + +``` +$ terraform import google_compute_vpn_gateway.default projects/{{project}}/regions/{{region}}/targetVpnGateways/{{name}} +$ terraform import google_compute_vpn_gateway.default {{project}}/{{region}}/{{name}} +$ terraform import google_compute_vpn_gateway.default {{name}} +```