diff --git a/internal/services/storage/storage_management_policy_resource.go b/internal/services/storage/storage_management_policy_resource.go index 02dcd69a277e..5abc7321911c 100644 --- a/internal/services/storage/storage_management_policy_resource.go +++ b/internal/services/storage/storage_management_policy_resource.go @@ -10,6 +10,7 @@ import ( "github.com/Azure/azure-sdk-for-go/services/storage/mgmt/2021-09-01/storage" // nolint: staticcheck "github.com/hashicorp/go-azure-helpers/resourcemanager/commonids" + "github.com/hashicorp/terraform-provider-azurerm/helpers/tf" "github.com/hashicorp/terraform-provider-azurerm/internal/clients" "github.com/hashicorp/terraform-provider-azurerm/internal/services/storage/parse" "github.com/hashicorp/terraform-provider-azurerm/internal/services/storage/validate" @@ -285,7 +286,17 @@ func resourceStorageManagementPolicyCreateOrUpdate(d *pluginsdk.ResourceData, me // 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) mgmtPolicyId := parse.NewStorageAccountManagementPolicyID(rid.SubscriptionId, rid.ResourceGroupName, rid.StorageAccountName, "default") - // TODO: support Requires Import + if d.IsNewResource() { + existing, err := client.Get(ctx, rid.ResourceGroupName, rid.StorageAccountName) + if err != nil { + if !utils.ResponseWasNotFound(existing.Response) { + return fmt.Errorf("checking for presence of existing %s: %s", mgmtPolicyId, err) + } + } + if !utils.ResponseWasNotFound(existing.Response) { + return tf.ImportAsExistsError("azurerm_storage_management_policy", mgmtPolicyId.ID()) + } + } parameters := storage.ManagementPolicy{ Name: &mgmtPolicyId.ManagementPolicyName, diff --git a/internal/services/storage/storage_management_policy_resource_test.go b/internal/services/storage/storage_management_policy_resource_test.go index b6a09b455fc5..9f909d1ab184 100644 --- a/internal/services/storage/storage_management_policy_resource_test.go +++ b/internal/services/storage/storage_management_policy_resource_test.go @@ -46,6 +46,21 @@ func TestAccStorageManagementPolicy_basic(t *testing.T) { }) } +func TestAccStorageManagementPolicy_requiresImport(t *testing.T) { + data := acceptance.BuildTestData(t, "azurerm_storage_management_policy", "test") + r := StorageManagementPolicyResource{} + + data.ResourceTest(t, r, []acceptance.TestStep{ + { + Config: r.basic(data), + Check: acceptance.ComposeTestCheckFunc( + check.That(data.ResourceName).ExistsInAzure(r), + ), + }, + data.RequiresImportErrorStep(r.requiresImport), + }) +} + func TestAccStorageManagementPolicy_singleAction(t *testing.T) { data := acceptance.BuildTestData(t, "azurerm_storage_management_policy", "test") r := StorageManagementPolicyResource{} @@ -472,6 +487,16 @@ resource "azurerm_storage_management_policy" "test" { `, data.RandomInteger, data.Locations.Primary, data.RandomString) } +func (r StorageManagementPolicyResource) requiresImport(data acceptance.TestData) string { + return fmt.Sprintf(` +%s + +resource "azurerm_storage_management_policy" "import" { + storage_account_id = azurerm_storage_management_policy.test.storage_account_id +} +`, r.basic(data)) +} + func (r StorageManagementPolicyResource) singleAction(data acceptance.TestData) string { return fmt.Sprintf(` provider "azurerm" {