Skip to content

Commit 0cb6e7d

Browse files
author
Peter McAtominey
committed
provider/azurerm: add enable_blob_encryption to storage_account resource
This allows Storage Service Encryption to be enabled. TF_ACC=1 go test ./builtin/providers/azurerm -v -run TestAccAzureRMStorageAccount -timeout 120m === RUN TestAccAzureRMStorageAccount_importBasic --- PASS: TestAccAzureRMStorageAccount_importBasic (139.00s) === RUN TestAccAzureRMStorageAccount_basic --- PASS: TestAccAzureRMStorageAccount_basic (151.03s) === RUN TestAccAzureRMStorageAccount_blobEncryption --- PASS: TestAccAzureRMStorageAccount_blobEncryption (149.94s) PASS ok github.com/hashicorp/terraform/builtin/providers/azurerm 440.051s
1 parent 6e84502 commit 0cb6e7d

3 files changed

Lines changed: 121 additions & 0 deletions

File tree

builtin/providers/azurerm/resource_arm_storage_account.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ import (
1414
"github.com/hashicorp/terraform/helper/signalwrapper"
1515
)
1616

17+
// The KeySource of storage.Encryption appears to require this value
18+
// for Encryption services to work
19+
var storageAccountEncryptionSource = "Microsoft.Storage"
20+
1721
func resourceArmStorageAccount() *schema.Resource {
1822
return &schema.Resource{
1923
Create: resourceArmStorageAccountCreate,
@@ -51,6 +55,11 @@ func resourceArmStorageAccount() *schema.Resource {
5155
ValidateFunc: validateArmStorageAccountType,
5256
},
5357

58+
"enable_blob_encryption": {
59+
Type: schema.TypeBool,
60+
Optional: true,
61+
},
62+
5463
"primary_location": {
5564
Type: schema.TypeString,
5665
Computed: true,
@@ -121,6 +130,7 @@ func resourceArmStorageAccountCreate(d *schema.ResourceData, meta interface{}) e
121130
accountType := d.Get("account_type").(string)
122131
location := d.Get("location").(string)
123132
tags := d.Get("tags").(map[string]interface{})
133+
enableBlobEncryption := d.Get("enable_blob_encryption").(bool)
124134

125135
sku := storage.Sku{
126136
Name: storage.SkuName(accountType),
@@ -130,6 +140,16 @@ func resourceArmStorageAccountCreate(d *schema.ResourceData, meta interface{}) e
130140
Location: &location,
131141
Sku: &sku,
132142
Tags: expandTags(tags),
143+
Properties: &storage.AccountPropertiesCreateParameters{
144+
Encryption: &storage.Encryption{
145+
Services: &storage.EncryptionServices{
146+
Blob: &storage.EncryptionService{
147+
Enabled: &enableBlobEncryption,
148+
},
149+
},
150+
KeySource: &storageAccountEncryptionSource,
151+
},
152+
},
133153
}
134154

135155
// Create the storage account. We wrap this so that it is cancellable
@@ -240,6 +260,29 @@ func resourceArmStorageAccountUpdate(d *schema.ResourceData, meta interface{}) e
240260
d.SetPartial("tags")
241261
}
242262

263+
if d.HasChange("enable_blob_encryption") {
264+
enableBlobEncryption := d.Get("enable_blob_encryption").(bool)
265+
266+
opts := storage.AccountUpdateParameters{
267+
Properties: &storage.AccountPropertiesUpdateParameters{
268+
Encryption: &storage.Encryption{
269+
Services: &storage.EncryptionServices{
270+
Blob: &storage.EncryptionService{
271+
Enabled: &enableBlobEncryption,
272+
},
273+
},
274+
KeySource: &storageAccountEncryptionSource,
275+
},
276+
},
277+
}
278+
_, err := client.Update(resourceGroupName, storageAccountName, opts)
279+
if err != nil {
280+
return fmt.Errorf("Error updating Azure Storage Account enable_blob_encryption %q: %s", storageAccountName, err)
281+
}
282+
283+
d.SetPartial("enable_blob_encryption")
284+
}
285+
243286
d.Partial(false)
244287
return nil
245288
}
@@ -301,6 +344,12 @@ func resourceArmStorageAccountRead(d *schema.ResourceData, meta interface{}) err
301344
}
302345
}
303346

347+
if resp.Properties.Encryption != nil {
348+
if resp.Properties.Encryption.Services.Blob != nil {
349+
d.Set("enable_blob_encryption", resp.Properties.Encryption.Services.Blob.Enabled)
350+
}
351+
}
352+
304353
d.Set("name", resp.Name)
305354

306355
flattenAndSetTags(d, resp.Tags)

builtin/providers/azurerm/resource_arm_storage_account_test.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,36 @@ func TestAccAzureRMStorageAccount_disappears(t *testing.T) {
109109
})
110110
}
111111

112+
func TestAccAzureRMStorageAccount_blobEncryption(t *testing.T) {
113+
ri := acctest.RandInt()
114+
rs := acctest.RandString(4)
115+
preConfig := fmt.Sprintf(testAccAzureRMStorageAccount_blobEncryption, ri, rs)
116+
postConfig := fmt.Sprintf(testAccAzureRMStorageAccount_blobEncryptionDisabled, ri, rs)
117+
118+
resource.Test(t, resource.TestCase{
119+
PreCheck: func() { testAccPreCheck(t) },
120+
Providers: testAccProviders,
121+
CheckDestroy: testCheckAzureRMStorageAccountDestroy,
122+
Steps: []resource.TestStep{
123+
resource.TestStep{
124+
Config: preConfig,
125+
Check: resource.ComposeTestCheckFunc(
126+
testCheckAzureRMStorageAccountExists("azurerm_storage_account.testsa"),
127+
resource.TestCheckResourceAttr("azurerm_storage_account.testsa", "enable_blob_encryption", "true"),
128+
),
129+
},
130+
131+
resource.TestStep{
132+
Config: postConfig,
133+
Check: resource.ComposeTestCheckFunc(
134+
testCheckAzureRMStorageAccountExists("azurerm_storage_account.testsa"),
135+
resource.TestCheckResourceAttr("azurerm_storage_account.testsa", "enable_blob_encryption", "false"),
136+
),
137+
},
138+
},
139+
})
140+
}
141+
112142
func testCheckAzureRMStorageAccountExists(name string) resource.TestCheckFunc {
113143
return func(s *terraform.State) error {
114144
// Ensure we have enough information in state to look up in API
@@ -218,3 +248,41 @@ resource "azurerm_storage_account" "testsa" {
218248
environment = "staging"
219249
}
220250
}`
251+
252+
var testAccAzureRMStorageAccount_blobEncryption = `
253+
resource "azurerm_resource_group" "testrg" {
254+
name = "testAccAzureRMSA-%d"
255+
location = "westus"
256+
}
257+
258+
resource "azurerm_storage_account" "testsa" {
259+
name = "unlikely23exst2acct%s"
260+
resource_group_name = "${azurerm_resource_group.testrg.name}"
261+
262+
location = "westus"
263+
account_type = "Standard_LRS"
264+
enable_blob_encryption = true
265+
266+
tags {
267+
environment = "production"
268+
}
269+
}`
270+
271+
var testAccAzureRMStorageAccount_blobEncryptionDisabled = `
272+
resource "azurerm_resource_group" "testrg" {
273+
name = "testAccAzureRMSA-%d"
274+
location = "westus"
275+
}
276+
277+
resource "azurerm_storage_account" "testsa" {
278+
name = "unlikely23exst2acct%s"
279+
resource_group_name = "${azurerm_resource_group.testrg.name}"
280+
281+
location = "westus"
282+
account_type = "Standard_LRS"
283+
enable_blob_encryption = false
284+
285+
tags {
286+
environment = "production"
287+
}
288+
}`

website/source/docs/providers/azurerm/r/storage_account.html.markdown

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ The following arguments are supported:
5151
documentation for more information on which types of accounts can be converted
5252
into other types.
5353

54+
* `enable_bool_encryption` - (Optional) Boolean flag which controls if Encryption
55+
Services are enabled for Blob storage, see [here](https://azure.microsoft.com/en-us/documentation/articles/storage-service-encryption/)
56+
for more information.
57+
5458
* `tags` - (Optional) A mapping of tags to assign to the resource.
5559

5660
Note that although the Azure API supports setting custom domain names for

0 commit comments

Comments
 (0)