Generate google_target_tcp_proxy using Magic Modules (#1415)

This commit is contained in:
The Magician 2018-05-02 10:01:37 -07:00 committed by Vincent Roseberry
parent 4ccb099699
commit 77a02b2b68
4 changed files with 349 additions and 139 deletions

View File

@ -22,6 +22,7 @@ var GeneratedComputeResourcesMap = map[string]*schema.Resource{
"google_compute_http_health_check": resourceComputeHttpHealthCheck(), "google_compute_http_health_check": resourceComputeHttpHealthCheck(),
"google_compute_https_health_check": resourceComputeHttpsHealthCheck(), "google_compute_https_health_check": resourceComputeHttpsHealthCheck(),
"google_compute_target_http_proxy": resourceComputeTargetHttpProxy(), "google_compute_target_http_proxy": resourceComputeTargetHttpProxy(),
"google_compute_target_https_proxy": resourceComputeTargetHttpsProxy(),
"google_compute_target_ssl_proxy": resourceComputeTargetSslProxy(), "google_compute_target_ssl_proxy": resourceComputeTargetSslProxy(),
"google_compute_target_tcp_proxy": resourceComputeTargetTcpProxy(), "google_compute_target_tcp_proxy": resourceComputeTargetTcpProxy(),
"google_compute_vpn_gateway": resourceComputeVpnGateway(), "google_compute_vpn_gateway": resourceComputeVpnGateway(),

View File

@ -1,37 +1,53 @@
// ----------------------------------------------------------------------------
//
// *** 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 package google
import ( import (
"fmt" "fmt"
"log" "log"
"strconv" "strconv"
"time"
"github.com/hashicorp/terraform/helper/schema" "github.com/hashicorp/terraform/helper/schema"
"google.golang.org/api/compute/v1" compute "google.golang.org/api/compute/v1"
)
const (
canonicalSslCertificateTemplate = "https://www.googleapis.com/compute/v1/projects/%s/global/sslCertificates/%s"
) )
func resourceComputeTargetHttpsProxy() *schema.Resource { func resourceComputeTargetHttpsProxy() *schema.Resource {
return &schema.Resource{ return &schema.Resource{
Create: resourceComputeTargetHttpsProxyCreate, Create: resourceComputeTargetHttpsProxyCreate,
Read: resourceComputeTargetHttpsProxyRead, Read: resourceComputeTargetHttpsProxyRead,
Delete: resourceComputeTargetHttpsProxyDelete,
Update: resourceComputeTargetHttpsProxyUpdate, Update: resourceComputeTargetHttpsProxyUpdate,
Delete: resourceComputeTargetHttpsProxyDelete,
Importer: &schema.ResourceImporter{ Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough, State: resourceComputeTargetHttpsProxyImport,
},
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{ Schema: map[string]*schema.Schema{
"name": &schema.Schema{ "name": {
Type: schema.TypeString, Type: schema.TypeString,
Required: true, Required: true,
ForceNew: true, ForceNew: true,
}, },
"ssl_certificates": {
"ssl_certificates": &schema.Schema{
Type: schema.TypeList, Type: schema.TypeList,
Required: true, Required: true,
Elem: &schema.Schema{ Elem: &schema.Schema{
@ -39,35 +55,34 @@ func resourceComputeTargetHttpsProxy() *schema.Resource {
DiffSuppressFunc: compareSelfLinkOrResourceName, DiffSuppressFunc: compareSelfLinkOrResourceName,
}, },
}, },
"url_map": {
"url_map": &schema.Schema{
Type: schema.TypeString, Type: schema.TypeString,
Required: true, Required: true,
DiffSuppressFunc: compareSelfLinkRelativePaths, DiffSuppressFunc: compareSelfLinkOrResourceName,
}, },
"description": {
"description": &schema.Schema{
Type: schema.TypeString, Type: schema.TypeString,
Optional: true, Optional: true,
ForceNew: true, ForceNew: true,
}, },
"creation_timestamp": {
"self_link": &schema.Schema{
Type: schema.TypeString, Type: schema.TypeString,
Computed: true, Computed: true,
}, },
"proxy_id": {
"proxy_id": &schema.Schema{ Type: schema.TypeInt,
Type: schema.TypeString,
Computed: true, Computed: true,
}, },
"project": {
"project": &schema.Schema{
Type: schema.TypeString, Type: schema.TypeString,
Optional: true, Optional: true,
Computed: true, Computed: true,
ForceNew: true, ForceNew: true,
}, },
"self_link": {
Type: schema.TypeString,
Computed: true,
},
}, },
} }
} }
@ -80,89 +95,64 @@ func resourceComputeTargetHttpsProxyCreate(d *schema.ResourceData, meta interfac
return err return err
} }
sslCertificates, err := expandSslCertificates(d, config) descriptionProp, err := expandComputeTargetHttpsProxyDescription(d.Get("description"), d, config)
if err != nil {
return err
}
nameProp, err := expandComputeTargetHttpsProxyName(d.Get("name"), d, config)
if err != nil {
return err
}
sslCertificatesProp, err := expandComputeTargetHttpsProxySslCertificates(d.Get("ssl_certificates"), d, config)
if err != nil {
return err
}
urlMapProp, err := expandComputeTargetHttpsProxyUrlMap(d.Get("url_map"), d, config)
if err != nil { if err != nil {
return err return err
} }
proxy := &compute.TargetHttpsProxy{ obj := map[string]interface{}{
Name: d.Get("name").(string), "description": descriptionProp,
UrlMap: d.Get("url_map").(string), "name": nameProp,
SslCertificates: sslCertificates, "sslCertificates": sslCertificatesProp,
"urlMap": urlMapProp,
} }
if v, ok := d.GetOk("description"); ok { url, err := replaceVars(d, config, "https://www.googleapis.com/compute/v1/projects/{{project}}/global/targetHttpsProxies")
proxy.Description = v.(string) if err != nil {
return err
} }
log.Printf("[DEBUG] TargetHttpsProxy insert request: %#v", proxy) log.Printf("[DEBUG] Creating new TargetHttpsProxy: %#v", obj)
op, err := config.clientCompute.TargetHttpsProxies.Insert( res, err := Post(config, url, obj)
project, proxy).Do()
if err != nil { if err != nil {
return fmt.Errorf("Error creating TargetHttpsProxy: %s", err) return fmt.Errorf("Error creating TargetHttpsProxy: %s", err)
} }
err = computeOperationWait(config.clientCompute, op, project, "Creating Target Https 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 { if err != nil {
return err return err
} }
d.SetId(proxy.Name) waitErr := computeOperationWaitTime(
config.clientCompute, op, project, "Creating TargetHttpsProxy",
int(d.Timeout(schema.TimeoutCreate).Minutes()))
return resourceComputeTargetHttpsProxyRead(d, meta) if waitErr != nil {
} // The resource didn't actually create
d.SetId("")
func resourceComputeTargetHttpsProxyUpdate(d *schema.ResourceData, meta interface{}) error { return fmt.Errorf("Error waiting to create TargetHttpsProxy: %s", waitErr)
config := meta.(*Config)
project, err := getProject(d, config)
if err != nil {
return err
} }
d.Partial(true)
if d.HasChange("url_map") {
url_map := d.Get("url_map").(string)
url_map_ref := &compute.UrlMapReference{UrlMap: url_map}
op, err := config.clientCompute.TargetHttpsProxies.SetUrlMap(
project, d.Id(), url_map_ref).Do()
if err != nil {
return fmt.Errorf("Error updating Target HTTPS proxy URL map: %s", err)
}
err = computeOperationWait(config.clientCompute, op, project, "Updating Target Https Proxy URL Map")
if err != nil {
return err
}
d.SetPartial("url_map")
}
if d.HasChange("ssl_certificates") {
certs, err := expandSslCertificates(d, config)
if err != nil {
return err
}
cert_ref := &compute.TargetHttpsProxiesSetSslCertificatesRequest{
SslCertificates: certs,
}
op, err := config.clientCompute.TargetHttpsProxies.SetSslCertificates(
project, d.Id(), cert_ref).Do()
if err != nil {
return fmt.Errorf("Error updating Target Https Proxy SSL Certificates: %s", err)
}
err = computeOperationWait(config.clientCompute, op, project, "Updating Target Https Proxy SSL certificates")
if err != nil {
return err
}
d.SetPartial("ssl_certificate")
}
d.Partial(false)
return resourceComputeTargetHttpsProxyRead(d, meta) return resourceComputeTargetHttpsProxyRead(d, meta)
} }
@ -174,23 +164,129 @@ func resourceComputeTargetHttpsProxyRead(d *schema.ResourceData, meta interface{
return err return err
} }
proxy, err := config.clientCompute.TargetHttpsProxies.Get( url, err := replaceVars(d, config, "https://www.googleapis.com/compute/v1/projects/{{project}}/global/targetHttpsProxies/{{name}}")
project, d.Id()).Do()
if err != nil { if err != nil {
return handleNotFoundError(err, d, fmt.Sprintf("Target HTTPS proxy %q", d.Get("name").(string))) return err
} }
d.Set("ssl_certificates", proxy.SslCertificates) res, err := Get(config, url)
d.Set("proxy_id", strconv.FormatUint(proxy.Id, 10)) if err != nil {
d.Set("self_link", proxy.SelfLink) return handleNotFoundError(err, d, fmt.Sprintf("ComputeTargetHttpsProxy %q", d.Id()))
d.Set("description", proxy.Description) }
d.Set("url_map", proxy.UrlMap) if err := d.Set("creation_timestamp", flattenComputeTargetHttpsProxyCreationTimestamp(res["creationTimestamp"])); err != nil {
d.Set("name", proxy.Name) return fmt.Errorf("Error reading TargetHttpsProxy: %s", err)
d.Set("project", project) }
if err := d.Set("description", flattenComputeTargetHttpsProxyDescription(res["description"])); err != nil {
return fmt.Errorf("Error reading TargetHttpsProxy: %s", err)
}
if err := d.Set("proxy_id", flattenComputeTargetHttpsProxyProxyId(res["id"])); err != nil {
return fmt.Errorf("Error reading TargetHttpsProxy: %s", err)
}
if err := d.Set("name", flattenComputeTargetHttpsProxyName(res["name"])); err != nil {
return fmt.Errorf("Error reading TargetHttpsProxy: %s", err)
}
if err := d.Set("ssl_certificates", flattenComputeTargetHttpsProxySslCertificates(res["sslCertificates"])); err != nil {
return fmt.Errorf("Error reading TargetHttpsProxy: %s", err)
}
if err := d.Set("url_map", flattenComputeTargetHttpsProxyUrlMap(res["urlMap"])); err != nil {
return fmt.Errorf("Error reading TargetHttpsProxy: %s", err)
}
if err := d.Set("self_link", res["selfLink"]); err != nil {
return fmt.Errorf("Error reading TargetHttpsProxy: %s", err)
}
if err := d.Set("project", project); err != nil {
return fmt.Errorf("Error reading TargetHttpsProxy: %s", err)
}
return nil return nil
} }
func resourceComputeTargetHttpsProxyUpdate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
project, err := getProject(d, config)
if err != nil {
return err
}
var url string
var res map[string]interface{}
op := &compute.Operation{}
d.Partial(true)
if d.HasChange("ssl_certificates") {
sslCertificatesProp, err := expandComputeTargetHttpsProxySslCertificates(d.Get("ssl_certificates"), d, config)
if err != nil {
return err
}
obj := map[string]interface{}{
"sslCertificates": sslCertificatesProp,
}
url, err = replaceVars(d, config, "https://www.googleapis.com/compute/v1/projects/{{project}}/targetHttpsProxies/{{name}}/setSslCertificates")
if err != nil {
return err
}
res, err = sendRequest(config, "POST", url, obj)
if err != nil {
return fmt.Errorf("Error updating TargetHttpsProxy %q: %s", d.Id(), err)
}
err = Convert(res, op)
if err != nil {
return err
}
err = computeOperationWaitTime(
config.clientCompute, op, project, "Updating TargetHttpsProxy",
int(d.Timeout(schema.TimeoutUpdate).Minutes()))
if err != nil {
return err
}
d.SetPartial("ssl_certificates")
}
if d.HasChange("url_map") {
urlMapProp, err := expandComputeTargetHttpsProxyUrlMap(d.Get("url_map"), d, config)
if err != nil {
return err
}
obj := map[string]interface{}{
"urlMap": urlMapProp,
}
url, err = replaceVars(d, config, "https://www.googleapis.com/compute/v1/projects/{{project}}/targetHttpsProxies/{{name}}/setUrlMap")
if err != nil {
return err
}
res, err = sendRequest(config, "POST", url, obj)
if err != nil {
return fmt.Errorf("Error updating TargetHttpsProxy %q: %s", d.Id(), err)
}
err = Convert(res, op)
if err != nil {
return err
}
err = computeOperationWaitTime(
config.clientCompute, op, project, "Updating TargetHttpsProxy",
int(d.Timeout(schema.TimeoutUpdate).Minutes()))
if err != nil {
return err
}
d.SetPartial("url_map")
}
d.Partial(false)
return resourceComputeTargetHttpsProxyRead(d, meta)
}
func resourceComputeTargetHttpsProxyDelete(d *schema.ResourceData, meta interface{}) error { func resourceComputeTargetHttpsProxyDelete(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config) config := meta.(*Config)
@ -199,35 +295,103 @@ func resourceComputeTargetHttpsProxyDelete(d *schema.ResourceData, meta interfac
return err return err
} }
// Delete the TargetHttpsProxy url, err := replaceVars(d, config, "https://www.googleapis.com/compute/v1/projects/{{project}}/global/targetHttpsProxies/{{name}}")
log.Printf("[DEBUG] TargetHttpsProxy delete request") if err != nil {
op, err := config.clientCompute.TargetHttpsProxies.Delete( return err
project, d.Id()).Do() }
if err != nil {
return fmt.Errorf("Error deleting TargetHttpsProxy: %s", err) log.Printf("[DEBUG] Deleting TargetHttpsProxy %q", d.Id())
} res, err := Delete(config, url)
if err != nil {
err = computeOperationWait(config.clientCompute, op, project, "Deleting Target Https Proxy") return fmt.Errorf("Error deleting TargetHttpsProxy %q: %s", d.Id(), err)
}
op := &compute.Operation{}
err = Convert(res, op)
if err != nil {
return err
}
err = computeOperationWaitTime(
config.clientCompute, op, project, "Deleting TargetHttpsProxy",
int(d.Timeout(schema.TimeoutDelete).Minutes()))
if err != nil { if err != nil {
return err return err
} }
d.SetId("")
return nil return nil
} }
func expandSslCertificates(d *schema.ResourceData, config *Config) ([]string, error) { func resourceComputeTargetHttpsProxyImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
configured := d.Get("ssl_certificates").([]interface{}) config := meta.(*Config)
certs := make([]string, 0, len(configured)) parseImportId([]string{"projects/(?P<project>[^/]+)/global/targetHttpsProxies/(?P<name>[^/]+)", "(?P<project>[^/]+)/(?P<name>[^/]+)", "(?P<name>[^/]+)"}, d, config)
for _, sslCertificate := range configured { // Replace import id for the resource id
sslCertificateFieldValue, err := ParseSslCertificateFieldValue(sslCertificate.(string), d, config) id, err := replaceVars(d, config, "{{name}}")
if err != nil { if err != nil {
return nil, fmt.Errorf("Invalid ssl certificate: %s", err) return nil, fmt.Errorf("Error constructing id: %s", err)
}
certs = append(certs, sslCertificateFieldValue.RelativeLink())
} }
d.SetId(id)
return certs, nil return []*schema.ResourceData{d}, nil
}
func flattenComputeTargetHttpsProxyCreationTimestamp(v interface{}) interface{} {
return v
}
func flattenComputeTargetHttpsProxyDescription(v interface{}) interface{} {
return v
}
func flattenComputeTargetHttpsProxyProxyId(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 flattenComputeTargetHttpsProxyName(v interface{}) interface{} {
return v
}
func flattenComputeTargetHttpsProxySslCertificates(v interface{}) interface{} {
return v
}
func flattenComputeTargetHttpsProxyUrlMap(v interface{}) interface{} {
return v
}
func expandComputeTargetHttpsProxyDescription(v interface{}, d *schema.ResourceData, config *Config) (interface{}, error) {
return v, nil
}
func expandComputeTargetHttpsProxyName(v interface{}, d *schema.ResourceData, config *Config) (interface{}, error) {
return v, nil
}
func expandComputeTargetHttpsProxySslCertificates(v interface{}, d *schema.ResourceData, config *Config) (interface{}, error) {
l := v.([]interface{})
req := make([]interface{}, 0, len(l))
for _, raw := range l {
f, err := parseGlobalFieldValue("sslCertificates", raw.(string), "project", d, config, true)
if err != nil {
return nil, fmt.Errorf("Invalid value for ssl_certificates: %s", err)
}
req = append(req, f.RelativeLink())
}
return req, nil
}
func expandComputeTargetHttpsProxyUrlMap(v interface{}, d *schema.ResourceData, config *Config) (interface{}, error) {
f, err := parseGlobalFieldValue("urlMaps", v.(string), "project", d, config, true)
if err != nil {
return nil, fmt.Errorf("Invalid value for url_map: %s", err)
}
return f.RelativeLink(), nil
} }

View File

@ -10,6 +10,10 @@ import (
"google.golang.org/api/compute/v1" "google.golang.org/api/compute/v1"
) )
const (
canonicalSslCertificateTemplate = "https://www.googleapis.com/compute/v1/projects/%s/global/sslCertificates/%s"
)
func TestAccComputeTargetHttpsProxy_basic(t *testing.T) { func TestAccComputeTargetHttpsProxy_basic(t *testing.T) {
t.Parallel() t.Parallel()

View File

@ -1,18 +1,35 @@
--- ---
# ----------------------------------------------------------------------------
#
# *** 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" layout: "google"
page_title: "Google: google_compute_target_https_proxy" page_title: "Google: google_compute_target_https_proxy"
sidebar_current: "docs-google-compute-target-https-proxy" sidebar_current: "docs-google-compute-target-https-proxy"
description: |- description: |-
Creates a Target HTTPS Proxy resource in GCE. Represents a TargetHttpsProxy resource, which is used by one or more
global forwarding rule to route incoming HTTPS requests to a URL map.
--- ---
# google\_compute\_target\_https\_proxy # google\_compute\_target\_https\_proxy
Creates a target HTTPS proxy resource in GCE. For more information see Represents a TargetHttpsProxy resource, which is used by one or more
[the official global forwarding rule to route incoming HTTPS requests to a URL map.
documentation](https://cloud.google.com/compute/docs/load-balancing/http/target-proxies) and
[API](https://cloud.google.com/compute/docs/reference/latest/targetHttpsProxies).
To get more information about TargetHttpsProxy, see:
* [API documentation](https://cloud.google.com/compute/docs/reference/latest/targetHttpsProxies)
* How-to Guides
* [Official Documentation](https://cloud.google.com/compute/docs/load-balancing/http/target-proxies)
## Example Usage ## Example Usage
@ -74,36 +91,60 @@ resource "google_compute_http_health_check" "default" {
The following arguments are supported: The following arguments are supported:
* `name` - (Required) A unique name for the resource, required by GCE. Changing * `name` -
this forces a new resource to be created. (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.
* `ssl_certificates` -
(Required)
A list of SslCertificate resources that are used to authenticate
connections between users and the load balancer. Currently, exactly
one SSL certificate must be specified.
* `url_map` -
(Required)
A reference to UrlMap resource
* `ssl_certificates` - (Required) The URLs or names of the SSL Certificate resources
that authenticate connections between users and load balancing.
* `url_map` - (Required) The URL of a URL Map resource that defines the mapping
from the URL to the BackendService.
- - - - - -
* `description` - (Optional) A description of this resource. Changing this * `description` -
forces a new resource to be created. (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.
* `project` - (Optional) The ID of the project in which the resource belongs. If it
is not provided, the provider project is used.
## Attributes Reference ## Attributes Reference
In addition to the arguments listed above, the following computed attributes are In addition to the arguments listed above, the following computed attributes are exported:
exported:
* `proxy_id` - A unique ID assigned by GCE.
* `creation_timestamp` -
Creation timestamp in RFC3339 text format.
* `proxy_id` -
The unique identifier for the resource.
* `self_link` - The URI of the created 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 ## Import
Target HTTPS Proxy can be imported using the `name`, e.g. TargetHttpsProxy can be imported using any of these accepted formats:
``` ```
$ terraform import compute_target_https_proxy.foobar foobar $ terraform import google_compute_target_https_proxy.default projects/{{project}}/global/targetHttpsProxies/{{name}}
$ terraform import google_compute_target_https_proxy.default {{project}}/{{name}}
$ terraform import google_compute_target_https_proxy.default {{name}}
``` ```