Skip to content

Commit

Permalink
kusto_database support data source kusto_database_data_source.go
Browse files Browse the repository at this point in the history
  • Loading branch information
qiqingzhang committed Apr 6, 2022
1 parent c0d41cb commit d91762e
Show file tree
Hide file tree
Showing 4 changed files with 195 additions and 1 deletion.
109 changes: 109 additions & 0 deletions internal/services/kusto/kusto_database_data_source.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
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(),

"location": commonschema.LocationComputed(),

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

"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()

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

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.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
Original file line number Diff line number Diff line change
@@ -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{}.basic(data))
}
3 changes: 2 additions & 1 deletion internal/services/kusto/registration.go
Original file line number Diff line number Diff line change
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
47 changes: 47 additions & 0 deletions website/docs/d/kusto_database.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
---
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` - The name of the Kusto Database to create.

* `resource_group_name` - Specifies the Resource Group where the Kusto Database should exist.

* `cluster_name` - Specifies the name of the Kusto Cluster this database will be added to.

## Attributes Reference

The following attributes are exported:

* `id` - The Kusto Cluster ID.

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

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

* `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.

0 comments on commit d91762e

Please sign in to comment.