From 849307251fc6d274004b4ec4b167d3320a05681f Mon Sep 17 00:00:00 2001 From: Vincent Roseberry Date: Thu, 26 Apr 2018 18:27:56 +0000 Subject: [PATCH] Release generated GlobalAddress --- google/resource_compute_global_address.go | 192 ++++++++++++++---- .../resource_compute_global_address_test.go | 2 + google/resource_compute_target_ssl_proxy.go | 8 +- .../r/compute_global_address.html.markdown | 76 +++++-- 4 files changed, 224 insertions(+), 54 deletions(-) diff --git a/google/resource_compute_global_address.go b/google/resource_compute_global_address.go index 2d2edb22a3d..24545a7e6b2 100644 --- a/google/resource_compute_global_address.go +++ b/google/resource_compute_global_address.go @@ -1,13 +1,27 @@ +// ---------------------------------------------------------------------------- +// +// *** 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" "github.com/hashicorp/terraform/helper/validation" - - "google.golang.org/api/compute/v1" + compute "google.golang.org/api/compute/v1" ) func resourceComputeGlobalAddress() *schema.Resource { @@ -15,39 +29,46 @@ func resourceComputeGlobalAddress() *schema.Resource { Create: resourceComputeGlobalAddressCreate, Read: resourceComputeGlobalAddressRead, Delete: resourceComputeGlobalAddressDelete, + Importer: &schema.ResourceImporter{ - State: schema.ImportStatePassthrough, + State: resourceComputeGlobalAddressImport, + }, + + 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, }, - - "ip_version": &schema.Schema{ + "description": { + Type: schema.TypeString, + Optional: true, + ForceNew: true, + }, + "ip_version": { Type: schema.TypeString, Optional: true, ForceNew: true, - ValidateFunc: validation.StringInSlice([]string{"IPV4", "IPV6", ""}, false), + ValidateFunc: validation.StringInSlice([]string{"IPV4", "IPV6"}, false), }, - - "project": &schema.Schema{ + "address": { Type: schema.TypeString, - Optional: true, Computed: true, - ForceNew: true, }, - - "address": &schema.Schema{ + "creation_timestamp": { Type: schema.TypeString, Computed: true, }, - - "self_link": &schema.Schema{ + "project": { Type: schema.TypeString, + Optional: true, Computed: true, + ForceNew: true, }, }, } @@ -61,25 +82,59 @@ func resourceComputeGlobalAddressCreate(d *schema.ResourceData, meta interface{} return err } - // Build the address parameter - addr := &compute.Address{ - Name: d.Get("name").(string), - IpVersion: d.Get("ip_version").(string), + descriptionProp, err := expandComputeGlobalAddressDescription(d.Get("description"), d, config) + if err != nil { + return err + } + nameProp, err := expandComputeGlobalAddressName(d.Get("name"), d, config) + if err != nil { + return err + } + ipVersionProp, err := expandComputeGlobalAddressIpVersion(d.Get("ip_version"), d, config) + if err != nil { + return err + } + + obj := map[string]interface{}{ + "description": descriptionProp, + "name": nameProp, + "ipVersion": ipVersionProp, } - op, err := config.clientCompute.GlobalAddresses.Insert(project, addr).Do() + url, err := replaceVars(d, config, "https://www.googleapis.com/compute/v1/projects/{{project}}/global/addresses") if err != nil { - return fmt.Errorf("Error creating address: %s", err) + return err } - // It probably maybe worked, so store the ID now - d.SetId(addr.Name) + log.Printf("[DEBUG] Creating new GlobalAddress: %#v", obj) + res, err := Post(config, url, obj) + if err != nil { + return fmt.Errorf("Error creating GlobalAddress: %s", err) + } - err = computeSharedOperationWait(config.clientCompute, op, project, "Creating Global Address") + // Store the ID now + id, err := replaceVars(d, config, "{{name}}") + if err != nil { + return fmt.Errorf("Error constructing id: %s", err) + } + d.SetId(id) + + op := &compute.Operation{} + err = Convert(res, op) if err != nil { return err } + waitErr := computeOperationWaitTime( + config.clientCompute, op, project, "Creating GlobalAddress", + int(d.Timeout(schema.TimeoutCreate).Minutes())) + + if waitErr != nil { + // The resource didn't actually create + d.SetId("") + return waitErr + } + return resourceComputeGlobalAddressRead(d, meta) } @@ -91,16 +146,23 @@ func resourceComputeGlobalAddressRead(d *schema.ResourceData, meta interface{}) return err } - addr, err := config.clientCompute.GlobalAddresses.Get(project, d.Id()).Do() + url, err := replaceVars(d, config, "https://www.googleapis.com/compute/v1/projects/{{project}}/global/addresses/{{name}}") if err != nil { - return handleNotFoundError(err, d, fmt.Sprintf("Global Address %q", d.Get("name").(string))) + return err } - d.Set("name", addr.Name) - d.Set("ip_version", addr.IpVersion) - d.Set("address", addr.Address) + res, err := Get(config, url) + if err != nil { + return handleNotFoundError(err, d, fmt.Sprintf("ComputeGlobalAddress %q", d.Id())) + } + + d.Set("address", flattenComputeGlobalAddressAddress(res["address"])) + d.Set("creation_timestamp", flattenComputeGlobalAddressCreationTimestamp(res["creationTimestamp"])) + d.Set("description", flattenComputeGlobalAddressDescription(res["description"])) + d.Set("name", flattenComputeGlobalAddressName(res["name"])) + d.Set("ip_version", flattenComputeGlobalAddressIpVersion(res["ipVersion"])) + d.Set("self_link", res["selfLink"]) d.Set("project", project) - d.Set("self_link", ConvertSelfLinkToV1(addr.SelfLink)) return nil } @@ -113,18 +175,76 @@ func resourceComputeGlobalAddressDelete(d *schema.ResourceData, meta interface{} return err } - // Delete the address - log.Printf("[DEBUG] address delete request") - op, err := config.clientCompute.GlobalAddresses.Delete(project, d.Id()).Do() + url, err := replaceVars(d, config, "https://www.googleapis.com/compute/v1/projects/{{project}}/global/addresses/{{name}}") + if err != nil { + return err + } + + log.Printf("[DEBUG] Deleting GlobalAddress %q", d.Id()) + res, err := Delete(config, url) + if err != nil { + return fmt.Errorf("Error deleting GlobalAddress %q: %s", d.Id(), err) + } + + op := &compute.Operation{} + err = Convert(res, op) if err != nil { - return fmt.Errorf("Error deleting address: %s", err) + return err } - err = computeSharedOperationWait(config.clientCompute, op, project, "Deleting Global Address") + err = computeOperationWaitTime( + config.clientCompute, op, project, "Deleting GlobalAddress", + int(d.Timeout(schema.TimeoutDelete).Minutes())) + if err != nil { return err } - d.SetId("") return nil } + +func resourceComputeGlobalAddressImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { + config := meta.(*Config) + parseImportId([]string{"projects/(?P[^/]+)/global/addresses/(?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 flattenComputeGlobalAddressAddress(v interface{}) interface{} { + return v +} + +func flattenComputeGlobalAddressCreationTimestamp(v interface{}) interface{} { + return v +} + +func flattenComputeGlobalAddressDescription(v interface{}) interface{} { + return v +} + +func flattenComputeGlobalAddressName(v interface{}) interface{} { + return v +} + +func flattenComputeGlobalAddressIpVersion(v interface{}) interface{} { + return v +} + +func expandComputeGlobalAddressDescription(v interface{}, d *schema.ResourceData, config *Config) (interface{}, error) { + return v, nil +} + +func expandComputeGlobalAddressName(v interface{}, d *schema.ResourceData, config *Config) (interface{}, error) { + return v, nil +} + +func expandComputeGlobalAddressIpVersion(v interface{}, d *schema.ResourceData, config *Config) (interface{}, error) { + return v, nil +} diff --git a/google/resource_compute_global_address_test.go b/google/resource_compute_global_address_test.go index d5e5290da4d..c93d724c3ca 100644 --- a/google/resource_compute_global_address_test.go +++ b/google/resource_compute_global_address_test.go @@ -144,6 +144,7 @@ func testAccComputeGlobalAddress_basic() string { return fmt.Sprintf(` resource "google_compute_global_address" "foobar" { name = "address-test-%s" + description = "Created for Terraform acceptance testing" }`, acctest.RandString(10)) } @@ -151,6 +152,7 @@ func testAccComputeGlobalAddress_ipv6() string { return fmt.Sprintf(` resource "google_compute_global_address" "foobar" { name = "address-test-%s" + description = "Created for Terraform acceptance testing" ip_version = "IPV6" }`, acctest.RandString(10)) } diff --git a/google/resource_compute_target_ssl_proxy.go b/google/resource_compute_target_ssl_proxy.go index 9b3fa31e56d..7b2f561df13 100644 --- a/google/resource_compute_target_ssl_proxy.go +++ b/google/resource_compute_target_ssl_proxy.go @@ -21,6 +21,7 @@ import ( "time" "github.com/hashicorp/terraform/helper/schema" + "github.com/hashicorp/terraform/helper/validation" compute "google.golang.org/api/compute/v1" ) @@ -67,9 +68,10 @@ func resourceComputeTargetSslProxy() *schema.Resource { ForceNew: true, }, "proxy_header": { - Type: schema.TypeString, - Optional: true, - Default: "NONE", + Type: schema.TypeString, + Optional: true, + ValidateFunc: validation.StringInSlice([]string{"NONE", "PROXY_V1"}, false), + Default: "NONE", }, "creation_timestamp": { Type: schema.TypeString, diff --git a/website/docs/r/compute_global_address.html.markdown b/website/docs/r/compute_global_address.html.markdown index 6a327cdb8db..aec86cdae66 100644 --- a/website/docs/r/compute_global_address.html.markdown +++ b/website/docs/r/compute_global_address.html.markdown @@ -1,17 +1,37 @@ + + --- layout: "google" page_title: "Google: google_compute_global_address" sidebar_current: "docs-google-compute-global-address" description: |- - Creates a static global IP address resource for a Google Compute Engine project. + Represents a Global Address resource. --- # google\_compute\_global\_address -Creates a static IP address resource global to a Google Compute Engine project. For more information see -[the official documentation](https://cloud.google.com/compute/docs/instances-and-network) and -[API](https://cloud.google.com/compute/docs/reference/latest/globalAddresses). +Represents a Global Address resource. Global addresses are used for +HTTP(S) load balancing. +To get more information about GlobalAddress, see: + +* [API documentation](https://cloud.google.com/compute/docs/reference/latest/globalAddresses) +* How-to Guides + * [Reserving a Static External IP Address](https://cloud.google.com/compute/docs/ip-addresses/reserve-static-external-ip-address) ## Example Usage @@ -25,29 +45,55 @@ resource "google_compute_global_address" "default" { 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. + - - - -* `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. +Provide this property when you create the resource. +* `ip_version` - + (Optional) + The IP Version that will be used by this address. Valid options are +IPV4 or IPV6. The default value is IPV4. +* `project` (Optional) The ID of the project in which the resource belongs. + If it is not provided, the provider project is used. -* `ip_version` - (Optional) The IP Version that will be used by this address. One of `"IPV4"` or `"IPV6"`. ## 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: + +* `address` - + The static external IP address represented by this resource. +* `creation_timestamp` - + Creation timestamp in RFC3339 text format. + + +## Timeouts -* `address` - The assigned address. +This resource provides the following +[Timeouts](/docs/configuration/resources.html#timeouts) configuration options: -* `self_link` - The URI of the created resource. +- `create` - Default is 4 minutes. +- `delete` - Default is 4 minutes. ## Import -Global addresses can be imported using the `name`, e.g. +GlobalAddress can be imported using any of these accepted formats: ``` -$ terraform import google_compute_global_address.default global-appserver-ip +$ terraform import google_compute_global_address.default projects/{{project}}/global/addresses/{{name}} +$ terraform import google_compute_global_address.default {{project}}/{{name}} +$ terraform import google_compute_global_address.default {{name}} ```