// ---------------------------------------------------------------------------- // // *** 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" compute "google.golang.org/api/compute/v1" ) func resourceComputeTargetHttpsProxy() *schema.Resource { return &schema.Resource{ Create: resourceComputeTargetHttpsProxyCreate, Read: resourceComputeTargetHttpsProxyRead, Update: resourceComputeTargetHttpsProxyUpdate, Delete: resourceComputeTargetHttpsProxyDelete, Importer: &schema.ResourceImporter{ 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": { Type: schema.TypeString, Required: true, ForceNew: true, }, "ssl_certificates": { Type: schema.TypeList, Required: true, Elem: &schema.Schema{ Type: schema.TypeString, DiffSuppressFunc: compareSelfLinkOrResourceName, }, }, "url_map": { Type: schema.TypeString, Required: true, DiffSuppressFunc: compareSelfLinkOrResourceName, }, "description": { Type: schema.TypeString, Optional: true, ForceNew: true, }, "ssl_policy": { Type: schema.TypeString, Optional: true, DiffSuppressFunc: compareSelfLinkOrResourceName, }, "creation_timestamp": { Type: schema.TypeString, Computed: true, }, "proxy_id": { Type: schema.TypeInt, Computed: true, }, "project": { Type: schema.TypeString, Optional: true, Computed: true, ForceNew: true, }, "self_link": { Type: schema.TypeString, Computed: true, }, }, } } func resourceComputeTargetHttpsProxyCreate(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) project, err := getProject(d, config) if err != nil { return err } 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 } sslPolicyProp, err := expandComputeTargetHttpsProxySslPolicy(d.Get("ssl_policy"), d, config) if err != nil { return err } obj := map[string]interface{}{ "description": descriptionProp, "name": nameProp, "sslCertificates": sslCertificatesProp, "urlMap": urlMapProp, "sslPolicy": sslPolicyProp, } url, err := replaceVars(d, config, "https://www.googleapis.com/compute/v1/projects/{{project}}/global/targetHttpsProxies") if err != nil { return err } 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) } // 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 } waitErr := computeOperationWaitTime( config.clientCompute, op, project, "Creating TargetHttpsProxy", int(d.Timeout(schema.TimeoutCreate).Minutes())) if waitErr != nil { // The resource didn't actually create d.SetId("") return fmt.Errorf("Error waiting to create TargetHttpsProxy: %s", waitErr) } return resourceComputeTargetHttpsProxyRead(d, meta) } func resourceComputeTargetHttpsProxyRead(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/targetHttpsProxies/{{name}}") if err != nil { return err } 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("ssl_policy", flattenComputeTargetHttpsProxySslPolicy(res["sslPolicy"])); 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 obj map[string]interface{} 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") } if d.HasChange("ssl_policy") { sslPolicyProp, err := expandComputeTargetHttpsProxySslPolicy(d.Get("ssl_policy"), d, config) if err != nil { return err } obj = map[string]interface{}{ "sslPolicy": sslPolicyProp, } url, err = replaceVars(d, config, "https://www.googleapis.com/compute/v1/projects/{{project}}/global/targetHttpsProxies/{{name}}/setSslPolicy") 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_policy") } d.Partial(false) return resourceComputeTargetHttpsProxyRead(d, meta) } func resourceComputeTargetHttpsProxyDelete(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/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 } return nil } func resourceComputeTargetHttpsProxyImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { config := meta.(*Config) parseImportId([]string{"projects/(?P[^/]+)/global/targetHttpsProxies/(?P[^/]+)", "(?P[^/]+)/(?P[^/]+)", "(?P[^/]+)"}, d, config) // 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 []*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.ParseInt(strVal, 10, 64); 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 flattenComputeTargetHttpsProxySslPolicy(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 } func expandComputeTargetHttpsProxySslPolicy(v interface{}, d *schema.ResourceData, config *Config) (interface{}, error) { f, err := parseGlobalFieldValue("sslPolicies", v.(string), "project", d, config, true) if err != nil { return nil, fmt.Errorf("Invalid value for ssl_policy: %s", err) } return f.RelativeLink(), nil }