Skip to content

Commit

Permalink
Cleanup BackendService after generation, add omitted fields. (hashico…
Browse files Browse the repository at this point in the history
…rp#584)

<!-- This change is generated by MagicModules. -->
Original Author: @rileykarson
  • Loading branch information
modular-magician authored and rileykarson committed Apr 4, 2019
1 parent 6bbd8fd commit 4e237f3
Show file tree
Hide file tree
Showing 4 changed files with 208 additions and 317 deletions.
189 changes: 189 additions & 0 deletions google-beta/resource_compute_backend_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,150 @@
package google

import (
"bytes"
"fmt"
"log"
"reflect"
"strconv"
"time"

"github.com/hashicorp/errwrap"
"github.com/hashicorp/terraform/helper/hashcode"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
"google.golang.org/api/compute/v1"
)

func resourceGoogleComputeBackendServiceBackendHash(v interface{}) int {
if v == nil {
return 0
}

var buf bytes.Buffer
m := v.(map[string]interface{})
log.Printf("[DEBUG] hashing %v", m)

if group, err := getRelativePath(m["group"].(string)); err != nil {
log.Printf("[WARN] Error on retrieving relative path of instance group: %s", err)
buf.WriteString(fmt.Sprintf("%s-", m["group"].(string)))
} else {
buf.WriteString(fmt.Sprintf("%s-", group))
}

if v, ok := m["balancing_mode"]; ok {
if v == nil {
v = ""
}

buf.WriteString(fmt.Sprintf("%s-", v.(string)))
}
if v, ok := m["capacity_scaler"]; ok {
if v == nil {
v = 0.0
}

buf.WriteString(fmt.Sprintf("%f-", v.(float64)))
}
if v, ok := m["description"]; ok {
if v == nil {
v = ""
}

log.Printf("[DEBUG] writing description %s", v)
buf.WriteString(fmt.Sprintf("%s-", v.(string)))
}
if v, ok := m["max_rate"]; ok {
if v == nil {
v = 0
}

buf.WriteString(fmt.Sprintf("%d-", int64(v.(int))))
}
if v, ok := m["max_rate_per_instance"]; ok {
if v == nil {
v = 0.0
}

buf.WriteString(fmt.Sprintf("%f-", v.(float64)))
}
if v, ok := m["max_connections"]; ok {
if v == nil {
v = 0
}

switch v := v.(type) {
case float64:
// The Golang JSON library can't tell int values apart from floats,
// because MM doesn't give fields strong types. Since another value
// in this block was a real float, it assumed this was a float too.
// It's not.
// Note that math.Round in Go is from float64 -> float64, so it will
// be a noop. int(floatVal) truncates extra parts, so if the float64
// representation of an int falls below the real value we'll have
// the wrong value. eg if 3 was represented as 2.999999, that would
// convert to 2. So we add 0.5, ensuring that we'll truncate to the
// correct value. This wouldn't remain true if we were far enough
// from 0 that we were off by > 0.5, but no float conversion *could*
// work correctly in that case. 53-bit floating types as the only
// numeric type was not a good idea, thanks Javascript.
var vInt int
if v < 0 {
vInt = int(v - 0.5)
} else {
vInt = int(v + 0.5)
}

log.Printf("[DEBUG] writing float value %f as integer value %v", v, vInt)
buf.WriteString(fmt.Sprintf("%d-", vInt))
default:
buf.WriteString(fmt.Sprintf("%d-", int64(v.(int))))
}
}
if v, ok := m["max_connections_per_instance"]; ok {
if v == nil {
v = 0
}

switch v := v.(type) {
case float64:
// The Golang JSON library can't tell int values apart from floats,
// because MM doesn't give fields strong types. Since another value
// in this block was a real float, it assumed this was a float too.
// It's not.
// Note that math.Round in Go is from float64 -> float64, so it will
// be a noop. int(floatVal) truncates extra parts, so if the float64
// representation of an int falls below the real value we'll have
// the wrong value. eg if 3 was represented as 2.999999, that would
// convert to 2. So we add 0.5, ensuring that we'll truncate to the
// correct value. This wouldn't remain true if we were far enough
// from 0 that we were off by > 0.5, but no float conversion *could*
// work correctly in that case. 53-bit floating types as the only
// numeric type was not a good idea, thanks Javascript.
var vInt int
if v < 0 {
vInt = int(v - 0.5)
} else {
vInt = int(v + 0.5)
}

log.Printf("[DEBUG] writing float value %f as integer value %v", v, vInt)
buf.WriteString(fmt.Sprintf("%d-", vInt))
default:
buf.WriteString(fmt.Sprintf("%d-", int64(v.(int))))
}
}
if v, ok := m["max_rate_per_instance"]; ok {
if v == nil {
v = 0.0
}

buf.WriteString(fmt.Sprintf("%f-", v.(float64)))
}

log.Printf("[DEBUG] computed hash value of %v from %v", hashcode.String(buf.String()), buf.String())
return hashcode.String(buf.String())
}

func resourceComputeBackendService() *schema.Resource {
return &schema.Resource{
Create: resourceComputeBackendServiceCreate,
Expand Down Expand Up @@ -116,6 +248,11 @@ func resourceComputeBackendService() *schema.Resource {
},
},
},
"signed_url_cache_max_age_sec": {
Type: schema.TypeInt,
Optional: true,
Default: 3600,
},
},
},
},
Expand Down Expand Up @@ -164,6 +301,12 @@ func resourceComputeBackendService() *schema.Resource {
},
},
},
"load_balancing_scheme": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice([]string{"INTERNAL", "EXTERNAL", ""}, false),
Default: "EXTERNAL",
},
"port_name": {
Type: schema.TypeString,
Computed: true,
Expand Down Expand Up @@ -325,6 +468,12 @@ func resourceComputeBackendServiceCreate(d *schema.ResourceData, meta interface{
} else if v, ok := d.GetOkExists("iap"); ok || !reflect.DeepEqual(v, iapProp) {
obj["iap"] = iapProp
}
loadBalancingSchemeProp, err := expandComputeBackendServiceLoadBalancingScheme(d.Get("load_balancing_scheme"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("load_balancing_scheme"); !isEmptyValue(reflect.ValueOf(loadBalancingSchemeProp)) && (ok || !reflect.DeepEqual(v, loadBalancingSchemeProp)) {
obj["loadBalancingScheme"] = loadBalancingSchemeProp
}
nameProp, err := expandComputeBackendServiceName(d.Get("name"), d, config)
if err != nil {
return err
Expand Down Expand Up @@ -492,6 +641,9 @@ func resourceComputeBackendServiceRead(d *schema.ResourceData, meta interface{})
if err := d.Set("iap", flattenComputeBackendServiceIap(res["iap"], d)); err != nil {
return fmt.Errorf("Error reading BackendService: %s", err)
}
if err := d.Set("load_balancing_scheme", flattenComputeBackendServiceLoadBalancingScheme(res["loadBalancingScheme"], d)); err != nil {
return fmt.Errorf("Error reading BackendService: %s", err)
}
if err := d.Set("name", flattenComputeBackendServiceName(res["name"], d)); err != nil {
return fmt.Errorf("Error reading BackendService: %s", err)
}
Expand Down Expand Up @@ -581,6 +733,12 @@ func resourceComputeBackendServiceUpdate(d *schema.ResourceData, meta interface{
} else if v, ok := d.GetOkExists("iap"); ok || !reflect.DeepEqual(v, iapProp) {
obj["iap"] = iapProp
}
loadBalancingSchemeProp, err := expandComputeBackendServiceLoadBalancingScheme(d.Get("load_balancing_scheme"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("load_balancing_scheme"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, loadBalancingSchemeProp)) {
obj["loadBalancingScheme"] = loadBalancingSchemeProp
}
nameProp, err := expandComputeBackendServiceName(d.Get("name"), d, config)
if err != nil {
return err
Expand Down Expand Up @@ -831,6 +989,8 @@ func flattenComputeBackendServiceCdnPolicy(v interface{}, d *schema.ResourceData
transformed := make(map[string]interface{})
transformed["cache_key_policy"] =
flattenComputeBackendServiceCdnPolicyCacheKeyPolicy(original["cacheKeyPolicy"], d)
transformed["signed_url_cache_max_age_sec"] =
flattenComputeBackendServiceCdnPolicySignedUrlCacheMaxAgeSec(original["signedUrlCacheMaxAgeSec"], d)
return []interface{}{transformed}
}
func flattenComputeBackendServiceCdnPolicyCacheKeyPolicy(v interface{}, d *schema.ResourceData) interface{} {
Expand Down Expand Up @@ -880,6 +1040,16 @@ func flattenComputeBackendServiceCdnPolicyCacheKeyPolicyQueryStringWhitelist(v i
return schema.NewSet(schema.HashString, v.([]interface{}))
}

func flattenComputeBackendServiceCdnPolicySignedUrlCacheMaxAgeSec(v interface{}, d *schema.ResourceData) interface{} {
// Handles the string fixed64 format
if strVal, ok := v.(string); ok {
if intVal, err := strconv.ParseInt(strVal, 10, 64); err == nil {
return intVal
} // let terraform core handle it if we can't convert the string to an int.
}
return v
}

func flattenComputeBackendServiceConnectionDrainingConnection_draining_timeout_sec(v interface{}, d *schema.ResourceData) interface{} {
// Handles the string fixed64 format
if strVal, ok := v.(string); ok {
Expand Down Expand Up @@ -949,6 +1119,10 @@ func flattenComputeBackendServiceIapOauth2ClientSecretSha256(v interface{}, d *s
return v
}

func flattenComputeBackendServiceLoadBalancingScheme(v interface{}, d *schema.ResourceData) interface{} {
return v
}

func flattenComputeBackendServiceName(v interface{}, d *schema.ResourceData) interface{} {
return v
}
Expand Down Expand Up @@ -1118,6 +1292,13 @@ func expandComputeBackendServiceCdnPolicy(v interface{}, d TerraformResourceData
transformed["cacheKeyPolicy"] = transformedCacheKeyPolicy
}

transformedSignedUrlCacheMaxAgeSec, err := expandComputeBackendServiceCdnPolicySignedUrlCacheMaxAgeSec(original["signed_url_cache_max_age_sec"], d, config)
if err != nil {
return nil, err
} else if val := reflect.ValueOf(transformedSignedUrlCacheMaxAgeSec); val.IsValid() && !isEmptyValue(val) {
transformed["signedUrlCacheMaxAgeSec"] = transformedSignedUrlCacheMaxAgeSec
}

return transformed, nil
}

Expand Down Expand Up @@ -1190,6 +1371,10 @@ func expandComputeBackendServiceCdnPolicyCacheKeyPolicyQueryStringWhitelist(v in
return v, nil
}

func expandComputeBackendServiceCdnPolicySignedUrlCacheMaxAgeSec(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}

func expandComputeBackendServiceConnectionDraining(d TerraformResourceData, config *Config) (interface{}, error) {
transformed := make(map[string]interface{})
// Note that nesting flattened objects won't work because we don't handle them properly here.
Expand Down Expand Up @@ -1274,6 +1459,10 @@ func expandComputeBackendServiceIapOauth2ClientSecretSha256(v interface{}, d Ter
return v, nil
}

func expandComputeBackendServiceLoadBalancingScheme(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}

func expandComputeBackendServiceName(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}
Expand Down
Loading

0 comments on commit 4e237f3

Please sign in to comment.