Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Resource: azurerm_storage_management_policy #3819

Merged
merged 42 commits into from
Oct 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
b8e019c
Initial implementation work
stuartleeks May 31, 2019
8895366
continue implementation
stuartleeks Jul 5, 2019
78f4fbb
remove name as there can be only one
stuartleeks Jul 5, 2019
47a2bcb
Remove debugging code
stuartleeks Jul 5, 2019
e5ff053
Add missing blob types code
stuartleeks Jul 5, 2019
e66c857
Add remaining assertions to test
stuartleeks Jul 8, 2019
0d80a78
Add multi-rule test
stuartleeks Jul 8, 2019
0ea613f
Add data source for storage management policy
stuartleeks Jul 9, 2019
7c9446a
Add docs
stuartleeks Sep 2, 2019
62efb48
Add field validation
stuartleeks Jul 9, 2019
a23ae1b
Tidy imports
stuartleeks Jul 9, 2019
96741d2
go fmt
stuartleeks Jul 9, 2019
8dbea4b
Add error checking on parseAzureResourceID calls
stuartleeks Jul 9, 2019
2cc02dd
Set resource ID on read
stuartleeks Jul 9, 2019
9981c1a
fix up validation
stuartleeks Jul 9, 2019
b4e4009
fixup-name
stuartleeks Sep 2, 2019
0845298
Add missing error check
stuartleeks Sep 2, 2019
e8b9478
Remove commented out code
stuartleeks Sep 2, 2019
3d30b19
gofmt
stuartleeks Sep 16, 2019
7597f50
fixup storage client casing
stuartleeks Sep 16, 2019
bf96d55
Incorporate PR review suggestions
stuartleeks Sep 27, 2019
dda9f2c
Add required import
stuartleeks Sep 27, 2019
b58ef23
Remove commented code
stuartleeks Sep 27, 2019
f32afae
Let SchemaVersion default to 0
stuartleeks Sep 27, 2019
1651ca5
Fix formatting
stuartleeks Sep 27, 2019
54fc3c1
Remove property
stuartleeks Sep 27, 2019
54baf7a
Add import tests
stuartleeks Oct 2, 2019
2af3987
Fixup if condition
stuartleeks Oct 2, 2019
2dd6c43
Use MinItems to enforce array content
stuartleeks Oct 2, 2019
aff17e9
Add nil check
stuartleeks Oct 2, 2019
55d7427
Add array nil check
stuartleeks Oct 2, 2019
4ea25fc
Avoid unnecessary pointer
stuartleeks Oct 2, 2019
8afeebf
Add validation for durations
stuartleeks Oct 2, 2019
4bcb6a0
lint: remove empty line
stuartleeks Oct 2, 2019
cbe5350
Add import section to docs
stuartleeks Oct 2, 2019
38e757c
make fmt
stuartleeks Oct 2, 2019
e8d324f
Add exported attributes section in docs
stuartleeks Oct 2, 2019
ac80fec
Add GET check on create as per review comment
stuartleeks Oct 2, 2019
ddb60b6
Use resource ID on Read/Delete
stuartleeks Oct 2, 2019
77b5dec
Add Exists and CheckDestroy tests
stuartleeks Oct 2, 2019
7c541b2
Add test to update resource
stuartleeks Oct 2, 2019
3d4b8c8
Add missing error check
stuartleeks Oct 2, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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