Repository: GoogleCloudPlatform/terraform-validator
Pull request #1274 (2023-01-11 19:48:24)

Added tests data for LogMetric
<!-- Put a description of what this PR is for here, along with any references to issues that this resolves or contributes to -->
Added test data for LogMetric, so terraform-validator can support this resource.



<!--
Replace each [ ] with [X] to check it. Switch to the preview view to make it easier to click on links.
These steps will speed up the review process, and we appreciate you spending time on them before sending
your code to be reviewed.
-->
If this PR is for Terraform, I acknowledge that I have:

- [X] Searched through the [issue tracker](https://github.com/hashicorp/terraform-provider-google/issues) for an open issue that this either resolves or contributes to, commented on it to claim it, and written "fixes {url}" or "part of {url}" in this PR description. If there were no relevant open issues, I opened one and commented that I would like to work on it (not necessary for very small changes).
- [ ] [Generated Terraform](https://github.com/GoogleCloudPlatform/magic-modules#generating-the-terraform-providers), and ran `make test` and `make lint` to ensure it passes unit and linter tests.
- [X] Ensured that all new fields I added that can be set by a user appear in at least one [example](https://github.com/GoogleCloudPlatform/magic-modules/tree/main/mmv1/templates/terraform/examples) (for generated resources) or [third_party test](https://github.com/GoogleCloudPlatform/magic-modules/tree/main/mmv1/third_party/terraform/tests) (for handwritten resources or update tests).
- [X] [Ran](https://github.com/hashicorp/terraform-provider-google/blob/main/.github/CONTRIBUTING.md#tests) relevant acceptance tests (If the acceptance tests do not yet pass or you are unable to run them, please let your reviewer know).
- [X] Read the [Release Notes Guide](https://github.com/GoogleCloudPlatform/magic-modules/blob/main/.ci/RELEASE_NOTES_GUIDE.md) before writing my release note below.

** The `make test` passed, but `make lint` could not run because it is not recognized.
<!-- AUTOCHANGELOG for Downstream PRs.

Please select one of the following "release-note:" headings:
    - release-note:enhancement
    - release-note:bug
    - release-note:note
    - release-note:new-resource
    - release-note:new-datasource
    - release-note:deprecation
    - release-note:breaking-change
    - release-note:none
    
Unless you choose release-note:none, please add a release note.

See .ci/RELEASE_NOTES_GUIDE.md for writing good release notes.

You can add more release note blocks if you want more than one CHANGELOG
entry for this PR.
-->
**Release Note Template for Downstream PRs (will be copied)**

```release-note:enhancement
 metric: added tests data for `google_logging_metric` resource.
```

Derived from https://github.com/GoogleCloudPlatform/magic-modules/pull/6660


Patch:
diff --git a/converters/google/resources/cai.go b/converters/google/resources/cai.go
index e3bedb3b..78ed7a19 100644
--- a/converters/google/resources/cai.go
+++ b/converters/google/resources/cai.go
@@ -97,7 +97,7 @@ type RestoreDefault struct {
 // generated unique string will be used: "placeholder-" + randomString().
 // This is done to preserve uniqueness of asset.name for a given asset.asset_type.
 func assetName(d TerraformResourceData, config *Config, linkTmpl string) (string, error) {
-	re := regexp.MustCompile("{{([[:word:]]+)}}")
+	re := regexp.MustCompile("{{([%[:word:]]+)}}")
 
 	// workaround for empty project
 	placeholderSet := false
diff --git a/converters/google/resources/resource_converters.go b/converters/google/resources/resource_converters.go
index c6f1c8fc..b53fa2ba 100644
--- a/converters/google/resources/resource_converters.go
+++ b/converters/google/resources/resource_converters.go
@@ -242,5 +242,6 @@ func ResourceConverters() map[string][]ResourceConverter {
 		"google_project_iam_custom_role":      {resourceConverterProjectIAMCustomRole()},
 		"google_organization_iam_custom_role": {resourceConverterOrganizationIAMCustomRole()},
 		"google_vpc_access_connector":         {resourceConverterVPCAccessConnector()},
+		"google_logging_metric":               {resourceConverterLoggingMetric()},
 	}
 }


Test patch:
diff --git a/test/cli_test.go b/test/cli_test.go
index 8ca9911a..660caaf5 100644
--- a/test/cli_test.go
+++ b/test/cli_test.go
@@ -116,6 +116,7 @@ func TestCLI(t *testing.T) {
 		{name: "example_kms_key_ring_iam_binding"},
 		{name: "example_kms_key_ring_iam_member"},
 		{name: "example_kms_key_ring_iam_policy"},
+		{name: "example_logging_metric"},
 		{name: "example_monitoring_notification_channel"},
 		{name: "example_organization_iam_binding"},
 		{name: "example_organization_iam_custom_role"},
diff --git a/test/read_test.go b/test/read_test.go
index 99c04c3a..4e46f2bb 100644
--- a/test/read_test.go
+++ b/test/read_test.go
@@ -78,6 +78,7 @@ func TestReadPlannedAssetsCoverage(t *testing.T) {
 		{name: "example_kms_key_ring_iam_binding"},
 		{name: "example_kms_key_ring_iam_member"},
 		{name: "example_kms_key_ring_iam_policy"},
+		{name: "example_logging_metric"},
 		{name: "example_monitoring_notification_channel"},
 		{name: "example_organization_iam_binding"},
 		{name: "example_organization_iam_custom_role"},
diff --git a/testdata/templates/example_logging_metric.json b/testdata/templates/example_logging_metric.json
new file mode 100644
index 00000000..3e9e52a0
--- /dev/null
+++ b/testdata/templates/example_logging_metric.json
@@ -0,0 +1,50 @@
+[
+    {
+        "name": "//logging.googleapis.com/projects/{{.Provider.project}}/metrics/my-test%2Fmetric",
+        "asset_type": "logging.googleapis.com/Metric",
+        "ancestry_path": "{{.Ancestry}}/project/{{.Provider.project}}",
+        "resource": {
+            "version": "v2",
+            "discovery_document_uri": "https://www.googleapis.com/discovery/v1/apis/logging/v2/rest",
+            "discovery_name": "Metric",
+            "parent": "//cloudresourcemanager.googleapis.com/projects/{{.Provider.project}}",
+            "data": {
+                "bucketOptions": {
+                    "linearBuckets": {
+                        "numFiniteBuckets": 3,
+                        "offset": 1,
+                        "width": 1
+                    }
+                },
+                "filter": "resource.type=gae_app AND severity\u003e=ERROR",
+                "labelExtractors": {
+                    "mass": "EXTRACT(jsonPayload.request)",
+                    "sku": "EXTRACT(jsonPayload.id)"
+                },
+                "metricDescriptor": {
+                    "displayName": "My metric",
+                    "labels": [
+                        {
+                            "description": "Identifying number for item",
+                            "key": "sku",
+                            "valueType": "INT64"
+                        },
+                        {
+                            "description": "amount of matter",
+                            "key": "mass",
+                            "valueType": "STRING"
+                        }
+                    ],
+                    "metricKind": "DELTA",
+                    "unit": "1",
+                    "valueType": "DISTRIBUTION"
+                },
+                "name": "my-test/metric",
+                "valueExtractor": "EXTRACT(jsonPayload.request)"
+            }
+        },
+        "ancestors": [
+            "organizations/{{.OrgID}}"
+        ]
+    }
+]
\ No newline at end of file
diff --git a/testdata/templates/example_logging_metric.tf b/testdata/templates/example_logging_metric.tf
new file mode 100644
index 00000000..963f6e6a
--- /dev/null
+++ b/testdata/templates/example_logging_metric.tf
@@ -0,0 +1,48 @@
+/**
+ * Copyright 2022 Google LLC
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+resource "google_logging_metric" "logging_metric" {
+  name   = "my-test/metric"
+  filter = "resource.type=gae_app AND severity>=ERROR"
+  metric_descriptor {
+    metric_kind = "DELTA"
+    value_type  = "DISTRIBUTION"
+    unit        = "1"
+    labels {
+      key         = "mass"
+      value_type  = "STRING"
+      description = "amount of matter"
+    }
+    labels {
+      key         = "sku"
+      value_type  = "INT64"
+      description = "Identifying number for item"
+    }
+    display_name = "My metric"
+  }
+  value_extractor = "EXTRACT(jsonPayload.request)"
+  label_extractors = {
+    "mass" = "EXTRACT(jsonPayload.request)"
+    "sku"  = "EXTRACT(jsonPayload.id)"
+  }
+  bucket_options {
+    linear_buckets {
+      num_finite_buckets = 3
+      width              = 1
+      offset             = 1
+    }
+  }
+}
\ No newline at end of file
diff --git a/testdata/templates/example_logging_metric.tfplan.json b/testdata/templates/example_logging_metric.tfplan.json
new file mode 100644
index 00000000..ec0e83ca
--- /dev/null
+++ b/testdata/templates/example_logging_metric.tfplan.json
@@ -0,0 +1,1 @@
+{"format_version":"1.0","terraform_version":"1.1.9","planned_values":{"root_module":{"resources":[{"address":"google_logging_metric.logging_metric","mode":"managed","type":"google_logging_metric","name":"logging_metric","provider_name":"registry.terraform.io/hashicorp/google","schema_version":0,"values":{"bucket_options":[{"explicit_buckets":[],"exponential_buckets":[],"linear_buckets":[{"num_finite_buckets":3,"offset":1,"width":1}]}],"description":null,"filter":"resource.type=gae_app AND severity\u003e=ERROR","label_extractors":{"mass":"EXTRACT(jsonPayload.request)","sku":"EXTRACT(jsonPayload.id)"},"metric_descriptor":[{"display_name":"My metric","labels":[{"description":"Identifying number for item","key":"sku","value_type":"INT64"},{"description":"amount of matter","key":"mass","value_type":"STRING"}],"metric_kind":"DELTA","unit":"1","value_type":"DISTRIBUTION"}],"name":"my-test/metric","timeouts":null,"value_extractor":"EXTRACT(jsonPayload.request)"},"sensitive_values":{"bucket_options":[{"explicit_buckets":[],"exponential_buckets":[],"linear_buckets":[{}]}],"label_extractors":{},"metric_descriptor":[{"labels":[{},{}]}]}}]}},"resource_changes":[{"address":"google_logging_metric.logging_metric","mode":"managed","type":"google_logging_metric","name":"logging_metric","provider_name":"registry.terraform.io/hashicorp/google","change":{"actions":["create"],"before":null,"after":{"bucket_options":[{"explicit_buckets":[],"exponential_buckets":[],"linear_buckets":[{"num_finite_buckets":3,"offset":1,"width":1}]}],"description":null,"filter":"resource.type=gae_app AND severity\u003e=ERROR","label_extractors":{"mass":"EXTRACT(jsonPayload.request)","sku":"EXTRACT(jsonPayload.id)"},"metric_descriptor":[{"display_name":"My metric","labels":[{"description":"Identifying number for item","key":"sku","value_type":"INT64"},{"description":"amount of matter","key":"mass","value_type":"STRING"}],"metric_kind":"DELTA","unit":"1","value_type":"DISTRIBUTION"}],"name":"my-test/metric","timeouts":null,"value_extractor":"EXTRACT(jsonPayload.request)"},"after_unknown":{"bucket_options":[{"explicit_buckets":[],"exponential_buckets":[],"linear_buckets":[{}]}],"id":true,"label_extractors":{},"metric_descriptor":[{"labels":[{},{}]}],"project":true},"before_sensitive":false,"after_sensitive":{"bucket_options":[{"explicit_buckets":[],"exponential_buckets":[],"linear_buckets":[{}]}],"label_extractors":{},"metric_descriptor":[{"labels":[{},{}]}]}}}],"configuration":{"root_module":{"resources":[{"address":"google_logging_metric.logging_metric","mode":"managed","type":"google_logging_metric","name":"logging_metric","provider_config_key":"google","expressions":{"bucket_options":[{"linear_buckets":[{"num_finite_buckets":{"constant_value":3},"offset":{"constant_value":1},"width":{"constant_value":1}}]}],"filter":{"constant_value":"resource.type=gae_app AND severity\u003e=ERROR"},"label_extractors":{"constant_value":{"mass":"EXTRACT(jsonPayload.request)","sku":"EXTRACT(jsonPayload.id)"}},"metric_descriptor":[{"display_name":{"constant_value":"My metric"},"labels":[{"description":{"constant_value":"amount of matter"},"key":{"constant_value":"mass"},"value_type":{"constant_value":"STRING"}},{"description":{"constant_value":"Identifying number for item"},"key":{"constant_value":"sku"},"value_type":{"constant_value":"INT64"}}],"metric_kind":{"constant_value":"DELTA"},"unit":{"constant_value":"1"},"value_type":{"constant_value":"DISTRIBUTION"}}],"name":{"constant_value":"my-test/metric"},"value_extractor":{"constant_value":"EXTRACT(jsonPayload.request)"}},"schema_version":0}]}}}