diff --git a/azurerm/resource_arm_metric_alertrule.go b/azurerm/resource_arm_metric_alertrule.go index 82018f065ad3..a8d0150fa6c8 100644 --- a/azurerm/resource_arm_metric_alertrule.go +++ b/azurerm/resource_arm_metric_alertrule.go @@ -4,6 +4,8 @@ import ( "fmt" "log" + "strings" + "github.com/Azure/azure-sdk-for-go/services/monitor/mgmt/2017-05-01-preview/insights" "github.com/hashicorp/terraform/helper/schema" "github.com/hashicorp/terraform/helper/validation" @@ -138,7 +140,12 @@ func resourceArmMetricAlertRule() *schema.Resource { }, }, - "tags": tagsForMetricAlertRuleSchema(), + "tags": { + Type: schema.TypeMap, + Optional: true, + Computed: true, + ValidateFunc: validateMetricAlertRuleTags, + }, }, } } @@ -414,3 +421,18 @@ func resourceGroupAndAlertRuleNameFromId(alertRuleId string) (string, string, er return resourceGroup, name, nil } + +func validateMetricAlertRuleTags(v interface{}, f string) (ws []string, es []error) { + // Normal validation required by any AzureRM resource. + ws, es = validateAzureRMTags(v, f) + + tagsMap := v.(map[string]interface{}) + + for k := range tagsMap { + if strings.EqualFold(k, "$type") { + es = append(es, fmt.Errorf("the %q is not allowed as tag name", k)) + } + } + + return ws, es +} diff --git a/azurerm/resource_arm_metric_alertrule_test.go b/azurerm/resource_arm_metric_alertrule_test.go index 0df638ff0d8c..c4e0a2254b21 100644 --- a/azurerm/resource_arm_metric_alertrule_test.go +++ b/azurerm/resource_arm_metric_alertrule_test.go @@ -11,6 +11,60 @@ import ( "github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils" ) +func TestValidateMetricAlertRuleTags(t *testing.T) { + cases := []struct { + Name string + Value map[string]interface{} + ErrCount int + }{ + { + Name: "Single Valid", + Value: map[string]interface{}{ + "hello": "world", + }, + ErrCount: 0, + }, + { + Name: "Single Invalid", + Value: map[string]interface{}{ + "$Type": "hello/world", + }, + ErrCount: 1, + }, + { + Name: "Single Invalid lowercase", + Value: map[string]interface{}{ + "$type": "hello/world", + }, + ErrCount: 1, + }, + { + Name: "Multiple Valid", + Value: map[string]interface{}{ + "hello": "world", + "foo": "bar", + }, + ErrCount: 0, + }, + { + Name: "Multiple Invalid", + Value: map[string]interface{}{ + "hello": "world", + "$type": "Microsoft.Foo/Bar", + }, + ErrCount: 1, + }, + } + + for _, tc := range cases { + _, errors := validateMetricAlertRuleTags(tc.Value, "azurerm_metric_alert_rule") + + if len(errors) != tc.ErrCount { + t.Fatalf("Expected %q to return %d errors but returned %d", tc.Name, tc.ErrCount, len(errors)) + } + } +} + func TestAccAzureRMMetricAlertRule_virtualMachineCpu(t *testing.T) { resourceName := "azurerm_metric_alertrule.test" ri := acctest.RandInt() diff --git a/azurerm/tags.go b/azurerm/tags.go index 689fa74074d5..cbb70069b84d 100644 --- a/azurerm/tags.go +++ b/azurerm/tags.go @@ -34,15 +34,6 @@ func tagsForDataSourceSchema() *schema.Schema { } } -func tagsForMetricAlertRuleSchema() *schema.Schema { - return &schema.Schema{ - Type: schema.TypeMap, - Optional: true, - Computed: true, - ValidateFunc: validateMetricAlertRuleTags, - } -} - func tagValueToString(v interface{}) (string, error) { switch value := v.(type) { case string: @@ -77,21 +68,6 @@ func validateAzureRMTags(v interface{}, f string) (ws []string, es []error) { return ws, es } -func validateMetricAlertRuleTags(v interface{}, f string) (ws []string, es []error) { - // Normal validation required by any AzureRM resource. - ws, es = validateAzureRMTags(v, f) - - tagsMap := v.(map[string]interface{}) - - for k := range tagsMap { - if strings.EqualFold(k, "$type") { - es = append(es, fmt.Errorf("the %q is not allowed as tag name", k)) - } - } - - return ws, es -} - func expandTags(tagsMap map[string]interface{}) map[string]*string { output := make(map[string]*string, len(tagsMap))