Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change group back to Optional in google_compute_backend_service #576

Merged
merged 2 commits into from
Oct 12, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This resource address may not actually be right, if the user names their backend something other than "backend". I'd prefer something along the lines of fmt.Errorf("group field on backend %q must be set", backend.SelfLink), but that's just me.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the address wouldn't contain the name of the backend though, that would be the value. the address would technically be something like google_compute_backend_service.backend.<hash>.group but I figured omitting the hash was fine in this case. We can't use the self link because there isn't a compute.Backend object to take the self link of

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could use the index, I guess. Would that work better?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, no, I'm just confused. Ignore me, nitpick withdrawn. :)

}

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