From d91762ef8557b7ca25ccac14f0e32e1a1f8a5220 Mon Sep 17 00:00:00 2001 From: zhangqiqing <1434071196@qq.com> Date: Wed, 6 Apr 2022 14:31:18 +0800 Subject: [PATCH 1/5] `kusto_database ` support data source `kusto_database_data_source.go` --- .../kusto/kusto_database_data_source.go | 109 ++++++++++++++++++ .../kusto/kusto_database_data_source_test.go | 37 ++++++ internal/services/kusto/registration.go | 3 +- website/docs/d/kusto_database.html.markdown | 47 ++++++++ 4 files changed, 195 insertions(+), 1 deletion(-) create mode 100644 internal/services/kusto/kusto_database_data_source.go create mode 100644 internal/services/kusto/kusto_database_data_source_test.go create mode 100644 website/docs/d/kusto_database.html.markdown diff --git a/internal/services/kusto/kusto_database_data_source.go b/internal/services/kusto/kusto_database_data_source.go new file mode 100644 index 000000000000..7e3c790b23e5 --- /dev/null +++ b/internal/services/kusto/kusto_database_data_source.go @@ -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 +} diff --git a/internal/services/kusto/kusto_database_data_source_test.go b/internal/services/kusto/kusto_database_data_source_test.go new file mode 100644 index 000000000000..852ad53e817f --- /dev/null +++ b/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{}.basic(data)) +} diff --git a/internal/services/kusto/registration.go b/internal/services/kusto/registration.go index 1779aaecfc07..a29f38799f8b 100644 --- a/internal/services/kusto/registration.go +++ b/internal/services/kusto/registration.go @@ -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(), } } diff --git a/website/docs/d/kusto_database.html.markdown b/website/docs/d/kusto_database.html.markdown new file mode 100644 index 000000000000..046845386ab5 --- /dev/null +++ b/website/docs/d/kusto_database.html.markdown @@ -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. From a25e0ed47d78728467b99277dd9999a4af9451d6 Mon Sep 17 00:00:00 2001 From: zhangqiqing <1434071196@qq.com> Date: Wed, 6 Apr 2022 14:40:29 +0800 Subject: [PATCH 2/5] remove `Default is unlimited` in markdown file --- website/docs/d/kusto_database.html.markdown | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/website/docs/d/kusto_database.html.markdown b/website/docs/d/kusto_database.html.markdown index 046845386ab5..a3b9b077151e 100644 --- a/website/docs/d/kusto_database.html.markdown +++ b/website/docs/d/kusto_database.html.markdown @@ -36,12 +36,14 @@ 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. +* `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. Default is unlimited. +* `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. From 9990bf691668e8ec46a9e422b600a289b39ea0ed Mon Sep 17 00:00:00 2001 From: zhangqiqing <1434071196@qq.com> Date: Thu, 7 Apr 2022 11:19:55 +0800 Subject: [PATCH 3/5] improve Markdown file and clean up code --- internal/services/kusto/kusto_database_data_source.go | 9 +++------ website/docs/d/kusto_database.html.markdown | 8 +++++--- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/internal/services/kusto/kusto_database_data_source.go b/internal/services/kusto/kusto_database_data_source.go index 7e3c790b23e5..0f566bf54dad 100644 --- a/internal/services/kusto/kusto_database_data_source.go +++ b/internal/services/kusto/kusto_database_data_source.go @@ -36,14 +36,14 @@ func dataSourceKustoDatabase() *pluginsdk.Resource { "resource_group_name": commonschema.ResourceGroupNameForDataSource(), - "location": commonschema.LocationComputed(), - "cluster_name": { Type: pluginsdk.TypeString, Required: true, ValidateFunc: kustoValidate.ClusterName, }, + "location": commonschema.LocationComputed(), + "soft_delete_period": { Type: pluginsdk.TypeString, Computed: true, @@ -68,10 +68,7 @@ func dataSourceKustoDatabaseRead(d *pluginsdk.ResourceData, meta interface{}) er 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) + 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 { diff --git a/website/docs/d/kusto_database.html.markdown b/website/docs/d/kusto_database.html.markdown index a3b9b077151e..1e4166816c45 100644 --- a/website/docs/d/kusto_database.html.markdown +++ b/website/docs/d/kusto_database.html.markdown @@ -24,11 +24,11 @@ data "azurerm_kusto_database" "example" { The following arguments are supported: -* `name` - The name of the Kusto Database to create. +* `name` - (Required) The name of the Kusto Database. -* `resource_group_name` - Specifies the Resource Group where the Kusto Database should exist. +* `resource_group_name` - (Required) The Resource Group where the Kusto Database exists. -* `cluster_name` - Specifies the name of the Kusto Cluster this database will be added to. +* `cluster_name` - (Required) The name of the Kusto Cluster this database is added to. ## Attributes Reference @@ -36,6 +36,8 @@ 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. From 3abeccb27d8d26501106ca475e103c19e55ee68d Mon Sep 17 00:00:00 2001 From: zhangqiqing <1434071196@qq.com> Date: Fri, 8 Apr 2022 14:55:12 +0800 Subject: [PATCH 4/5] modify id set in read function --- internal/services/kusto/kusto_database_data_source.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/internal/services/kusto/kusto_database_data_source.go b/internal/services/kusto/kusto_database_data_source.go index 0f566bf54dad..1469f6e6a33d 100644 --- a/internal/services/kusto/kusto_database_data_source.go +++ b/internal/services/kusto/kusto_database_data_source.go @@ -88,6 +88,8 @@ func dataSourceKustoDatabaseRead(d *pluginsdk.ResourceData, meta interface{}) er 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) From ee831f386d7aba09b7f431c87ed4b91e486e46e0 Mon Sep 17 00:00:00 2001 From: zhangqiqing <1434071196@qq.com> Date: Mon, 11 Apr 2022 11:30:31 +0800 Subject: [PATCH 5/5] fix data_source test soft_delete_period/hot_cache_period don't have vule --- .../kusto/kusto_database_data_source_test.go | 2 +- .../kusto/kusto_database_resource_test.go | 48 +++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/internal/services/kusto/kusto_database_data_source_test.go b/internal/services/kusto/kusto_database_data_source_test.go index 852ad53e817f..ed1d46c53b6f 100644 --- a/internal/services/kusto/kusto_database_data_source_test.go +++ b/internal/services/kusto/kusto_database_data_source_test.go @@ -33,5 +33,5 @@ data "azurerm_kusto_database" "test" { resource_group_name = azurerm_kusto_database.test.resource_group_name cluster_name = azurerm_kusto_database.test.cluster_name } -`, KustoDatabaseResource{}.basic(data)) +`, KustoDatabaseResource{}.complete(data)) } diff --git a/internal/services/kusto/kusto_database_resource_test.go b/internal/services/kusto/kusto_database_resource_test.go index 6f78dcad6b0e..fc322b8d0173 100644 --- a/internal/services/kusto/kusto_database_resource_test.go +++ b/internal/services/kusto/kusto_database_resource_test.go @@ -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{} @@ -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" {