Skip to content

Commit

Permalink
Change group back to Optional in google_compute_backend_service (ha…
Browse files Browse the repository at this point in the history
…shicorp#576)

* change group back to optional

* fix build
  • Loading branch information
danawillow authored and Nic Cope committed Oct 16, 2017
1 parent 2eee510 commit bd6505a
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 14 deletions.
40 changes: 28 additions & 12 deletions google/resource_compute_backend_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package google

import (
"bytes"
"errors"
"fmt"
"log"

Expand Down Expand Up @@ -44,7 +45,7 @@ func resourceComputeBackendService() *schema.Resource {
Schema: map[string]*schema.Schema{
"group": &schema.Schema{
Type: schema.TypeString,
Required: true,
Optional: true,
DiffSuppressFunc: compareSelfLinkRelativePaths,
},
"balancing_mode": &schema.Schema{
Expand Down Expand Up @@ -150,7 +151,10 @@ func resourceComputeBackendService() *schema.Resource {
func resourceComputeBackendServiceCreate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)

service := expandBackendService(d)
service, err := expandBackendService(d)
if err != nil {
return err
}

project, err := getProject(d, config)
if err != nil {
Expand All @@ -159,7 +163,7 @@ func resourceComputeBackendServiceCreate(d *schema.ResourceData, meta interface{

log.Printf("[DEBUG] Creating new Backend Service: %#v", service)
op, err := config.clientCompute.BackendServices.Insert(
project, &service).Do()
project, service).Do()
if err != nil {
return fmt.Errorf("Error creating backend service: %s", err)
}
Expand Down Expand Up @@ -214,7 +218,10 @@ func resourceComputeBackendServiceRead(d *schema.ResourceData, meta interface{})
func resourceComputeBackendServiceUpdate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)

service := expandBackendService(d)
service, err := expandBackendService(d)
if err != nil {
return err
}
service.Fingerprint = d.Get("fingerprint").(string)

project, err := getProject(d, config)
Expand All @@ -224,7 +231,7 @@ func resourceComputeBackendServiceUpdate(d *schema.ResourceData, meta interface{

log.Printf("[DEBUG] Updating existing Backend Service %q: %#v", d.Id(), service)
op, err := config.clientCompute.BackendServices.Update(
project, d.Id(), &service).Do()
project, d.Id(), service).Do()
if err != nil {
return fmt.Errorf("Error updating backend service: %s", err)
}
Expand Down Expand Up @@ -263,14 +270,19 @@ func resourceComputeBackendServiceDelete(d *schema.ResourceData, meta interface{
return nil
}

func expandBackends(configured []interface{}) []*compute.Backend {
func expandBackends(configured []interface{}) ([]*compute.Backend, error) {
backends := make([]*compute.Backend, 0, len(configured))

for _, raw := range configured {
data := raw.(map[string]interface{})

g, ok := data["group"]
if !ok {
return nil, errors.New("google_compute_backend_service.backend.group must be set")
}

b := compute.Backend{
Group: data["group"].(string),
Group: g.(string),
}

if v, ok := data["balancing_mode"]; ok {
Expand All @@ -295,7 +307,7 @@ func expandBackends(configured []interface{}) []*compute.Backend {
backends = append(backends, &b)
}

return backends
return backends, nil
}

func flattenBackends(backends []*compute.Backend) []map[string]interface{} {
Expand All @@ -318,20 +330,24 @@ func flattenBackends(backends []*compute.Backend) []map[string]interface{} {
return result
}

func expandBackendService(d *schema.ResourceData) compute.BackendService {
func expandBackendService(d *schema.ResourceData) (*compute.BackendService, error) {
hc := d.Get("health_checks").(*schema.Set).List()
healthChecks := make([]string, 0, len(hc))
for _, v := range hc {
healthChecks = append(healthChecks, v.(string))
}

service := compute.BackendService{
service := &compute.BackendService{
Name: d.Get("name").(string),
HealthChecks: healthChecks,
}

var err error
if v, ok := d.GetOk("backend"); ok {
service.Backends = expandBackends(v.(*schema.Set).List())
service.Backends, err = expandBackends(v.(*schema.Set).List())
if err != nil {
return nil, err
}
}

if v, ok := d.GetOk("description"); ok {
Expand Down Expand Up @@ -365,7 +381,7 @@ func expandBackendService(d *schema.ResourceData) compute.BackendService {

service.ConnectionDraining = connectionDraining

return service
return service, nil
}

func resourceGoogleComputeBackendServiceBackendHash(v interface{}) int {
Expand Down
11 changes: 9 additions & 2 deletions google/resource_compute_region_backend_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,12 @@ func resourceComputeRegionBackendServiceCreate(d *schema.ResourceData, meta inte
LoadBalancingScheme: "INTERNAL",
}

var err error
if v, ok := d.GetOk("backend"); ok {
service.Backends = expandBackends(v.(*schema.Set).List())
service.Backends, err = expandBackends(v.(*schema.Set).List())
if err != nil {
return err
}
}

if v, ok := d.GetOk("description"); ok {
Expand Down Expand Up @@ -239,7 +243,10 @@ func resourceComputeRegionBackendServiceUpdate(d *schema.ResourceData, meta inte

// Optional things
if v, ok := d.GetOk("backend"); ok {
service.Backends = expandBackends(v.(*schema.Set).List())
service.Backends, err = expandBackends(v.(*schema.Set).List())
if err != nil {
return err
}
}
if v, ok := d.GetOk("description"); ok {
service.Description = v.(string)
Expand Down

0 comments on commit bd6505a

Please sign in to comment.