Skip to content

Commit

Permalink
[#13139] Add bucket_name argument to logging metric resource (#6887) (#…
Browse files Browse the repository at this point in the history
…13210)

Co-authored-by: Shuya Ma <87669292+shuyama1@users.noreply.github.com>
Co-authored-by: Luca Prete <lucaprete@google.com>
resolve #13139

Signed-off-by: Modular Magician <magic-modules@google.com>

Signed-off-by: Modular Magician <magic-modules@google.com>
  • Loading branch information
modular-magician committed Dec 9, 2022
1 parent 50bb03d commit 5c2c295
Show file tree
Hide file tree
Showing 5 changed files with 184 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .changelog/6887.txt
@@ -0,0 +1,3 @@
```release-note:enhancement
logging: added `bucket_name` argument to `google_logging_metric`
```
29 changes: 29 additions & 0 deletions google/resource_logging_metric.go
Expand Up @@ -106,6 +106,12 @@ Metric identifiers are limited to 100 characters and can include only the follow
characters A-Z, a-z, 0-9, and the special characters _-.,+!*',()%/. The forward-slash
character (/) denotes a hierarchy of name pieces, and it cannot be the first character
of the name.`,
},
"bucket_name": {
Type: schema.TypeString,
Optional: true,
Description: `The resource name of the Log Bucket that owns the Log Metric. Only Log Buckets in projects
are supported. The bucket has to be in the same project as the metric.`,
},
"bucket_options": {
Type: schema.TypeList,
Expand Down Expand Up @@ -280,6 +286,12 @@ func resourceLoggingMetricCreate(d *schema.ResourceData, meta interface{}) error
} else if v, ok := d.GetOkExists("description"); !isEmptyValue(reflect.ValueOf(descriptionProp)) && (ok || !reflect.DeepEqual(v, descriptionProp)) {
obj["description"] = descriptionProp
}
bucketNameProp, err := expandLoggingMetricBucketName(d.Get("bucket_name"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("bucket_name"); !isEmptyValue(reflect.ValueOf(bucketNameProp)) && (ok || !reflect.DeepEqual(v, bucketNameProp)) {
obj["bucketName"] = bucketNameProp
}
filterProp, err := expandLoggingMetricFilter(d.Get("filter"), d, config)
if err != nil {
return err
Expand Down Expand Up @@ -412,6 +424,9 @@ func resourceLoggingMetricRead(d *schema.ResourceData, meta interface{}) error {
if err := d.Set("description", flattenLoggingMetricDescription(res["description"], d, config)); err != nil {
return fmt.Errorf("Error reading Metric: %s", err)
}
if err := d.Set("bucket_name", flattenLoggingMetricBucketName(res["bucketName"], d, config)); err != nil {
return fmt.Errorf("Error reading Metric: %s", err)
}
if err := d.Set("filter", flattenLoggingMetricFilter(res["filter"], d, config)); err != nil {
return fmt.Errorf("Error reading Metric: %s", err)
}
Expand Down Expand Up @@ -459,6 +474,12 @@ func resourceLoggingMetricUpdate(d *schema.ResourceData, meta interface{}) error
} else if v, ok := d.GetOkExists("description"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, descriptionProp)) {
obj["description"] = descriptionProp
}
bucketNameProp, err := expandLoggingMetricBucketName(d.Get("bucket_name"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("bucket_name"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, bucketNameProp)) {
obj["bucketName"] = bucketNameProp
}
filterProp, err := expandLoggingMetricFilter(d.Get("filter"), d, config)
if err != nil {
return err
Expand Down Expand Up @@ -584,6 +605,10 @@ func flattenLoggingMetricDescription(v interface{}, d *schema.ResourceData, conf
return v
}

func flattenLoggingMetricBucketName(v interface{}, d *schema.ResourceData, config *Config) interface{} {
return v
}

func flattenLoggingMetricFilter(v interface{}, d *schema.ResourceData, config *Config) interface{} {
return v
}
Expand Down Expand Up @@ -795,6 +820,10 @@ func expandLoggingMetricDescription(v interface{}, d TerraformResourceData, conf
return v, nil
}

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

func expandLoggingMetricFilter(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}
Expand Down
47 changes: 47 additions & 0 deletions google/resource_logging_metric_generated_test.go
Expand Up @@ -166,6 +166,53 @@ resource "google_logging_metric" "logging_metric" {
`, context)
}

func TestAccLoggingMetric_loggingMetricLoggingBucketExample(t *testing.T) {
t.Parallel()

context := map[string]interface{}{
"project": getTestProjectFromEnv(),
"random_suffix": randString(t, 10),
}

vcrTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckLoggingMetricDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccLoggingMetric_loggingMetricLoggingBucketExample(context),
},
{
ResourceName: "google_logging_metric.logging_metric",
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func testAccLoggingMetric_loggingMetricLoggingBucketExample(context map[string]interface{}) string {
return Nprintf(`
resource "google_logging_project_bucket_config" "logging_metric" {
location = "global"
project = "%{project}"
bucket_id = "_Default"
}
resource "google_logging_metric" "logging_metric" {
name = "tf-test-my-(custom)/metric%{random_suffix}"
filter = "resource.type=gae_app AND severity>=ERROR"
bucket_name = google_logging_project_bucket_config.logging_metric.id
metric_descriptor {
metric_kind = "DELTA"
value_type = "INT64"
unit = "1"
}
}
`, context)
}

func testAccCheckLoggingMetricDestroyProducer(t *testing.T) func(s *terraform.State) error {
return func(s *terraform.State) error {
for name, rs := range s.RootModule().Resources {
Expand Down
79 changes: 78 additions & 1 deletion google/resource_logging_metric_test.go
Expand Up @@ -62,6 +62,46 @@ func TestAccLoggingMetric_explicitBucket(t *testing.T) {
})
}

func TestAccLoggingMetric_loggingBucket(t *testing.T) {
t.Parallel()

filter := "resource.type=gae_app AND severity>=ERROR"
project_id := getTestProjectFromEnv()
suffix := randString(t, 10)

vcrTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckLoggingMetricDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccLoggingMetric_loggingBucketBase(suffix, filter),
},
{
ResourceName: "google_logging_metric.logging_metric",
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccLoggingMetric_loggingBucket(suffix, filter, project_id),
},
{
ResourceName: "google_logging_metric.logging_metric",
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccLoggingMetric_loggingBucketBase(suffix, filter),
},
{
ResourceName: "google_logging_metric.logging_metric",
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func TestAccLoggingMetric_descriptionUpdated(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -128,11 +168,48 @@ resource "google_logging_metric" "logging_metric" {
`, suffix, filter)
}

func testAccLoggingMetric_loggingBucketBase(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"
unit = "1"
value_type = "INT64"
}
}
`, suffix, filter)
}

func testAccLoggingMetric_loggingBucket(suffix string, filter string, project_id string) string {
return fmt.Sprintf(`
resource "google_logging_project_bucket_config" "logging_bucket" {
location = "global"
project = "%s"
bucket_id = "_Default"
}
resource "google_logging_metric" "logging_metric" {
name = "my-custom-metric-%s"
bucket_name = google_logging_project_bucket_config.logging_bucket.id
filter = "%s"
metric_descriptor {
metric_kind = "DELTA"
unit = "1"
value_type = "INT64"
}
}
`, project_id, suffix, filter)
}

func testAccLoggingMetric_descriptionUpdated(suffix, description string) string {
return fmt.Sprintf(`
resource "google_logging_metric" "logging_metric" {
name = "my-custom-metric-%s"
description = "Counter for VM instances that have hostError's"
description = "Counter for VM instances that have hostError's"
filter = "resource.type=gce_instance AND protoPayload.methodName=compute.instances.hostError"
metric_descriptor {
metric_kind = "DELTA"
Expand Down
27 changes: 27 additions & 0 deletions website/docs/r/logging_metric.html.markdown
Expand Up @@ -118,6 +118,28 @@ resource "google_logging_metric" "logging_metric" {
}
}
```
## Example Usage - Logging Metric Logging Bucket


```hcl
resource "google_logging_project_bucket_config" "logging_metric" {
location = "global"
project = "my-project-name"
bucket_id = "_Default"
}
resource "google_logging_metric" "logging_metric" {
name = "my-(custom)/metric"
filter = "resource.type=gae_app AND severity>=ERROR"
bucket_name = google_logging_project_bucket_config.logging_metric.id
metric_descriptor {
metric_kind = "DELTA"
value_type = "INT64"
unit = "1"
}
}
```

## Argument Reference

Expand Down Expand Up @@ -204,6 +226,11 @@ The following arguments are supported:
A description of this metric, which is used in documentation. The maximum length of the
description is 8000 characters.

* `bucket_name` -
(Optional)
The resource name of the Log Bucket that owns the Log Metric. Only Log Buckets in projects
are supported. The bucket has to be in the same project as the metric.

* `label_extractors` -
(Optional)
A map from a label key string to an extractor expression which is used to extract data from a log
Expand Down

0 comments on commit 5c2c295

Please sign in to comment.