Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
new resource
azurerm_monitor_action_rule
- Loading branch information
Showing
22 changed files
with
6,246 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,186 @@ | ||
package monitor | ||
|
||
import ( | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/schema" | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/validation" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils" | ||
|
||
"github.com/Azure/azure-sdk-for-go/services/preview/alertsmanagement/mgmt/2019-05-05/alertsmanagement" | ||
) | ||
|
||
var weekDays = []string{ | ||
"Sunday", | ||
"Monday", | ||
"Tuesday", | ||
"Wednesday", | ||
"Thursday", | ||
"Friday", | ||
"Saturday", | ||
} | ||
|
||
var weekDayMap = map[string]int{ | ||
"Sunday": 0, | ||
"Monday": 1, | ||
"Tuesday": 2, | ||
"Wednesday": 3, | ||
"Thursday": 4, | ||
"Friday": 5, | ||
"Saturday": 6, | ||
} | ||
|
||
func schemaActionRuleAlertContextCondtion() *schema.Schema { | ||
return schemaActionRuleCondtion( | ||
validation.StringInSlice([]string{ | ||
string(alertsmanagement.Equals), | ||
string(alertsmanagement.NotEquals), | ||
string(alertsmanagement.Contains), | ||
string(alertsmanagement.DoesNotContain), | ||
}, false), | ||
validation.StringIsNotEmpty, | ||
) | ||
} | ||
|
||
func schemaActionRuleAlertRuleIDCondtion() *schema.Schema { | ||
return schemaActionRuleCondtion( | ||
validation.StringInSlice([]string{ | ||
string(alertsmanagement.Equals), | ||
string(alertsmanagement.NotEquals), | ||
string(alertsmanagement.Contains), | ||
string(alertsmanagement.DoesNotContain), | ||
}, false), | ||
validation.StringIsNotEmpty, | ||
) | ||
} | ||
|
||
func schemaActionRuleDescriptionCondtion() *schema.Schema { | ||
return schemaActionRuleCondtion( | ||
validation.StringInSlice([]string{ | ||
string(alertsmanagement.Equals), | ||
string(alertsmanagement.NotEquals), | ||
string(alertsmanagement.Contains), | ||
string(alertsmanagement.DoesNotContain), | ||
}, false), | ||
validation.StringIsNotEmpty, | ||
) | ||
} | ||
|
||
func schemaActionRuleMonitorCondtion() *schema.Schema { | ||
return schemaActionRuleCondtion( | ||
validation.StringInSlice([]string{ | ||
string(alertsmanagement.Equals), | ||
string(alertsmanagement.NotEquals), | ||
}, false), | ||
validation.StringInSlice([]string{ | ||
string(alertsmanagement.Fired), | ||
string(alertsmanagement.Resolved), | ||
}, false), | ||
) | ||
} | ||
|
||
func schemaActionRuleMonitorServiceCondtion() *schema.Schema { | ||
return schemaActionRuleCondtion( | ||
validation.StringInSlice([]string{ | ||
string(alertsmanagement.Equals), | ||
string(alertsmanagement.NotEquals), | ||
}, false), | ||
// the supported type list is not consistent with the swagger and sdk | ||
// https://github.com/Azure/azure-rest-api-specs/issues/9076 | ||
// directly use string constant | ||
validation.StringInSlice([]string{ | ||
"ActivityLog Administrative", | ||
"ActivityLog Autoscale", | ||
"ActivityLog Policy", | ||
"ActivityLog Recommendation", | ||
"ActivityLog Security", | ||
"Application Insights", | ||
"Azure Backup", | ||
"Data Box Edge", | ||
"Data Box Gateway", | ||
"Health Platform", | ||
"Log Analytics", | ||
"Platform", | ||
"Resource Health", | ||
}, false), | ||
) | ||
} | ||
|
||
func schemaActionRuleSeverityCondtion() *schema.Schema { | ||
return schemaActionRuleCondtion( | ||
validation.StringInSlice([]string{ | ||
string(alertsmanagement.Equals), | ||
string(alertsmanagement.NotEquals), | ||
}, false), | ||
validation.StringInSlice([]string{ | ||
string(alertsmanagement.Sev0), | ||
string(alertsmanagement.Sev1), | ||
string(alertsmanagement.Sev2), | ||
string(alertsmanagement.Sev3), | ||
string(alertsmanagement.Sev4), | ||
}, false), | ||
) | ||
} | ||
|
||
func schemaActionRuleTargetResourceTypeCondtion() *schema.Schema { | ||
return schemaActionRuleCondtion( | ||
validation.StringInSlice([]string{ | ||
string(alertsmanagement.Equals), | ||
string(alertsmanagement.NotEquals), | ||
}, false), | ||
validation.StringIsNotEmpty, | ||
) | ||
} | ||
|
||
func schemaActionRuleCondtion(operatorValidateFunc, valuesValidateFunc schema.SchemaValidateFunc) *schema.Schema { | ||
return &schema.Schema{ | ||
Type: schema.TypeList, | ||
Optional: true, | ||
MaxItems: 1, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"operator": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ValidateFunc: operatorValidateFunc, | ||
}, | ||
|
||
"values": { | ||
Type: schema.TypeSet, | ||
Required: true, | ||
Elem: &schema.Schema{ | ||
Type: schema.TypeString, | ||
ValidateFunc: valuesValidateFunc, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func expandArmActionRuleCondition(input []interface{}) *alertsmanagement.Condition { | ||
if len(input) == 0 { | ||
return nil | ||
} | ||
|
||
v := input[0].(map[string]interface{}) | ||
return &alertsmanagement.Condition{ | ||
Operator: alertsmanagement.Operator(v["operator"].(string)), | ||
Values: utils.ExpandStringSlice(v["values"].(*schema.Set).List()), | ||
} | ||
} | ||
|
||
func flattenArmActionRuleCondition(input *alertsmanagement.Condition) []interface{} { | ||
if input == nil { | ||
return make([]interface{}, 0) | ||
} | ||
|
||
var operator string | ||
if input.Operator != "" { | ||
operator = string(input.Operator) | ||
} | ||
return []interface{}{ | ||
map[string]interface{}{ | ||
"operator": operator, | ||
"values": utils.FlattenStringSlice(input.Values), | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
azurerm/internal/services/monitor/parse/monitor_action_group.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package parse | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure" | ||
) | ||
|
||
type ActionGroupId struct { | ||
ResourceGroup string | ||
Name string | ||
} | ||
|
||
func ActionGroupID(input string) (*ActionGroupId, error) { | ||
id, err := azure.ParseAzureResourceID(input) | ||
if err != nil { | ||
return nil, fmt.Errorf("parsing Action Group ID %q: %+v", input, err) | ||
} | ||
|
||
actionGroup := ActionGroupId{ | ||
ResourceGroup: id.ResourceGroup, | ||
} | ||
|
||
if actionGroup.Name, err = id.PopSegment("actionGroups"); err != nil { | ||
return nil, fmt.Errorf("parsing Action Group ID %q: %+v", input, err) | ||
} | ||
|
||
if err := id.ValidateNoEmptySegments(input); err != nil { | ||
return nil, fmt.Errorf("parsing Action Group ID %q: %+v", input, err) | ||
} | ||
|
||
return &actionGroup, nil | ||
} |
73 changes: 73 additions & 0 deletions
73
azurerm/internal/services/monitor/parse/monitor_action_group_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package parse | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestActionGroupId(t *testing.T) { | ||
testData := []struct { | ||
Name string | ||
Input string | ||
Expected *ActionGroupId | ||
}{ | ||
{ | ||
Name: "Empty", | ||
Input: "", | ||
Expected: nil, | ||
}, | ||
{ | ||
Name: "No Resource Groups Segment", | ||
Input: "/subscriptions/00000000-0000-0000-0000-000000000000", | ||
Expected: nil, | ||
}, | ||
{ | ||
Name: "No Resource Groups Value", | ||
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/", | ||
Expected: nil, | ||
}, | ||
{ | ||
Name: "Resource Group ID", | ||
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/foo/", | ||
Expected: nil, | ||
}, | ||
{ | ||
Name: "Missing Action Group Value", | ||
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/microsoft.insights/actionGroups/", | ||
Expected: nil, | ||
}, | ||
{ | ||
Name: "Action Group ID", | ||
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/microsoft.insights/actionGroups/actionGroup1", | ||
Expected: &ActionGroupId{ | ||
Name: "actionGroup1", | ||
ResourceGroup: "resGroup1", | ||
}, | ||
}, | ||
{ | ||
Name: "Wrong Casing", | ||
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/microsoft.insights/actiongroups/actionGroup1", | ||
Expected: nil, | ||
}, | ||
} | ||
|
||
for _, v := range testData { | ||
t.Logf("[DEBUG] Testing %q", v.Name) | ||
|
||
actual, err := ActionGroupID(v.Input) | ||
if err != nil { | ||
if v.Expected == nil { | ||
continue | ||
} | ||
|
||
t.Fatalf("Expected a value but got an error: %s", err) | ||
} | ||
|
||
if actual.Name != v.Expected.Name { | ||
t.Fatalf("Expected %q but got %q for Name", v.Expected.Name, actual.Name) | ||
} | ||
|
||
if actual.ResourceGroup != v.Expected.ResourceGroup { | ||
t.Fatalf("Expected %q but got %q for Resource Group", v.Expected.ResourceGroup, actual.ResourceGroup) | ||
} | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
azurerm/internal/services/monitor/parse/monitor_action_rule.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package parse | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure" | ||
) | ||
|
||
type ActionRuleId struct { | ||
ResourceGroup string | ||
Name string | ||
} | ||
|
||
func ActionRuleID(input string) (*ActionRuleId, error) { | ||
id, err := azure.ParseAzureResourceID(input) | ||
if err != nil { | ||
return nil, fmt.Errorf("parsing Action Rule ID %q: %+v", input, err) | ||
} | ||
|
||
actionRule := ActionRuleId{ | ||
ResourceGroup: id.ResourceGroup, | ||
} | ||
if actionRule.Name, err = id.PopSegment("actionRules"); err != nil { | ||
return nil, fmt.Errorf("parsing Action Rule ID %q: %+v", input, err) | ||
} | ||
if err := id.ValidateNoEmptySegments(input); err != nil { | ||
return nil, fmt.Errorf("parsing ActionRule ID %q: %+v", input, err) | ||
} | ||
|
||
return &actionRule, nil | ||
} |
Oops, something went wrong.