Skip to content

Commit

Permalink
azurerm_storage_management_policy - Add existance check (#22966)
Browse files Browse the repository at this point in the history
* `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 <steph@hashicorp.com>

---------

Co-authored-by: stephybun <steph@hashicorp.com>
  • Loading branch information
magodo and stephybun authored Aug 17, 2023
1 parent 0ace82c commit b1de359
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
13 changes: 12 additions & 1 deletion internal/services/storage/storage_management_policy_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down Expand Up @@ -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" {
Expand Down

0 comments on commit b1de359

Please sign in to comment.