github.com/ndarilek/terraform@v0.3.8-0.20150320140257-d3135c1b2bac/builtin/providers/google/resource_compute_http_health_check.go (about) 1 package google 2 3 import ( 4 "fmt" 5 "log" 6 "time" 7 8 "code.google.com/p/google-api-go-client/compute/v1" 9 "code.google.com/p/google-api-go-client/googleapi" 10 "github.com/hashicorp/terraform/helper/schema" 11 ) 12 13 func resourceComputeHttpHealthCheck() *schema.Resource { 14 return &schema.Resource{ 15 Create: resourceComputeHttpHealthCheckCreate, 16 Read: resourceComputeHttpHealthCheckRead, 17 Delete: resourceComputeHttpHealthCheckDelete, 18 Update: resourceComputeHttpHealthCheckUpdate, 19 20 Schema: map[string]*schema.Schema{ 21 "check_interval_sec": &schema.Schema{ 22 Type: schema.TypeInt, 23 Optional: true, 24 Computed: true, 25 }, 26 27 "description": &schema.Schema{ 28 Type: schema.TypeString, 29 Optional: true, 30 }, 31 32 "healthy_threshold": &schema.Schema{ 33 Type: schema.TypeInt, 34 Optional: true, 35 Computed: true, 36 }, 37 38 "host": &schema.Schema{ 39 Type: schema.TypeString, 40 Optional: true, 41 }, 42 43 "name": &schema.Schema{ 44 Type: schema.TypeString, 45 Required: true, 46 ForceNew: true, 47 }, 48 49 "port": &schema.Schema{ 50 Type: schema.TypeInt, 51 Optional: true, 52 Computed: true, 53 }, 54 55 "request_path": &schema.Schema{ 56 Type: schema.TypeString, 57 Optional: true, 58 Computed: true, 59 }, 60 61 "self_link": &schema.Schema{ 62 Type: schema.TypeString, 63 Computed: true, 64 }, 65 66 "timeout_sec": &schema.Schema{ 67 Type: schema.TypeInt, 68 Optional: true, 69 Computed: true, 70 }, 71 72 "unhealthy_threshold": &schema.Schema{ 73 Type: schema.TypeInt, 74 Optional: true, 75 Computed: true, 76 }, 77 }, 78 } 79 } 80 81 func resourceComputeHttpHealthCheckCreate(d *schema.ResourceData, meta interface{}) error { 82 config := meta.(*Config) 83 84 // Build the parameter 85 hchk := &compute.HttpHealthCheck{ 86 Name: d.Get("name").(string), 87 } 88 // Optional things 89 if v, ok := d.GetOk("description"); ok { 90 hchk.Description = v.(string) 91 } 92 if v, ok := d.GetOk("host"); ok { 93 hchk.Host = v.(string) 94 } 95 if v, ok := d.GetOk("request_path"); ok { 96 hchk.RequestPath = v.(string) 97 } 98 if v, ok := d.GetOk("check_interval_sec"); ok { 99 hchk.CheckIntervalSec = int64(v.(int)) 100 } 101 if v, ok := d.GetOk("health_threshold"); ok { 102 hchk.HealthyThreshold = int64(v.(int)) 103 } 104 if v, ok := d.GetOk("port"); ok { 105 hchk.Port = int64(v.(int)) 106 } 107 if v, ok := d.GetOk("timeout_sec"); ok { 108 hchk.TimeoutSec = int64(v.(int)) 109 } 110 if v, ok := d.GetOk("unhealthy_threshold"); ok { 111 hchk.UnhealthyThreshold = int64(v.(int)) 112 } 113 114 log.Printf("[DEBUG] HttpHealthCheck insert request: %#v", hchk) 115 op, err := config.clientCompute.HttpHealthChecks.Insert( 116 config.Project, hchk).Do() 117 if err != nil { 118 return fmt.Errorf("Error creating HttpHealthCheck: %s", err) 119 } 120 121 // It probably maybe worked, so store the ID now 122 d.SetId(hchk.Name) 123 124 // Wait for the operation to complete 125 w := &OperationWaiter{ 126 Service: config.clientCompute, 127 Op: op, 128 Project: config.Project, 129 Type: OperationWaitGlobal, 130 } 131 state := w.Conf() 132 state.Timeout = 2 * time.Minute 133 state.MinTimeout = 1 * time.Second 134 opRaw, err := state.WaitForState() 135 if err != nil { 136 return fmt.Errorf("Error waiting for HttpHealthCheck to create: %s", err) 137 } 138 op = opRaw.(*compute.Operation) 139 if op.Error != nil { 140 // The resource didn't actually create 141 d.SetId("") 142 143 // Return the error 144 return OperationError(*op.Error) 145 } 146 147 return resourceComputeHttpHealthCheckRead(d, meta) 148 } 149 150 func resourceComputeHttpHealthCheckUpdate(d *schema.ResourceData, meta interface{}) error { 151 config := meta.(*Config) 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("health_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 config.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 // Wait for the operation to complete 194 w := &OperationWaiter{ 195 Service: config.clientCompute, 196 Op: op, 197 Project: config.Project, 198 Type: OperationWaitGlobal, 199 } 200 state := w.Conf() 201 state.Timeout = 2 * time.Minute 202 state.MinTimeout = 1 * time.Second 203 opRaw, err := state.WaitForState() 204 if err != nil { 205 return fmt.Errorf("Error waiting for HttpHealthCheck to patch: %s", err) 206 } 207 op = opRaw.(*compute.Operation) 208 if op.Error != nil { 209 // The resource didn't actually create 210 d.SetId("") 211 212 // Return the error 213 return OperationError(*op.Error) 214 } 215 216 return resourceComputeHttpHealthCheckRead(d, meta) 217 } 218 219 func resourceComputeHttpHealthCheckRead(d *schema.ResourceData, meta interface{}) error { 220 config := meta.(*Config) 221 222 hchk, err := config.clientCompute.HttpHealthChecks.Get( 223 config.Project, d.Id()).Do() 224 if err != nil { 225 if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 { 226 // The resource doesn't exist anymore 227 d.SetId("") 228 229 return nil 230 } 231 232 return fmt.Errorf("Error reading HttpHealthCheck: %s", err) 233 } 234 235 d.Set("host", hchk.Host) 236 d.Set("request_path", hchk.RequestPath) 237 d.Set("check_interval_sec", hchk.CheckIntervalSec) 238 d.Set("health_threshold", hchk.HealthyThreshold) 239 d.Set("port", hchk.Port) 240 d.Set("timeout_sec", hchk.TimeoutSec) 241 d.Set("unhealthy_threshold", hchk.UnhealthyThreshold) 242 d.Set("self_link", hchk.SelfLink) 243 244 return nil 245 } 246 247 func resourceComputeHttpHealthCheckDelete(d *schema.ResourceData, meta interface{}) error { 248 config := meta.(*Config) 249 250 // Delete the HttpHealthCheck 251 op, err := config.clientCompute.HttpHealthChecks.Delete( 252 config.Project, d.Id()).Do() 253 if err != nil { 254 return fmt.Errorf("Error deleting HttpHealthCheck: %s", err) 255 } 256 257 // Wait for the operation to complete 258 w := &OperationWaiter{ 259 Service: config.clientCompute, 260 Op: op, 261 Project: config.Project, 262 Type: OperationWaitGlobal, 263 } 264 state := w.Conf() 265 state.Timeout = 2 * time.Minute 266 state.MinTimeout = 1 * time.Second 267 opRaw, err := state.WaitForState() 268 if err != nil { 269 return fmt.Errorf("Error waiting for HttpHealthCheck to delete: %s", err) 270 } 271 op = opRaw.(*compute.Operation) 272 if op.Error != nil { 273 // Return the error 274 return OperationError(*op.Error) 275 } 276 277 d.SetId("") 278 return nil 279 }