Skip to content

Commit 13a5eb6

Browse files
committed
Fix immutability_period_in_days validation
This commit fixes #28660
1 parent 470ef6d commit 13a5eb6

File tree

2 files changed

+44
-24
lines changed

2 files changed

+44
-24
lines changed

internal/services/storage/storage_container_immutability_policy_resource.go

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ func (r StorageContainerImmutabilityPolicyResource) CustomizeDiff() sdk.Resource
104104

105105
if lockedOld.(bool) {
106106
if diff.HasChange("immutability_period_in_days") {
107-
if periodOld, periodNew := diff.GetChange("immutability_period_in_days"); periodOld.(int) < periodNew.(int) {
107+
if periodOld, periodNew := diff.GetChange("immutability_period_in_days"); periodOld.(int) > periodNew.(int) {
108108
return fmt.Errorf("`immutability_period_in_days` cannot be decreased once an immutability policy has been locked")
109109
}
110110
}
@@ -228,28 +228,37 @@ func (r StorageContainerImmutabilityPolicyResource) Update() sdk.ResourceFunc {
228228
},
229229
}
230230

231-
options := blobcontainers.CreateOrUpdateImmutabilityPolicyOperationOptions{
232-
IfMatch: resp.Model.Etag,
233-
}
234-
235-
updateResp, err := client.CreateOrUpdateImmutabilityPolicy(ctx, *containerId, input, options)
236-
if err != nil {
237-
return fmt.Errorf("updating %s: %+v", id, err)
238-
}
239-
240-
// Lock the policy if requested - note that this is a one-way operation that prevents subsequent changes or
241-
// deletion to the policy, the container it applies to, and the storage account where it resides.
242-
if model.Locked {
243-
if updateResp.Model == nil {
244-
return fmt.Errorf("preparing to lock %s: model was nil", id)
231+
if *resp.Model.Properties.State == blobcontainers.ImmutabilityPolicyStateLocked {
232+
// Only extending the immutability policy is allowed when the policy is locked
233+
options := blobcontainers.ExtendImmutabilityPolicyOperationOptions{
234+
IfMatch: resp.Model.Etag,
245235
}
246-
247-
lockOptions := blobcontainers.LockImmutabilityPolicyOperationOptions{
248-
IfMatch: updateResp.Model.Etag,
236+
if _, err := client.ExtendImmutabilityPolicy(ctx, *containerId, input, options); err != nil {
237+
return fmt.Errorf("extending %s: %+v", id, err)
238+
}
239+
} else {
240+
options := blobcontainers.CreateOrUpdateImmutabilityPolicyOperationOptions{
241+
IfMatch: resp.Model.Etag,
242+
}
243+
updateResp, err := client.CreateOrUpdateImmutabilityPolicy(ctx, *containerId, input, options)
244+
if err != nil {
245+
return fmt.Errorf("updating %s: %+v", id, err)
249246
}
250247

251-
if _, err = client.LockImmutabilityPolicy(ctx, *containerId, lockOptions); err != nil {
252-
return fmt.Errorf("locking %s: %+v", id, err)
248+
// Lock the policy if requested - note that this is a one-way operation that prevents subsequent changes or
249+
// deletion to the policy, the container it applies to, and the storage account where it resides.
250+
if model.Locked {
251+
if updateResp.Model == nil {
252+
return fmt.Errorf("preparing to lock %s: model was nil", id)
253+
}
254+
255+
lockOptions := blobcontainers.LockImmutabilityPolicyOperationOptions{
256+
IfMatch: updateResp.Model.Etag,
257+
}
258+
259+
if _, err = client.LockImmutabilityPolicy(ctx, *containerId, lockOptions); err != nil {
260+
return fmt.Errorf("locking %s: %+v", id, err)
261+
}
253262
}
254263
}
255264

internal/services/storage/storage_container_immutability_policy_resource_test.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ func TestAccStorageContainerImmutabilityPolicy_completeLocked(t *testing.T) {
9999
},
100100
data.ImportStep(),
101101
{
102-
Config: r.completeLocked(data),
102+
Config: r.completeLocked(data, 2),
103103
Check: acceptance.ComposeTestCheckFunc(
104104
check.That(data.ResourceName).ExistsInAzure(r),
105105
),
@@ -109,6 +109,17 @@ func TestAccStorageContainerImmutabilityPolicy_completeLocked(t *testing.T) {
109109
Config: r.basic(data),
110110
ExpectError: regexp.MustCompile("unable to set `locked = false` - once an immutability policy locked it cannot be unlocked"),
111111
},
112+
{
113+
Config: r.completeLocked(data, 1),
114+
ExpectError: regexp.MustCompile("`immutability_period_in_days` cannot be decreased once an immutability policy has been locked"),
115+
},
116+
{
117+
Config: r.completeLocked(data, 3),
118+
Check: acceptance.ComposeTestCheckFunc(
119+
check.That(data.ResourceName).ExistsInAzure(r),
120+
),
121+
},
122+
data.ImportStep(),
112123
})
113124
}
114125

@@ -154,20 +165,20 @@ resource "azurerm_storage_container_immutability_policy" "test" {
154165
`, template)
155166
}
156167

157-
func (r StorageContainerImmutabilityPolicyResource) completeLocked(data acceptance.TestData) string {
168+
func (r StorageContainerImmutabilityPolicyResource) completeLocked(data acceptance.TestData, period uint) string {
158169
template := r.template(data)
159170
return fmt.Sprintf(`
160171
%[1]s
161172
162173
resource "azurerm_storage_container_immutability_policy" "test" {
163174
storage_container_resource_manager_id = azurerm_storage_container.test.resource_manager_id
164-
immutability_period_in_days = 2
175+
immutability_period_in_days = %d
165176
protected_append_writes_all_enabled = true
166177
protected_append_writes_enabled = false
167178
168179
locked = true
169180
}
170-
`, template)
181+
`, template, period)
171182
}
172183

173184
func (r StorageContainerImmutabilityPolicyResource) template(data acceptance.TestData) string {

0 commit comments

Comments
 (0)