-
Notifications
You must be signed in to change notification settings - Fork 4.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
New Resource: azurerm_monitor_action_group
#1725
Changes from 16 commits
1ec27cc
4584f46
24ed6ac
3c89f4a
1f3eba2
d2f5e65
bc9e096
21e1183
589eb7e
463341e
e498d2d
f26338b
9514ac2
2fb2a06
b545939
a0236f5
598e264
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,315 @@ | ||
package azurerm | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/Azure/azure-sdk-for-go/services/preview/monitor/mgmt/2018-03-01/insights" | ||
"github.com/hashicorp/terraform/helper/schema" | ||
"github.com/hashicorp/terraform/helper/validation" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/response" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils" | ||
) | ||
|
||
func resourceArmMonitorActionGroup() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceArmMonitorActionGroupCreateOrUpdate, | ||
Read: resourceArmMonitorActionGroupRead, | ||
Update: resourceArmMonitorActionGroupCreateOrUpdate, | ||
Delete: resourceArmMonitorActionGroupDelete, | ||
Importer: &schema.ResourceImporter{ | ||
State: schema.ImportStatePassthrough, | ||
}, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
ValidateFunc: validation.NoZeroValues, | ||
}, | ||
|
||
"resource_group_name": resourceGroupNameSchema(), | ||
|
||
"short_name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ValidateFunc: validation.NoZeroValues, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this string can be a maximum of 12 characters long, we should add validation to support this #Resolved |
||
}, | ||
|
||
"enabled": { | ||
Type: schema.TypeBool, | ||
Optional: true, | ||
Default: true, | ||
}, | ||
|
||
"email_receiver": { | ||
Type: schema.TypeList, | ||
Optional: true, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ValidateFunc: validation.NoZeroValues, | ||
}, | ||
"email_address": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ValidateFunc: validation.NoZeroValues, | ||
}, | ||
}, | ||
}, | ||
}, | ||
|
||
"sms_receiver": { | ||
Type: schema.TypeList, | ||
Optional: true, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ValidateFunc: validation.NoZeroValues, | ||
}, | ||
"country_code": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ValidateFunc: validation.NoZeroValues, | ||
}, | ||
"phone_number": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ValidateFunc: validation.NoZeroValues, | ||
}, | ||
}, | ||
}, | ||
}, | ||
|
||
"webhook_receiver": { | ||
Type: schema.TypeList, | ||
Optional: true, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ValidateFunc: validation.NoZeroValues, | ||
}, | ||
"service_uri": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ValidateFunc: validation.NoZeroValues, | ||
}, | ||
}, | ||
}, | ||
}, | ||
|
||
"tags": tagsSchema(), | ||
}, | ||
} | ||
} | ||
|
||
func resourceArmMonitorActionGroupCreateOrUpdate(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*ArmClient).actionGroupsClient | ||
ctx := meta.(*ArmClient).StopContext | ||
|
||
name := d.Get("name").(string) | ||
resGroup := d.Get("resource_group_name").(string) | ||
|
||
shortName := d.Get("short_name").(string) | ||
enabled := d.Get("enabled").(bool) | ||
|
||
emailReceiversRaw := d.Get("email_receiver").([]interface{}) | ||
smsReceiversRaw := d.Get("sms_receiver").([]interface{}) | ||
webhookReceiversRaw := d.Get("webhook_receiver").([]interface{}) | ||
|
||
tags := d.Get("tags").(map[string]interface{}) | ||
expandedTags := expandTags(tags) | ||
|
||
parameters := insights.ActionGroupResource{ | ||
Location: utils.String(azureRMNormalizeLocation("Global")), | ||
ActionGroup: &insights.ActionGroup{ | ||
GroupShortName: &shortName, | ||
Enabled: &enabled, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we should update these to be these |
||
EmailReceivers: expandMonitorActionGroupEmailReceiver(emailReceiversRaw), | ||
SmsReceivers: expandMonitorActionGroupSmsReceiver(smsReceiversRaw), | ||
WebhookReceivers: expandMonitorActionGroupWebHookReceiver(webhookReceiversRaw), | ||
}, | ||
Tags: expandedTags, | ||
} | ||
|
||
_, err := client.CreateOrUpdate(ctx, resGroup, name, parameters) | ||
if err != nil { | ||
return fmt.Errorf("Error creating or updating action group %q (resource group %q): %+v", name, resGroup, err) | ||
} | ||
|
||
read, err := client.Get(ctx, resGroup, name) | ||
if err != nil { | ||
return fmt.Errorf("Error getting action group %q (resource group %q) after creation: %+v", name, resGroup, err) | ||
} | ||
if read.ID == nil { | ||
return fmt.Errorf("Action group %q (resource group %q) ID is empty", name, resGroup) | ||
} | ||
|
||
d.SetId(*read.ID) | ||
|
||
return resourceArmMonitorActionGroupRead(d, meta) | ||
} | ||
|
||
func resourceArmMonitorActionGroupRead(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*ArmClient).actionGroupsClient | ||
ctx := meta.(*ArmClient).StopContext | ||
|
||
id, err := parseAzureResourceID(d.Id()) | ||
if err != nil { | ||
return err | ||
} | ||
resGroup := id.ResourceGroup | ||
name := id.Path["actionGroups"] | ||
|
||
resp, err := client.Get(ctx, resGroup, name) | ||
if err != nil { | ||
if response.WasNotFound(resp.Response.Response) { | ||
d.SetId("") | ||
return nil | ||
} | ||
return fmt.Errorf("Error getting action group %q (resource group %q): %+v", name, resGroup, err) | ||
} | ||
|
||
d.Set("name", name) | ||
d.Set("resource_group_name", resGroup) | ||
|
||
d.Set("short_name", resp.ActionGroup.GroupShortName) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if ActionGroup is nil here this'll crash; as such this needs to become:
(I've also fixed the error messages here, since the name/resource group aren't helpful in this context #Resolved |
||
d.Set("enabled", resp.ActionGroup.Enabled) | ||
|
||
if err = d.Set("email_receiver", flattenMonitorActionGroupEmailReceiver(resp.ActionGroup.EmailReceivers)); err != nil { | ||
return fmt.Errorf("Error setting `email_receiver` of action group %q (resource group %q): %+v", name, resGroup, err) | ||
} | ||
|
||
if err = d.Set("sms_receiver", flattenMonitorActionGroupSmsReceiver(resp.ActionGroup.SmsReceivers)); err != nil { | ||
return fmt.Errorf("Error setting `sms_receiver` of action group %q (resource group %q): %+v", name, resGroup, err) | ||
} | ||
|
||
if err = d.Set("webhook_receiver", flattenMonitorActionGroupWebHookReceiver(resp.ActionGroup.WebhookReceivers)); err != nil { | ||
return fmt.Errorf("Error setting `webhook_receiver` of action group %q (resource group %q): %+v", name, resGroup, err) | ||
} | ||
|
||
flattenAndSetTags(d, resp.Tags) | ||
|
||
return nil | ||
} | ||
|
||
func resourceArmMonitorActionGroupDelete(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*ArmClient).actionGroupsClient | ||
ctx := meta.(*ArmClient).StopContext | ||
|
||
id, err := parseAzureResourceID(d.Id()) | ||
if err != nil { | ||
return err | ||
} | ||
resGroup := id.ResourceGroup | ||
name := id.Path["actionGroups"] | ||
|
||
resp, err := client.Delete(ctx, resGroup, name) | ||
if err != nil { | ||
if !response.WasNotFound(resp.Response) { | ||
return fmt.Errorf("Error deleting action group %q (resource group %q): %+v", name, resGroup, err) | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func expandMonitorActionGroupEmailReceiver(v []interface{}) *[]insights.EmailReceiver { | ||
receivers := make([]insights.EmailReceiver, 0) | ||
for _, receiverValue := range v { | ||
val := receiverValue.(map[string]interface{}) | ||
receiver := insights.EmailReceiver{ | ||
Name: utils.String(val["name"].(string)), | ||
EmailAddress: utils.String(val["email_address"].(string)), | ||
} | ||
receivers = append(receivers, receiver) | ||
} | ||
return &receivers | ||
} | ||
|
||
func expandMonitorActionGroupSmsReceiver(v []interface{}) *[]insights.SmsReceiver { | ||
receivers := make([]insights.SmsReceiver, 0) | ||
for _, receiverValue := range v { | ||
val := receiverValue.(map[string]interface{}) | ||
receiver := insights.SmsReceiver{ | ||
Name: utils.String(val["name"].(string)), | ||
CountryCode: utils.String(val["country_code"].(string)), | ||
PhoneNumber: utils.String(val["phone_number"].(string)), | ||
} | ||
receivers = append(receivers, receiver) | ||
} | ||
return &receivers | ||
} | ||
|
||
func expandMonitorActionGroupWebHookReceiver(v []interface{}) *[]insights.WebhookReceiver { | ||
receivers := make([]insights.WebhookReceiver, 0) | ||
for _, receiverValue := range v { | ||
val := receiverValue.(map[string]interface{}) | ||
receiver := insights.WebhookReceiver{ | ||
Name: utils.String(val["name"].(string)), | ||
ServiceURI: utils.String(val["service_uri"].(string)), | ||
} | ||
receivers = append(receivers, receiver) | ||
} | ||
return &receivers | ||
} | ||
|
||
func flattenMonitorActionGroupEmailReceiver(receivers *[]insights.EmailReceiver) []interface{} { | ||
result := make([]interface{}, 0) | ||
if receivers != nil { | ||
for _, receiver := range *receivers { | ||
val := make(map[string]interface{}, 0) | ||
if receiver.Name != nil { | ||
val["name"] = *receiver.Name | ||
} | ||
if receiver.EmailAddress != nil { | ||
val["email_address"] = *receiver.EmailAddress | ||
} | ||
result = append(result, val) | ||
} | ||
} | ||
return result | ||
} | ||
|
||
func flattenMonitorActionGroupSmsReceiver(receivers *[]insights.SmsReceiver) []interface{} { | ||
result := make([]interface{}, 0) | ||
if receivers != nil { | ||
for _, receiver := range *receivers { | ||
val := make(map[string]interface{}, 0) | ||
if receiver.Name != nil { | ||
val["name"] = *receiver.Name | ||
} | ||
if receiver.CountryCode != nil { | ||
val["country_code"] = *receiver.CountryCode | ||
} | ||
if receiver.PhoneNumber != nil { | ||
val["phone_number"] = *receiver.PhoneNumber | ||
} | ||
result = append(result, val) | ||
} | ||
} | ||
return result | ||
} | ||
|
||
func flattenMonitorActionGroupWebHookReceiver(receivers *[]insights.WebhookReceiver) []interface{} { | ||
result := make([]interface{}, 0) | ||
if receivers != nil { | ||
for _, receiver := range *receivers { | ||
val := make(map[string]interface{}, 0) | ||
if receiver.Name != nil { | ||
val["name"] = *receiver.Name | ||
} | ||
if receiver.ServiceURI != nil { | ||
val["service_uri"] = *receiver.ServiceURI | ||
} | ||
result = append(result, val) | ||
} | ||
} | ||
return result | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we sort this alphabetically like the rest of the resources? #Resolved