Skip to content

Commit

Permalink
Generate google_target_http_proxy using MagicModule
Browse files Browse the repository at this point in the history
  • Loading branch information
rosbo authored and modular-magician committed Apr 26, 2018
1 parent 31a5c9d commit 9653fe6
Show file tree
Hide file tree
Showing 2 changed files with 267 additions and 79 deletions.
265 changes: 206 additions & 59 deletions google/resource_compute_target_http_proxy.go
Original file line number Diff line number Diff line change
@@ -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,
},
Expand All @@ -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)

Expand All @@ -101,75 +180,143 @@ 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)
if err != nil {
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<project>[^/]+)/global/targetHttpProxies/(?P<name>[^/]+)", "(?P<project>[^/]+)/(?P<name>[^/]+)", "(?P<name>[^/]+)"}, 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
}
Loading

0 comments on commit 9653fe6

Please sign in to comment.