From cf62668ae55dcbde69a3d0519759cde862b94110 Mon Sep 17 00:00:00 2001 From: Taylor Thomas Date: Thu, 3 May 2018 00:07:38 -0700 Subject: [PATCH] feat(cosmosdb): Adds connection_strings attribute to cosmos_db_account Adds connection_strings attribute to cosmos_db_account with documentation. Also adds in some missing error handling in the same function I modified for the feature. --- azurerm/resource_arm_cosmos_db_account.go | 36 ++++++++++++++----- .../resource_arm_cosmos_db_account_test.go | 1 + website/docs/r/cosmosdb_account.html.markdown | 3 ++ 3 files changed, 32 insertions(+), 8 deletions(-) diff --git a/azurerm/resource_arm_cosmos_db_account.go b/azurerm/resource_arm_cosmos_db_account.go index b4c0e1bed192..0257c9d391de 100644 --- a/azurerm/resource_arm_cosmos_db_account.go +++ b/azurerm/resource_arm_cosmos_db_account.go @@ -227,6 +227,14 @@ func resourceArmCosmosDBAccount() *schema.Resource { Type: schema.TypeString, Computed: true, }, + + "connection_strings": { + Type: schema.TypeList, + Computed: true, + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, }, } } @@ -490,19 +498,31 @@ func resourceArmCosmosDBAccountRead(d *schema.ResourceData, meta interface{}) er keys, err := client.ListKeys(ctx, resourceGroup, name) if err != nil { - log.Printf("[ERROR] Unable to List Write keys for CosmosDB Account %s: %s", name, err) - } else { - d.Set("primary_master_key", keys.PrimaryMasterKey) - d.Set("secondary_master_key", keys.SecondaryMasterKey) + return fmt.Errorf("[ERROR] Unable to List Write keys for CosmosDB Account %s: %s", name, err) } + d.Set("primary_master_key", keys.PrimaryMasterKey) + d.Set("secondary_master_key", keys.SecondaryMasterKey) readonlyKeys, err := client.ListReadOnlyKeys(ctx, resourceGroup, name) if err != nil { - log.Printf("[ERROR] Unable to List read-only keys for CosmosDB Account %s: %s", name, err) - } else { - d.Set("primary_readonly_master_key", readonlyKeys.PrimaryReadonlyMasterKey) - d.Set("secondary_readonly_master_key", readonlyKeys.SecondaryReadonlyMasterKey) + return fmt.Errorf("[ERROR] Unable to List read-only keys for CosmosDB Account %s: %s", name, err) + } + d.Set("primary_readonly_master_key", readonlyKeys.PrimaryReadonlyMasterKey) + d.Set("secondary_readonly_master_key", readonlyKeys.SecondaryReadonlyMasterKey) + + connStringResp, err := client.ListConnectionStrings(ctx, resourceGroup, name) + if err != nil { + return fmt.Errorf("[ERROR] Unable to List connection strings for CosmosDB Account %s: %s", name, err) + } + var connStrings []string + if connStringResp.ConnectionStrings != nil { + connStrings = make([]string, len(*connStringResp.ConnectionStrings)) + for i, v := range *connStringResp.ConnectionStrings { + connStrings[i] = *v.ConnectionString + } + } + d.Set("connection_strings", connStrings) return nil } diff --git a/azurerm/resource_arm_cosmos_db_account_test.go b/azurerm/resource_arm_cosmos_db_account_test.go index af543dfba05a..904df9dc7c0d 100644 --- a/azurerm/resource_arm_cosmos_db_account_test.go +++ b/azurerm/resource_arm_cosmos_db_account_test.go @@ -215,6 +215,7 @@ func TestAccAzureRMCosmosDBAccount_mongoDB(t *testing.T) { Check: resource.ComposeAggregateTestCheckFunc( checkAccAzureRMCosmosDBAccount_basic(resourceName, testLocation(), string(documentdb.BoundedStaleness), 1), resource.TestCheckResourceAttr(resourceName, "kind", "MongoDB"), + resource.TestCheckResourceAttr(resourceName, "connection_strings.#", "1"), ), }, }, diff --git a/website/docs/r/cosmosdb_account.html.markdown b/website/docs/r/cosmosdb_account.html.markdown index 1fd8e3f44b9a..24688254ffc5 100644 --- a/website/docs/r/cosmosdb_account.html.markdown +++ b/website/docs/r/cosmosdb_account.html.markdown @@ -111,6 +111,9 @@ The following attributes are exported: * `secondary_readonly_master_key` - The Secondary read-only master key for the CosmosDB Account. +* `connection_strings` - A list of connection strings available for this CosmosDB + account. If the kind is `GlobalDocumentDB`, this will be empty. + ## Import