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

kusto_database support data source kusto_database_data_source.go #16180

Merged
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
108 changes: 108 additions & 0 deletions internal/services/kusto/kusto_database_data_source.go
@@ -0,0 +1,108 @@
package kusto

import (
"fmt"
"time"

"github.com/hashicorp/go-azure-helpers/resourcemanager/commonschema"
"github.com/hashicorp/go-azure-helpers/resourcemanager/location"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/kusto/parse"
kustoValidate "github.com/hashicorp/terraform-provider-azurerm/internal/services/kusto/validate"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/timeouts"
"github.com/hashicorp/terraform-provider-azurerm/utils"
)

func dataSourceKustoDatabase() *pluginsdk.Resource {
return &pluginsdk.Resource{
Read: dataSourceKustoDatabaseRead,

Importer: pluginsdk.ImporterValidatingResourceId(func(id string) error {
_, err := parse.DatabaseID(id)
return err
}),

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

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

"resource_group_name": commonschema.ResourceGroupNameForDataSource(),

"cluster_name": {
Type: pluginsdk.TypeString,
Required: true,
ValidateFunc: kustoValidate.ClusterName,
},

"location": commonschema.LocationComputed(),

"soft_delete_period": {
Type: pluginsdk.TypeString,
Computed: true,
},

"hot_cache_period": {
Type: pluginsdk.TypeString,
Computed: true,
},

"size": {
Type: pluginsdk.TypeFloat,
Computed: true,
},
},
}
}

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

id := parse.NewDatabaseID(subscriptionId, d.Get("resource_group_name").(string), d.Get("cluster_name").(string), d.Get("name").(string))

resp, err := client.Get(ctx, id.ResourceGroup, id.ClusterName, id.Name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
return fmt.Errorf("%s does not exist", id)
}

return fmt.Errorf("retrieving %s: %+v", id, err)
}

if resp.Value == nil {
return fmt.Errorf("retrieving %s: response was nil", id)
}

database, ok := resp.Value.AsReadWriteDatabase()
if !ok {
return fmt.Errorf("%s was not a Read/Write Database", id)
}

d.SetId(id.ID())

d.Set("name", id.Name)
d.Set("resource_group_name", id.ResourceGroup)
d.Set("cluster_name", id.ClusterName)
d.Set("location", location.NormalizeNilable(database.Location))

if props := database.ReadWriteDatabaseProperties; props != nil {
d.Set("hot_cache_period", props.HotCachePeriod)
d.Set("soft_delete_period", props.SoftDeletePeriod)

if statistics := props.Statistics; statistics != nil {
d.Set("size", statistics.Size)
}
}

return nil
}
37 changes: 37 additions & 0 deletions internal/services/kusto/kusto_database_data_source_test.go
@@ -0,0 +1,37 @@
package kusto_test

import (
"fmt"
"testing"

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

func TestAccKustoDatabaseDataSource_basic(t *testing.T) {
data := acceptance.BuildTestData(t, "data.azurerm_kusto_database", "test")

data.DataSourceTest(t, []acceptance.TestStep{
{
Config: testAccDataSourceKustoDatabase_basic(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(KustoDatabaseResource{}),
check.That(data.ResourceName).Key("soft_delete_period").Exists(),
check.That(data.ResourceName).Key("hot_cache_period").Exists(),
check.That(data.ResourceName).Key("size").Exists(),
),
},
})
}

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

data "azurerm_kusto_database" "test" {
name = azurerm_kusto_database.test.name
resource_group_name = azurerm_kusto_database.test.resource_group_name
cluster_name = azurerm_kusto_database.test.cluster_name
}
`, KustoDatabaseResource{}.complete(data))
}
48 changes: 48 additions & 0 deletions internal/services/kusto/kusto_database_resource_test.go
Expand Up @@ -30,6 +30,21 @@ func TestAccKustoDatabase_basic(t *testing.T) {
})
}

func TestAccKustoDatabase_complete(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_kusto_database", "test")
r := KustoDatabaseResource{}

data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.complete(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
})
}

func TestAccKustoDatabase_softDeletePeriod(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_kusto_database", "test")
r := KustoDatabaseResource{}
Expand Down Expand Up @@ -105,6 +120,39 @@ resource "azurerm_kusto_database" "test" {
`, data.RandomInteger, data.Locations.Primary, data.RandomString, data.RandomInteger)
}

func (KustoDatabaseResource) complete(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
features {}
}

resource "azurerm_resource_group" "rg" {
name = "acctestRG-%d"
location = "%s"
}

resource "azurerm_kusto_cluster" "cluster" {
name = "acctestkc%s"
location = azurerm_resource_group.rg.location
resource_group_name = azurerm_resource_group.rg.name

sku {
name = "Dev(No SLA)_Standard_D11_v2"
capacity = 1
}
}

resource "azurerm_kusto_database" "test" {
name = "acctestkd-%d"
resource_group_name = azurerm_resource_group.rg.name
location = azurerm_resource_group.rg.location
cluster_name = azurerm_kusto_cluster.cluster.name
soft_delete_period = "P31D"
hot_cache_period = "P7D"
}
`, data.RandomInteger, data.Locations.Primary, data.RandomString, data.RandomInteger)
}

func (KustoDatabaseResource) softDeletePeriod(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
Expand Down
3 changes: 2 additions & 1 deletion internal/services/kusto/registration.go
Expand Up @@ -28,7 +28,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_kusto_cluster": dataSourceKustoCluster(),
"azurerm_kusto_cluster": dataSourceKustoCluster(),
"azurerm_kusto_database": dataSourceKustoDatabase(),
}
}

Expand Down
51 changes: 51 additions & 0 deletions website/docs/d/kusto_database.html.markdown
@@ -0,0 +1,51 @@
---
subcategory: "Data Explorer"
layout: "azurerm"
page_title: "Azure Resource Manager: azurerm_kusto_database"
description: |-
Manages Kusto / Data Explorer Database
---

# Data Source: azurerm_kusto_database

Use this data source to access information about an existing Kusto Database

## Example Usage

```hcl
data "azurerm_kusto_database" "example" {
name = "my-kusto-database"
resource_group_name = "test_resource_group"
cluster_name = "test_cluster"
}
```

## Argument Reference

The following arguments are supported:

* `name` - (Required) The name of the Kusto Database.

* `resource_group_name` - (Required) The Resource Group where the Kusto Database exists.

* `cluster_name` - (Required) The name of the Kusto Cluster this database is added to.

## Attributes Reference

The following attributes are exported:

* `id` - The Kusto Cluster ID.

* `location` - The Azure Region in which the managed Kusto Database exists.

* `hot_cache_period` - The time the data that should be kept in cache for fast queries as ISO 8601 timespan.

* `soft_delete_period` - The time the data should be kept before it stops being accessible to queries as ISO 8601 timespan.

* `size` - The size of the database in bytes.

## 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 Kusto Database.