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_mongo_database new data source #13123

Merged
merged 9 commits into from
Aug 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions internal/services/cosmos/cosmosdb_mongo_database_data_source.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package cosmos

import (
"fmt"
"time"

"github.com/hashicorp/terraform-provider-azurerm/helpers/azure"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/cosmos/parse"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/cosmos/validate"
"github.com/hashicorp/terraform-provider-azurerm/internal/tags"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/validation"
"github.com/hashicorp/terraform-provider-azurerm/internal/timeouts"
"github.com/hashicorp/terraform-provider-azurerm/utils"
)

func dataSourceCosmosDbMongoDatabase() *pluginsdk.Resource {
return &pluginsdk.Resource{
Read: dataSourceCosmosDbMongoDatabaseRead,

Timeouts: &pluginsdk.ResourceTimeout{
Read: pluginsdk.DefaultTimeout(5 * time.Minute),
},

Schema: map[string]*pluginsdk.Schema{
"name": {
Type: pluginsdk.TypeString,
Required: true,
ValidateFunc: validation.StringIsNotEmpty,
},

"account_name": {
Type: pluginsdk.TypeString,
Required: true,
ValidateFunc: validate.CosmosAccountName,
},

"resource_group_name": azure.SchemaResourceGroupNameForDataSource(),

"tags": tags.SchemaDataSource(),
},
}
}

func dataSourceCosmosDbMongoDatabaseRead(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Cosmos.MongoDbClient
subscriptionId := meta.(*clients.Client).Account.SubscriptionId
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()

id := parse.NewMongodbDatabaseID(subscriptionId, d.Get("resource_group_name").(string), d.Get("account_name").(string), d.Get("name").(string))

resp, err := client.GetMongoDBDatabase(ctx, id.ResourceGroup, id.DatabaseAccountName, id.Name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
return fmt.Errorf("%s was not found", id)
}
return fmt.Errorf("retrieving %s: %+v", id, err)
}

d.SetId(id.ID())
d.Set("name", id.Name)
d.Set("account_name", id.DatabaseAccountName)
d.Set("resource_group_name", id.ResourceGroup)

return tags.FlattenAndSet(d, resp.Tags)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package cosmos_test

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance"
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance/check"
)

type CosmosMongoDatabaseDataSourceResource struct {
}

func TestAccDataSourceCosmosMongoDatabase_basic(t *testing.T) {
data := acceptance.BuildTestData(t, "data.azurerm_cosmosdb_mongo_database", "test")
r := CosmosMongoDatabaseDataSourceResource{}

data.DataSourceTest(t, []acceptance.TestStep{
{
Config: r.basic(data),
Check: acceptance.ComposeAggregateTestCheckFunc(
check.That(data.ResourceName).Key("name").Exists(),
check.That(data.ResourceName).Key("account_name").Exists(),
check.That(data.ResourceName).Key("resource_group_name").Exists(),
check.That(data.ResourceName).Key("tags.%").HasValue("0"),
),
},
})
}

func (CosmosMongoDatabaseDataSourceResource) basic(data acceptance.TestData) string {
return fmt.Sprintf(`
%s

data "azurerm_cosmosdb_mongo_database" "test" {
name = azurerm_cosmosdb_mongo_database.test.name
resource_group_name = azurerm_cosmosdb_account.test.resource_group_name
account_name = azurerm_cosmosdb_account.test.name
}
`, CosmosMongoDatabaseResource{}.basic(data))
}
3 changes: 2 additions & 1 deletion internal/services/cosmos/registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ func (r Registration) WebsiteCategories() []string {
// SupportedDataSources returns the supported Data Sources supported by this Service
func (r Registration) SupportedDataSources() map[string]*pluginsdk.Resource {
return map[string]*pluginsdk.Resource{
"azurerm_cosmosdb_account": dataSourceCosmosDbAccount(),
"azurerm_cosmosdb_account": dataSourceCosmosDbAccount(),
"azurerm_cosmosdb_mongo_database": dataSourceCosmosDbMongoDatabase(),
}
}

Expand Down
49 changes: 49 additions & 0 deletions website/docs/d/cosmosdb_mongo_database.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
---
subcategory: "CosmosDB (DocumentDB)"
layout: "azurerm"
page_title: "Azure Resource Manager: azurerm_cosmosdb_mongo_database"
description: |-
Gets information about an existing Cosmos DB Mongo Database.
---

# Data Source: azurerm_cosmosdb_mongo_database

Use this data source to access information about an existing Cosmos DB Mongo Database.

## Example Usage

```hcl
data "azurerm_cosmosdb_mongo_database" "example" {
name = "test-cosmosdb-mongo-db"
resource_group_name = "test-cosmosdb-account-rg"
account_name = "test-cosmosdb-account"
}

output "id" {
value = data.azurerm_cosmosdb_mongo_database.example.id
}
```

## Arguments Reference

The following arguments are supported:

* `account_name` - (Required) The name of the Cosmos DB Account where the Mongo Database exists.

* `name` - (Required) The name of this Cosmos DB Mongo Database.

* `resource_group_name` - (Required) The name of the Resource Group where the Cosmos DB Mongo Database exists.

## Attributes Reference

In addition to the Arguments listed above - the following Attributes are exported:

* `id` - The ID of the Cosmos DB Mongo Database.

* `tags` - A mapping of tags assigned to the Cosmos DB Mongo Database.

## Timeouts

The `timeouts` block allows you to specify [timeouts](https://www.terraform.io/docs/configuration/resources.html#timeouts) for certain actions:

* `read` - (Defaults to 5 minutes) Used when retrieving the Cosmos Mongo Database.