Skip to content

Commit

Permalink
feat(cosmosdb): Adds connection_strings attribute to cosmos_db_account
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
thomastaylor312 committed May 4, 2018
1 parent 65aa4f3 commit cf62668
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 8 deletions.
36 changes: 28 additions & 8 deletions azurerm/resource_arm_cosmos_db_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
},
},
}
}
Expand Down Expand Up @@ -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
}
Expand Down
1 change: 1 addition & 0 deletions azurerm/resource_arm_cosmos_db_account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
),
},
},
Expand Down
3 changes: 3 additions & 0 deletions website/docs/r/cosmosdb_account.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit cf62668

Please sign in to comment.