Add documentation support to the stackdriver alertPolicy (#2226)

<!-- This change is generated by MagicModules. -->
/cc @chrisst
This commit is contained in:
The Magician 2018-10-10 17:59:44 -07:00 committed by Nathan McKinley
parent 5d8ef3d8f9
commit 22ecabd739
3 changed files with 113 additions and 0 deletions

View File

@ -233,6 +233,24 @@ func resourceMonitoringAlertPolicy() *schema.Resource {
Type: schema.TypeBool,
Required: true,
},
"documentation": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"content": {
Type: schema.TypeString,
Optional: true,
},
"mime_type": {
Type: schema.TypeString,
Optional: true,
Default: "text/markdown",
},
},
},
},
"labels": {
Type: schema.TypeList,
Optional: true,
@ -318,6 +336,12 @@ func resourceMonitoringAlertPolicyCreate(d *schema.ResourceData, meta interface{
} else if v, ok := d.GetOkExists("labels"); !isEmptyValue(reflect.ValueOf(labelsProp)) && (ok || !reflect.DeepEqual(v, labelsProp)) {
obj["labels"] = labelsProp
}
documentationProp, err := expandMonitoringAlertPolicyDocumentation(d.Get("documentation"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("documentation"); !isEmptyValue(reflect.ValueOf(documentationProp)) && (ok || !reflect.DeepEqual(v, documentationProp)) {
obj["documentation"] = documentationProp
}
lockName, err := replaceVars(d, config, "alertPolicy/{{project}}")
if err != nil {
@ -394,6 +418,9 @@ func resourceMonitoringAlertPolicyRead(d *schema.ResourceData, meta interface{})
if err := d.Set("labels", flattenMonitoringAlertPolicyLabels(res["labels"])); err != nil {
return fmt.Errorf("Error reading AlertPolicy: %s", err)
}
if err := d.Set("documentation", flattenMonitoringAlertPolicyDocumentation(res["documentation"])); err != nil {
return fmt.Errorf("Error reading AlertPolicy: %s", err)
}
project, err := getProject(d, config)
if err != nil {
return err
@ -445,6 +472,12 @@ func resourceMonitoringAlertPolicyUpdate(d *schema.ResourceData, meta interface{
} else if v, ok := d.GetOkExists("labels"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, labelsProp)) {
obj["labels"] = labelsProp
}
documentationProp, err := expandMonitoringAlertPolicyDocumentation(d.Get("documentation"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("documentation"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, documentationProp)) {
obj["documentation"] = documentationProp
}
lockName, err := replaceVars(d, config, "alertPolicy/{{project}}")
if err != nil {
@ -792,6 +825,26 @@ func flattenMonitoringAlertPolicyLabels(v interface{}) interface{} {
return v
}
func flattenMonitoringAlertPolicyDocumentation(v interface{}) interface{} {
if v == nil {
return nil
}
original := v.(map[string]interface{})
transformed := make(map[string]interface{})
transformed["content"] =
flattenMonitoringAlertPolicyDocumentationContent(original["content"])
transformed["mime_type"] =
flattenMonitoringAlertPolicyDocumentationMimeType(original["mimeType"])
return []interface{}{transformed}
}
func flattenMonitoringAlertPolicyDocumentationContent(v interface{}) interface{} {
return v
}
func flattenMonitoringAlertPolicyDocumentationMimeType(v interface{}) interface{} {
return v
}
func expandMonitoringAlertPolicyDisplayName(v interface{}, d *schema.ResourceData, config *Config) (interface{}, error) {
return v, nil
}
@ -1243,3 +1296,37 @@ func expandMonitoringAlertPolicyNotificationChannels(v interface{}, d *schema.Re
func expandMonitoringAlertPolicyLabels(v interface{}, d *schema.ResourceData, config *Config) (interface{}, error) {
return v, nil
}
func expandMonitoringAlertPolicyDocumentation(v interface{}, d *schema.ResourceData, config *Config) (interface{}, error) {
l := v.([]interface{})
if len(l) == 0 {
return nil, nil
}
raw := l[0]
original := raw.(map[string]interface{})
transformed := make(map[string]interface{})
transformedContent, err := expandMonitoringAlertPolicyDocumentationContent(original["content"], d, config)
if err != nil {
return nil, err
} else if val := reflect.ValueOf(transformedContent); val.IsValid() && !isEmptyValue(val) {
transformed["content"] = transformedContent
}
transformedMimeType, err := expandMonitoringAlertPolicyDocumentationMimeType(original["mime_type"], d, config)
if err != nil {
return nil, err
} else if val := reflect.ValueOf(transformedMimeType); val.IsValid() && !isEmptyValue(val) {
transformed["mimeType"] = transformedMimeType
}
return transformed, nil
}
func expandMonitoringAlertPolicyDocumentationContent(v interface{}, d *schema.ResourceData, config *Config) (interface{}, error) {
return v, nil
}
func expandMonitoringAlertPolicyDocumentationMimeType(v interface{}, d *schema.ResourceData, config *Config) (interface{}, error) {
return v, nil
}

View File

@ -206,6 +206,11 @@ resource "google_monitoring_alert_policy" "full" {
display_name = "%s"
},
]
documentation {
content = "test content"
mime_type = "text/markdown"
}
}
`, alertName, conditionName1, conditionName2)
}

View File

@ -586,10 +586,31 @@ The `aggregations` block supports:
* `labels` -
(Optional)
User-supplied key/value data to be used for organizing AlertPolicy objects.
* `documentation` -
(Optional)
A short name or phrase used to identify the policy in dashboards,
notifications, and incidents. To avoid confusion, don't use the same
display name for multiple policies in the same project. The name is
limited to 512 Unicode characters. Structure is documented below.
* `project` - (Optional) The ID of the project in which the resource belongs.
If it is not provided, the provider project is used.
The `documentation` block supports:
* `content` -
(Optional)
The text of the documentation, interpreted according to mimeType.
The content may not exceed 8,192 Unicode characters and may not
exceed more than 10,240 bytes when encoded in UTF-8 format,
whichever is smaller.
* `mime_type` -
(Optional)
The format of the content field. Presently, only the value
"text/markdown" is supported.
## Attributes Reference
In addition to the arguments listed above, the following computed attributes are exported: