-
Notifications
You must be signed in to change notification settings - Fork 4.6k
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
Introduce Action Group resource of Azure Monitor #1419
Conversation
azurerm/resource_arm_action_group.go
Outdated
"name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, |
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.
Could we add some validation here, like no empty values?
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.
Done.
azurerm/resource_arm_action_group.go
Outdated
}, | ||
|
||
"location": locationSchema(), | ||
"resource_group_name": resourceGroupNameSchema(), |
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.
This is really minor, but can we get a space between these two?
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.
Sure.
azurerm/resource_arm_action_group.go
Outdated
"location": locationSchema(), | ||
"resource_group_name": resourceGroupNameSchema(), | ||
|
||
"short_name": { |
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 ensure that there is no empty value check here as well?
ValidateFunc: validation.NoZeroValues,
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.
Nice catch 👍
azurerm/resource_arm_action_group.go
Outdated
Schema: map[string]*schema.Schema{ | ||
"name": { | ||
Type: schema.TypeString, | ||
Required: true, |
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.
Same as above? Can we please validate all values that should not be empty?
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.
Tried with Azure APIs to see which fields should be non-empty.
azurerm/resource_arm_action_group.go
Outdated
parameters := insights.ActionGroupResource{ | ||
Location: utils.String(location), | ||
ActionGroup: &insights.ActionGroup{ | ||
GroupShortName: utils.String(shortName), |
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.
I don't think you need to use utils.String here you can just use & to get the value.
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.
Resolved.
azurerm/resource_arm_action_group.go
Outdated
|
||
func expandActionGroupEmailReceiver(d *schema.ResourceData) *[]insights.EmailReceiver { | ||
v, ok := d.GetOk("email_receiver") | ||
if !ok { |
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.
Could we always return a valid empty array here (i.e. and only if the value is not nil and the length is greater than 0). In all cases we should return an empty array rather than nil.
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.
Absolutely.
return &receivers | ||
} | ||
|
||
func flattenActionGroupEmailReceiver(receivers *[]insights.EmailReceiver) []interface{} { |
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.
Minor, generally we group expand and flattens together. Could we reorder these so they are grouped together?
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.
Definitely.
azurerm/resource_arm_action_group.go
Outdated
func expandActionGroupSmsReceiver(d *schema.ResourceData) *[]insights.SmsReceiver { | ||
v, ok := d.GetOk("sms_receiver") | ||
if !ok { | ||
return nil |
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.
Unless there is a reason to return nil here, can we return an empty array as above?
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.
Done.
azurerm/resource_arm_action_group.go
Outdated
func expandActionGroupWebHookReceiver(d *schema.ResourceData) *[]insights.WebhookReceiver { | ||
v, ok := d.GetOk("webhook_receiver") | ||
if !ok { | ||
return nil |
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.
Unless there is a reason to return nil here, can we return an empty array as above?
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.
Done.
"github.com/hashicorp/terraform/helper/resource" | ||
) | ||
|
||
func TestAccAzureRMActionGroup_importBasic(t *testing.T) { |
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.
Minor, you could move the import tests in with the regular tests if you would like. It looks like this is the direction that other providers are going, so we may want to do this as well.
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.
Moved to resource_arm_action_group_test.go
Updated the code according to the reviews.
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.
Hey @JunyiYi , everything LGTM except for the import test part, I think I may not have been clear in my communication of what I suggested to be changed, my bad. Can you please update the PR per my new inline suggestion?
}) | ||
} | ||
|
||
func TestAccAzureRMActionGroup_importBasic(t *testing.T) { |
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 delete the import function and add the import test as an additional test step in the existing resource test? Sorta like this (do this for all import tests in the PR)...
Steps: []resource.TestStep{
{
Config: config,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMActionGroupExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "enabled", "true"),
resource.TestCheckResourceAttr(resourceName, "email_receiver.#", "2"),
resource.TestCheckResourceAttr(resourceName, "email_receiver.0.email_address", "admin@contoso.com"),
resource.TestCheckResourceAttr(resourceName, "email_receiver.1.email_address", "devops@contoso.com"),
resource.TestCheckResourceAttr(resourceName, "sms_receiver.#", "1"),
resource.TestCheckResourceAttr(resourceName, "sms_receiver.0.country_code", "1"),
resource.TestCheckResourceAttr(resourceName, "sms_receiver.0.phone_number", "1231231234"),
resource.TestCheckResourceAttr(resourceName, "webhook_receiver.#", "1"),
resource.TestCheckResourceAttr(resourceName, "webhook_receiver.0.service_uri", "http://example.com/alert"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
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.
I've changed the test cases.
Alread updated code according to the feedback.
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.
@JunyiYi, Thanks for updating the tests. LGTM now!
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.
hi @JunyiYi @jeffreyCline
Unfortunately this PR has several issues (which I've left comments for) - but primarily there's a number of potential crashes which could occur due to invalid API responses which need to be addressed; so that we're able to merge other PR's I'm going to have to revert this PR for the moment. In order to get this merged please open a new PR to address the comments and we'll be happy to take another look.
for _, receiver := range *receivers { | ||
val := make(map[string]interface{}, 0) | ||
val["name"] = *receiver.Name | ||
val["service_uri"] = *receiver.ServiceURI |
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.
there's two potential crashes here - these should be nil-checked
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.
Azure won't allow nil values for these fields.
val := make(map[string]interface{}, 0) | ||
val["name"] = *receiver.Name | ||
val["country_code"] = *receiver.CountryCode | ||
val["phone_number"] = *receiver.PhoneNumber |
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.
there's three potential crashes here - can we nil-check these?
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.
Azure won't allow nil values for these fields.
for _, receiver := range *receivers { | ||
val := make(map[string]interface{}, 0) | ||
val["name"] = *receiver.Name | ||
val["email_address"] = *receiver.EmailAddress |
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.
there's two potential crashes here - can we nil-check these?
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.
Azure won't allow nil values for these fields.
} | ||
|
||
d.Set("short_name", *resp.GroupShortName) | ||
d.Set("enabled", *resp.Enabled) |
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.
we don't need to de-pointer these; also we should be accessing these in the .Properties
block
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.
It is a shortcut in go instead of accessing .Properties
.
|
||
id, err := parseAzureResourceID(d.Id()) | ||
if err != nil { | ||
return fmt.Errorf("Error parsing action group resource ID \"%s\" during get: %+v", d.Id(), err) |
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.
we don't need to wrap this error message
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.
That's my personal style. I don't think it really matters.
* `enabled` - (Optional) Whether this action group is enabled. If an action group is not enabled, then none of its receivers will receive communications. Defaults to `true`. | ||
* `email_receiver` - (Optional) The list of `email_receiver` blocks as defined below that are part of this action group. | ||
* `sms_receiver` - (Optional) The list of `sms_receiver` blocks as defined below that are part of this action group. | ||
* `webhook_receiver` - (Optional) The list of `webhook_receiver` blocks as defined below that are part of this action group. |
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.
All three of these should be updated to match the other resources:
* `email_receiver ` - (Optional) One or more `email_receiver ` blocks as defined below.
* `sms_receiver ` - (Optional) One or more `sms_receiver ` blocks as defined below.
* `webhook_receiver` - (Optional) One or more `webhook_receiver` blocks as defined below.
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.
Align with whom? Check this documentation: https://github.com/terraform-providers/terraform-provider-azurerm/blob/master/website/docs/r/dns_ptr_record.html.markdown#argument-reference; and this one: https://github.com/terraform-providers/terraform-provider-azurerm/blob/master/website/docs/r/virtual_machine.html.markdown#argument-reference. None of them use your text.
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.
Newer resources / changes use this format, there's a PR in-flight to migrate others over e.g.
|
||
The following attributes are exported: | ||
|
||
* `id` - The Route ID. |
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.
presumably this should be "The ID of the Action Group"?
resource.TestCheckResourceAttr(resourceName, "sms_receiver.0.country_code", "1"), | ||
resource.TestCheckResourceAttr(resourceName, "sms_receiver.0.phone_number", "1231231234"), | ||
resource.TestCheckResourceAttr(resourceName, "webhook_receiver.#", "1"), | ||
resource.TestCheckResourceAttr(resourceName, "webhook_receiver.0.service_uri", "http://example.com/alert"), |
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 add some tests to go from empty -> basic and ensure these fields are set?
ResourceName: resourceName, | ||
ImportState: true, | ||
ImportStateVerify: true, | ||
}, |
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.
in general we don't test importing the updates, but rather full configurations. in this case can we split this test into two: one to test with it in the disabled state, and one to ensure it can go from disabled -> enabled -> disabled
|
||
id, err := parseAzureResourceID(d.Id()) | ||
if err != nil { | ||
return fmt.Errorf("Error parsing action group resource ID \"%s\" during delete: %+v", d.Id(), err) |
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.
we don't need to wrap this error message - so can we just return it directly?
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.
Align with all other error messages in this code base would be a good style.
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.
we consistently across the codebase don't wrap these error messages since they're an internal error users aren't going to be able to work around (vs a temporary issue in the Azure API in CRUD/Polling where they may be able to self-diagnose) - as such ID parsing error don't need wrapping.
I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues. If you feel this issue should be reopened, we encourage creating a new issue linking back to this one for added context. If you feel I made an error 🤖 🙉 , please reach out to my human friends 👉 hashibot-feedback@hashicorp.com. Thanks! |
In this PR, I introduced a new simple action group resource of Azure monitor.
I enabled three receivers (Email, SMS and Webhook) which should cover most of the use cases.
This PR is a first step of #1252 , because the resource (signal based alert) proposed in it relies on Action Group.
(fixes #1252)