Skip to content

Commit b1de359

Browse files
magodostephybun
andauthored
azurerm_storage_management_policy - Add existance check (#22966)
* `azurerm_storage_management_policy` - Add existance check * typo * Remove the locks * Update internal/services/storage/storage_management_policy_resource_test.go Co-authored-by: stephybun <[email protected]> --------- Co-authored-by: stephybun <[email protected]>
1 parent 0ace82c commit b1de359

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

internal/services/storage/storage_management_policy_resource.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010

1111
"github.com/Azure/azure-sdk-for-go/services/storage/mgmt/2021-09-01/storage" // nolint: staticcheck
1212
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonids"
13+
"github.com/hashicorp/terraform-provider-azurerm/helpers/tf"
1314
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
1415
"github.com/hashicorp/terraform-provider-azurerm/internal/services/storage/parse"
1516
"github.com/hashicorp/terraform-provider-azurerm/internal/services/storage/validate"
@@ -285,7 +286,17 @@ func resourceStorageManagementPolicyCreateOrUpdate(d *pluginsdk.ResourceData, me
285286
// The name of the Storage Account Management Policy. It should always be 'default' (from https://docs.microsoft.com/en-us/rest/api/storagerp/managementpolicies/createorupdate)
286287
mgmtPolicyId := parse.NewStorageAccountManagementPolicyID(rid.SubscriptionId, rid.ResourceGroupName, rid.StorageAccountName, "default")
287288

288-
// TODO: support Requires Import
289+
if d.IsNewResource() {
290+
existing, err := client.Get(ctx, rid.ResourceGroupName, rid.StorageAccountName)
291+
if err != nil {
292+
if !utils.ResponseWasNotFound(existing.Response) {
293+
return fmt.Errorf("checking for presence of existing %s: %s", mgmtPolicyId, err)
294+
}
295+
}
296+
if !utils.ResponseWasNotFound(existing.Response) {
297+
return tf.ImportAsExistsError("azurerm_storage_management_policy", mgmtPolicyId.ID())
298+
}
299+
}
289300

290301
parameters := storage.ManagementPolicy{
291302
Name: &mgmtPolicyId.ManagementPolicyName,

internal/services/storage/storage_management_policy_resource_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,21 @@ func TestAccStorageManagementPolicy_basic(t *testing.T) {
4646
})
4747
}
4848

49+
func TestAccStorageManagementPolicy_requiresImport(t *testing.T) {
50+
data := acceptance.BuildTestData(t, "azurerm_storage_management_policy", "test")
51+
r := StorageManagementPolicyResource{}
52+
53+
data.ResourceTest(t, r, []acceptance.TestStep{
54+
{
55+
Config: r.basic(data),
56+
Check: acceptance.ComposeTestCheckFunc(
57+
check.That(data.ResourceName).ExistsInAzure(r),
58+
),
59+
},
60+
data.RequiresImportErrorStep(r.requiresImport),
61+
})
62+
}
63+
4964
func TestAccStorageManagementPolicy_singleAction(t *testing.T) {
5065
data := acceptance.BuildTestData(t, "azurerm_storage_management_policy", "test")
5166
r := StorageManagementPolicyResource{}
@@ -472,6 +487,16 @@ resource "azurerm_storage_management_policy" "test" {
472487
`, data.RandomInteger, data.Locations.Primary, data.RandomString)
473488
}
474489

490+
func (r StorageManagementPolicyResource) requiresImport(data acceptance.TestData) string {
491+
return fmt.Sprintf(`
492+
%s
493+
494+
resource "azurerm_storage_management_policy" "import" {
495+
storage_account_id = azurerm_storage_management_policy.test.storage_account_id
496+
}
497+
`, r.basic(data))
498+
}
499+
475500
func (r StorageManagementPolicyResource) singleAction(data acceptance.TestData) string {
476501
return fmt.Sprintf(`
477502
provider "azurerm" {

0 commit comments

Comments
 (0)