diff --git a/google/resource_compute_vpn_gateway.go b/google/resource_compute_vpn_gateway.go index 5ad2e3badd3..80cec9685d9 100644 --- a/google/resource_compute_vpn_gateway.go +++ b/google/resource_compute_vpn_gateway.go @@ -188,7 +188,7 @@ func resourceComputeVpnGatewayRead(d *schema.ResourceData, meta interface{}) err if err := d.Set("name", flattenComputeVpnGatewayName(res["name"], d)); err != nil { return fmt.Errorf("Error reading VpnGateway: %s", err) } - if err := d.Set("gateway_id", flattenComputeVpnGatewayGateway_id(res["id"], d)); err != nil { + if err := d.Set("gateway_id", flattenComputeVpnGatewayGatewayId(res["id"], d)); err != nil { return fmt.Errorf("Error reading VpnGateway: %s", err) } if err := d.Set("network", flattenComputeVpnGatewayNetwork(res["network"], d)); err != nil { @@ -276,7 +276,7 @@ func flattenComputeVpnGatewayName(v interface{}, d *schema.ResourceData) interfa return v } -func flattenComputeVpnGatewayGateway_id(v interface{}, d *schema.ResourceData) interface{} { +func flattenComputeVpnGatewayGatewayId(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 { diff --git a/google/resource_compute_vpn_tunnel.go b/google/resource_compute_vpn_tunnel.go index 70fa93d6425..802e577915d 100644 --- a/google/resource_compute_vpn_tunnel.go +++ b/google/resource_compute_vpn_tunnel.go @@ -377,7 +377,7 @@ func resourceComputeVpnTunnelRead(d *schema.ResourceData, meta interface{}) erro return fmt.Errorf("Error reading VpnTunnel: %s", err) } - if err := d.Set("tunnel_id", flattenComputeVpnTunnelTunnel_id(res["id"], d)); err != nil { + if err := d.Set("tunnel_id", flattenComputeVpnTunnelTunnelId(res["id"], d)); err != nil { return fmt.Errorf("Error reading VpnTunnel: %s", err) } if err := d.Set("creation_timestamp", flattenComputeVpnTunnelCreationTimestamp(res["creationTimestamp"], d)); err != nil { @@ -483,7 +483,7 @@ func resourceComputeVpnTunnelImport(d *schema.ResourceData, meta interface{}) ([ return []*schema.ResourceData{d}, nil } -func flattenComputeVpnTunnelTunnel_id(v interface{}, d *schema.ResourceData) interface{} { +func flattenComputeVpnTunnelTunnelId(v interface{}, d *schema.ResourceData) interface{} { return v } diff --git a/google/resource_logging_metric.go b/google/resource_logging_metric.go index 33e992d2a47..a477b223f90 100644 --- a/google/resource_logging_metric.go +++ b/google/resource_logging_metric.go @@ -288,6 +288,18 @@ func resourceLoggingMetricRead(d *schema.ResourceData, meta interface{}) error { return handleNotFoundError(err, d, fmt.Sprintf("LoggingMetric %q", d.Id())) } + res, err = resourceLoggingMetricDecoder(d, meta, res) + if err != nil { + return err + } + + if res == nil { + // Decoding the object has resulted in it being gone. It may be marked deleted + log.Printf("[DEBUG] Removing LoggingMetric because it no longer exists.") + d.SetId("") + return nil + } + if err := d.Set("project", project); err != nil { return fmt.Errorf("Error reading Metric: %s", err) } @@ -529,7 +541,7 @@ func flattenLoggingMetricBucketOptions(v interface{}, d *schema.ResourceData) in transformed["exponential_buckets"] = flattenLoggingMetricBucketOptionsExponentialBuckets(original["exponentialBuckets"], d) transformed["explicit"] = - flattenLoggingMetricBucketOptionsExplicit(original["explicit"], d) + flattenLoggingMetricBucketOptionsExplicit(original["explicitBuckets"], d) return []interface{}{transformed} } func flattenLoggingMetricBucketOptionsLinearBuckets(v interface{}, d *schema.ResourceData) interface{} { @@ -774,7 +786,7 @@ func expandLoggingMetricBucketOptions(v interface{}, d TerraformResourceData, co if err != nil { return nil, err } else if val := reflect.ValueOf(transformedExplicit); val.IsValid() && !isEmptyValue(val) { - transformed["explicit"] = transformedExplicit + transformed["explicitBuckets"] = transformedExplicit } return transformed, nil @@ -892,3 +904,48 @@ func expandLoggingMetricBucketOptionsExplicit(v interface{}, d TerraformResource func expandLoggingMetricBucketOptionsExplicitBounds(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) { return v, nil } + +func resourceLoggingMetricDecoder(d *schema.ResourceData, meta interface{}, res map[string]interface{}) (map[string]interface{}, error) { + // TODO: megan + // bound returned as a list of floats, but item_type `String` in configuration + // this should be removed when we change item_type to type `Double` + new := map[string]map[string]interface{}{} + + if bucketOptions, ok := res["bucketOptions"].(map[string]interface{}); ok { + new["bucketOptions"] = make(map[string]interface{}) + + if len(bucketOptions) > 0 { + if explicitBuckets, ok := bucketOptions["explicitBuckets"].(map[string]interface{}); ok { + if len(explicitBuckets) > 0 { + new["bucketOptions"]["explicitBuckets"] = make(map[string]interface{}) + options := map[string]map[string]interface{}{} + + if bounds, ok := explicitBuckets["bounds"].([]interface{}); ok { + options["explicitBuckets"] = make(map[string]interface{}) + + if len(bounds) > 0 { + buckets := map[string][]string{} + for _, b := range bounds { + buckets["bounds"] = append(buckets["bounds"], fmt.Sprintf("%g", b)) + } + + for k, v := range buckets { + options["explicitBuckets"][k] = v + } + } + } + + for k, v := range options { + new["bucketOptions"][k] = v + } + } + } + } + } + + for k, v := range new { + res[k] = v + } + + return res, nil +} diff --git a/google/resource_logging_metric_test.go b/google/resource_logging_metric_test.go index f1585e1e955..48d0bc729fc 100644 --- a/google/resource_logging_metric_test.go +++ b/google/resource_logging_metric_test.go @@ -40,6 +40,29 @@ func TestAccLoggingMetric_update(t *testing.T) { }) } +func TestAccLoggingMetric_explicitBucket(t *testing.T) { + t.Parallel() + + suffix := acctest.RandString(10) + filter := "resource.type=gae_app AND severity>=ERROR" + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckLoggingMetricDestroy, + Steps: []resource.TestStep{ + { + Config: testAccLoggingMetric_explicitBucket(suffix, filter), + }, + { + ResourceName: "google_logging_metric.logging_metric", + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + func testAccLoggingMetric_update(suffix string, filter string) string { return fmt.Sprintf(` resource "google_logging_metric" "logging_metric" { @@ -51,3 +74,24 @@ resource "google_logging_metric" "logging_metric" { } }`, suffix, filter) } + +func testAccLoggingMetric_explicitBucket(suffix string, filter string) string { + return fmt.Sprintf(` +resource "google_logging_metric" "logging_metric" { + name = "my-custom-metric-%s" + filter = "%s" + + metric_descriptor { + metric_kind = "DELTA" + value_type = "DISTRIBUTION" + } + + value_extractor = "EXTRACT(jsonPayload.metrics.running_jobs)" + + bucket_options { + explicit { + bounds = ["0","1","2","3","4"] + } + } +}`, suffix, filter) +}