如何在Terraform中配置GCP监视策略文档?

时间:2020-10-12 06:51:06

标签: google-cloud-platform terraform terraform-provider-gcp

我们在Terraform中配置的GCP中有一个监视警报策略。我们还希望使用Terraform创建文档。

我们正在GCP中使用以下命令来创建文档。

gcloud alpha monitoring policies update projects/project_name/alertPolicies/5861347861929375791 \
--documentation-format="text/markdown" \
--documentation="API has exceeded its quota limit. The systems load has increased beyond capacity, consider increasing your Global and Regional quotas. If this is unexpected behaviour, validate that this is not a bug within your platform."

有什么办法可以在Terraform中创建相同的内容?

监视策略的配置:

# Alerts when API seeing errors
resource "google_monitoring_alert_policy" "dlp_api_see_errors" {
  project      = PROJECT_NAME
  display_name = "API is seeing errors"
  combiner     = "OR"
  
  conditions {
    display_name = "API is seeing errors"

    condition_threshold {
      filter          = "metric.type=\"serviceruntime.googleapis.com/api/request_count\" resource.type=\"consumed_api\" metric.label.\"response_code\"!=\"200\" resource.label.\"service\"=\"dlp.googleapis.com\""
      duration        = "60s"
      comparison      = "COMPARISON_GT"
      aggregations {
        alignment_period     = "60s"
        per_series_aligner   = "ALIGN_SUM"
        cross_series_reducer = "REDUCE_SUM"
      }

      trigger {
        count   = 1
      }
    }
  }

  notification_channels = "${concat(google_monitoring_notification_channel.ndw_alerting_email.*.id, google_monitoring_notification_channel.ndw_alerting_phone.*.id)}"
}

2 个答案:

答案 0 :(得分:2)

google_monitoring_alert_policy resource具有documentation block parameter,可用于设置警报的Markdown文档。

您的资源应如下所示:

# Alerts when API seeing errors
resource "google_monitoring_alert_policy" "dlp_api_see_errors" {
  project      = PROJECT_NAME
  display_name = "API is seeing errors"
  combiner     = "OR"
  
  conditions {
    display_name = "API is seeing errors"

    condition_threshold {
      filter          = "metric.type=\"serviceruntime.googleapis.com/api/request_count\" resource.type=\"consumed_api\" metric.label.\"response_code\"!=\"200\" resource.label.\"service\"=\"dlp.googleapis.com\""
      duration        = "60s"
      comparison      = "COMPARISON_GT"
      aggregations {
        alignment_period     = "60s"
        per_series_aligner   = "ALIGN_SUM"
        cross_series_reducer = "REDUCE_SUM"
      }

      trigger {
        count   = 1
      }
    }
  }

  notification_channels = "${concat(google_monitoring_notification_channel.ndw_alerting_email.*.id, google_monitoring_notification_channel.ndw_alerting_phone.*.id)}"

  documentation {
    mime_type = "text/markdown"
    content   = "API has exceeded its quota limit. The systems load has increased beyond capacity, consider increasing your Global and Regional quotas. If this is unexpected behaviour, validate that this is not a bug within your platform."
  }
}

答案 1 :(得分:0)

您可以在某些限制下使用Terraform创建/更新"documentation block"

documentation块支持:

content-(可选)文档文本,经解释 根据mimeType。内容不得超过8,192个Unicode 字符,并且以以下形式编码时不得超过10,240个字节 UTF-8格式,以较小者为准。

mime_type-(可选)内容字段的格式。目前, 仅支持“ text / markdown”值。

但是,您可以使用“ mime /文本”格式并直接包含所有信息。

另外,在此功能中查看GCP's official documentation regarding documentation block可能会很有用,但是从外部文件加载内容也未在此处进行记录。

Modyfying Policy's Documentation中仅提及官方文档中的此功能。

但是由于您可以使用gcloud alpha来完成操作,所以我会试用Terraform,也许您会找到一种解决方法,该方法如何将“文件”类型放入文档块中...