Skip to content

Commit

Permalink
New Resource: azurerm_storage_management_policy (#3819)
Browse files Browse the repository at this point in the history
  • Loading branch information
stuartleeks authored and mbfrahry committed Oct 2, 2019
1 parent 89f9122 commit 66a46ab
Show file tree
Hide file tree
Showing 9 changed files with 1,291 additions and 5 deletions.
132 changes: 132 additions & 0 deletions azurerm/data_source_storage_management_policy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
package azurerm

import (
"fmt"

"github.com/hashicorp/terraform/helper/schema"
)

func dataSourceArmStorageManagementPolicy() *schema.Resource {
return &schema.Resource{
Read: dataSourceArmStorageManagementPolicyRead,

Schema: map[string]*schema.Schema{
"storage_account_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"rule": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Computed: true,
},
"enabled": {
Type: schema.TypeBool,
Computed: true,
},
"filters": {
Type: schema.TypeList,
Computed: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"prefix_match": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
},
"blob_types": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
},
},
},
},
"actions": {
Type: schema.TypeList,
Computed: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"base_blob": {
Type: schema.TypeList,
Computed: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"tier_to_cool_after_days_since_modification_greater_than": {
Type: schema.TypeInt,
Computed: true,
},
"tier_to_archive_after_days_since_modification_greater_than": {
Type: schema.TypeInt,
Computed: true,
},
"delete_after_days_since_modification_greater_than": {
Type: schema.TypeInt,
Computed: true,
},
},
},
},
"snapshot": {
Type: schema.TypeList,
Computed: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"delete_after_days_since_creation_greater_than": {
Type: schema.TypeInt,
Computed: true,
},
},
},
},
},
},
},
},
},
},
},
}
}

func dataSourceArmStorageManagementPolicyRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).Storage.ManagementPoliciesClient
ctx := meta.(*ArmClient).StopContext

storageAccountId := d.Get("storage_account_id").(string)

rid, err := parseAzureResourceID(storageAccountId)
if err != nil {
return err
}
resourceGroupName := rid.ResourceGroup
storageAccountName := rid.Path["storageAccounts"]

result, err := client.Get(ctx, resourceGroupName, storageAccountName)
if err != nil {
return err
}
d.SetId(*result.ID)

if result.Policy != nil {
policy := result.Policy
if policy.Rules != nil {
if err := d.Set("rule", flattenStorageManagementPolicyRules(policy.Rules)); err != nil {
return fmt.Errorf("Error flattening `rule`: %+v", err)
}
}
}

return nil
}
92 changes: 92 additions & 0 deletions azurerm/data_source_storage_management_policy_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package azurerm

import (
"fmt"
"testing"

"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf"
)

func TestAccDataSourceAzureRMStorageManagementPolicy_basic(t *testing.T) {
dataSourceName := "data.azurerm_storage_management_policy.testpolicy"
ri := tf.AccRandTimeInt()

rs := acctest.RandString(4)
location := testLocation()
config := testAccDataSourceAzureRMStorageManagementPolicy_basic(ri, rs, location)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: config,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(dataSourceName, "rule.#", "1"),
resource.TestCheckResourceAttr(dataSourceName, "rule.0.name", "rule1"),
resource.TestCheckResourceAttr(dataSourceName, "rule.0.enabled", "true"),
resource.TestCheckResourceAttr(dataSourceName, "rule.0.filters.#", "1"),
resource.TestCheckResourceAttr(dataSourceName, "rule.0.filters.0.prefix_match.#", "1"),
resource.TestCheckResourceAttr(dataSourceName, "rule.0.filters.0.prefix_match.3439697764", "container1/prefix1"),
resource.TestCheckResourceAttr(dataSourceName, "rule.0.filters.0.blob_types.#", "1"),
resource.TestCheckResourceAttr(dataSourceName, "rule.0.filters.0.blob_types.1068358194", "blockBlob"),
resource.TestCheckResourceAttr(dataSourceName, "rule.0.actions.#", "1"),
resource.TestCheckResourceAttr(dataSourceName, "rule.0.actions.0.base_blob.#", "1"),
resource.TestCheckResourceAttr(dataSourceName, "rule.0.actions.0.base_blob.0.tier_to_cool_after_days_since_modification_greater_than", "10"),
resource.TestCheckResourceAttr(dataSourceName, "rule.0.actions.0.base_blob.0.tier_to_archive_after_days_since_modification_greater_than", "50"),
resource.TestCheckResourceAttr(dataSourceName, "rule.0.actions.0.base_blob.0.delete_after_days_since_modification_greater_than", "100"),
resource.TestCheckResourceAttr(dataSourceName, "rule.0.actions.0.snapshot.#", "1"),
resource.TestCheckResourceAttr(dataSourceName, "rule.0.actions.0.snapshot.0.delete_after_days_since_creation_greater_than", "30"),
),
},
},
})
}

func testAccDataSourceAzureRMStorageManagementPolicy_basic(rInt int, rString string, location string) string {
return fmt.Sprintf(`
resource "azurerm_resource_group" "testrg" {
name = "acctestAzureRMSA-%d"
location = "%s"
}
resource "azurerm_storage_account" "testsa" {
name = "unlikely23exst2acct%s"
resource_group_name = "${azurerm_resource_group.testrg.name}"
location = "${azurerm_resource_group.testrg.location}"
account_tier = "Standard"
account_replication_type = "LRS"
account_kind = "BlobStorage"
}
resource "azurerm_storage_management_policy" "testpolicy" {
storage_account_id = "${azurerm_storage_account.testsa.id}"
rule {
name = "rule1"
enabled = true
filters {
prefix_match = [ "container1/prefix1" ]
blob_types = [ "blockBlob" ]
}
actions {
base_blob {
tier_to_cool_after_days_since_modification_greater_than = 10
tier_to_archive_after_days_since_modification_greater_than = 50
delete_after_days_since_modification_greater_than = 100
}
snapshot {
delete_after_days_since_creation_greater_than = 30
}
}
}
}
data "azurerm_storage_management_policy" "testpolicy" {
storage_account_id = "${azurerm_storage_management_policy.testpolicy.storage_account_id}"
}
`, rInt, location, rString)
}
15 changes: 10 additions & 5 deletions azurerm/internal/services/storage/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ import (
)

type Client struct {
AccountsClient *storage.AccountsClient
FileSystemsClient *filesystems.Client
AccountsClient *storage.AccountsClient
FileSystemsClient *filesystems.Client
ManagementPoliciesClient storage.ManagementPoliciesClient

environment az.Environment
}
Expand All @@ -33,12 +34,16 @@ func BuildClient(options *common.ClientOptions) *Client {
fileSystemsClient := filesystems.NewWithEnvironment(options.Environment)
fileSystemsClient.Authorizer = options.StorageAuthorizer

managementPoliciesClient := storage.NewManagementPoliciesClientWithBaseURI(options.ResourceManagerEndpoint, options.SubscriptionId)
options.ConfigureClient(&managementPoliciesClient.Client, options.ResourceManagerAuthorizer)

// TODO: switch Storage Containers to using the storage.BlobContainersClient
// (which should fix #2977) when the storage clients have been moved in here
return &Client{
AccountsClient: &accountsClient,
FileSystemsClient: &fileSystemsClient,
environment: options.Environment,
AccountsClient: &accountsClient,
FileSystemsClient: &fileSystemsClient,
ManagementPoliciesClient: managementPoliciesClient,
environment: options.Environment,
}
}

Expand Down
2 changes: 2 additions & 0 deletions azurerm/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ func Provider() terraform.ResourceProvider {
"azurerm_storage_account_blob_container_sas": dataSourceArmStorageAccountBlobContainerSharedAccessSignature(),
"azurerm_storage_account_sas": dataSourceArmStorageAccountSharedAccessSignature(),
"azurerm_storage_account": dataSourceArmStorageAccount(),
"azurerm_storage_management_policy": dataSourceArmStorageManagementPolicy(),
"azurerm_subnet": dataSourceArmSubnet(),
"azurerm_subscription": dataSourceArmSubscription(),
"azurerm_subscriptions": dataSourceArmSubscriptions(),
Expand Down Expand Up @@ -416,6 +417,7 @@ func Provider() terraform.ResourceProvider {
"azurerm_storage_blob": resourceArmStorageBlob(),
"azurerm_storage_container": resourceArmStorageContainer(),
"azurerm_storage_data_lake_gen2_filesystem": resourceArmStorageDataLakeGen2FileSystem(),
"azurerm_storage_management_policy": resourceArmStorageManagementPolicy(),
"azurerm_storage_queue": resourceArmStorageQueue(),
"azurerm_storage_share": resourceArmStorageShare(),
"azurerm_storage_share_directory": resourceArmStorageShareDirectory(),
Expand Down
Loading

0 comments on commit 66a46ab

Please sign in to comment.