Repository: GoogleCloudPlatform/terraform-validator
Pull request #403 (2021-11-24 17:55:34)

Add support for monitoring_alert_policy resource to terraform validator.
<!-- Put a description of what this PR is for here, along with any references to issues that this resolves or contributes to -->

Added google_monitoring_alert_policy entry to resource_converters.go.erb for adding support in tfv.

<!--
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:

- [ ] 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-downstream-tools), and ran `make test` and `make lint` to ensure it passes unit and linter tests.
- [ ] 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/master/templates/terraform/examples) (for generated resources) or [third_party test](https://github.com/GoogleCloudPlatform/magic-modules/tree/master/third_party/terraform/tests) (for handwritten resources or update tests).
- [ ] [Ran](https://github.com/hashicorp/terraform-provider-google/blob/master/.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).
- [ ] Read the [Release Notes Guide](https://github.com/GoogleCloudPlatform/magic-modules/blob/master/.ci/RELEASE_NOTES_GUIDE.md) before writing my release note below.

<!-- 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:none

```

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


Patch:
diff --git a/converters/google/resources/resource_converters.go b/converters/google/resources/resource_converters.go
index 85555b6f..c5634bf0 100644
--- a/converters/google/resources/resource_converters.go
+++ b/converters/google/resources/resource_converters.go
@@ -51,6 +51,7 @@ func ResourceConverters() map[string][]ResourceConverter {
 		"google_access_context_manager_service_perimeter":  {resourceConverterAccessContextManagerServicePerimeter()},
 		"google_cloud_run_service":                         {resourceConverterCloudRunService()},
 		"google_cloud_run_domain_mapping":                  {resourceConverterCloudRunDomainMapping()},
+		"google_monitoring_alert_policy":                   {resourceConverterMonitoringAlertPolicy()},
 		"google_apigee_environment_iam_policy":             {resourceConverterApigeeEnvironmentIamPolicy()},
 		"google_apigee_environment_iam_binding":            {resourceConverterApigeeEnvironmentIamBinding()},
 		"google_apigee_environment_iam_member":             {resourceConverterApigeeEnvironmentIamMember()},


Test patch:
diff --git a/test/cli_test.go b/test/cli_test.go
index 7fd331cf..1b1ccf3f 100644
--- a/test/cli_test.go
+++ b/test/cli_test.go
@@ -97,6 +97,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_monitoring_alert_policy"},
 		{name: "example_organization_iam_binding"},
 		{name: "example_organization_iam_custom_role"},
 		{name: "example_organization_iam_member"},
diff --git a/test/read_test.go b/test/read_test.go
index 4f33be08..f3f4f2da 100644
--- a/test/read_test.go
+++ b/test/read_test.go
@@ -56,6 +56,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_monitoring_alert_policy"},
 		{name: "example_organization_iam_binding"},
 		{name: "example_organization_iam_custom_role"},
 		{name: "example_organization_iam_member"},
diff --git a/testdata/templates/example_monitoring_alert_policy.json b/testdata/templates/example_monitoring_alert_policy.json
new file mode 100644
index 00000000..9654f83f
--- /dev/null
+++ b/testdata/templates/example_monitoring_alert_policy.json
@@ -0,0 +1,39 @@
+[
+  {
+    "name": "//monitoring.googleapis.com/placeholder-foobar",
+    "asset_type": "monitoring.googleapis.com/AlertPolicy",
+    "ancestry_path": "{{.Ancestry}}/project/{{.Provider.project}}",
+    "resource": {
+      "version": "v3",
+      "discovery_document_uri": "https://www.googleapis.com/discovery/v1/apis/monitoring/v3/rest",
+      "discovery_name": "AlertPolicy",
+      "parent": "//cloudresourcemanager.googleapis.com/projects/{{.Provider.project}}",
+      "data": {
+        "combiner": "OR",
+        "conditions": [
+          {
+            "conditionThreshold": {
+              "aggregations": [
+                {
+                  "alignmentPeriod": "60s",
+                  "perSeriesAligner": "ALIGN_RATE"
+                }
+              ],
+              "comparison": "COMPARISON_GT",
+              "duration": "60s",
+              "filter": "metric.type=\"compute.googleapis.com/instance/disk/write_bytes_count\" AND resource.type=\"gce_instance\""
+            },
+            "displayName": "test condition"
+          }
+        ],
+        "displayName": "My Alert Policy",
+        "enabled": true,
+        "notificationChannels": [
+          "projects/{{.Provider.project}}/notificationChannels/channel-1",
+          "projects/{{.Provider.project}}/notificationChannels/channel-2",
+          "projects/{{.Provider.project}}/notificationChannels/channel-3"
+        ]
+      }
+    }
+  }
+]
\ No newline at end of file
diff --git a/testdata/templates/example_monitoring_alert_policy.tf b/testdata/templates/example_monitoring_alert_policy.tf
new file mode 100644
index 00000000..8ca97d8f
--- /dev/null
+++ b/testdata/templates/example_monitoring_alert_policy.tf
@@ -0,0 +1,52 @@
+/**
+ * Copyright 2021 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.
+ */
+
+terraform {
+  required_providers {
+    google = {
+      source  = "hashicorp/google"
+      version = "~> {{.Provider.version}}"
+    }
+  }
+}
+
+provider "google" {
+  {{if .Provider.credentials}}credentials = "{{.Provider.credentials}}"{{end}}
+}
+
+resource "google_monitoring_alert_policy" "alert_policy" {
+  project = "{{.Provider.project}}"
+  display_name = "My Alert Policy"
+  combiner = "OR"
+  conditions {
+    display_name = "test condition"
+    condition_threshold {
+      filter = "metric.type=\"compute.googleapis.com/instance/disk/write_bytes_count\" AND resource.type=\"gce_instance\""
+      duration = "60s"
+      comparison = "COMPARISON_GT"
+      aggregations {
+        alignment_period = "60s"
+        per_series_aligner = "ALIGN_RATE"
+      }
+    }
+  }
+
+  notification_channels = [
+    "projects/{{.Provider.project}}/notificationChannels/channel-1",
+    "projects/{{.Provider.project}}/notificationChannels/channel-2",
+    "projects/{{.Provider.project}}/notificationChannels/channel-3"
+  ]
+}
\ No newline at end of file
diff --git a/testdata/templates/example_monitoring_alert_policy.tfplan.json b/testdata/templates/example_monitoring_alert_policy.tfplan.json
new file mode 100644
index 00000000..aa7d264f
--- /dev/null
+++ b/testdata/templates/example_monitoring_alert_policy.tfplan.json
@@ -0,0 +1,202 @@
+{
+  "format_version": "0.1",
+  "terraform_version": "0.13.5",
+  "planned_values": {
+    "root_module": {
+      "resources": [
+        {
+          "address": "google_monitoring_alert_policy.alert_policy",
+          "mode": "managed",
+          "type": "google_monitoring_alert_policy",
+          "name": "alert_policy",
+          "provider_name": "registry.terraform.io/hashicorp/google",
+          "schema_version": 0,
+          "values": {
+            "combiner": "OR",
+            "conditions": [
+              {
+                "condition_absent": [],
+                "condition_monitoring_query_language": [],
+                "condition_threshold": [
+                  {
+                    "aggregations": [
+                      {
+                        "alignment_period": "60s",
+                        "cross_series_reducer": null,
+                        "group_by_fields": null,
+                        "per_series_aligner": "ALIGN_RATE"
+                      }
+                    ],
+                    "comparison": "COMPARISON_GT",
+                    "denominator_aggregations": [],
+                    "denominator_filter": null,
+                    "duration": "60s",
+                    "filter": "metric.type=\"compute.googleapis.com/instance/disk/write_bytes_count\" AND resource.type=\"gce_instance\"",
+                    "threshold_value": null,
+                    "trigger": []
+                  }
+                ],
+                "display_name": "test condition"
+              }
+            ],
+            "display_name": "My Alert Policy",
+            "documentation": [],
+            "enabled": true,
+            "notification_channels": [
+              "projects/{{.Provider.project}}/notificationChannels/channel-1",
+              "projects/{{.Provider.project}}/notificationChannels/channel-2",
+              "projects/{{.Provider.project}}/notificationChannels/channel-3"
+            ],
+            "project": "{{.Provider.project}}",
+            "timeouts": null,
+            "user_labels": null
+          }
+        }
+      ]
+    }
+  },
+  "resource_changes": [
+    {
+      "address": "google_monitoring_alert_policy.alert_policy",
+      "mode": "managed",
+      "type": "google_monitoring_alert_policy",
+      "name": "alert_policy",
+      "provider_name": "registry.terraform.io/hashicorp/google",
+      "change": {
+        "actions": [
+          "create"
+        ],
+        "before": null,
+        "after": {
+          "combiner": "OR",
+          "conditions": [
+            {
+              "condition_absent": [],
+              "condition_monitoring_query_language": [],
+              "condition_threshold": [
+                {
+                  "aggregations": [
+                    {
+                      "alignment_period": "60s",
+                      "cross_series_reducer": null,
+                      "group_by_fields": null,
+                      "per_series_aligner": "ALIGN_RATE"
+                    }
+                  ],
+                  "comparison": "COMPARISON_GT",
+                  "denominator_aggregations": [],
+                  "denominator_filter": null,
+                  "duration": "60s",
+                  "filter": "metric.type=\"compute.googleapis.com/instance/disk/write_bytes_count\" AND resource.type=\"gce_instance\"",
+                  "threshold_value": null,
+                  "trigger": []
+                }
+              ],
+              "display_name": "test condition"
+            }
+          ],
+          "display_name": "My Alert Policy",
+          "documentation": [],
+          "enabled": true,
+          "notification_channels": [
+            "projects/{{.Provider.project}}/notificationChannels/channel-1",
+            "projects/{{.Provider.project}}/notificationChannels/channel-2",
+            "projects/{{.Provider.project}}/notificationChannels/channel-3"
+          ],
+          "project": "{{.Provider.project}}",
+          "timeouts": null,
+          "user_labels": null
+        },
+        "after_unknown": {
+          "conditions": [
+            {
+              "condition_absent": [],
+              "condition_monitoring_query_language": [],
+              "condition_threshold": [
+                {
+                  "aggregations": [
+                    {}
+                  ],
+                  "denominator_aggregations": [],
+                  "trigger": []
+                }
+              ],
+              "name": true
+            }
+          ],
+          "creation_record": true,
+          "documentation": [],
+          "id": true,
+          "name": true,
+          "notification_channels": [
+            false,
+            false,
+            false
+          ]
+        }
+      }
+    }
+  ],
+  "configuration": {
+    "root_module": {
+      "resources": [
+        {
+          "address": "google_monitoring_alert_policy.alert_policy",
+          "mode": "managed",
+          "type": "google_monitoring_alert_policy",
+          "name": "alert_policy",
+          "provider_config_key": "google",
+          "expressions": {
+            "combiner": {
+              "constant_value": "OR"
+            },
+            "conditions": [
+              {
+                "condition_threshold": [
+                  {
+                    "aggregations": [
+                      {
+                        "alignment_period": {
+                          "constant_value": "60s"
+                        },
+                        "per_series_aligner": {
+                          "constant_value": "ALIGN_RATE"
+                        }
+                      }
+                    ],
+                    "comparison": {
+                      "constant_value": "COMPARISON_GT"
+                    },
+                    "duration": {
+                      "constant_value": "60s"
+                    },
+                    "filter": {
+                      "constant_value": "metric.type=\"compute.googleapis.com/instance/disk/write_bytes_count\" AND resource.type=\"gce_instance\""
+                    }
+                  }
+                ],
+                "display_name": {
+                  "constant_value": "test condition"
+                }
+              }
+            ],
+            "display_name": {
+              "constant_value": "My Alert Policy"
+            },
+            "notification_channels": {
+              "constant_value": [
+                "projects/{{.Provider.project}}/notificationChannels/channel-1",
+                "projects/{{.Provider.project}}/notificationChannels/channel-2",
+                "projects/{{.Provider.project}}/notificationChannels/channel-3"
+              ]
+            },
+            "project": {
+              "constant_value": "{{.Provider.project}}"
+            }
+          },
+          "schema_version": 0
+        }
+      ]
+    }
+  }
+}
\ No newline at end of file