Skip to content

Commit

Permalink
d/azurerm_cosmosdb_account - add support for connection_strings, …
Browse files Browse the repository at this point in the history
…`primary_sql_connection_string`, `secondary_sql_connection_string`, `primary_readonly_sql_connection_string`, `secondary_readonly_sql_connection_string`, `primary_mongodb_connection_string`, `secondary_mongodb_connection_string`, `primary_readonly_mongodb_connection_string`, `secondary_readonly_mongodb_connection_string` (#24129)
  • Loading branch information
lonegunmanb authored Dec 7, 2023
1 parent 2d48f47 commit 90f4130
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 11 deletions.
78 changes: 78 additions & 0 deletions internal/services/cosmos/cosmosdb_account_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,64 @@ func dataSourceCosmosDbAccount() *pluginsdk.Resource {
Computed: true,
Sensitive: true,
},

"connection_strings": {
Type: pluginsdk.TypeList,
Computed: true,
Sensitive: true,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
Sensitive: true,
},
},

"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,
},

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

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

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

"secondary_readonly_mongodb_connection_string": {
Type: pluginsdk.TypeString,
Computed: true,
Sensitive: true,
},
},
}
}
Expand Down Expand Up @@ -327,7 +385,27 @@ func dataSourceCosmosDbAccountRead(d *pluginsdk.ResourceData, meta interface{})
d.Set("primary_readonly_key", readonlyKeys.PrimaryReadonlyMasterKey)
d.Set("secondary_readonly_key", readonlyKeys.SecondaryReadonlyMasterKey)
}
connStringResp, err := client.ListConnectionStrings(ctx, id.ResourceGroup, id.Name)
if err != nil {
if utils.ResponseWasNotFound(keys.Response) {
log.Printf("[DEBUG] Connection Strings were not found for CosmosDB Account %q (Resource Group %q) - removing from state!", id.Name, id.ResourceGroup)
} else {
log.Printf("[ERROR] Unable to List connection strings for CosmosDB Account %s: %s", id.Name, err)
}
} else {
var connStrings []string
if connStringResp.ConnectionStrings != nil {
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) // lintignore:R001
}
}
}

d.Set("connection_strings", connStrings)
}
return tags.FlattenAndSet(d, resp.Tags)
}

Expand Down
54 changes: 45 additions & 9 deletions internal/services/cosmos/cosmosdb_account_data_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,24 +48,60 @@ func TestAccDataSourceCosmosDBAccount_complete(t *testing.T) {
})
}

func (CosmosDBAccountDataSourceResource) basic(data acceptance.TestData) string {
return fmt.Sprintf(`
%s
func TestAccDataSourceCosmosDBAccount_globalDocumentDBConnectionString(t *testing.T) {
data := acceptance.BuildTestData(t, "data.azurerm_cosmosdb_account", "test")
r := CosmosDBAccountDataSourceResource{}

data "azurerm_cosmosdb_account" "test" {
name = azurerm_cosmosdb_account.test.name
resource_group_name = azurerm_resource_group.test.name
data.DataSourceTest(t, []acceptance.TestStep{
{
Config: r.globalDocumentDB(data),
Check: acceptance.ComposeAggregateTestCheckFunc(
checkAccCosmosDBAccount_sql(data),
),
},
})
}
`, CosmosDBAccountResource{}.basic(data, documentdb.DatabaseAccountKindGlobalDocumentDB, documentdb.DefaultConsistencyLevelBoundedStaleness))

func TestAccDataSourceCosmosDBAccount_mongoDBConnectionString(t *testing.T) {
data := acceptance.BuildTestData(t, "data.azurerm_cosmosdb_account", "test")
r := CosmosDBAccountDataSourceResource{}

data.DataSourceTest(t, []acceptance.TestStep{
{
Config: r.mongoDB(data),
Check: acceptance.ComposeAggregateTestCheckFunc(
check.That(data.ResourceName).Key("primary_mongodb_connection_string").Exists(),
check.That(data.ResourceName).Key("secondary_mongodb_connection_string").Exists(),
check.That(data.ResourceName).Key("primary_readonly_mongodb_connection_string").Exists(),
check.That(data.ResourceName).Key("secondary_readonly_mongodb_connection_string").Exists(),
),
},
})
}

func (c CosmosDBAccountDataSourceResource) basic(data acceptance.TestData) string {
return c.dataConfig(CosmosDBAccountResource{}.basic(data, documentdb.DatabaseAccountKindGlobalDocumentDB, documentdb.DefaultConsistencyLevelBoundedStaleness))
}

func (c CosmosDBAccountDataSourceResource) complete(data acceptance.TestData) string {
return c.dataConfig(CosmosDBAccountResource{}.complete(data, documentdb.DatabaseAccountKindGlobalDocumentDB, documentdb.DefaultConsistencyLevelBoundedStaleness))
}

func (c CosmosDBAccountDataSourceResource) globalDocumentDB(data acceptance.TestData) string {
return c.dataConfig(CosmosDBAccountResource{}.basic(data, documentdb.DatabaseAccountKindGlobalDocumentDB, documentdb.DefaultConsistencyLevelBoundedStaleness))
}

func (c CosmosDBAccountDataSourceResource) mongoDB(data acceptance.TestData) string {
return c.dataConfig(CosmosDBAccountResource{}.basicMongoDB(data, documentdb.DefaultConsistencyLevelStrong))
}

func (CosmosDBAccountDataSourceResource) complete(data acceptance.TestData) string {
func (c CosmosDBAccountDataSourceResource) dataConfig(baseConfig string) string {
return fmt.Sprintf(`
%s
data "azurerm_cosmosdb_account" "test" {
name = azurerm_cosmosdb_account.test.name
resource_group_name = azurerm_resource_group.test.name
}
`, CosmosDBAccountResource{}.complete(data, documentdb.DatabaseAccountKindGlobalDocumentDB, documentdb.DefaultConsistencyLevelBoundedStaleness))
`, baseConfig)
}
14 changes: 12 additions & 2 deletions website/docs/d/cosmosdb_account.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,24 @@ The following attributes are exported:

* `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.
* `connection_strings` - A list of connection strings available for this CosmosDB account.

* `secondary_sql_connection_string` - The secondary SQL connection string 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.

* `primary_mongodb_connection_string` - The primary Mongodb connection string for the CosmosDB account.

* `secondary_mongodb_connection_string` - The secondary Mongodb connection string for the CosmosDB account.

* `primary_readonly_mongodb_connection_string` - The primary readonly Mongodb connection string for the CosmosDB account.

* `secondary_readonly_mongodb_connection_string` - The secondary readonly Mongodb connection string for the CosmosDB account.

## Timeouts

The `timeouts` block allows you to specify [timeouts](https://www.terraform.io/language/resources/syntax#operation-timeouts) for certain actions:
Expand Down

0 comments on commit 90f4130

Please sign in to comment.