Skip to content

Commit

Permalink
Merge pull request #5649 from aqche/storage_account_static_website
Browse files Browse the repository at this point in the history
`azurerm_storage_account` - add support for static website
  • Loading branch information
tombuildsstuff committed Feb 17, 2020
2 parents c4cf372 + 576fa5b commit 3926d81
Show file tree
Hide file tree
Showing 11 changed files with 517 additions and 0 deletions.
6 changes: 6 additions & 0 deletions azurerm/internal/services/storage/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/Azure/azure-sdk-for-go/services/storage/mgmt/2019-04-01/storage"
az "github.com/Azure/go-autorest/autorest/azure"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/common"
"github.com/tombuildsstuff/giovanni/storage/2018-11-09/blob/accounts"
"github.com/tombuildsstuff/giovanni/storage/2018-11-09/blob/blobs"
"github.com/tombuildsstuff/giovanni/storage/2018-11-09/blob/containers"
"github.com/tombuildsstuff/giovanni/storage/2018-11-09/file/directories"
Expand All @@ -24,6 +25,7 @@ type Client struct {
FileSystemsClient *filesystems.Client
ManagementPoliciesClient storage.ManagementPoliciesClient
BlobServicesClient storage.BlobServicesClient
BlobAccountsClient *accounts.Client

environment az.Environment
storageAdAuth *autorest.Authorizer
Expand All @@ -42,13 +44,17 @@ func NewClient(options *common.ClientOptions) *Client {
blobServicesClient := storage.NewBlobServicesClientWithBaseURI(options.ResourceManagerEndpoint, options.SubscriptionId)
options.ConfigureClient(&blobServicesClient.Client, options.ResourceManagerAuthorizer)

blobAccountsClient := accounts.NewWithEnvironment(options.Environment)
options.ConfigureClient(&blobAccountsClient.Client, options.StorageAuthorizer)

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

Expand Down
114 changes: 114 additions & 0 deletions azurerm/internal/services/storage/resource_arm_storage_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tags"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
"github.com/tombuildsstuff/giovanni/storage/2018-11-09/blob/accounts"
"github.com/tombuildsstuff/giovanni/storage/2018-11-09/queue/queues"
)

Expand Down Expand Up @@ -397,6 +398,26 @@ func resourceArmStorageAccount() *schema.Resource {
},
},

"static_website": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"index_document": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringIsNotEmpty,
},
"error_404_document": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringIsNotEmpty,
},
},
},
},

"primary_location": {
Type: schema.TypeString,
Computed: true,
Expand Down Expand Up @@ -767,6 +788,20 @@ func resourceArmStorageAccountCreate(d *schema.ResourceData, meta interface{}) e
}
}

if val, ok := d.GetOk("static_website"); ok {
// static website only supported on Storage V2
if accountKind != string(storage.StorageV2) {
return fmt.Errorf("`static_website` is only supported for Storage V2.")
}
blobAccountClient := meta.(*clients.Client).Storage.BlobAccountsClient

staticWebsiteProps := expandStaticWebsiteProperties(val.([]interface{}))

if _, err = blobAccountClient.SetServiceProperties(ctx, storageAccountName, staticWebsiteProps); err != nil {
return fmt.Errorf("Error updating Azure Storage Account `static_website` %q: %+v", storageAccountName, err)
}
}

return resourceArmStorageAccountRead(d, meta)
}

Expand Down Expand Up @@ -991,6 +1026,22 @@ func resourceArmStorageAccountUpdate(d *schema.ResourceData, meta interface{}) e
d.SetPartial("queue_properties")
}

if d.HasChange("static_website") {
// static website only supported on Storage V2
if accountKind != string(storage.StorageV2) {
return fmt.Errorf("`static_website` is only supported for Storage V2.")
}
blobAccountClient := meta.(*clients.Client).Storage.BlobAccountsClient

staticWebsiteProps := expandStaticWebsiteProperties(d.Get("static_website").([]interface{}))

if _, err = blobAccountClient.SetServiceProperties(ctx, storageAccountName, staticWebsiteProps); err != nil {
return fmt.Errorf("Error updating Azure Storage Account `static_website` %q: %+v", storageAccountName, err)
}

d.SetPartial("static_website")
}

d.Partial(false)
return resourceArmStorageAccountRead(d, meta)
}
Expand Down Expand Up @@ -1205,6 +1256,26 @@ func resourceArmStorageAccountRead(d *schema.ResourceData, meta interface{}) err
}
}

var staticWebsite []interface{}

// static website only supported on Storage V2
if resp.Kind == storage.StorageV2 {
blobAccountClient := storageClient.BlobAccountsClient

staticWebsiteProps, err := blobAccountClient.GetServiceProperties(ctx, name)
if err != nil {
if staticWebsiteProps.Response.Response != nil && !utils.ResponseWasNotFound(staticWebsiteProps.Response) {
return fmt.Errorf("Error reading static website for AzureRM Storage Account %q: %+v", name, err)
}
}

staticWebsite = flattenStaticWebsiteProperties(staticWebsiteProps)
}

if err := d.Set("static_website", staticWebsite); err != nil {
return fmt.Errorf("Error setting `static_website `for AzureRM Storage Account %q: %+v", name, err)
}

return tags.FlattenAndSet(d, resp.Tags)
}

Expand Down Expand Up @@ -1560,6 +1631,31 @@ func expandQueuePropertiesCors(input []interface{}) *queues.Cors {
return cors
}

func expandStaticWebsiteProperties(input []interface{}) accounts.StorageServiceProperties {
properties := accounts.StorageServiceProperties{
StaticWebsite: &accounts.StaticWebsite{
Enabled: false,
},
}
if len(input) == 0 {
return properties
}

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

properties.StaticWebsite.Enabled = true

if v, ok := attr["index_document"]; ok {
properties.StaticWebsite.IndexDocument = v.(string)
}

if v, ok := attr["error_404_document"]; ok {
properties.StaticWebsite.ErrorDocument404Path = v.(string)
}

return properties
}

func flattenStorageAccountNetworkRules(input *storage.NetworkRuleSet) []interface{} {
if input == nil {
return []interface{}{}
Expand Down Expand Up @@ -1802,6 +1898,24 @@ func flattenCorsProperty(input string) []interface{} {
return results
}

func flattenStaticWebsiteProperties(input accounts.GetServicePropertiesResult) []interface{} {
if storageServiceProps := input.StorageServiceProperties; storageServiceProps != nil {
if staticWebsite := storageServiceProps.StaticWebsite; staticWebsite != nil {
if !staticWebsite.Enabled {
return []interface{}{}
}

return []interface{}{
map[string]interface{}{
"index_document": staticWebsite.IndexDocument,
"error_404_document": staticWebsite.ErrorDocument404Path,
},
}
}
}
return []interface{}{}
}

func flattenStorageAccountBypass(input storage.Bypass) []interface{} {
bypassValues := strings.Split(string(input), ", ")
bypass := make([]interface{}, len(bypassValues))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,32 @@ func TestAccAzureRMStorageAccount_queueProperties(t *testing.T) {
})
}

func TestAccAzureRMStorageAccount_staticWebsiteProperties(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_storage_account", "test")

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acceptance.PreCheck(t) },
Providers: acceptance.SupportedProviders,
CheckDestroy: testCheckAzureRMStorageAccountDestroy,
Steps: []resource.TestStep{
{
Config: testAccAzureRMStorageAccount_staticWebsiteProperties(data),
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMStorageAccountExists(data.ResourceName),
),
},
data.ImportStep(),
{
Config: testAccAzureRMStorageAccount_staticWebsitePropertiesUpdated(data),
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMStorageAccountExists(data.ResourceName),
),
},
data.ImportStep(),
},
})
}

func testCheckAzureRMStorageAccountExists(resourceName string) resource.TestCheckFunc {
return func(s *terraform.State) error {
// Ensure resource group exists in API
Expand Down Expand Up @@ -1551,3 +1577,51 @@ resource "azurerm_storage_account" "test" {
}
`, data.RandomInteger, data.Locations.Primary, data.RandomString)
}

func testAccAzureRMStorageAccount_staticWebsiteProperties(data acceptance.TestData) string {
return fmt.Sprintf(`
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_kind = "StorageV2"
account_tier = "Standard"
account_replication_type = "LRS"
static_website {
index_document = "index.html"
error_404_document = "404.html"
}
}
`, data.RandomInteger, data.Locations.Primary, data.RandomString)
}

func testAccAzureRMStorageAccount_staticWebsitePropertiesUpdated(data acceptance.TestData) string {
return fmt.Sprintf(`
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_kind = "StorageV2"
account_tier = "Standard"
account_replication_type = "LRS"
static_website {
index_document = "index-2.html"
error_404_document = "404-2.html"
}
}
`, data.RandomInteger, data.Locations.Primary, data.RandomString)
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 3926d81

Please sign in to comment.