-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
Changes from 3 commits
e480825
030c397
46fa676
7613d69
bcd0e2f
8d50852
56c0805
dac0ca1
044447f
0edbdaf
df7c9e7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,6 +34,7 @@ func TestAccAzureRMMetricAlertRule_virtualMachineCpu(t *testing.T) { | |
Check: resource.ComposeTestCheckFunc( | ||
testCheckAzureRMMetricAlertRuleExists(resourceName), | ||
resource.TestCheckResourceAttr(resourceName, "enabled", "false"), | ||
resource.TestCheckNoResourceAttr(resourceName, "$type"), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is looking for the field There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch. |
||
), | ||
}, | ||
}, | ||
|
@@ -54,6 +55,7 @@ func TestAccAzureRMMetricAlertRule_sqlDatabaseStorage(t *testing.T) { | |
Config: config, | ||
Check: resource.ComposeTestCheckFunc( | ||
testCheckAzureRMMetricAlertRuleExists(resourceName), | ||
resource.TestCheckNoResourceAttr(resourceName, "$type"), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is looking for the field There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch. |
||
), | ||
}, | ||
}, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ package azurerm | |
import ( | ||
"errors" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/hashicorp/terraform/helper/schema" | ||
) | ||
|
@@ -44,7 +45,7 @@ func tagValueToString(v interface{}) (string, error) { | |
} | ||
} | ||
|
||
func validateAzureRMTags(v interface{}, k string) (ws []string, es []error) { | ||
func validateAzureRMTags(v interface{}, f string) (ws []string, es []error) { | ||
tagsMap := v.(map[string]interface{}) | ||
|
||
if len(tagsMap) > 15 { | ||
|
@@ -56,6 +57,10 @@ func validateAzureRMTags(v interface{}, k string) (ws []string, es []error) { | |
es = append(es, fmt.Errorf("the maximum length for a tag key is 512 characters: %q is %d characters", k, len(k))) | ||
} | ||
|
||
if strings.EqualFold(k, "$type") { | ||
es = append(es, fmt.Errorf("the %q is not allowed as tag name", k)) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. given this method is shared across all tags (and this validation is only specific to the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Interesting, it seems that this file is inconsistent between local and remote. Let me check it and refresh it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @tombuildsstuff
In general, I feel that $type is not supposed as customized naming, it's reserved for internal usage, no matter from TF or Azure ARM API. I understand that you're worried that we're forcing TF user to follow a more strict naming for all the resources, but, it also introduces inconsistency among all the resources. User will get confuse that sometimes it's allowed to use "$type" as tag name, the other times it's not allowed for some "unknown" story. If we still want to limit the change for this special resource, we will need to create a new tag schema instead. Please see my updated PR, I don't it's worth taking this change. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's true for this specific API but isn't the case across the whole of Azure, for instance I'm able to create a resource with
in the case where this value isn't allowed we'll add validation logic to Terraform to the specific resources to handle this - but in general the key There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please review my latest change, it seems that even if the PR is updated, the comment is still shown on old commit. |
||
|
||
value, err := tagValueToString(v) | ||
if err != nil { | ||
es = append(es, err) | ||
|
@@ -79,16 +84,24 @@ func expandTags(tagsMap map[string]interface{}) map[string]*string { | |
return output | ||
} | ||
|
||
func flattenAndSetTags(d *schema.ResourceData, tagsMap map[string]*string) { | ||
func flattenAndSetTags(d *schema.ResourceData, tagsMap map[string]*string, skipPropNames ...string) { | ||
if tagsMap == nil { | ||
d.Set("tags", make(map[string]interface{})) | ||
return | ||
} | ||
|
||
output := make(map[string]interface{}, len(tagsMap)) | ||
|
||
// Only first optional parameter will be processed. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What about the rest of the parameters? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is going to support optional parameter. A better way is to build a map instead. Let me refactor the code. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. let's instead revert the changes to this method entirely and leave it in the single resource where this change is needed - this method's already doing too much |
||
skipPropName := "" | ||
if len(skipPropNames) > 0 && len(skipPropNames[0]) > 0 { | ||
skipPropName = skipPropNames[0] | ||
} | ||
|
||
for i, v := range tagsMap { | ||
output[i] = *v | ||
if !strings.EqualFold(i, skipPropName) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why using case insensitive comparison instead of "==" operator here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "$type" seems to be reserved usage in PLAN, it should be case insensitive. Or, to be safer, we should use case sensitive instead. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 for it being case insensitive |
||
output[i] = *v | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
in addition - can we add some validation to ensure that the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've updated the code and tests. |
||
} | ||
|
||
d.Set("tags", output) | ||
|
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 function's already doing too much as it is - can we filter out the tags to remove before passing this in? e.g.
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.
To be honest, I don't quite understand why the filtering logic breaks the KISS principle. Anyway, I moved the filtering logic into a utility function supporting tag list. If in the near future, any other resource needs to filter out any tags, it can reuse the logic instead of another copy and paste.