From 04431df7a27d2978edcedfcefaf47adb0ab9d995 Mon Sep 17 00:00:00 2001 From: teowa <104055472+teowa@users.noreply.github.com> Date: Tue, 23 Sep 2025 07:26:26 +0000 Subject: [PATCH] storage_blob_inventory_policy - support creation_time property --- .../storage_blob_inventory_policy_resource.go | 47 +++++++++++++++++-- ...age_blob_inventory_policy_resource_test.go | 6 +++ ...torage_blob_inventory_policy.html.markdown | 10 ++++ 3 files changed, 60 insertions(+), 3 deletions(-) diff --git a/internal/services/storage/storage_blob_inventory_policy_resource.go b/internal/services/storage/storage_blob_inventory_policy_resource.go index 2e0253a62e7d..efcaca92a154 100644 --- a/internal/services/storage/storage_blob_inventory_policy_resource.go +++ b/internal/services/storage/storage_blob_inventory_policy_resource.go @@ -8,6 +8,7 @@ import ( "log" "time" + "github.com/hashicorp/go-azure-helpers/lang/pointer" "github.com/hashicorp/go-azure-helpers/lang/response" "github.com/hashicorp/go-azure-helpers/resourcemanager/commonids" "github.com/hashicorp/go-azure-helpers/resourcemanager/commonschema" @@ -125,6 +126,21 @@ func resourceStorageBlobInventoryPolicy() *pluginsdk.Resource { }, }, + "creation_time": { + Type: pluginsdk.TypeList, + Optional: true, + MaxItems: 1, + Elem: &pluginsdk.Resource{ + Schema: map[string]*pluginsdk.Schema{ + "last_n_days": { + Type: pluginsdk.TypeInt, + Required: true, + ValidateFunc: validation.IntBetween(1, 36500), + }, + }, + }, + }, + "include_blob_versions": { Type: pluginsdk.TypeBool, Optional: true, @@ -307,9 +323,10 @@ func expandBlobInventoryPolicyFilter(input []interface{}, objectType string) (*b PrefixMatch: utils.ExpandStringSlice(v["prefix_match"].(*pluginsdk.Set).List()), ExcludePrefix: utils.ExpandStringSlice(v["exclude_prefixes"].(*pluginsdk.Set).List()), BlobTypes: utils.ExpandStringSlice(v["blob_types"].(*pluginsdk.Set).List()), - IncludeBlobVersions: utils.Bool(v["include_blob_versions"].(bool)), - IncludeDeleted: utils.Bool(v["include_deleted"].(bool)), - IncludeSnapshots: utils.Bool(v["include_snapshots"].(bool)), + IncludeBlobVersions: pointer.To(v["include_blob_versions"].(bool)), + IncludeDeleted: pointer.To(v["include_deleted"].(bool)), + IncludeSnapshots: pointer.To(v["include_snapshots"].(bool)), + CreationTime: expandBlobInventoryPolicyCreationTime(v["creation_time"].([]interface{})), } // If the objectType is Container, the following values must be nil when passed to the API @@ -325,6 +342,17 @@ func expandBlobInventoryPolicyFilter(input []interface{}, objectType string) (*b return policyFilter, nil } +func expandBlobInventoryPolicyCreationTime(input []interface{}) *blobinventorypolicies.BlobInventoryCreationTime { + if len(input) == 0 || input[0] == nil { + return nil + } + + v := input[0].(map[string]interface{}) + return &blobinventorypolicies.BlobInventoryCreationTime{ + LastNDays: pointer.To(int64(v["last_n_days"].(int))), + } +} + func flattenBlobInventoryPolicyRules(input []blobinventorypolicies.BlobInventoryPolicyRule) []interface{} { results := make([]interface{}, 0) if input == nil { @@ -374,6 +402,19 @@ func flattenBlobInventoryPolicyFilter(input *blobinventorypolicies.BlobInventory "include_snapshots": includeSnapshots, "prefix_match": utils.FlattenStringSlice(input.PrefixMatch), "exclude_prefixes": utils.FlattenStringSlice(input.ExcludePrefix), + "creation_time": flattenBlobInventoryPolicyCreationTime(input.CreationTime), + }, + } +} + +func flattenBlobInventoryPolicyCreationTime(input *blobinventorypolicies.BlobInventoryCreationTime) []interface{} { + if input == nil { + return make([]interface{}, 0) + } + + return []interface{}{ + map[string]interface{}{ + "last_n_days": pointer.From(input.LastNDays), }, } } diff --git a/internal/services/storage/storage_blob_inventory_policy_resource_test.go b/internal/services/storage/storage_blob_inventory_policy_resource_test.go index 4103aeeba693..a6a99aaa7f9c 100644 --- a/internal/services/storage/storage_blob_inventory_policy_resource_test.go +++ b/internal/services/storage/storage_blob_inventory_policy_resource_test.go @@ -241,6 +241,9 @@ resource "azurerm_storage_blob_inventory_policy" "test" { include_snapshots = true prefix_match = ["*/test"] exclude_prefixes = ["syslog.log"] + creation_time { + last_n_days = 20 + } } } } @@ -277,6 +280,9 @@ resource "azurerm_storage_blob_inventory_policy" "test" { include_deleted = true include_snapshots = true prefix_match = ["*/test"] + creation_time { + last_n_days = 100 + } } } diff --git a/website/docs/r/storage_blob_inventory_policy.html.markdown b/website/docs/r/storage_blob_inventory_policy.html.markdown index cb1694111011..f8dd9ad85057 100644 --- a/website/docs/r/storage_blob_inventory_policy.html.markdown +++ b/website/docs/r/storage_blob_inventory_policy.html.markdown @@ -73,6 +73,10 @@ A `filter` block supports the following: ~> **Note:** The `rules.*.schema_fields` for this rule has to include `BlobType` so that you can specify the `blob_types`. +* `creation_time` - (Optional) A `creation_time` block as defined below. + +~> **Note:** The `rules.*.schema_fields` for this rule has to include `Creation-Time` so that you can specify the `creation_time` filter. + * `include_blob_versions` - (Optional) Includes blob versions in blob inventory or not? Defaults to `false`. ~> **Note:** The `rules.*.schema_fields` for this rule has to include `IsCurrentVersion` and `VersionId` so that you can specify the `include_blob_versions`. @@ -91,6 +95,12 @@ A `filter` block supports the following: --- +A `creation_time` block supports the following: + +* `last_n_days` - (Required) The number of days in the past to filter blob creation time. Possible value is integer between `1` and `36500`. + +--- + A `rules` block supports the following: * `name` - (Required) The name which should be used for this Blob Inventory Policy Rule.