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_https_health_check": resourceComputeHttpsHealthCheck(),
"google_compute_target_http_proxy": resourceComputeTargetHttpProxy(),
"google_compute_target_https_proxy": resourceComputeTargetHttpsProxy(),
"google_compute_target_ssl_proxy": resourceComputeTargetSslProxy(),
"google_compute_target_tcp_proxy": resourceComputeTargetTcpProxy(),
"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
import (
"fmt"
"log"
"strconv"
"time"
"github.com/hashicorp/terraform/helper/schema"
"google.golang.org/api/compute/v1"
)
const (
canonicalSslCertificateTemplate = "https://www.googleapis.com/compute/v1/projects/%s/global/sslCertificates/%s"
compute "google.golang.org/api/compute/v1"
)
func resourceComputeTargetHttpsProxy() *schema.Resource {
return &schema.Resource{
Create: resourceComputeTargetHttpsProxyCreate,
Read: resourceComputeTargetHttpsProxyRead,
Delete: resourceComputeTargetHttpsProxyDelete,
Update: resourceComputeTargetHttpsProxyUpdate,
Delete: resourceComputeTargetHttpsProxyDelete,
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{
"name": &schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"ssl_certificates": &schema.Schema{
"ssl_certificates": {
Type: schema.TypeList,
Required: true,
Elem: &schema.Schema{
@ -39,35 +55,34 @@ func resourceComputeTargetHttpsProxy() *schema.Resource {
DiffSuppressFunc: compareSelfLinkOrResourceName,
},
},
"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,
},
"self_link": &schema.Schema{
"creation_timestamp": {
Type: schema.TypeString,
Computed: true,
},
"proxy_id": &schema.Schema{
Type: schema.TypeString,
"proxy_id": {
Type: schema.TypeInt,
Computed: true,
},
"project": &schema.Schema{
"project": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"self_link": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
@ -80,89 +95,64 @@ func resourceComputeTargetHttpsProxyCreate(d *schema.ResourceData, meta interfac
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 {
return err
}
proxy := &compute.TargetHttpsProxy{
Name: d.Get("name").(string),
UrlMap: d.Get("url_map").(string),
SslCertificates: sslCertificates,
obj := map[string]interface{}{
"description": descriptionProp,
"name": nameProp,
"sslCertificates": sslCertificatesProp,
"urlMap": urlMapProp,
}
if v, ok := d.GetOk("description"); ok {
proxy.Description = v.(string)
url, err := replaceVars(d, config, "https://www.googleapis.com/compute/v1/projects/{{project}}/global/targetHttpsProxies")
if err != nil {
return err
}
log.Printf("[DEBUG] TargetHttpsProxy insert request: %#v", proxy)
op, err := config.clientCompute.TargetHttpsProxies.Insert(
project, proxy).Do()
log.Printf("[DEBUG] Creating new TargetHttpsProxy: %#v", obj)
res, err := Post(config, url, obj)
if err != nil {
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 {
return err
}
d.SetId(proxy.Name)
waitErr := computeOperationWaitTime(
config.clientCompute, op, project, "Creating TargetHttpsProxy",
int(d.Timeout(schema.TimeoutCreate).Minutes()))
return resourceComputeTargetHttpsProxyRead(d, meta)
}
func resourceComputeTargetHttpsProxyUpdate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
project, err := getProject(d, config)
if err != nil {
return err
if waitErr != nil {
// The resource didn't actually create
d.SetId("")
return fmt.Errorf("Error waiting to create TargetHttpsProxy: %s", waitErr)
}
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)
}
@ -174,23 +164,129 @@ func resourceComputeTargetHttpsProxyRead(d *schema.ResourceData, meta interface{
return err
}
proxy, err := config.clientCompute.TargetHttpsProxies.Get(
project, d.Id()).Do()
url, err := replaceVars(d, config, "https://www.googleapis.com/compute/v1/projects/{{project}}/global/targetHttpsProxies/{{name}}")
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)
d.Set("proxy_id", strconv.FormatUint(proxy.Id, 10))
d.Set("self_link", proxy.SelfLink)
d.Set("description", proxy.Description)
d.Set("url_map", proxy.UrlMap)
d.Set("name", proxy.Name)
d.Set("project", project)
res, err := Get(config, url)
if err != nil {
return handleNotFoundError(err, d, fmt.Sprintf("ComputeTargetHttpsProxy %q", d.Id()))
}
if err := d.Set("creation_timestamp", flattenComputeTargetHttpsProxyCreationTimestamp(res["creationTimestamp"])); err != nil {
return fmt.Errorf("Error reading TargetHttpsProxy: %s", err)
}
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
}
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 {
config := meta.(*Config)
@ -199,35 +295,103 @@ func resourceComputeTargetHttpsProxyDelete(d *schema.ResourceData, meta interfac
return err
}
// Delete the TargetHttpsProxy
log.Printf("[DEBUG] TargetHttpsProxy delete request")
op, err := config.clientCompute.TargetHttpsProxies.Delete(
project, d.Id()).Do()
if err != nil {
return fmt.Errorf("Error deleting TargetHttpsProxy: %s", err)
}
err = computeOperationWait(config.clientCompute, op, project, "Deleting Target Https Proxy")
url, err := replaceVars(d, config, "https://www.googleapis.com/compute/v1/projects/{{project}}/global/targetHttpsProxies/{{name}}")
if err != nil {
return err
}
log.Printf("[DEBUG] Deleting TargetHttpsProxy %q", d.Id())
res, err := Delete(config, url)
if err != nil {
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 {
return err
}
d.SetId("")
return nil
}
func expandSslCertificates(d *schema.ResourceData, config *Config) ([]string, error) {
configured := d.Get("ssl_certificates").([]interface{})
certs := make([]string, 0, len(configured))
func resourceComputeTargetHttpsProxyImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
config := meta.(*Config)
parseImportId([]string{"projects/(?P<project>[^/]+)/global/targetHttpsProxies/(?P<name>[^/]+)", "(?P<project>[^/]+)/(?P<name>[^/]+)", "(?P<name>[^/]+)"}, d, config)
for _, sslCertificate := range configured {
sslCertificateFieldValue, err := ParseSslCertificateFieldValue(sslCertificate.(string), d, config)
if err != nil {
return nil, fmt.Errorf("Invalid ssl certificate: %s", err)
}
certs = append(certs, sslCertificateFieldValue.RelativeLink())
// 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 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"
)
const (
canonicalSslCertificateTemplate = "https://www.googleapis.com/compute/v1/projects/%s/global/sslCertificates/%s"
)
func TestAccComputeTargetHttpsProxy_basic(t *testing.T) {
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"
page_title: "Google: google_compute_target_https_proxy"
sidebar_current: "docs-google-compute-target-https-proxy"
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
Creates a target HTTPS 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/targetHttpsProxies).
Represents a TargetHttpsProxy resource, which is used by one or more
global forwarding rule to route incoming HTTPS requests to a URL map.
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
@ -74,36 +91,60 @@ 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.
* `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
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.
* `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 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}}
```