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

azurerm_storage_account - Support for sas_policy #19222

Merged
merged 2 commits into from
Nov 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
78 changes: 78 additions & 0 deletions internal/services/storage/storage_account_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -786,6 +786,31 @@ func resourceStorageAccount() *pluginsdk.Resource {
ForceNew: true,
},

"sas_policy": {
Type: pluginsdk.TypeList,
Optional: true,
MinItems: 1,
MaxItems: 1,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"expiration_period": {
Type: pluginsdk.TypeString,
Required: true,
ValidateFunc: validation.StringIsNotEmpty,
},
"expiration_action": {
Type: pluginsdk.TypeString,
Optional: true,
Default: "Log",
ValidateFunc: validation.StringInSlice([]string{
// There is no definition of this enum in the Track1 SDK due to: https://github.com/Azure/azure-sdk-for-go/issues/14589
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that issue was closed as wont't fix - has it been added to the swagger or should we be opening an issue on the rest api specs?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is not a swagger issue, but a Track1 SDK issue. As they are not support track1 SDK, they closed that issue. This comment is to explain why there isn't an enum defined in the SDK, not meant to be a TODO. If there are more than one enum defined for that attribute in later API, then there will be enums defined in the SDK.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so the swagger is correct?

also is there a reason for this to be required with a single value? we could make it optional with a default?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, swagger is correct. Good idea, will make the change then.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

perfect thanks for checking - this'll be fine and be fixed by the pandora SDK then

"Log",
}, false),
},
},
},
},

"large_file_share_enabled": {
Type: pluginsdk.TypeBool,
Optional: true,
Expand Down Expand Up @@ -1071,6 +1096,7 @@ func resourceStorageAccountCreate(d *pluginsdk.ResourceData, meta interface{}) e
AllowSharedKeyAccess: &allowSharedKeyAccess,
DefaultToOAuthAuthentication: &defaultToOAuthAuthentication,
AllowCrossTenantReplication: &crossTenantReplication,
SasPolicy: expandStorageAccountSASPolicy(d.Get("sas_policy").([]interface{})),
},
}

Expand Down Expand Up @@ -1677,6 +1703,18 @@ func resourceStorageAccountUpdate(d *pluginsdk.ResourceData, meta interface{}) e
}
}

if d.HasChange("sas_policy") {
// TODO: Currently, due to Track1 SDK has no way to represent a `null` value in the payload - instead it will be omitted, `sas_policy` can not be disabled once enabled.
opts := storage.AccountUpdateParameters{
AccountPropertiesUpdateParameters: &storage.AccountPropertiesUpdateParameters{
SasPolicy: expandStorageAccountSASPolicy(d.Get("sas_policy").([]interface{})),
},
}
if _, err := client.Update(ctx, id.ResourceGroup, id.Name, opts); err != nil {
return fmt.Errorf("updating Azure Storage Account sas_policy %q: %+v", id.Name, err)
}
}

supportLevel := resolveStorageAccountServiceSupportLevel(storage.Kind(accountKind), storage.SkuTier(accountTier))

if d.HasChange("blob_properties") {
Expand Down Expand Up @@ -2004,6 +2042,10 @@ func resourceStorageAccountRead(d *pluginsdk.ResourceData, meta interface{}) err
infrastructureEncryption = *encryption.RequireInfrastructureEncryption
}
d.Set("infrastructure_encryption_enabled", infrastructureEncryption)

if err := d.Set("sas_policy", flattenStorageAccountSASPolicy(props.SasPolicy)); err != nil {
return fmt.Errorf("setting `sas_policy`: %+v", err)
}
}

if accessKeys := keys.Keys; accessKeys != nil {
Expand Down Expand Up @@ -3466,3 +3508,39 @@ func flattenEdgeZone(input *storage.ExtendedLocation) string {
}
return edgezones.NormalizeNilable(input.Name)
}

func expandStorageAccountSASPolicy(input []interface{}) *storage.SasPolicy {
if len(input) == 0 {
return nil
}

e := input[0].(map[string]interface{})

return &storage.SasPolicy{
ExpirationAction: utils.String(e["expiration_action"].(string)),
SasExpirationPeriod: utils.String(e["expiration_period"].(string)),
}
}

func flattenStorageAccountSASPolicy(input *storage.SasPolicy) []interface{} {
if input == nil {
return []interface{}{}
}

var expirationAction string
if input.ExpirationAction != nil {
expirationAction = *input.ExpirationAction
}

var expirationPeriod string
if input.SasExpirationPeriod != nil {
expirationPeriod = *input.SasExpirationPeriod
}

return []interface{}{
map[string]interface{}{
"expiration_action": expirationAction,
"expiration_period": expirationPeriod,
},
}
}
50 changes: 50 additions & 0 deletions internal/services/storage/storage_account_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func TestAccStorageAccount_basic(t *testing.T) {
check.That(data.ResourceName).Key("cross_tenant_replication_enabled").HasValue("true"),
),
},
data.ImportStep(),
})
}

Expand Down Expand Up @@ -1327,6 +1328,28 @@ func TestAccStorageAccount_premiumFileCustomerManagedKey(t *testing.T) {
})
}

func TestAccStorageAccount_sasPolicy(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_storage_account", "test")
r := StorageAccountResource{}

data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.basic(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
{
Config: r.sasPolicy(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
})
}

func (r StorageAccountResource) Exists(ctx context.Context, client *clients.Client, state *pluginsdk.InstanceState) (*bool, error) {
id, err := parse.StorageAccountID(state.ID)
if err != nil {
Expand Down Expand Up @@ -4057,3 +4080,30 @@ resource "azurerm_storage_account" "test" {
}
`, r.cmkTemplate(data), data.RandomString)
}

func (r StorageAccountResource) sasPolicy(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
features {}
}

resource "azurerm_resource_group" "test" {
name = "acctestRG-storage-%d"
location = "%s"
}

resource "azurerm_storage_account" "test" {
name = "unlikely23exst2acct%s"
resource_group_name = azurerm_resource_group.test.name

location = azurerm_resource_group.test.location
account_tier = "Standard"
account_replication_type = "LRS"

sas_policy {
expiration_action = "Log"
expiration_period = "1.15:5:05"
}
}
`, data.RandomInteger, data.Locations.Primary, data.RandomString)
}
10 changes: 10 additions & 0 deletions website/docs/r/storage_account.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,8 @@ The following arguments are supported:

* `immutability_policy` - (Optional) An `immutability_policy` block as defined below.

* `sas_policy` - (Optional) A `sas_policy` block as defined below.

* `tags` - (Optional) A mapping of tags to assign to the resource.

---
Expand Down Expand Up @@ -369,6 +371,14 @@ A `queue_properties` block supports the following:

---

A `sas_policy` block supports the following:

* `expiration_period` - (Required) The SAS expiration period in format of `DD.HH:MM:SS`.

* `expiration_action` - (Optional) The SAS expiration action. The only possible value is `Log` at this moment. Defaults to `Log`.

---

A `static_website` block supports the following:

* `index_document` - (Optional) The webpage that Azure Storage serves for requests to the root of a website or any subfolder. For example, index.html. The value is case-sensitive.
Expand Down