Skip to content

Commit

Permalink
update explicit buckets name and make item_type of bounds integers
Browse files Browse the repository at this point in the history
Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
megan07 authored and modular-magician committed Sep 3, 2019
1 parent b3ec63a commit d42be98
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 6 deletions.
4 changes: 2 additions & 2 deletions google/resource_compute_vpn_gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions google/resource_compute_vpn_tunnel.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}

Expand Down
61 changes: 59 additions & 2 deletions google/resource_logging_metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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{} {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
44 changes: 44 additions & 0 deletions google/resource_logging_metric_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand All @@ -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)
}

0 comments on commit d42be98

Please sign in to comment.