Skip to content

Commit

Permalink
Refactoring tags to match the others
Browse files Browse the repository at this point in the history
```
$ acctests azurerm TestValidateMetricAlertRuleTags
=== RUN   TestValidateMetricAlertRuleTags
--- PASS: TestValidateMetricAlertRuleTags (0.00s)
PASS
ok  	github.com/terraform-providers/terraform-provider-azurerm/azurerm	0.025s
```
  • Loading branch information
tombuildsstuff committed Apr 20, 2018
1 parent 0edbdaf commit df7c9e7
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 25 deletions.
24 changes: 23 additions & 1 deletion azurerm/resource_arm_metric_alertrule.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -138,7 +140,12 @@ func resourceArmMetricAlertRule() *schema.Resource {
},
},

"tags": tagsForMetricAlertRuleSchema(),
"tags": {
Type: schema.TypeMap,
Optional: true,
Computed: true,
ValidateFunc: validateMetricAlertRuleTags,
},
},
}
}
Expand Down Expand Up @@ -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
}
54 changes: 54 additions & 0 deletions azurerm/resource_arm_metric_alertrule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
24 changes: 0 additions & 24 deletions azurerm/tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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))

Expand Down

0 comments on commit df7c9e7

Please sign in to comment.