Skip to content

Commit

Permalink
Merge pull request #142 from terraform-providers/storageaccount-conne…
Browse files Browse the repository at this point in the history
…ction-strings

`azurerm_storage_account` - exposing a formatted Connection String for Blob access
  • Loading branch information
tombuildsstuff authored Jun 29, 2017
2 parents fa1ae38 + 114fed9 commit 66923f4
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 34 deletions.
8 changes: 3 additions & 5 deletions azurerm/import_arm_storage_account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package azurerm
import (
"testing"

"fmt"

"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
)
Expand All @@ -14,18 +12,18 @@ func TestAccAzureRMStorageAccount_importBasic(t *testing.T) {

ri := acctest.RandInt()
rs := acctest.RandString(4)
config := fmt.Sprintf(testAccAzureRMStorageAccount_basic, ri, rs)
config := testAccAzureRMStorageAccount_basic(ri, rs)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMStorageAccountDestroy,
Steps: []resource.TestStep{
resource.TestStep{
{
Config: config,
},

resource.TestStep{
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
Expand Down
18 changes: 18 additions & 0 deletions azurerm/resource_arm_storage_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,16 @@ func resourceArmStorageAccount() *schema.Resource {
Computed: true,
},

"primary_blob_connection_string": {
Type: schema.TypeString,
Computed: true,
},

"secondary_blob_connection_string": {
Type: schema.TypeString,
Computed: true,
},

"tags": tagsSchema(),
},
}
Expand Down Expand Up @@ -366,13 +376,21 @@ func resourceArmStorageAccountRead(d *schema.ResourceData, meta interface{}) err
d.Set("primary_queue_endpoint", resp.AccountProperties.PrimaryEndpoints.Queue)
d.Set("primary_table_endpoint", resp.AccountProperties.PrimaryEndpoints.Table)
d.Set("primary_file_endpoint", resp.AccountProperties.PrimaryEndpoints.File)

pscs := fmt.Sprintf("DefaultEndpointsProtocol=https;BlobEndpoint=%s;AccountName=%s;AccountKey=%s",
*resp.AccountProperties.PrimaryEndpoints.Blob, *resp.Name, *accessKeys[0].Value)
d.Set("primary_blob_connection_string", pscs)
}

if resp.AccountProperties.SecondaryEndpoints != nil {
if resp.AccountProperties.SecondaryEndpoints.Blob != nil {
d.Set("secondary_blob_endpoint", resp.AccountProperties.SecondaryEndpoints.Blob)
sscs := fmt.Sprintf("DefaultEndpointsProtocol=https;BlobEndpoint=%s;AccountName=%s;AccountKey=%s",
*resp.AccountProperties.SecondaryEndpoints.Blob, *resp.Name, *accessKeys[1].Value)
d.Set("secondary_blob_connection_string", sscs)
} else {
d.Set("secondary_blob_endpoint", "")
d.Set("secondary_blob_connection_string", "")
}
if resp.AccountProperties.SecondaryEndpoints.Queue != nil {
d.Set("secondary_queue_endpoint", resp.AccountProperties.SecondaryEndpoints.Queue)
Expand Down
91 changes: 63 additions & 28 deletions azurerm/resource_arm_storage_account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,15 @@ func TestValidateArmStorageAccountName(t *testing.T) {
func TestAccAzureRMStorageAccount_basic(t *testing.T) {
ri := acctest.RandInt()
rs := acctest.RandString(4)
preConfig := fmt.Sprintf(testAccAzureRMStorageAccount_basic, ri, rs)
postConfig := fmt.Sprintf(testAccAzureRMStorageAccount_update, ri, rs)
preConfig := testAccAzureRMStorageAccount_basic(ri, rs)
postConfig := testAccAzureRMStorageAccount_update(ri, rs)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMStorageAccountDestroy,
Steps: []resource.TestStep{
resource.TestStep{
{
Config: preConfig,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMStorageAccountExists("azurerm_storage_account.testsa"),
Expand All @@ -71,7 +71,7 @@ func TestAccAzureRMStorageAccount_basic(t *testing.T) {
),
},

resource.TestStep{
{
Config: postConfig,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMStorageAccountExists("azurerm_storage_account.testsa"),
Expand All @@ -87,14 +87,14 @@ func TestAccAzureRMStorageAccount_basic(t *testing.T) {
func TestAccAzureRMStorageAccount_disappears(t *testing.T) {
ri := acctest.RandInt()
rs := acctest.RandString(4)
preConfig := fmt.Sprintf(testAccAzureRMStorageAccount_basic, ri, rs)
preConfig := testAccAzureRMStorageAccount_basic(ri, rs)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMStorageAccountDestroy,
Steps: []resource.TestStep{
resource.TestStep{
{
Config: preConfig,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMStorageAccountExists("azurerm_storage_account.testsa"),
Expand All @@ -109,26 +109,47 @@ func TestAccAzureRMStorageAccount_disappears(t *testing.T) {
})
}

func TestAccAzureRMStorageAccount_blobConnectionString(t *testing.T) {
ri := acctest.RandInt()
rs := acctest.RandString(4)
preConfig := testAccAzureRMStorageAccount_basic(ri, rs)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMStorageAccountDestroy,
Steps: []resource.TestStep{
{
Config: preConfig,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMStorageAccountExists("azurerm_storage_account.testsa"),
resource.TestCheckResourceAttrSet("azurerm_storage_account.testsa", "primary_blob_connection_string"),
),
},
},
})
}

func TestAccAzureRMStorageAccount_blobEncryption(t *testing.T) {
ri := acctest.RandInt()
rs := acctest.RandString(4)
preConfig := fmt.Sprintf(testAccAzureRMStorageAccount_blobEncryption, ri, rs)
postConfig := fmt.Sprintf(testAccAzureRMStorageAccount_blobEncryptionDisabled, ri, rs)
preConfig := testAccAzureRMStorageAccount_blobEncryption(ri, rs)
postConfig := testAccAzureRMStorageAccount_blobEncryptionDisabled(ri, rs)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMStorageAccountDestroy,
Steps: []resource.TestStep{
resource.TestStep{
{
Config: preConfig,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMStorageAccountExists("azurerm_storage_account.testsa"),
resource.TestCheckResourceAttr("azurerm_storage_account.testsa", "enable_blob_encryption", "true"),
),
},

resource.TestStep{
{
Config: postConfig,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMStorageAccountExists("azurerm_storage_account.testsa"),
Expand All @@ -142,15 +163,15 @@ func TestAccAzureRMStorageAccount_blobEncryption(t *testing.T) {
func TestAccAzureRMStorageAccount_blobStorageWithUpdate(t *testing.T) {
ri := acctest.RandInt()
rs := acctest.RandString(4)
preConfig := fmt.Sprintf(testAccAzureRMStorageAccount_blobStorage, ri, rs)
postConfig := fmt.Sprintf(testAccAzureRMStorageAccount_blobStorageUpdate, ri, rs)
preConfig := testAccAzureRMStorageAccount_blobStorage(ri, rs)
postConfig := testAccAzureRMStorageAccount_blobStorageUpdate(ri, rs)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMStorageAccountDestroy,
Steps: []resource.TestStep{
resource.TestStep{
{
Config: preConfig,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMStorageAccountExists("azurerm_storage_account.testsa"),
Expand All @@ -159,7 +180,7 @@ func TestAccAzureRMStorageAccount_blobStorageWithUpdate(t *testing.T) {
),
},

resource.TestStep{
{
Config: postConfig,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMStorageAccountExists("azurerm_storage_account.testsa"),
Expand All @@ -180,14 +201,14 @@ func TestAccAzureRMStorageAccount_NonStandardCasing(t *testing.T) {
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMStorageAccountDestroy,
Steps: []resource.TestStep{
resource.TestStep{
{
Config: preConfig,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMStorageAccountExists("azurerm_storage_account.testsa"),
),
},

resource.TestStep{
{
Config: preConfig,
PlanOnly: true,
ExpectNonEmptyPlan: false,
Expand Down Expand Up @@ -270,7 +291,8 @@ func testCheckAzureRMStorageAccountDestroy(s *terraform.State) error {
return nil
}

var testAccAzureRMStorageAccount_basic = `
func testAccAzureRMStorageAccount_basic(rInt int, rString string) string {
return fmt.Sprintf(`
resource "azurerm_resource_group" "testrg" {
name = "testAccAzureRMSA-%d"
location = "westus"
Expand All @@ -286,9 +308,11 @@ resource "azurerm_storage_account" "testsa" {
tags {
environment = "production"
}
}`
}`, rInt, rString)
}

var testAccAzureRMStorageAccount_update = `
func testAccAzureRMStorageAccount_update(rInt int, rString string) string {
return fmt.Sprintf(`
resource "azurerm_resource_group" "testrg" {
name = "testAccAzureRMSA-%d"
location = "westus"
Expand All @@ -304,9 +328,11 @@ resource "azurerm_storage_account" "testsa" {
tags {
environment = "staging"
}
}`
}`, rInt, rString)
}

var testAccAzureRMStorageAccount_blobEncryption = `
func testAccAzureRMStorageAccount_blobEncryption(rInt int, rString string) string {
return fmt.Sprintf(`
resource "azurerm_resource_group" "testrg" {
name = "testAccAzureRMSA-%d"
location = "westus"
Expand All @@ -323,9 +349,11 @@ resource "azurerm_storage_account" "testsa" {
tags {
environment = "production"
}
}`
}`, rInt, rString)
}

var testAccAzureRMStorageAccount_blobEncryptionDisabled = `
func testAccAzureRMStorageAccount_blobEncryptionDisabled(rInt int, rString string) string {
return fmt.Sprintf(`
resource "azurerm_resource_group" "testrg" {
name = "testAccAzureRMSA-%d"
location = "westus"
Expand All @@ -342,10 +370,12 @@ resource "azurerm_storage_account" "testsa" {
tags {
environment = "production"
}
}`
}`, rInt, rString)
}

// BlobStorage accounts are not available in WestUS
var testAccAzureRMStorageAccount_blobStorage = `
func testAccAzureRMStorageAccount_blobStorage(rInt int, rString string) string {
return fmt.Sprintf(`
resource "azurerm_resource_group" "testrg" {
name = "testAccAzureRMSA-%d"
location = "northeurope"
Expand All @@ -362,9 +392,12 @@ resource "azurerm_storage_account" "testsa" {
tags {
environment = "production"
}
}`
}
`, rInt, rString)
}

var testAccAzureRMStorageAccount_blobStorageUpdate = `
func testAccAzureRMStorageAccount_blobStorageUpdate(rInt int, rString string) string {
return fmt.Sprintf(`
resource "azurerm_resource_group" "testrg" {
name = "testAccAzureRMSA-%d"
location = "northeurope"
Expand All @@ -382,7 +415,9 @@ resource "azurerm_storage_account" "testsa" {
tags {
environment = "production"
}
}`
}
`, rInt, rString)
}

func testAccAzureRMStorageAccountNonStandardCasing(ri int, rs string) string {
return fmt.Sprintf(`
Expand Down
3 changes: 2 additions & 1 deletion website/docs/r/storage_account.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ The following attributes are exported in addition to the arguments listed above:
* `primary_file_endpoint` - The endpoint URL for file storage in the primary location.
* `primary_access_key` - The primary access key for the storage account
* `secondary_access_key` - The secondary access key for the storage account
* `primary_blob_connection_string` - The connection string associated with the primary blob location
* `secondary_blob_connection_string` - The connection string associated with the secondary blob location

## Import

Expand All @@ -92,4 +94,3 @@ Storage Accounts can be imported using the `resource id`, e.g.
```
terraform import azurerm_storage_account.storageAcc1 /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/myresourcegroup/providers/Microsoft.Storage/storageAccounts/myaccount
```

0 comments on commit 66923f4

Please sign in to comment.