2018-04-27 01:06:10 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// *** 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.
|
|
|
|
//
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2015-11-01 15:39:08 +00:00
|
|
|
package google
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
2017-09-25 14:23:46 +00:00
|
|
|
"strconv"
|
2018-04-27 01:06:10 +00:00
|
|
|
"time"
|
2015-11-01 15:39:08 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
2018-04-27 01:06:10 +00:00
|
|
|
compute "google.golang.org/api/compute/v1"
|
2015-11-01 15:39:08 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func resourceComputeTargetHttpProxy() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
|
|
|
Create: resourceComputeTargetHttpProxyCreate,
|
|
|
|
Read: resourceComputeTargetHttpProxyRead,
|
|
|
|
Update: resourceComputeTargetHttpProxyUpdate,
|
2018-04-27 01:06:10 +00:00
|
|
|
Delete: resourceComputeTargetHttpProxyDelete,
|
2015-11-01 15:39:08 +00:00
|
|
|
|
2017-11-09 18:16:57 +00:00
|
|
|
Importer: &schema.ResourceImporter{
|
2018-04-27 01:06:10 +00:00
|
|
|
State: resourceComputeTargetHttpProxyImport,
|
|
|
|
},
|
|
|
|
|
|
|
|
Timeouts: &schema.ResourceTimeout{
|
|
|
|
Create: schema.DefaultTimeout(240 * time.Second),
|
|
|
|
Update: schema.DefaultTimeout(240 * time.Second),
|
|
|
|
Delete: schema.DefaultTimeout(240 * time.Second),
|
2017-11-09 18:16:57 +00:00
|
|
|
},
|
|
|
|
|
2015-11-01 15:39:08 +00:00
|
|
|
Schema: map[string]*schema.Schema{
|
2018-04-27 01:06:10 +00:00
|
|
|
"name": {
|
2015-11-01 15:39:08 +00:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
2018-04-27 01:06:10 +00:00
|
|
|
"url_map": {
|
2018-03-27 22:38:32 +00:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
2018-04-27 01:06:10 +00:00
|
|
|
DiffSuppressFunc: compareSelfLinkOrResourceName,
|
2015-11-01 15:39:08 +00:00
|
|
|
},
|
2018-04-27 01:06:10 +00:00
|
|
|
"description": {
|
2015-11-01 15:39:08 +00:00
|
|
|
Type: schema.TypeString,
|
2016-04-10 21:34:15 +00:00
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
2015-11-01 15:39:08 +00:00
|
|
|
},
|
2018-04-27 01:06:10 +00:00
|
|
|
"creation_timestamp": {
|
2017-09-25 14:23:46 +00:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
2018-04-27 01:06:10 +00:00
|
|
|
"proxy_id": {
|
|
|
|
Type: schema.TypeInt,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
"project": {
|
2016-04-10 16:59:57 +00:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
2017-11-28 00:32:20 +00:00
|
|
|
Computed: true,
|
2016-04-10 16:59:57 +00:00
|
|
|
ForceNew: true,
|
|
|
|
},
|
2018-04-27 01:06:10 +00:00
|
|
|
"self_link": {
|
2016-04-10 21:34:15 +00:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
2015-11-01 15:39:08 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceComputeTargetHttpProxyCreate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
config := meta.(*Config)
|
|
|
|
|
2016-04-10 16:59:57 +00:00
|
|
|
project, err := getProject(d, config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-04-27 01:06:10 +00:00
|
|
|
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
|
2015-11-01 15:39:08 +00:00
|
|
|
}
|
|
|
|
|
2018-04-27 01:06:10 +00:00
|
|
|
obj := map[string]interface{}{
|
|
|
|
"description": descriptionProp,
|
|
|
|
"name": nameProp,
|
|
|
|
"urlMap": urlMapProp,
|
2015-11-01 15:39:08 +00:00
|
|
|
}
|
|
|
|
|
2018-04-27 01:06:10 +00:00
|
|
|
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)
|
2015-11-01 15:39:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error creating TargetHttpProxy: %s", err)
|
|
|
|
}
|
|
|
|
|
2018-04-27 01:06:10 +00:00
|
|
|
// 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)
|
2015-11-01 15:39:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-04-27 01:06:10 +00:00
|
|
|
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("")
|
2018-05-01 20:48:56 +00:00
|
|
|
return fmt.Errorf("Error waiting to create TargetHttpProxy: %s", waitErr)
|
2018-04-27 01:06:10 +00:00
|
|
|
}
|
2015-11-01 15:39:08 +00:00
|
|
|
|
|
|
|
return resourceComputeTargetHttpProxyRead(d, meta)
|
|
|
|
}
|
|
|
|
|
2018-04-27 01:06:10 +00:00
|
|
|
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()))
|
|
|
|
}
|
2018-04-30 23:18:17 +00:00
|
|
|
if err := d.Set("creation_timestamp", flattenComputeTargetHttpProxyCreationTimestamp(res["creationTimestamp"])); err != nil {
|
|
|
|
return fmt.Errorf("Error reading TargetHttpProxy: %s", err)
|
|
|
|
}
|
|
|
|
if err := d.Set("description", flattenComputeTargetHttpProxyDescription(res["description"])); err != nil {
|
|
|
|
return fmt.Errorf("Error reading TargetHttpProxy: %s", err)
|
|
|
|
}
|
|
|
|
if err := d.Set("proxy_id", flattenComputeTargetHttpProxyProxyId(res["id"])); err != nil {
|
|
|
|
return fmt.Errorf("Error reading TargetHttpProxy: %s", err)
|
|
|
|
}
|
|
|
|
if err := d.Set("name", flattenComputeTargetHttpProxyName(res["name"])); err != nil {
|
|
|
|
return fmt.Errorf("Error reading TargetHttpProxy: %s", err)
|
|
|
|
}
|
|
|
|
if err := d.Set("url_map", flattenComputeTargetHttpProxyUrlMap(res["urlMap"])); err != nil {
|
|
|
|
return fmt.Errorf("Error reading TargetHttpProxy: %s", err)
|
|
|
|
}
|
|
|
|
if err := d.Set("self_link", res["selfLink"]); err != nil {
|
|
|
|
return fmt.Errorf("Error reading TargetHttpProxy: %s", err)
|
|
|
|
}
|
|
|
|
if err := d.Set("project", project); err != nil {
|
|
|
|
return fmt.Errorf("Error reading TargetHttpProxy: %s", err)
|
|
|
|
}
|
2018-04-27 01:06:10 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-11-01 15:39:08 +00:00
|
|
|
func resourceComputeTargetHttpProxyUpdate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
config := meta.(*Config)
|
|
|
|
|
2016-04-10 16:59:57 +00:00
|
|
|
project, err := getProject(d, config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-04-27 01:06:10 +00:00
|
|
|
var url string
|
|
|
|
var res map[string]interface{}
|
|
|
|
op := &compute.Operation{}
|
2015-11-01 15:39:08 +00:00
|
|
|
|
2018-04-27 17:20:39 +00:00
|
|
|
d.Partial(true)
|
|
|
|
|
2015-11-01 15:39:08 +00:00
|
|
|
if d.HasChange("url_map") {
|
2018-04-27 01:06:10 +00:00
|
|
|
urlMapProp, err := expandComputeTargetHttpProxyUrlMap(d.Get("url_map"), d, config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2015-11-01 15:39:08 +00:00
|
|
|
}
|
|
|
|
|
2018-04-27 01:06:10 +00:00
|
|
|
obj := map[string]interface{}{
|
2018-04-27 23:33:57 +00:00
|
|
|
"urlMap": urlMapProp,
|
2018-04-27 01:06:10 +00:00
|
|
|
}
|
|
|
|
url, err = replaceVars(d, config, "https://www.googleapis.com/compute/v1/projects/{{project}}/targetHttpProxies/{{name}}/setUrlMap")
|
2015-11-01 15:39:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-04-27 01:06:10 +00:00
|
|
|
res, err = sendRequest(config, "POST", url, obj)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error updating TargetHttpProxy %q: %s", d.Id(), err)
|
|
|
|
}
|
2015-11-01 15:39:08 +00:00
|
|
|
|
2018-04-27 01:06:10 +00:00
|
|
|
err = Convert(res, op)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-11-01 15:39:08 +00:00
|
|
|
|
2018-04-27 01:06:10 +00:00
|
|
|
err = computeOperationWaitTime(
|
|
|
|
config.clientCompute, op, project, "Updating TargetHttpProxy",
|
|
|
|
int(d.Timeout(schema.TimeoutUpdate).Minutes()))
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-04-27 17:20:39 +00:00
|
|
|
|
|
|
|
d.SetPartial("url_map")
|
2018-04-27 01:06:10 +00:00
|
|
|
}
|
2015-11-01 15:39:08 +00:00
|
|
|
|
2018-04-27 17:20:39 +00:00
|
|
|
d.Partial(false)
|
|
|
|
|
2015-11-01 15:39:08 +00:00
|
|
|
return resourceComputeTargetHttpProxyRead(d, meta)
|
|
|
|
}
|
|
|
|
|
2018-04-27 01:06:10 +00:00
|
|
|
func resourceComputeTargetHttpProxyDelete(d *schema.ResourceData, meta interface{}) error {
|
2015-11-01 15:39:08 +00:00
|
|
|
config := meta.(*Config)
|
|
|
|
|
2016-04-10 16:59:57 +00:00
|
|
|
project, err := getProject(d, config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-04-27 01:06:10 +00:00
|
|
|
url, err := replaceVars(d, config, "https://www.googleapis.com/compute/v1/projects/{{project}}/global/targetHttpProxies/{{name}}")
|
2015-11-01 15:39:08 +00:00
|
|
|
if err != nil {
|
2018-04-27 01:06:10 +00:00
|
|
|
return err
|
2015-11-01 15:39:08 +00:00
|
|
|
}
|
|
|
|
|
2018-04-27 01:06:10 +00:00
|
|
|
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
|
|
|
|
}
|
2015-11-01 15:39:08 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-04-27 01:06:10 +00:00
|
|
|
func resourceComputeTargetHttpProxyImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
|
2015-11-01 15:39:08 +00:00
|
|
|
config := meta.(*Config)
|
2018-04-27 01:06:10 +00:00
|
|
|
parseImportId([]string{"projects/(?P<project>[^/]+)/global/targetHttpProxies/(?P<name>[^/]+)", "(?P<project>[^/]+)/(?P<name>[^/]+)", "(?P<name>[^/]+)"}, d, config)
|
2015-11-01 15:39:08 +00:00
|
|
|
|
2018-04-27 01:06:10 +00:00
|
|
|
// Replace import id for the resource id
|
|
|
|
id, err := replaceVars(d, config, "{{name}}")
|
2016-04-10 16:59:57 +00:00
|
|
|
if err != nil {
|
2018-04-27 01:06:10 +00:00
|
|
|
return nil, fmt.Errorf("Error constructing id: %s", err)
|
2016-04-10 16:59:57 +00:00
|
|
|
}
|
2018-04-27 01:06:10 +00:00
|
|
|
d.SetId(id)
|
2016-04-10 16:59:57 +00:00
|
|
|
|
2018-04-27 01:06:10 +00:00
|
|
|
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.
|
2015-11-01 15:39:08 +00:00
|
|
|
}
|
2018-04-27 01:06:10 +00:00
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
|
|
|
func flattenComputeTargetHttpProxyName(v interface{}) interface{} {
|
|
|
|
return v
|
|
|
|
}
|
2015-11-01 15:39:08 +00:00
|
|
|
|
2018-04-27 01:06:10 +00:00
|
|
|
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)
|
2015-11-01 15:39:08 +00:00
|
|
|
if err != nil {
|
2018-04-27 01:06:10 +00:00
|
|
|
return nil, fmt.Errorf("Invalid value for url_map: %s", err)
|
2015-11-01 15:39:08 +00:00
|
|
|
}
|
2018-04-27 01:06:10 +00:00
|
|
|
return f.RelativeLink(), nil
|
2015-11-01 15:39:08 +00:00
|
|
|
}
|