Skip to content
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

Filter out from properties of tags object. #1107

Merged
merged 11 commits into from
Apr 20, 2018
7 changes: 6 additions & 1 deletion azurerm/import_arm_metric_alertrule_test.go
Original file line number Diff line number Diff line change
@@ -20,13 +20,18 @@ func TestAccAzureRMMetricAlertRule_importVirtualMachineCpu(t *testing.T) {
Steps: []resource.TestStep{
{
Config: config,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMMetricAlertRuleExists(resourceName),
resource.TestCheckNoResourceAttr(resourceName, "tags.$type"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckNoResourceAttr(resourceName, "$type"),
testCheckAzureRMMetricAlertRuleExists(resourceName),
resource.TestCheckNoResourceAttr(resourceName, "tags.$type"),
),
},
},
7 changes: 5 additions & 2 deletions azurerm/resource_arm_metric_alertrule.go
Original file line number Diff line number Diff line change
@@ -138,7 +138,7 @@ func resourceArmMetricAlertRule() *schema.Resource {
},
},

"tags": tagsSchema(),
"tags": tagsForMetricAlertRuleSchema(),
},
}
}
@@ -276,7 +276,10 @@ func resourceArmMetricAlertRuleRead(d *schema.ResourceData, meta interface{}) er
d.Set("webhook_action", webhook_actions)
}

flattenAndSetTags(d, resp.Tags, "$type")
// Return a new tag map filtered by the specified tag names.
tagMap := filterTags(resp.Tags, "$type")

flattenAndSetTags(d, tagMap)

return nil
}
6 changes: 3 additions & 3 deletions azurerm/resource_arm_metric_alertrule_test.go
Original file line number Diff line number Diff line change
@@ -27,15 +27,15 @@ func TestAccAzureRMMetricAlertRule_virtualMachineCpu(t *testing.T) {
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMMetricAlertRuleExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "enabled", "true"),
resource.TestCheckNoResourceAttr(resourceName, "$type"),
resource.TestCheckNoResourceAttr(resourceName, "tags.$type"),
),
},
{
Config: postConfig,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMMetricAlertRuleExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "enabled", "false"),
resource.TestCheckNoResourceAttr(resourceName, "$type"),
resource.TestCheckNoResourceAttr(resourceName, "tags.$type"),
),
},
},
@@ -56,7 +56,7 @@ func TestAccAzureRMMetricAlertRule_sqlDatabaseStorage(t *testing.T) {
Config: config,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMMetricAlertRuleExists(resourceName),
resource.TestCheckNoResourceAttr(resourceName, "$type"),
resource.TestCheckNoResourceAttr(resourceName, "tags.$type"),
),
},
},
65 changes: 51 additions & 14 deletions azurerm/tags.go
Original file line number Diff line number Diff line change
@@ -34,6 +34,15 @@ 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:
@@ -69,7 +78,22 @@ func validateAzureRMTags(v interface{}, f string) (ws []string, es []error) {
}
}

return
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 {
@@ -84,25 +108,38 @@ func expandTags(tagsMap map[string]interface{}) map[string]*string {
return output
}

func flattenAndSetTags(d *schema.ResourceData, tagsMap map[string]*string, skipPropNames ...string) {
if tagsMap == nil {
d.Set("tags", make(map[string]interface{}))
return
func filterTags(tagsMap map[string]*string, tagNames ...string) map[string]*string {
if len(tagNames) == 0 {
return tagsMap
}

output := make(map[string]interface{}, len(tagsMap))

// Only first optional parameter will be processed.
skipPropName := ""
if len(skipPropNames) > 0 && len(skipPropNames[0]) > 0 {
skipPropName = skipPropNames[0]
// Build the filter dictionary from passed tag names.
filterDict := make(map[string]bool)
for _, name := range tagNames {
if len(name) > 0 {
filterDict[strings.ToLower(name)] = true
}
}

for i, v := range tagsMap {
if !strings.EqualFold(i, skipPropName) {
output[i] = *v
// Filter out tag if it exists(case insensitive) in the dictionary.
tagsRet := make(map[string]*string)
for k, v := range tagsMap {
if !filterDict[strings.ToLower(k)] {
tagsRet[k] = v
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's best to filter this on the individual resource where this needs to be filtered out (as people may have a legitimate use for the $type tag) - can we instead filter this out on the resource before calling this function? e.g.

tagsToOutput := make([]string, 0)
for i, v := range resp.Tags {
  tag = *v
  // Filter out $type from tags object to avoid unexpected change on plan.
  if tag != "$type" {
    tagsToOutput[i] = tag
  }
}

in addition - can we add some validation to ensure that the $type tag can't be added on that resource? We can do this using a ValidateFunc on the tags schema item for this resource :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've updated the code and tests.

}

return tagsRet
}

func flattenAndSetTags(d *schema.ResourceData, tagMap map[string]*string) {

// If tagsMap is nil, len(tagsMap) will be 0.
output := make(map[string]interface{}, len(tagMap))

for i, v := range tagMap {
output[i] = *v
}

d.Set("tags", output)
}