-
Notifications
You must be signed in to change notification settings - Fork 4.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New Resource:
azurerm_storage_management_policy
(#3819)
- Loading branch information
1 parent
89f9122
commit 66a46ab
Showing
9 changed files
with
1,291 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.