Skip to content

Commit

Permalink
Convenience fields of N depth (#3668)
Browse files Browse the repository at this point in the history
Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
modular-magician authored and chrisst committed May 22, 2019
1 parent d6021ef commit 77de8e2
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 35 deletions.
35 changes: 25 additions & 10 deletions google/resource_compute_backend_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ func resourceComputeBackendServiceCreate(d *schema.ResourceData, meta interface{
} else if v, ok := d.GetOkExists("cdn_policy"); !isEmptyValue(reflect.ValueOf(cdnPolicyProp)) && (ok || !reflect.DeepEqual(v, cdnPolicyProp)) {
obj["cdnPolicy"] = cdnPolicyProp
}
connectionDrainingProp, err := expandComputeBackendServiceConnectionDraining(d, config)
connectionDrainingProp, err := expandComputeBackendServiceConnectionDraining(nil, d, config)
if err != nil {
return err
} else if !isEmptyValue(reflect.ValueOf(connectionDrainingProp)) {
Expand Down Expand Up @@ -557,12 +557,15 @@ func resourceComputeBackendServiceRead(d *schema.ResourceData, meta interface{})
if err := d.Set("cdn_policy", flattenComputeBackendServiceCdnPolicy(res["cdnPolicy"], d)); err != nil {
return fmt.Errorf("Error reading BackendService: %s", err)
}
if v, ok := res["connectionDraining"].(map[string]interface{}); res["connectionDraining"] != nil && ok {
if err := d.Set("connection_draining_timeout_sec", flattenComputeBackendServiceConnectionDrainingConnection_draining_timeout_sec(v["drainingTimeoutSec"], d)); err != nil {
return fmt.Errorf("Error reading BackendService: %s", err)
// Terraform must set the top level schema field, but since this object contains collapsed properties
// it's difficult to know what the top level should be. Instead we just loop over the map returned from flatten.
if flattenedProp := flattenComputeBackendServiceConnectionDraining(res["connectionDraining"], d); flattenedProp != nil {
casted := flattenedProp.([]interface{})[0]
if casted != nil {
for k, v := range casted.(map[string]interface{}) {
d.Set(k, v)
}
}
} else {
d.Set("connection_draining_timeout_sec", nil)
}
if err := d.Set("creation_timestamp", flattenComputeBackendServiceCreationTimestamp(res["creationTimestamp"], d)); err != nil {
return fmt.Errorf("Error reading BackendService: %s", err)
Expand Down Expand Up @@ -632,10 +635,10 @@ func resourceComputeBackendServiceUpdate(d *schema.ResourceData, meta interface{
} else if v, ok := d.GetOkExists("cdn_policy"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, cdnPolicyProp)) {
obj["cdnPolicy"] = cdnPolicyProp
}
connectionDrainingProp, err := expandComputeBackendServiceConnectionDraining(d, config)
connectionDrainingProp, err := expandComputeBackendServiceConnectionDraining(nil, d, config)
if err != nil {
return err
} else if !isEmptyValue(reflect.ValueOf(connectionDrainingProp)) {
} else if v, ok := d.GetOkExists("connection_draining"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, connectionDrainingProp)) {
obj["connectionDraining"] = connectionDrainingProp
}
fingerprintProp, err := expandComputeBackendServiceFingerprint(d.Get("fingerprint"), d, config)
Expand Down Expand Up @@ -985,6 +988,19 @@ func flattenComputeBackendServiceCdnPolicySignedUrlCacheMaxAgeSec(v interface{},
return v
}

func flattenComputeBackendServiceConnectionDraining(v interface{}, d *schema.ResourceData) interface{} {
if v == nil {
return nil
}
original := v.(map[string]interface{})
if len(original) == 0 {
return nil
}
transformed := make(map[string]interface{})
transformed["connection_draining_timeout_sec"] =
flattenComputeBackendServiceConnectionDrainingConnection_draining_timeout_sec(original["drainingTimeoutSec"], d)
return []interface{}{transformed}
}
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 @@ -1299,9 +1315,8 @@ func expandComputeBackendServiceCdnPolicySignedUrlCacheMaxAgeSec(v interface{},
return v, nil
}

func expandComputeBackendServiceConnectionDraining(d TerraformResourceData, config *Config) (interface{}, error) {
func expandComputeBackendServiceConnectionDraining(v interface{}, 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.
transformedConnection_draining_timeout_sec, err := expandComputeBackendServiceConnectionDrainingConnection_draining_timeout_sec(d.Get("connection_draining_timeout_sec"), d, config)
if err != nil {
return nil, err
Expand Down
41 changes: 26 additions & 15 deletions google/resource_compute_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func resourceComputeNetworkCreate(d *schema.ResourceData, meta interface{}) erro
} else if v, ok := d.GetOkExists("auto_create_subnetworks"); !isEmptyValue(reflect.ValueOf(autoCreateSubnetworksProp)) && (ok || !reflect.DeepEqual(v, autoCreateSubnetworksProp)) {
obj["autoCreateSubnetworks"] = autoCreateSubnetworksProp
}
routingConfigProp, err := expandComputeNetworkRoutingConfig(d, config)
routingConfigProp, err := expandComputeNetworkRoutingConfig(nil, d, config)
if err != nil {
return err
} else if !isEmptyValue(reflect.ValueOf(routingConfigProp)) {
Expand Down Expand Up @@ -248,12 +248,15 @@ func resourceComputeNetworkRead(d *schema.ResourceData, meta interface{}) error
if err := d.Set("auto_create_subnetworks", flattenComputeNetworkAutoCreateSubnetworks(res["autoCreateSubnetworks"], d)); err != nil {
return fmt.Errorf("Error reading Network: %s", err)
}
if v, ok := res["routingConfig"].(map[string]interface{}); res["routingConfig"] != nil && ok {
if err := d.Set("routing_mode", flattenComputeNetworkRoutingConfigRoutingMode(v["routingMode"], d)); err != nil {
return fmt.Errorf("Error reading Network: %s", err)
// Terraform must set the top level schema field, but since this object contains collapsed properties
// it's difficult to know what the top level should be. Instead we just loop over the map returned from flatten.
if flattenedProp := flattenComputeNetworkRoutingConfig(res["routingConfig"], d); flattenedProp != nil {
casted := flattenedProp.([]interface{})[0]
if casted != nil {
for k, v := range casted.(map[string]interface{}) {
d.Set(k, v)
}
}
} else {
d.Set("routing_mode", nil)
}
if err := d.Set("self_link", ConvertSelfLinkToV1(res["selfLink"].(string))); err != nil {
return fmt.Errorf("Error reading Network: %s", err)
Expand All @@ -269,12 +272,8 @@ func resourceComputeNetworkUpdate(d *schema.ResourceData, meta interface{}) erro

if d.HasChange("routing_mode") {
obj := make(map[string]interface{})
routingConfigProp, err := expandComputeNetworkRoutingConfig(d, config)
if err != nil {
return err
} else if !isEmptyValue(reflect.ValueOf(routingConfigProp)) {
obj["routingConfig"] = routingConfigProp
}
routingModeProp := d.Get("routing_mode")
obj["routingMode"] = routingModeProp

url, err := replaceVars(d, config, "https://www.googleapis.com/compute/v1/projects/{{project}}/global/networks/{{name}}")
if err != nil {
Expand Down Expand Up @@ -303,7 +302,7 @@ func resourceComputeNetworkUpdate(d *schema.ResourceData, meta interface{}) erro
return err
}

d.SetPartial("routing_config")
d.SetPartial("routing_mode")
}

d.Partial(false)
Expand Down Expand Up @@ -387,6 +386,19 @@ func flattenComputeNetworkAutoCreateSubnetworks(v interface{}, d *schema.Resourc
return v
}

func flattenComputeNetworkRoutingConfig(v interface{}, d *schema.ResourceData) interface{} {
if v == nil {
return nil
}
original := v.(map[string]interface{})
if len(original) == 0 {
return nil
}
transformed := make(map[string]interface{})
transformed["routing_mode"] =
flattenComputeNetworkRoutingConfigRoutingMode(original["routingMode"], d)
return []interface{}{transformed}
}
func flattenComputeNetworkRoutingConfigRoutingMode(v interface{}, d *schema.ResourceData) interface{} {
return v
}
Expand All @@ -407,9 +419,8 @@ func expandComputeNetworkAutoCreateSubnetworks(v interface{}, d TerraformResourc
return v, nil
}

func expandComputeNetworkRoutingConfig(d TerraformResourceData, config *Config) (interface{}, error) {
func expandComputeNetworkRoutingConfig(v interface{}, 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.
transformedRoutingMode, err := expandComputeNetworkRoutingConfigRoutingMode(d.Get("routing_mode"), d, config)
if err != nil {
return nil, err
Expand Down
35 changes: 25 additions & 10 deletions google/resource_compute_region_backend_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ func resourceComputeRegionBackendServiceCreate(d *schema.ResourceData, meta inte
} else if v, ok := d.GetOkExists("timeout_sec"); !isEmptyValue(reflect.ValueOf(timeoutSecProp)) && (ok || !reflect.DeepEqual(v, timeoutSecProp)) {
obj["timeoutSec"] = timeoutSecProp
}
connectionDrainingProp, err := expandComputeRegionBackendServiceConnectionDraining(d, config)
connectionDrainingProp, err := expandComputeRegionBackendServiceConnectionDraining(nil, d, config)
if err != nil {
return err
} else if !isEmptyValue(reflect.ValueOf(connectionDrainingProp)) {
Expand Down Expand Up @@ -303,12 +303,15 @@ func resourceComputeRegionBackendServiceRead(d *schema.ResourceData, meta interf
if err := d.Set("timeout_sec", flattenComputeRegionBackendServiceTimeoutSec(res["timeoutSec"], d)); err != nil {
return fmt.Errorf("Error reading RegionBackendService: %s", err)
}
if v, ok := res["connectionDraining"].(map[string]interface{}); res["connectionDraining"] != nil && ok {
if err := d.Set("connection_draining_timeout_sec", flattenComputeRegionBackendServiceConnectionDrainingConnection_draining_timeout_sec(v["drainingTimeoutSec"], d)); err != nil {
return fmt.Errorf("Error reading RegionBackendService: %s", err)
// Terraform must set the top level schema field, but since this object contains collapsed properties
// it's difficult to know what the top level should be. Instead we just loop over the map returned from flatten.
if flattenedProp := flattenComputeRegionBackendServiceConnectionDraining(res["connectionDraining"], d); flattenedProp != nil {
casted := flattenedProp.([]interface{})[0]
if casted != nil {
for k, v := range casted.(map[string]interface{}) {
d.Set(k, v)
}
}
} else {
d.Set("connection_draining_timeout_sec", nil)
}
if err := d.Set("load_balancing_scheme", flattenComputeRegionBackendServiceLoadBalancingScheme(res["loadBalancingScheme"], d)); err != nil {
return fmt.Errorf("Error reading RegionBackendService: %s", err)
Expand Down Expand Up @@ -378,10 +381,10 @@ func resourceComputeRegionBackendServiceUpdate(d *schema.ResourceData, meta inte
} else if v, ok := d.GetOkExists("timeout_sec"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, timeoutSecProp)) {
obj["timeoutSec"] = timeoutSecProp
}
connectionDrainingProp, err := expandComputeRegionBackendServiceConnectionDraining(d, config)
connectionDrainingProp, err := expandComputeRegionBackendServiceConnectionDraining(nil, d, config)
if err != nil {
return err
} else if !isEmptyValue(reflect.ValueOf(connectionDrainingProp)) {
} else if v, ok := d.GetOkExists("connection_draining"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, connectionDrainingProp)) {
obj["connectionDraining"] = connectionDrainingProp
}
loadBalancingSchemeProp, err := expandComputeRegionBackendServiceLoadBalancingScheme(d.Get("load_balancing_scheme"), d, config)
Expand Down Expand Up @@ -551,6 +554,19 @@ func flattenComputeRegionBackendServiceTimeoutSec(v interface{}, d *schema.Resou
return v
}

func flattenComputeRegionBackendServiceConnectionDraining(v interface{}, d *schema.ResourceData) interface{} {
if v == nil {
return nil
}
original := v.(map[string]interface{})
if len(original) == 0 {
return nil
}
transformed := make(map[string]interface{})
transformed["connection_draining_timeout_sec"] =
flattenComputeRegionBackendServiceConnectionDrainingConnection_draining_timeout_sec(original["drainingTimeoutSec"], d)
return []interface{}{transformed}
}
func flattenComputeRegionBackendServiceConnectionDrainingConnection_draining_timeout_sec(v interface{}, d *schema.ResourceData) interface{} {
// Handles the string fixed64 format
if strVal, ok := v.(string); ok {
Expand Down Expand Up @@ -640,9 +656,8 @@ func expandComputeRegionBackendServiceTimeoutSec(v interface{}, d TerraformResou
return v, nil
}

func expandComputeRegionBackendServiceConnectionDraining(d TerraformResourceData, config *Config) (interface{}, error) {
func expandComputeRegionBackendServiceConnectionDraining(v interface{}, 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.
transformedConnection_draining_timeout_sec, err := expandComputeRegionBackendServiceConnectionDrainingConnection_draining_timeout_sec(d.Get("connection_draining_timeout_sec"), d, config)
if err != nil {
return nil, err
Expand Down

0 comments on commit 77de8e2

Please sign in to comment.