Skip to content

Commit

Permalink
Add table gcp_monitoring_alert_policy. closes #145 (#168)
Browse files Browse the repository at this point in the history
  • Loading branch information
Paulami30 authored Apr 14, 2021
1 parent 998ac54 commit bf10592
Show file tree
Hide file tree
Showing 14 changed files with 379 additions and 0 deletions.
46 changes: 46 additions & 0 deletions docs/tables/gcp_monitoring_alert_policy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Table: gcp_monitoring_alert_policy

An alerting policy describes a set of conditions that you want to monitor. These conditions might relate to the state of an unhealthy system or to resource consumption.

## Examples

### Basic info

```sql
select
display_name,
name,
enabled,
documentation ->> 'content' as doc_content,
tags
from
gcp_monitoring_alert_policy;
```


### Get the creation record for each alert policy

```sql
select
display_name,
name,
creation_record ->> 'mutateTime' as mutation_time,
creation_record ->> 'mutatedBy' as mutated_by
from
gcp_monitoring_alert_policy;
```


### Get the condition details for each alert policy

```sql
select
display_name,
con ->> 'displayName' as filter_display_name,
con -> 'conditionThreshold' ->> 'filter' as filter,
con -> 'conditionThreshold' ->> 'thresholdValue' as threshold_value,
con -> 'conditionThreshold' ->> 'trigger' as trigger
from
gcp_monitoring_alert_policy,
jsonb_array_elements(conditions) as con;
```
Empty file.
17 changes: 17 additions & 0 deletions gcp-test/tests/gcp_monitoring_alert_policy/test-get-expected.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[
{
"combiner": "OR",
"display_name": "{{ resourceName }}",
"documentation": {
"content": "This is for testing purpose",
"mimeType": "text/markdown"
},
"enabled": true,
"location": "global",
"name": "{{ output.resource_id.value.split('/').pop() }}",
"project": "{{ output.project_id.value }}",
"user_labels": {
"foo": "bar"
}
}
]
3 changes: 3 additions & 0 deletions gcp-test/tests/gcp_monitoring_alert_policy/test-get-query.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
select name, display_name, combiner, enabled, documentation, project, location, user_labels
from gcp.gcp_monitoring_alert_policy
where name = '{{ output.resource_id.value.split("/").pop() }}';
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[
{
"combiner": "OR",
"display_name": "{{ resourceName }}",
"enabled": true,
"name": "{{ output.resource_id.value.split('/').pop() }}",
"project": "{{ output.project_id.value }}",
"tags": {
"foo": "bar"
}
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
select name, display_name, combiner, enabled, tags, project
from gcp.gcp_monitoring_alert_policy
where name = '{{ output.resource_id.value.split("/").pop() }}';
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[
{
"combiner": "OR",
"display_name": "{{ resourceName }}",
"name": "{{ output.resource_id.value.split('/').pop() }}"
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
select name, display_name, combiner
from gcp.gcp_monitoring_alert_policy
where display_name = '{{ resourceName }}';
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
null
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
select title, akas
from gcp.gcp_monitoring_alert_policy
where name = 'dummy';
1 change: 1 addition & 0 deletions gcp-test/tests/gcp_monitoring_alert_policy/variables.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
88 changes: 88 additions & 0 deletions gcp-test/tests/gcp_monitoring_alert_policy/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@

variable "resource_name" {
type = string
default = "turbot-test-20200125-create-update"
description = "Name of the resource used throughout the test."
}

variable "gcp_project" {
type = string
default = "niteowl-aaa"
description = "GCP project used for the test."
}

variable "gcp_region" {
type = string
default = "us-east1"
description = "GCP region used for the test."
}

variable "gcp_zone" {
type = string
default = "us-east1-b"
}

provider "google" {
project = var.gcp_project
region = var.gcp_region
zone = var.gcp_zone
}

data "google_client_config" "current" {}

data "null_data_source" "resource" {
inputs = {
scope = "gcp://cloudresourcemanager.googleapis.com/projects/${data.google_client_config.current.project}"
}
}

resource "google_monitoring_alert_policy" "named_test_resource" {
display_name = var.resource_name
documentation {
content = "This is for testing purpose"
mime_type = "text/markdown"
}
combiner = "OR"
conditions {
display_name = var.resource_name
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"
}
}
}
user_labels = {
foo = "bar"
}
}

data "template_file" "resource_aka" {
template = "gcp://monitoring.googleapis.com/${google_monitoring_alert_policy.named_test_resource.id}"
vars = {
resource_name = var.resource_name
project = data.google_client_config.current.project
region = data.google_client_config.current.region
zone = data.google_client_config.current.zone
}
}

output "resource_aka" {
depends_on = [google_monitoring_alert_policy.named_test_resource]
value = data.template_file.resource_aka.rendered
}

output "resource_name" {
value = var.resource_name
}

output "resource_id" {
value = google_monitoring_alert_policy.named_test_resource.name
}

output "project_id" {
value = var.gcp_project
}
1 change: 1 addition & 0 deletions gcp/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ func Plugin(ctx context.Context) *plugin.Plugin {
"gcp_logging_exclusion": tableGcpLoggingExclusion(ctx),
"gcp_logging_metric": tableGcpLoggingMetric(ctx),
"gcp_logging_sink": tableGcpLoggingSink(ctx),
"gcp_monitoring_alert_policy": tableGcpMonitoringAlert(ctx),
"gcp_monitoring_group": tableGcpMonitoringGroup(ctx),
"gcp_monitoring_notification_channel": tableGcpMonitoringNotificationChannel(ctx),
"gcp_project_service": tableGcpProjectService(ctx),
Expand Down
Loading

0 comments on commit bf10592

Please sign in to comment.