diff --git a/google/resource_compute_target_http_proxy.go b/google/resource_compute_target_http_proxy.go index 14501dffefb..db3cdfb927c 100644 --- a/google/resource_compute_target_http_proxy.go +++ b/google/resource_compute_target_http_proxy.go @@ -1,57 +1,77 @@ +// ---------------------------------------------------------------------------- +// +// *** 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" "strconv" + "time" "github.com/hashicorp/terraform/helper/schema" - "google.golang.org/api/compute/v1" + compute "google.golang.org/api/compute/v1" ) func resourceComputeTargetHttpProxy() *schema.Resource { return &schema.Resource{ Create: resourceComputeTargetHttpProxyCreate, Read: resourceComputeTargetHttpProxyRead, - Delete: resourceComputeTargetHttpProxyDelete, Update: resourceComputeTargetHttpProxyUpdate, + Delete: resourceComputeTargetHttpProxyDelete, Importer: &schema.ResourceImporter{ - State: schema.ImportStatePassthrough, + State: resourceComputeTargetHttpProxyImport, + }, + + Timeouts: &schema.ResourceTimeout{ + Create: schema.DefaultTimeout(240 * time.Second), + Update: 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, }, - - "url_map": &schema.Schema{ + "url_map": { Type: schema.TypeString, Required: true, - DiffSuppressFunc: compareSelfLinkRelativePaths, + DiffSuppressFunc: compareSelfLinkOrResourceName, }, - - "description": &schema.Schema{ + "description": { Type: schema.TypeString, Optional: true, ForceNew: true, }, - - "proxy_id": &schema.Schema{ + "creation_timestamp": { Type: schema.TypeString, Computed: true, }, - - "project": &schema.Schema{ + "proxy_id": { + Type: schema.TypeInt, + Computed: true, + }, + "project": { Type: schema.TypeString, Optional: true, Computed: true, ForceNew: true, }, - - "self_link": &schema.Schema{ + "self_link": { Type: schema.TypeString, Computed: true, }, @@ -67,32 +87,91 @@ func resourceComputeTargetHttpProxyCreate(d *schema.ResourceData, meta interface return err } - proxy := &compute.TargetHttpProxy{ - Name: d.Get("name").(string), - UrlMap: d.Get("url_map").(string), + descriptionProp, err := expandComputeTargetHttpProxyDescription(d.Get("description"), d, config) + if err != nil { + return err + } + nameProp, err := expandComputeTargetHttpProxyName(d.Get("name"), d, config) + if err != nil { + return err + } + urlMapProp, err := expandComputeTargetHttpProxyUrlMap(d.Get("url_map"), d, config) + if err != nil { + return err } - if v, ok := d.GetOk("description"); ok { - proxy.Description = v.(string) + obj := map[string]interface{}{ + "description": descriptionProp, + "name": nameProp, + "urlMap": urlMapProp, } - log.Printf("[DEBUG] TargetHttpProxy insert request: %#v", proxy) - op, err := config.clientCompute.TargetHttpProxies.Insert( - project, proxy).Do() + url, err := replaceVars(d, config, "https://www.googleapis.com/compute/v1/projects/{{project}}/global/targetHttpProxies") + if err != nil { + return err + } + + log.Printf("[DEBUG] Creating new TargetHttpProxy: %#v", obj) + res, err := Post(config, url, obj) if err != nil { return fmt.Errorf("Error creating TargetHttpProxy: %s", err) } - err = computeOperationWait(config.clientCompute, op, project, "Creating Target Http Proxy") + // 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 } - d.SetId(proxy.Name) + waitErr := computeOperationWaitTime( + config.clientCompute, op, project, "Creating TargetHttpProxy", + int(d.Timeout(schema.TimeoutCreate).Minutes())) + + if waitErr != nil { + // The resource didn't actually create + d.SetId("") + return waitErr + } return resourceComputeTargetHttpProxyRead(d, meta) } +func resourceComputeTargetHttpProxyRead(d *schema.ResourceData, meta interface{}) error { + config := meta.(*Config) + + project, err := getProject(d, config) + if err != nil { + return err + } + + url, err := replaceVars(d, config, "https://www.googleapis.com/compute/v1/projects/{{project}}/global/targetHttpProxies/{{name}}") + if err != nil { + return err + } + + res, err := Get(config, url) + if err != nil { + return handleNotFoundError(err, d, fmt.Sprintf("ComputeTargetHttpProxy %q", d.Id())) + } + + d.Set("creation_timestamp", flattenComputeTargetHttpProxyCreationTimestamp(res["creationTimestamp"])) + d.Set("description", flattenComputeTargetHttpProxyDescription(res["description"])) + d.Set("proxy_id", flattenComputeTargetHttpProxyProxyId(res["id"])) + d.Set("name", flattenComputeTargetHttpProxyName(res["name"])) + d.Set("url_map", flattenComputeTargetHttpProxyUrlMap(res["urlMap"])) + d.Set("self_link", res["selfLink"]) + d.Set("project", project) + + return nil +} + func resourceComputeTargetHttpProxyUpdate(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) @@ -101,31 +180,56 @@ func resourceComputeTargetHttpProxyUpdate(d *schema.ResourceData, meta interface return err } - d.Partial(true) + var url string + var res map[string]interface{} + op := &compute.Operation{} if d.HasChange("url_map") { - url_map := d.Get("url_map").(string) - url_map_ref := &compute.UrlMapReference{UrlMap: url_map} - op, err := config.clientCompute.TargetHttpProxies.SetUrlMap( - project, d.Id(), url_map_ref).Do() + descriptionProp, err := expandComputeTargetHttpProxyDescription(d.Get("description"), d, config) + if err != nil { + return err + } + nameProp, err := expandComputeTargetHttpProxyName(d.Get("name"), d, config) if err != nil { - return fmt.Errorf("Error updating target: %s", err) + return err + } + urlMapProp, err := expandComputeTargetHttpProxyUrlMap(d.Get("url_map"), d, config) + if err != nil { + return err } - err = computeOperationWait(config.clientCompute, op, project, "Updating Target Http Proxy") + obj := map[string]interface{}{ + "description": descriptionProp, + "name": nameProp, + "urlMap": urlMapProp, + } + url, err = replaceVars(d, config, "https://www.googleapis.com/compute/v1/projects/{{project}}/targetHttpProxies/{{name}}/setUrlMap") if err != nil { return err } + res, err = sendRequest(config, "POST", url, obj) + if err != nil { + return fmt.Errorf("Error updating TargetHttpProxy %q: %s", d.Id(), err) + } - d.SetPartial("url_map") - } + err = Convert(res, op) + if err != nil { + return err + } - d.Partial(false) + err = computeOperationWaitTime( + config.clientCompute, op, project, "Updating TargetHttpProxy", + int(d.Timeout(schema.TimeoutUpdate).Minutes())) + + if err != nil { + return err + } + } return resourceComputeTargetHttpProxyRead(d, meta) } -func resourceComputeTargetHttpProxyRead(d *schema.ResourceData, meta interface{}) error { +func resourceComputeTargetHttpProxyDelete(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) project, err := getProject(d, config) @@ -133,43 +237,86 @@ func resourceComputeTargetHttpProxyRead(d *schema.ResourceData, meta interface{} return err } - proxy, err := config.clientCompute.TargetHttpProxies.Get( - project, d.Id()).Do() + url, err := replaceVars(d, config, "https://www.googleapis.com/compute/v1/projects/{{project}}/global/targetHttpProxies/{{name}}") if err != nil { - return handleNotFoundError(err, d, fmt.Sprintf("Target HTTP Proxy %q", d.Get("name").(string))) + return err } - d.Set("self_link", proxy.SelfLink) - d.Set("proxy_id", strconv.FormatUint(proxy.Id, 10)) - d.Set("description", proxy.Description) - d.Set("url_map", proxy.UrlMap) - d.Set("name", proxy.Name) - d.Set("project", project) + log.Printf("[DEBUG] Deleting TargetHttpProxy %q", d.Id()) + res, err := Delete(config, url) + if err != nil { + return fmt.Errorf("Error deleting TargetHttpProxy %q: %s", d.Id(), err) + } + + op := &compute.Operation{} + err = Convert(res, op) + if err != nil { + return err + } + + err = computeOperationWaitTime( + config.clientCompute, op, project, "Deleting TargetHttpProxy", + int(d.Timeout(schema.TimeoutDelete).Minutes())) + + if err != nil { + return err + } return nil } -func resourceComputeTargetHttpProxyDelete(d *schema.ResourceData, meta interface{}) error { +func resourceComputeTargetHttpProxyImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { config := meta.(*Config) + parseImportId([]string{"projects/(?P[^/]+)/global/targetHttpProxies/(?P[^/]+)", "(?P[^/]+)/(?P[^/]+)", "(?P[^/]+)"}, d, config) - project, err := getProject(d, config) + // Replace import id for the resource id + id, err := replaceVars(d, config, "{{name}}") if err != nil { - return err + return nil, fmt.Errorf("Error constructing id: %s", err) } + d.SetId(id) - // Delete the TargetHttpProxy - log.Printf("[DEBUG] TargetHttpProxy delete request") - op, err := config.clientCompute.TargetHttpProxies.Delete( - project, d.Id()).Do() - if err != nil { - return fmt.Errorf("Error deleting TargetHttpProxy: %s", err) + return []*schema.ResourceData{d}, nil +} + +func flattenComputeTargetHttpProxyCreationTimestamp(v interface{}) interface{} { + return v +} + +func flattenComputeTargetHttpProxyDescription(v interface{}) interface{} { + return v +} + +func flattenComputeTargetHttpProxyProxyId(v interface{}) interface{} { + // Handles the string fixed64 format + if strVal, ok := v.(string); ok { + if intVal, err := strconv.Atoi(strVal); err == nil { + return intVal + } // let terraform core handle it if we can't convert the string to an int. } + return v +} + +func flattenComputeTargetHttpProxyName(v interface{}) interface{} { + return v +} - err = computeOperationWait(config.clientCompute, op, project, "Deleting Target Http Proxy") +func flattenComputeTargetHttpProxyUrlMap(v interface{}) interface{} { + return v +} + +func expandComputeTargetHttpProxyDescription(v interface{}, d *schema.ResourceData, config *Config) (interface{}, error) { + return v, nil +} + +func expandComputeTargetHttpProxyName(v interface{}, d *schema.ResourceData, config *Config) (interface{}, error) { + return v, nil +} + +func expandComputeTargetHttpProxyUrlMap(v interface{}, d *schema.ResourceData, config *Config) (interface{}, error) { + f, err := parseGlobalFieldValue("urlMaps", v.(string), "project", d, config, true) if err != nil { - return err + return nil, fmt.Errorf("Invalid value for url_map: %s", err) } - - d.SetId("") - return nil + return f.RelativeLink(), nil } diff --git a/website/docs/r/compute_target_http_proxy.html.markdown b/website/docs/r/compute_target_http_proxy.html.markdown index edad94d091b..20be6d3d2d0 100644 --- a/website/docs/r/compute_target_http_proxy.html.markdown +++ b/website/docs/r/compute_target_http_proxy.html.markdown @@ -1,18 +1,38 @@ + + --- layout: "google" page_title: "Google: google_compute_target_http_proxy" sidebar_current: "docs-google-compute-target-http-proxy" description: |- - Creates a Target HTTP Proxy resource in GCE. + Represents a TargetHttpProxy resource, which is used by one or more global +forwarding rule to route incoming HTTP requests to a URL map. --- # google\_compute\_target\_http\_proxy -Creates a target HTTP proxy resource in GCE. For more information see -[the official -documentation](https://cloud.google.com/compute/docs/load-balancing/http/target-proxies) and -[API](https://cloud.google.com/compute/docs/reference/latest/targetHttpProxies). +Represents a TargetHttpProxy resource, which is used by one or more global +forwarding rule to route incoming HTTP requests to a URL map. + +To get more information about TargetHttpProxy, see: +* [API documentation](https://cloud.google.com/compute/docs/reference/latest/targetHttpProxies) +* How-to Guides + * [Official Documentation](https://cloud.google.com/compute/docs/load-balancing/http/target-proxies) ## Example Usage @@ -66,34 +86,55 @@ resource "google_compute_http_health_check" "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. +* `url_map` - + (Required) + A reference to UrlMap resource -* `url_map` - (Required) The URL of a URL Map resource that defines the mapping - from the URL to the BackendService. - -* `project` - (Optional) The ID of the project in which the resource belongs. If it - is not provided, the provider project is used. - - - -* `description` - (Optional) A description of this resource. Changing this - forces a new resource to be created. +* `description` - + (Optional) + An optional description of this resource. +* `project` (Optional) The ID of the project in which the resource belongs. + If it is not provided, the provider project is used. ## Attributes Reference -In addition to the arguments listed above, the following computed attributes are -exported: - -* `proxy_id` - A unique ID assigned by GCE. +In addition to the arguments listed above, the following computed attributes are exported: +* `creation_timestamp` - + Creation timestamp in RFC3339 text format. +* `proxy_id` - + The unique identifier for the resource. * `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. +- `update` - Default is 4 minutes. +- `delete` - Default is 4 minutes. + ## Import -Target HTTP Proxy can be imported using the `name`, e.g. +TargetHttpProxy can be imported using any of these accepted formats: ``` -$ terraform import compute_target_http_proxy.foobar foobar +$ terraform import google_compute_target_http_proxy.default projects/{{project}}/global/targetHttpProxies/{{name}} +$ terraform import google_compute_target_http_proxy.default {{project}}/{{name}} +$ terraform import google_compute_target_http_proxy.default {{name}} ```