From d01739eaff9dd419ab5ea0e3ad3f2566cb636813 Mon Sep 17 00:00:00 2001 From: Lewis O'Driscoll Date: Thu, 20 Oct 2022 20:56:19 +0100 Subject: [PATCH] azurerm_cosmosdb_account - primary_sql_connection_string, secondary_sql_connection_string, primary_readonly_sql_connection_string, secondary_readonly_sql_connection_string (#17810) --- .../cosmos/cosmosdb_account_resource.go | 34 +++++++++++++++++ .../cosmos/cosmosdb_account_resource_test.go | 37 ++++++++++++++++--- website/docs/d/cosmosdb_account.html.markdown | 16 ++++++-- 3 files changed, 77 insertions(+), 10 deletions(-) diff --git a/internal/services/cosmos/cosmosdb_account_resource.go b/internal/services/cosmos/cosmosdb_account_resource.go index 249b88b7e6d8..58112e34a0ce 100644 --- a/internal/services/cosmos/cosmosdb_account_resource.go +++ b/internal/services/cosmos/cosmosdb_account_resource.go @@ -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 { @@ -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(), }, } @@ -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 { + d.Set(propertyName, *v.ConnectionString) + } } } d.Set("connection_strings", connStrings) diff --git a/internal/services/cosmos/cosmosdb_account_resource_test.go b/internal/services/cosmos/cosmosdb_account_resource_test.go index a478f8ca52c8..b526c5885b7f 100644 --- a/internal/services/cosmos/cosmosdb_account_resource_test.go +++ b/internal/services/cosmos/cosmosdb_account_resource_test.go @@ -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) { @@ -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) { @@ -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{} @@ -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" { diff --git a/website/docs/d/cosmosdb_account.html.markdown b/website/docs/d/cosmosdb_account.html.markdown index 7b9350ef4b1e..1a9ef7e3076c 100644 --- a/website/docs/d/cosmosdb_account.html.markdown +++ b/website/docs/d/cosmosdb_account.html.markdown @@ -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