// ---------------------------------------------------------------------------- // // *** 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 resourceComputeHttpHealthCheck() *schema.Resource { return &schema.Resource{ Create: resourceComputeHttpHealthCheckCreate, Read: resourceComputeHttpHealthCheckRead, Update: resourceComputeHttpHealthCheckUpdate, Delete: resourceComputeHttpHealthCheckDelete, Importer: &schema.ResourceImporter{ State: resourceComputeHttpHealthCheckImport, }, 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, }, "check_interval_sec": { Type: schema.TypeInt, Optional: true, Default: 5, }, "description": { Type: schema.TypeString, Optional: true, }, "healthy_threshold": { Type: schema.TypeInt, Optional: true, Default: 2, }, "host": { Type: schema.TypeString, Optional: true, }, "port": { Type: schema.TypeInt, Optional: true, Default: 80, }, "request_path": { Type: schema.TypeString, Optional: true, Default: "/", }, "timeout_sec": { Type: schema.TypeInt, Optional: true, Default: 5, }, "unhealthy_threshold": { Type: schema.TypeInt, Optional: true, Default: 2, }, "creation_timestamp": { Type: schema.TypeString, Computed: true, }, "project": { Type: schema.TypeString, Optional: true, Computed: true, ForceNew: true, }, "self_link": { Type: schema.TypeString, Computed: true, }, }, } } func resourceComputeHttpHealthCheckCreate(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) project, err := getProject(d, config) if err != nil { return err } checkIntervalSecProp, err := expandComputeHttpHealthCheckCheckIntervalSec(d.Get("check_interval_sec"), d, config) if err != nil { return err } descriptionProp, err := expandComputeHttpHealthCheckDescription(d.Get("description"), d, config) if err != nil { return err } healthyThresholdProp, err := expandComputeHttpHealthCheckHealthyThreshold(d.Get("healthy_threshold"), d, config) if err != nil { return err } hostProp, err := expandComputeHttpHealthCheckHost(d.Get("host"), d, config) if err != nil { return err } nameProp, err := expandComputeHttpHealthCheckName(d.Get("name"), d, config) if err != nil { return err } portProp, err := expandComputeHttpHealthCheckPort(d.Get("port"), d, config) if err != nil { return err } requestPathProp, err := expandComputeHttpHealthCheckRequestPath(d.Get("request_path"), d, config) if err != nil { return err } timeoutSecProp, err := expandComputeHttpHealthCheckTimeoutSec(d.Get("timeout_sec"), d, config) if err != nil { return err } unhealthyThresholdProp, err := expandComputeHttpHealthCheckUnhealthyThreshold(d.Get("unhealthy_threshold"), d, config) if err != nil { return err } obj := map[string]interface{}{ "checkIntervalSec": checkIntervalSecProp, "description": descriptionProp, "healthyThreshold": healthyThresholdProp, "host": hostProp, "name": nameProp, "port": portProp, "requestPath": requestPathProp, "timeoutSec": timeoutSecProp, "unhealthyThreshold": unhealthyThresholdProp, } url, err := replaceVars(d, config, "https://www.googleapis.com/compute/v1/projects/{{project}}/global/httpHealthChecks") if err != nil { return err } log.Printf("[DEBUG] Creating new HttpHealthCheck: %#v", obj) res, err := Post(config, url, obj) if err != nil { return fmt.Errorf("Error creating HttpHealthCheck: %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 HttpHealthCheck", int(d.Timeout(schema.TimeoutCreate).Minutes())) if waitErr != nil { // The resource didn't actually create d.SetId("") return fmt.Errorf("Error waiting to create HttpHealthCheck: %s", waitErr) } log.Printf("[DEBUG] Finished creating HttpHealthCheck %q: %#v", d.Id(), res) return resourceComputeHttpHealthCheckRead(d, meta) } func resourceComputeHttpHealthCheckRead(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/httpHealthChecks/{{name}}") if err != nil { return err } res, err := Get(config, url) if err != nil { return handleNotFoundError(err, d, fmt.Sprintf("ComputeHttpHealthCheck %q", d.Id())) } if err := d.Set("check_interval_sec", flattenComputeHttpHealthCheckCheckIntervalSec(res["checkIntervalSec"])); err != nil { return fmt.Errorf("Error reading HttpHealthCheck: %s", err) } if err := d.Set("creation_timestamp", flattenComputeHttpHealthCheckCreationTimestamp(res["creationTimestamp"])); err != nil { return fmt.Errorf("Error reading HttpHealthCheck: %s", err) } if err := d.Set("description", flattenComputeHttpHealthCheckDescription(res["description"])); err != nil { return fmt.Errorf("Error reading HttpHealthCheck: %s", err) } if err := d.Set("healthy_threshold", flattenComputeHttpHealthCheckHealthyThreshold(res["healthyThreshold"])); err != nil { return fmt.Errorf("Error reading HttpHealthCheck: %s", err) } if err := d.Set("host", flattenComputeHttpHealthCheckHost(res["host"])); err != nil { return fmt.Errorf("Error reading HttpHealthCheck: %s", err) } if err := d.Set("name", flattenComputeHttpHealthCheckName(res["name"])); err != nil { return fmt.Errorf("Error reading HttpHealthCheck: %s", err) } if err := d.Set("port", flattenComputeHttpHealthCheckPort(res["port"])); err != nil { return fmt.Errorf("Error reading HttpHealthCheck: %s", err) } if err := d.Set("request_path", flattenComputeHttpHealthCheckRequestPath(res["requestPath"])); err != nil { return fmt.Errorf("Error reading HttpHealthCheck: %s", err) } if err := d.Set("timeout_sec", flattenComputeHttpHealthCheckTimeoutSec(res["timeoutSec"])); err != nil { return fmt.Errorf("Error reading HttpHealthCheck: %s", err) } if err := d.Set("unhealthy_threshold", flattenComputeHttpHealthCheckUnhealthyThreshold(res["unhealthyThreshold"])); err != nil { return fmt.Errorf("Error reading HttpHealthCheck: %s", err) } if err := d.Set("self_link", res["selfLink"]); err != nil { return fmt.Errorf("Error reading HttpHealthCheck: %s", err) } if err := d.Set("project", project); err != nil { return fmt.Errorf("Error reading HttpHealthCheck: %s", err) } return nil } func resourceComputeHttpHealthCheckUpdate(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) project, err := getProject(d, config) if err != nil { return err } checkIntervalSecProp, err := expandComputeHttpHealthCheckCheckIntervalSec(d.Get("check_interval_sec"), d, config) if err != nil { return err } descriptionProp, err := expandComputeHttpHealthCheckDescription(d.Get("description"), d, config) if err != nil { return err } healthyThresholdProp, err := expandComputeHttpHealthCheckHealthyThreshold(d.Get("healthy_threshold"), d, config) if err != nil { return err } hostProp, err := expandComputeHttpHealthCheckHost(d.Get("host"), d, config) if err != nil { return err } nameProp, err := expandComputeHttpHealthCheckName(d.Get("name"), d, config) if err != nil { return err } portProp, err := expandComputeHttpHealthCheckPort(d.Get("port"), d, config) if err != nil { return err } requestPathProp, err := expandComputeHttpHealthCheckRequestPath(d.Get("request_path"), d, config) if err != nil { return err } timeoutSecProp, err := expandComputeHttpHealthCheckTimeoutSec(d.Get("timeout_sec"), d, config) if err != nil { return err } unhealthyThresholdProp, err := expandComputeHttpHealthCheckUnhealthyThreshold(d.Get("unhealthy_threshold"), d, config) if err != nil { return err } obj := map[string]interface{}{ "checkIntervalSec": checkIntervalSecProp, "description": descriptionProp, "healthyThreshold": healthyThresholdProp, "host": hostProp, "name": nameProp, "port": portProp, "requestPath": requestPathProp, "timeoutSec": timeoutSecProp, "unhealthyThreshold": unhealthyThresholdProp, } url, err := replaceVars(d, config, "https://www.googleapis.com/compute/v1/projects/{{project}}/global/httpHealthChecks/{{name}}") if err != nil { return err } log.Printf("[DEBUG] Updating HttpHealthCheck %q: %#v", d.Id(), obj) res, err := sendRequest(config, "PUT", url, obj) if err != nil { return fmt.Errorf("Error updating HttpHealthCheck %q: %s", d.Id(), err) } op := &compute.Operation{} err = Convert(res, op) if err != nil { return err } err = computeOperationWaitTime( config.clientCompute, op, project, "Updating HttpHealthCheck", int(d.Timeout(schema.TimeoutUpdate).Minutes())) if err != nil { return err } return resourceComputeHttpHealthCheckRead(d, meta) } func resourceComputeHttpHealthCheckDelete(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/httpHealthChecks/{{name}}") if err != nil { return err } log.Printf("[DEBUG] Deleting HttpHealthCheck %q", d.Id()) res, err := Delete(config, url) if err != nil { return fmt.Errorf("Error deleting HttpHealthCheck %q: %s", d.Id(), err) } op := &compute.Operation{} err = Convert(res, op) if err != nil { return err } err = computeOperationWaitTime( config.clientCompute, op, project, "Deleting HttpHealthCheck", int(d.Timeout(schema.TimeoutDelete).Minutes())) if err != nil { return err } log.Printf("[DEBUG] Finished deleting HttpHealthCheck %q: %#v", d.Id(), res) return nil } func resourceComputeHttpHealthCheckImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { config := meta.(*Config) parseImportId([]string{"projects/(?P[^/]+)/global/httpHealthChecks/(?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 flattenComputeHttpHealthCheckCheckIntervalSec(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 flattenComputeHttpHealthCheckCreationTimestamp(v interface{}) interface{} { return v } func flattenComputeHttpHealthCheckDescription(v interface{}) interface{} { return v } func flattenComputeHttpHealthCheckHealthyThreshold(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 flattenComputeHttpHealthCheckHost(v interface{}) interface{} { return v } func flattenComputeHttpHealthCheckName(v interface{}) interface{} { return v } func flattenComputeHttpHealthCheckPort(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 flattenComputeHttpHealthCheckRequestPath(v interface{}) interface{} { return v } func flattenComputeHttpHealthCheckTimeoutSec(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 flattenComputeHttpHealthCheckUnhealthyThreshold(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 expandComputeHttpHealthCheckCheckIntervalSec(v interface{}, d *schema.ResourceData, config *Config) (interface{}, error) { return v, nil } func expandComputeHttpHealthCheckDescription(v interface{}, d *schema.ResourceData, config *Config) (interface{}, error) { return v, nil } func expandComputeHttpHealthCheckHealthyThreshold(v interface{}, d *schema.ResourceData, config *Config) (interface{}, error) { return v, nil } func expandComputeHttpHealthCheckHost(v interface{}, d *schema.ResourceData, config *Config) (interface{}, error) { return v, nil } func expandComputeHttpHealthCheckName(v interface{}, d *schema.ResourceData, config *Config) (interface{}, error) { return v, nil } func expandComputeHttpHealthCheckPort(v interface{}, d *schema.ResourceData, config *Config) (interface{}, error) { return v, nil } func expandComputeHttpHealthCheckRequestPath(v interface{}, d *schema.ResourceData, config *Config) (interface{}, error) { return v, nil } func expandComputeHttpHealthCheckTimeoutSec(v interface{}, d *schema.ResourceData, config *Config) (interface{}, error) { return v, nil } func expandComputeHttpHealthCheckUnhealthyThreshold(v interface{}, d *schema.ResourceData, config *Config) (interface{}, error) { return v, nil }