github.com/recobe182/terraform@v0.8.5-0.20170117231232-49ab22a935b7/builtin/providers/google/resource_compute_region_backend_service.go (about) 1 package google 2 3 import ( 4 "bytes" 5 "fmt" 6 "log" 7 "regexp" 8 9 "github.com/hashicorp/terraform/helper/hashcode" 10 "github.com/hashicorp/terraform/helper/schema" 11 "google.golang.org/api/compute/v1" 12 "google.golang.org/api/googleapi" 13 ) 14 15 func resourceComputeRegionBackendService() *schema.Resource { 16 return &schema.Resource{ 17 Create: resourceComputeRegionBackendServiceCreate, 18 Read: resourceComputeRegionBackendServiceRead, 19 Update: resourceComputeRegionBackendServiceUpdate, 20 Delete: resourceComputeRegionBackendServiceDelete, 21 22 Schema: map[string]*schema.Schema{ 23 "name": &schema.Schema{ 24 Type: schema.TypeString, 25 Required: true, 26 ForceNew: true, 27 ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) { 28 value := v.(string) 29 re := `^(?:[a-z](?:[-a-z0-9]{0,61}[a-z0-9])?)$` 30 if !regexp.MustCompile(re).MatchString(value) { 31 errors = append(errors, fmt.Errorf( 32 "%q (%q) doesn't match regexp %q", k, value, re)) 33 } 34 return 35 }, 36 }, 37 38 "health_checks": &schema.Schema{ 39 Type: schema.TypeSet, 40 Elem: &schema.Schema{Type: schema.TypeString}, 41 Required: true, 42 Set: schema.HashString, 43 }, 44 45 "backend": &schema.Schema{ 46 Type: schema.TypeSet, 47 Elem: &schema.Resource{ 48 Schema: map[string]*schema.Schema{ 49 "group": &schema.Schema{ 50 Type: schema.TypeString, 51 Optional: true, 52 }, 53 "description": &schema.Schema{ 54 Type: schema.TypeString, 55 Optional: true, 56 }, 57 }, 58 }, 59 Optional: true, 60 Set: resourceGoogleComputeRegionBackendServiceBackendHash, 61 }, 62 63 "description": &schema.Schema{ 64 Type: schema.TypeString, 65 Optional: true, 66 }, 67 68 "fingerprint": &schema.Schema{ 69 Type: schema.TypeString, 70 Computed: true, 71 }, 72 73 "project": &schema.Schema{ 74 Type: schema.TypeString, 75 Optional: true, 76 ForceNew: true, 77 }, 78 79 "protocol": &schema.Schema{ 80 Type: schema.TypeString, 81 Optional: true, 82 Computed: true, 83 }, 84 85 "region": &schema.Schema{ 86 Type: schema.TypeString, 87 Optional: true, 88 ForceNew: true, 89 }, 90 91 "self_link": &schema.Schema{ 92 Type: schema.TypeString, 93 Computed: true, 94 }, 95 96 "timeout_sec": &schema.Schema{ 97 Type: schema.TypeInt, 98 Optional: true, 99 Computed: true, 100 }, 101 }, 102 } 103 } 104 105 func resourceComputeRegionBackendServiceCreate(d *schema.ResourceData, meta interface{}) error { 106 config := meta.(*Config) 107 108 hc := d.Get("health_checks").(*schema.Set).List() 109 healthChecks := make([]string, 0, len(hc)) 110 for _, v := range hc { 111 healthChecks = append(healthChecks, v.(string)) 112 } 113 114 service := compute.BackendService{ 115 Name: d.Get("name").(string), 116 HealthChecks: healthChecks, 117 LoadBalancingScheme: "INTERNAL", 118 } 119 120 if v, ok := d.GetOk("backend"); ok { 121 service.Backends = expandBackends(v.(*schema.Set).List()) 122 } 123 124 if v, ok := d.GetOk("description"); ok { 125 service.Description = v.(string) 126 } 127 128 if v, ok := d.GetOk("protocol"); ok { 129 service.Protocol = v.(string) 130 } 131 132 if v, ok := d.GetOk("timeout_sec"); ok { 133 service.TimeoutSec = int64(v.(int)) 134 } 135 136 project, err := getProject(d, config) 137 if err != nil { 138 return err 139 } 140 141 region, err := getRegion(d, config) 142 if err != nil { 143 return err 144 } 145 146 log.Printf("[DEBUG] Creating new Region Backend Service: %#v", service) 147 148 op, err := config.clientCompute.RegionBackendServices.Insert( 149 project, region, &service).Do() 150 if err != nil { 151 return fmt.Errorf("Error creating backend service: %s", err) 152 } 153 154 log.Printf("[DEBUG] Waiting for new backend service, operation: %#v", op) 155 156 d.SetId(service.Name) 157 158 err = computeOperationWaitRegion(config, op, project, region, "Creating Region Backend Service") 159 if err != nil { 160 return err 161 } 162 163 return resourceComputeRegionBackendServiceRead(d, meta) 164 } 165 166 func resourceComputeRegionBackendServiceRead(d *schema.ResourceData, meta interface{}) error { 167 config := meta.(*Config) 168 169 project, err := getProject(d, config) 170 if err != nil { 171 return err 172 } 173 174 region, err := getRegion(d, config) 175 if err != nil { 176 return err 177 } 178 179 service, err := config.clientCompute.RegionBackendServices.Get( 180 project, region, d.Id()).Do() 181 if err != nil { 182 if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 { 183 // The resource doesn't exist anymore 184 log.Printf("[WARN] Removing Backend Service %q because it's gone", d.Get("name").(string)) 185 d.SetId("") 186 187 return nil 188 } 189 190 return fmt.Errorf("Error reading service: %s", err) 191 } 192 193 d.Set("description", service.Description) 194 d.Set("protocol", service.Protocol) 195 d.Set("timeout_sec", service.TimeoutSec) 196 d.Set("fingerprint", service.Fingerprint) 197 d.Set("self_link", service.SelfLink) 198 199 d.Set("backend", flattenBackends(service.Backends)) 200 d.Set("health_checks", service.HealthChecks) 201 202 return nil 203 } 204 205 func resourceComputeRegionBackendServiceUpdate(d *schema.ResourceData, meta interface{}) error { 206 config := meta.(*Config) 207 208 project, err := getProject(d, config) 209 if err != nil { 210 return err 211 } 212 213 region, err := getRegion(d, config) 214 if err != nil { 215 return err 216 } 217 218 hc := d.Get("health_checks").(*schema.Set).List() 219 healthChecks := make([]string, 0, len(hc)) 220 for _, v := range hc { 221 healthChecks = append(healthChecks, v.(string)) 222 } 223 224 service := compute.BackendService{ 225 Name: d.Get("name").(string), 226 Fingerprint: d.Get("fingerprint").(string), 227 HealthChecks: healthChecks, 228 LoadBalancingScheme: "INTERNAL", 229 } 230 231 // Optional things 232 if v, ok := d.GetOk("backend"); ok { 233 service.Backends = expandBackends(v.(*schema.Set).List()) 234 } 235 if v, ok := d.GetOk("description"); ok { 236 service.Description = v.(string) 237 } 238 if v, ok := d.GetOk("protocol"); ok { 239 service.Protocol = v.(string) 240 } 241 if v, ok := d.GetOk("timeout_sec"); ok { 242 service.TimeoutSec = int64(v.(int)) 243 } 244 245 log.Printf("[DEBUG] Updating existing Backend Service %q: %#v", d.Id(), service) 246 op, err := config.clientCompute.RegionBackendServices.Update( 247 project, region, d.Id(), &service).Do() 248 if err != nil { 249 return fmt.Errorf("Error updating backend service: %s", err) 250 } 251 252 d.SetId(service.Name) 253 254 err = computeOperationWaitRegion(config, op, project, region, "Updating Backend Service") 255 if err != nil { 256 return err 257 } 258 259 return resourceComputeRegionBackendServiceRead(d, meta) 260 } 261 262 func resourceComputeRegionBackendServiceDelete(d *schema.ResourceData, meta interface{}) error { 263 config := meta.(*Config) 264 265 project, err := getProject(d, config) 266 if err != nil { 267 return err 268 } 269 270 region, err := getRegion(d, config) 271 if err != nil { 272 return err 273 } 274 275 log.Printf("[DEBUG] Deleting backend service %s", d.Id()) 276 op, err := config.clientCompute.RegionBackendServices.Delete( 277 project, region, d.Id()).Do() 278 if err != nil { 279 return fmt.Errorf("Error deleting backend service: %s", err) 280 } 281 282 err = computeOperationWaitRegion(config, op, project, region, "Deleting Backend Service") 283 if err != nil { 284 return err 285 } 286 287 d.SetId("") 288 return nil 289 } 290 291 func resourceGoogleComputeRegionBackendServiceBackendHash(v interface{}) int { 292 if v == nil { 293 return 0 294 } 295 296 var buf bytes.Buffer 297 m := v.(map[string]interface{}) 298 299 buf.WriteString(fmt.Sprintf("%s-", m["group"].(string))) 300 301 if v, ok := m["description"]; ok { 302 buf.WriteString(fmt.Sprintf("%s-", v.(string))) 303 } 304 305 return hashcode.String(buf.String()) 306 }