Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

azurerm_cosmosdb_account - primary_sql_connection_string, secondary_sql_connection_string, primary_readonly_sql_connection_string, secondary_readonly_sql_connection_string #17810

Merged
merged 10 commits into from
Oct 20, 2022
34 changes: 34 additions & 0 deletions internal/services/cosmos/cosmosdb_account_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ import (

var CosmosDbAccountResourceName = "azurerm_cosmosdb_account"

var connStringPropertyMap = map[string]string{
"Primary SQL Connection String": "primary_sql_connection_string",
"Secondary SQL Connection String": "secondary_sql_connection_string",
"Primary Read-Only SQL Connection String": "primary_readonly_sql_connection_string",
"Secondary Read-Only SQL Connection String": "secondary_readonly_sql_connection_string",
}

// If the consistency policy of the Cosmos DB Database Account is not bounded staleness,
// any changes to the configuration for bounded staleness should be suppressed.
func suppressConsistencyPolicyStalenessConfiguration(_, _, _ string, d *pluginsdk.ResourceData) bool {
Expand Down Expand Up @@ -565,6 +572,30 @@ func resourceCosmosDbAccount() *pluginsdk.Resource {
},
},

"primary_sql_connection_string": {
Type: pluginsdk.TypeString,
Computed: true,
Sensitive: true,
},

"secondary_sql_connection_string": {
Type: pluginsdk.TypeString,
Computed: true,
Sensitive: true,
},

"primary_readonly_sql_connection_string": {
Type: pluginsdk.TypeString,
Computed: true,
Sensitive: true,
},

"secondary_readonly_sql_connection_string": {
Type: pluginsdk.TypeString,
Computed: true,
Sensitive: true,
},

"tags": tags.Schema(),
},
}
Expand Down Expand Up @@ -1106,6 +1137,9 @@ func resourceCosmosDbAccountRead(d *pluginsdk.ResourceData, meta interface{}) er
connStrings = make([]string, len(*connStringResp.ConnectionStrings))
for i, v := range *connStringResp.ConnectionStrings {
connStrings[i] = *v.ConnectionString
if propertyName, propertyExists := connStringPropertyMap[*v.Description]; propertyExists {
lewis-od marked this conversation as resolved.
Show resolved Hide resolved
d.Set(propertyName, *v.ConnectionString)
}
}
}
d.Set("connection_strings", connStrings)
Expand Down
37 changes: 31 additions & 6 deletions internal/services/cosmos/cosmosdb_account_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,23 @@ import (
type CosmosDBAccountResource struct{}

func TestAccCosmosDBAccount_basic_global_boundedStaleness(t *testing.T) {
testAccCosmosDBAccount_basicWith(t, documentdb.DatabaseAccountKindGlobalDocumentDB, documentdb.DefaultConsistencyLevelBoundedStaleness)
testAccCosmosDBAccount_basicDocumentDbWith(t, documentdb.DefaultConsistencyLevelBoundedStaleness)
}

func TestAccCosmosDBAccount_basic_global_consistentPrefix(t *testing.T) {
testAccCosmosDBAccount_basicWith(t, documentdb.DatabaseAccountKindGlobalDocumentDB, documentdb.DefaultConsistencyLevelConsistentPrefix)
testAccCosmosDBAccount_basicDocumentDbWith(t, documentdb.DefaultConsistencyLevelConsistentPrefix)
}

func TestAccCosmosDBAccount_basic_global_eventual(t *testing.T) {
testAccCosmosDBAccount_basicWith(t, documentdb.DatabaseAccountKindGlobalDocumentDB, documentdb.DefaultConsistencyLevelEventual)
testAccCosmosDBAccount_basicDocumentDbWith(t, documentdb.DefaultConsistencyLevelEventual)
}

func TestAccCosmosDBAccount_basic_global_session(t *testing.T) {
testAccCosmosDBAccount_basicWith(t, documentdb.DatabaseAccountKindGlobalDocumentDB, documentdb.DefaultConsistencyLevelSession)
testAccCosmosDBAccount_basicDocumentDbWith(t, documentdb.DefaultConsistencyLevelSession)
}

func TestAccCosmosDBAccount_basic_global_strong(t *testing.T) {
testAccCosmosDBAccount_basicWith(t, documentdb.DatabaseAccountKindGlobalDocumentDB, documentdb.DefaultConsistencyLevelStrong)
testAccCosmosDBAccount_basicDocumentDbWith(t, documentdb.DefaultConsistencyLevelStrong)
}

func TestAccCosmosDBAccount_basic_mongo_boundedStaleness(t *testing.T) {
Expand All @@ -61,7 +61,7 @@ func TestAccCosmosDBAccount_basic_mongo_strong(t *testing.T) {
}

func TestAccCosmosDBAccount_basic_mongo_strong_without_capability(t *testing.T) {
testAccCosmosDBAccount_basicWith(t, documentdb.DatabaseAccountKindMongoDB, documentdb.DefaultConsistencyLevelStrong)
testAccCosmosDBAccount_basicMongoDBWith(t, documentdb.DefaultConsistencyLevelStrong)
}

func TestAccCosmosDBAccount_basic_parse_boundedStaleness(t *testing.T) {
Expand Down Expand Up @@ -191,6 +191,22 @@ func testAccCosmosDBAccount_basicWith(t *testing.T, kind documentdb.DatabaseAcco
})
}

func testAccCosmosDBAccount_basicDocumentDbWith(t *testing.T, consistency documentdb.DefaultConsistencyLevel) {
data := acceptance.BuildTestData(t, "azurerm_cosmosdb_account", "test")
r := CosmosDBAccountResource{}

data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.basic(data, documentdb.DatabaseAccountKindGlobalDocumentDB, consistency),
Check: acceptance.ComposeAggregateTestCheckFunc(
checkAccCosmosDBAccount_basic(data, consistency, 1),
checkAccCosmosDBAccount_sql(data),
),
},
data.ImportStep(),
})
}

func testAccCosmosDBAccount_basicMongoDBWith(t *testing.T, consistency documentdb.DefaultConsistencyLevel) {
data := acceptance.BuildTestData(t, "azurerm_cosmosdb_account", "test")
r := CosmosDBAccountResource{}
Expand Down Expand Up @@ -2137,6 +2153,15 @@ func checkAccCosmosDBAccount_basic(data acceptance.TestData, consistency documen
)
}

func checkAccCosmosDBAccount_sql(data acceptance.TestData) acceptance.TestCheckFunc {
return acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).Key("primary_sql_connection_string").Exists(),
check.That(data.ResourceName).Key("secondary_sql_connection_string").Exists(),
check.That(data.ResourceName).Key("primary_readonly_sql_connection_string").Exists(),
check.That(data.ResourceName).Key("secondary_readonly_sql_connection_string").Exists(),
)
}

func (CosmosDBAccountResource) network_access_enabled(data acceptance.TestData, kind documentdb.DatabaseAccountKind, consistency documentdb.DefaultConsistencyLevel) string {
return fmt.Sprintf(`
provider "azurerm" {
Expand Down
16 changes: 12 additions & 4 deletions website/docs/d/cosmosdb_account.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,21 @@ The following attributes are exported:

* `write_endpoints` - A list of write endpoints available for this CosmosDB account.

* `primary_key` - The Primary key for the CosmosDB Account.
* `primary_key` - The primary key for the CosmosDB account.

* `secondary_key` - The Secondary key for the CosmosDB Account.
* `secondary_key` - The secondary key for the CosmosDB account.

* `primary_readonly_key` - The Primary read-only Key for the CosmosDB Account.
* `primary_readonly_key` - The primary read-only Key for the CosmosDB account.

* `secondary_readonly_key` - The Secondary read-only key for the CosmosDB Account.
* `secondary_readonly_key` - The secondary read-only key for the CosmosDB account.

* `primary_sql_connection_string` - The primary SQL connection string for the CosmosDB account.

* `secondary_sql_connection_string` - The secondary SQL connection string for the CosmosDB account.

* `primary_readonly_sql_connection_string` - The primary read-only SQL connection string for the CosmosDB account.

* `secondary_readonly_sql_connection_string` - The secondary read-only SQL connection string for the CosmosDB account.

## Timeouts

Expand Down