github.com/koding/terraform@v0.6.4-0.20170608090606-5d7e0339779d/builtin/providers/google/resource_compute_http_health_check.go (about) 1 package google 2 3 import ( 4 "fmt" 5 "log" 6 7 "github.com/hashicorp/terraform/helper/schema" 8 "google.golang.org/api/compute/v1" 9 ) 10 11 func resourceComputeHttpHealthCheck() *schema.Resource { 12 return &schema.Resource{ 13 Create: resourceComputeHttpHealthCheckCreate, 14 Read: resourceComputeHttpHealthCheckRead, 15 Delete: resourceComputeHttpHealthCheckDelete, 16 Update: resourceComputeHttpHealthCheckUpdate, 17 Importer: &schema.ResourceImporter{ 18 State: schema.ImportStatePassthrough, 19 }, 20 21 Schema: map[string]*schema.Schema{ 22 "name": &schema.Schema{ 23 Type: schema.TypeString, 24 Required: true, 25 ForceNew: true, 26 }, 27 28 "check_interval_sec": &schema.Schema{ 29 Type: schema.TypeInt, 30 Optional: true, 31 Default: 5, 32 }, 33 34 "description": &schema.Schema{ 35 Type: schema.TypeString, 36 Optional: true, 37 }, 38 39 "healthy_threshold": &schema.Schema{ 40 Type: schema.TypeInt, 41 Optional: true, 42 Default: 2, 43 }, 44 45 "host": &schema.Schema{ 46 Type: schema.TypeString, 47 Optional: true, 48 }, 49 50 "port": &schema.Schema{ 51 Type: schema.TypeInt, 52 Optional: true, 53 Default: 80, 54 }, 55 56 "project": &schema.Schema{ 57 Type: schema.TypeString, 58 Optional: true, 59 ForceNew: true, 60 Computed: true, 61 }, 62 63 "request_path": &schema.Schema{ 64 Type: schema.TypeString, 65 Optional: true, 66 Default: "/", 67 }, 68 69 "self_link": &schema.Schema{ 70 Type: schema.TypeString, 71 Computed: true, 72 }, 73 74 "timeout_sec": &schema.Schema{ 75 Type: schema.TypeInt, 76 Optional: true, 77 Default: 5, 78 }, 79 80 "unhealthy_threshold": &schema.Schema{ 81 Type: schema.TypeInt, 82 Optional: true, 83 Default: 2, 84 }, 85 }, 86 } 87 } 88 89 func resourceComputeHttpHealthCheckCreate(d *schema.ResourceData, meta interface{}) error { 90 config := meta.(*Config) 91 92 project, err := getProject(d, config) 93 if err != nil { 94 return err 95 } 96 97 // Build the parameter 98 hchk := &compute.HttpHealthCheck{ 99 Name: d.Get("name").(string), 100 } 101 // Optional things 102 if v, ok := d.GetOk("description"); ok { 103 hchk.Description = v.(string) 104 } 105 if v, ok := d.GetOk("host"); ok { 106 hchk.Host = v.(string) 107 } 108 if v, ok := d.GetOk("request_path"); ok { 109 hchk.RequestPath = v.(string) 110 } 111 if v, ok := d.GetOk("check_interval_sec"); ok { 112 hchk.CheckIntervalSec = int64(v.(int)) 113 } 114 if v, ok := d.GetOk("healthy_threshold"); ok { 115 hchk.HealthyThreshold = int64(v.(int)) 116 } 117 if v, ok := d.GetOk("port"); ok { 118 hchk.Port = int64(v.(int)) 119 } 120 if v, ok := d.GetOk("timeout_sec"); ok { 121 hchk.TimeoutSec = int64(v.(int)) 122 } 123 if v, ok := d.GetOk("unhealthy_threshold"); ok { 124 hchk.UnhealthyThreshold = int64(v.(int)) 125 } 126 127 log.Printf("[DEBUG] HttpHealthCheck insert request: %#v", hchk) 128 op, err := config.clientCompute.HttpHealthChecks.Insert( 129 project, hchk).Do() 130 if err != nil { 131 return fmt.Errorf("Error creating HttpHealthCheck: %s", err) 132 } 133 134 // It probably maybe worked, so store the ID now 135 d.SetId(hchk.Name) 136 137 err = computeOperationWaitGlobal(config, op, project, "Creating Http Health Check") 138 if err != nil { 139 return err 140 } 141 142 return resourceComputeHttpHealthCheckRead(d, meta) 143 } 144 145 func resourceComputeHttpHealthCheckUpdate(d *schema.ResourceData, meta interface{}) error { 146 config := meta.(*Config) 147 148 project, err := getProject(d, config) 149 if err != nil { 150 return err 151 } 152 153 // Build the parameter 154 hchk := &compute.HttpHealthCheck{ 155 Name: d.Get("name").(string), 156 } 157 // Optional things 158 if v, ok := d.GetOk("description"); ok { 159 hchk.Description = v.(string) 160 } 161 if v, ok := d.GetOk("host"); ok { 162 hchk.Host = v.(string) 163 } 164 if v, ok := d.GetOk("request_path"); ok { 165 hchk.RequestPath = v.(string) 166 } 167 if v, ok := d.GetOk("check_interval_sec"); ok { 168 hchk.CheckIntervalSec = int64(v.(int)) 169 } 170 if v, ok := d.GetOk("healthy_threshold"); ok { 171 hchk.HealthyThreshold = int64(v.(int)) 172 } 173 if v, ok := d.GetOk("port"); ok { 174 hchk.Port = int64(v.(int)) 175 } 176 if v, ok := d.GetOk("timeout_sec"); ok { 177 hchk.TimeoutSec = int64(v.(int)) 178 } 179 if v, ok := d.GetOk("unhealthy_threshold"); ok { 180 hchk.UnhealthyThreshold = int64(v.(int)) 181 } 182 183 log.Printf("[DEBUG] HttpHealthCheck patch request: %#v", hchk) 184 op, err := config.clientCompute.HttpHealthChecks.Patch( 185 project, hchk.Name, hchk).Do() 186 if err != nil { 187 return fmt.Errorf("Error patching HttpHealthCheck: %s", err) 188 } 189 190 // It probably maybe worked, so store the ID now 191 d.SetId(hchk.Name) 192 193 err = computeOperationWaitGlobal(config, op, project, "Updating Http Health Check") 194 if err != nil { 195 return err 196 } 197 198 return resourceComputeHttpHealthCheckRead(d, meta) 199 } 200 201 func resourceComputeHttpHealthCheckRead(d *schema.ResourceData, meta interface{}) error { 202 config := meta.(*Config) 203 204 project, err := getProject(d, config) 205 if err != nil { 206 return err 207 } 208 209 hchk, err := config.clientCompute.HttpHealthChecks.Get( 210 project, d.Id()).Do() 211 if err != nil { 212 return handleNotFoundError(err, d, fmt.Sprintf("HTTP Health Check %q", d.Get("name").(string))) 213 } 214 215 d.Set("host", hchk.Host) 216 d.Set("request_path", hchk.RequestPath) 217 d.Set("check_interval_sec", hchk.CheckIntervalSec) 218 d.Set("healthy_threshold", hchk.HealthyThreshold) 219 d.Set("port", hchk.Port) 220 d.Set("timeout_sec", hchk.TimeoutSec) 221 d.Set("unhealthy_threshold", hchk.UnhealthyThreshold) 222 d.Set("self_link", hchk.SelfLink) 223 d.Set("name", hchk.Name) 224 d.Set("description", hchk.Description) 225 d.Set("project", project) 226 227 return nil 228 } 229 230 func resourceComputeHttpHealthCheckDelete(d *schema.ResourceData, meta interface{}) error { 231 config := meta.(*Config) 232 233 project, err := getProject(d, config) 234 if err != nil { 235 return err 236 } 237 238 // Delete the HttpHealthCheck 239 op, err := config.clientCompute.HttpHealthChecks.Delete( 240 project, d.Id()).Do() 241 if err != nil { 242 return fmt.Errorf("Error deleting HttpHealthCheck: %s", err) 243 } 244 245 err = computeOperationWaitGlobal(config, op, project, "Deleting Http Health Check") 246 if err != nil { 247 return err 248 } 249 250 d.SetId("") 251 return nil 252 }