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