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
Fix bug for azurerm_storage_management_policy #5803
Fix bug for azurerm_storage_management_policy #5803
Conversation
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.
Hi @neil-yechenwei
Thanks for the PR. There are few comments indicating changes needed at this stage.
@@ -375,14 +377,20 @@ func flattenStorageManagementPolicyRules(armRules *[]storage.ManagementPolicyRul | |||
if armActionBaseBlob.TierToCool != nil && armActionBaseBlob.TierToCool.DaysAfterModificationGreaterThan != nil { | |||
intTemp := int(*armActionBaseBlob.TierToCool.DaysAfterModificationGreaterThan) | |||
baseBlob["tier_to_cool_after_days_since_modification_greater_than"] = intTemp | |||
} else { | |||
baseBlob["tier_to_cool_after_days_since_modification_greater_than"] = -1 |
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.
Setting a "-1" value here will break updates to the resource as this value won't pass validation.
Additionally, flatten should take the same approach as has been done with expand to prevent updates re-introducing the issue being addressed. (an update after creation will set values from state, in this case 0
, but should only be added to the state if user configured)
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.
No. I already tested. It wouldn't break updates. Could you give me a example?
Below is the test result after updated:
Here there are two issues.
First issue, the value of property in nested block would be set as "0" for TypeInt when tf config doesn't specify this property. But we actually don't want this is set as 0. We just want this is set as nil. So I have to use this way "d.GetOkExists(fmt.Sprintf("rule.%d.actions.0.base_blob.0.tier_to_archive_after_days_since_modification_greater_than", idx));" to fix this issue.
Second issue, d.set() would set the value of property in nested block as "0" for TypeInt after service api returns nil. Actually we just want this is set as nil. So I have to use this way "baseBlob["tier_to_cool_after_days_since_modification_greater_than"] = -1" to fix this issue.
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.
The above example doesn't exercise your new code where the values are "-1" in the state (i.e. unset by config). If you extend your testing to do something like TestAccAzureRMStorageManagementPolicy_withOneActionWithUpdate
and update the single value set in your new test, I believe you should see what I'm referring to. i.e. The update will read -1
from the state for the values not explicitly configured which will fail validation and you'll get something like:
ManagementPolicy rule rule1 is invalid. Invalid value for parameter : daysAfterModificationGreaterThan
To avoid this, we should not d.Set
any value if it's not supplied in the config or read back from the API, meaning the approach for traversing the rules with an index value in flatten
as you have done for expand
is necessary. Does that make sense?
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.
Yes. I repro the issue what you mentioned. But seems that I cannot remove d.Set for the properties since the properties would be set as "null" when the properties aren't specified in tfconfig after removed d.set for these properties. Then it always show the difference when I explicitly update the properties for several times. So I've updated code with other solution. Please help to have a look. If the solution is not better, could you share you sample code for the solution? Thanks.
tfstate result when the properties aren't specified in tfconfig after removed d.set for these properties:
"rule": [
{
"actions": [
{
"base_blob": [
{
"delete_after_days_since_modification_greater_than": null,
"tier_to_archive_after_days_since_modification_greater_than": null,
"tier_to_cool_after_days_since_modification_greater_than": null
}
],
"snapshot": [
{
"delete_after_days_since_creation_greater_than": 30
}
]
}
],
"enabled": true,
"filters": [
{
"blob_types": [
"blockBlob"
],
"prefix_match": [
"container1/prefix1"
]
}
],
"name": "rule1"
}
],
tfconfig for updating this resource several times:
provider "azurerm" {
features {}
}
resource "azurerm_resource_group" "test" {
name = "acctestRG-storage-neil"
location = "westus"
}
resource "azurerm_storage_account" "test" {
name = "unlikelyneiltest"
resource_group_name = "${azurerm_resource_group.test.name}"
location = "${azurerm_resource_group.test.location}"
account_tier = "Standard"
account_replication_type = "LRS"
account_kind = "BlobStorage"
}
resource "azurerm_storage_management_policy" "test" {
storage_account_id = "${azurerm_storage_account.test.id}"
rule {
name = "rule1"
enabled = true
filters {
prefix_match = ["container1/prefix1"]
blob_types = ["blockBlob"]
}
actions {
base_blob {
delete_after_days_since_modification_greater_than = 102
}
snapshot {
delete_after_days_since_creation_greater_than = 30
}
}
}
}
Always show the difference while updating this resource several times:
} | ||
if armActionBaseBlob.TierToArchive != nil && armActionBaseBlob.TierToArchive.DaysAfterModificationGreaterThan != nil { | ||
intTemp := int(*armActionBaseBlob.TierToArchive.DaysAfterModificationGreaterThan) | ||
baseBlob["tier_to_archive_after_days_since_modification_greater_than"] = intTemp | ||
} else { | ||
baseBlob["tier_to_archive_after_days_since_modification_greater_than"] = -1 |
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.
As above - setting a "-1" value here will break updates to the resource as this value won't pass validation.
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.
Same as above reason
} | ||
if armActionBaseBlob.Delete != nil && armActionBaseBlob.Delete.DaysAfterModificationGreaterThan != nil { | ||
intTemp := int(*armActionBaseBlob.Delete.DaysAfterModificationGreaterThan) | ||
baseBlob["delete_after_days_since_modification_greater_than"] = intTemp | ||
} else { | ||
baseBlob["delete_after_days_since_modification_greater_than"] = -1 |
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.
As above - setting a "-1" value here will break updates to the resource as this value won't pass validation.
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.
Same as above reason
resource.TestCheckResourceAttr(data.ResourceName, "rule.0.actions.0.base_blob.0.tier_to_cool_after_days_since_modification_greater_than", "-1"), | ||
resource.TestCheckResourceAttr(data.ResourceName, "rule.0.actions.0.base_blob.0.tier_to_archive_after_days_since_modification_greater_than", "-1"), |
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.
As mentioned above, these should not be set if not defined by the configuration.
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.
Same as above reason.
@jackofallops , I'v updated code. Please have a look. THanks. |
18238ba
to
c27728b
Compare
Hi @neil-yechenwei Many thanks for your contribution 👍 |
@jackofallops , Thanks for your commit. But I still found a bug from your commit. when the property tfconfig without
tfstate with
update tfconfig to add
|
Hi @neil-yechenwei |
This has been released in version 2.2.0 of the provider. Please see the Terraform documentation on provider versioning or reach out if you need any assistance upgrading. As an example: provider "azurerm" {
version = "~> 2.2.0"
}
# ... other configuration ... |
I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues. If you feel this issue should be reopened, we encourage creating a new issue linking back to this one for added context. If you feel I made an error 🤖 🙉 , please reach out to my human friends 👉 hashibot-feedback@hashicorp.com. Thanks! |
This PR is for fixing the issue #4580
Fixes #4580