From 0e7b9e2392e9ae1b2c307be90ce3ea3178e87051 Mon Sep 17 00:00:00 2001 From: Monika Date: Fri, 6 May 2022 09:40:30 -0700 Subject: [PATCH 1/9] Add lock to cluster and kusto scripts --- .../services/kusto/kusto_cluster_resource.go | 4 + .../kusto/kusto_database_script_resource.go | 4 +- .../kusto_database_script_resource_test.go | 124 +++++++++++++++++- 3 files changed, 128 insertions(+), 4 deletions(-) diff --git a/internal/services/kusto/kusto_cluster_resource.go b/internal/services/kusto/kusto_cluster_resource.go index 871fb9531b1a..edf16951fe67 100644 --- a/internal/services/kusto/kusto_cluster_resource.go +++ b/internal/services/kusto/kusto_cluster_resource.go @@ -18,6 +18,7 @@ import ( "github.com/hashicorp/terraform-provider-azurerm/helpers/tf" "github.com/hashicorp/terraform-provider-azurerm/internal/clients" "github.com/hashicorp/terraform-provider-azurerm/internal/features" + "github.com/hashicorp/terraform-provider-azurerm/internal/locks" "github.com/hashicorp/terraform-provider-azurerm/internal/services/kusto/parse" "github.com/hashicorp/terraform-provider-azurerm/internal/services/kusto/validate" "github.com/hashicorp/terraform-provider-azurerm/internal/tags" @@ -337,6 +338,9 @@ func resourceKustoClusterCreateUpdate(d *pluginsdk.ResourceData, meta interface{ } } + locks.ByID(id.ID()) + defer locks.UnlockByID(id.ID()) + sku, err := expandKustoClusterSku(d.Get("sku").([]interface{})) if err != nil { return err diff --git a/internal/services/kusto/kusto_database_script_resource.go b/internal/services/kusto/kusto_database_script_resource.go index a6ec1414728b..ea9c0d646631 100644 --- a/internal/services/kusto/kusto_database_script_resource.go +++ b/internal/services/kusto/kusto_database_script_resource.go @@ -98,8 +98,8 @@ func resourceKustoDatabaseScriptCreateUpdate(d *pluginsdk.ResourceData, meta int } } - locks.ByID(databaseId.ID()) - defer locks.UnlockByID(databaseId.ID()) + locks.ByID(databaseId.ClusterName.ID()) + defer locks.UnlockByID(databaseId.ClusterName.ID()) forceUpdateTag := d.Get("force_an_update_when_value_changed").(string) if len(forceUpdateTag) == 0 { diff --git a/internal/services/kusto/kusto_database_script_resource_test.go b/internal/services/kusto/kusto_database_script_resource_test.go index f026a5900f89..93c9f960ab51 100644 --- a/internal/services/kusto/kusto_database_script_resource_test.go +++ b/internal/services/kusto/kusto_database_script_resource_test.go @@ -234,14 +234,14 @@ func (r KustoScriptResource) multiple(data acceptance.TestData) string { return fmt.Sprintf(` %s -resource "azurerm_kusto_script" "test" { +resource "azurerm_kusto_script" "script" { name = "acctest-ks-%d" database_id = azurerm_kusto_database.test.id url = azurerm_storage_blob.test.id sas_token = data.azurerm_storage_account_blob_container_sas.test.sas } -resource "azurerm_kusto_script" "test2" { +resource "azurerm_kusto_script" "script2" { name = "acctest-ks-2-%d" database_id = azurerm_kusto_database.test.id url = azurerm_storage_blob.test.id @@ -249,3 +249,123 @@ resource "azurerm_kusto_script" "test2" { } `, template, data.RandomInteger, data.RandomInteger) } + +func (KustoDatabaseResource) multiple_diff(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" "database" { + name = "acctestkd-%d" + resource_group_name = azurerm_resource_group.rg.name + location = azurerm_resource_group.rg.location + cluster_name = azurerm_kusto_cluster.cluster.name +} + +resource "azurerm_kusto_database" "database2" { + name = "acctestkd-2-%d" + resource_group_name = azurerm_resource_group.rg.name + location = azurerm_resource_group.rg.location + cluster_name = azurerm_kusto_cluster.cluster.name +} + +resource "azurerm_kusto_database" "database3" { + name = "acctestkd-3-%d" + resource_group_name = azurerm_resource_group.rg.name + location = azurerm_resource_group.rg.location + cluster_name = azurerm_kusto_cluster.cluster.name +} + +resource "azurerm_kusto_database" "database4" { + name = "acctestkd-4-%d" + resource_group_name = azurerm_resource_group.rg.name + location = azurerm_resource_group.rg.location + cluster_name = azurerm_kusto_cluster.cluster.name +} + +resource "azurerm_storage_account" "test" { + name = "acctestsa%[1]d" + resource_group_name = azurerm_resource_group.test.name + location = azurerm_resource_group.test.location + account_tier = "Standard" + account_replication_type = "LRS" +} + +resource "azurerm_storage_container" "test" { + name = "setup-files" + storage_account_name = azurerm_storage_account.test.name + container_access_type = "private" +} + +resource "azurerm_storage_blob" "test" { + name = "script.txt" + storage_account_name = azurerm_storage_account.test.name + storage_container_name = azurerm_storage_container.test.name + type = "Block" + source_content = ".create table MyTable (Level:string, Timestamp:datetime, UserId:string, TraceId:string, Message:string, ProcessId:int32)" +} + +data "azurerm_storage_account_blob_container_sas" "test" { + connection_string = azurerm_storage_account.test.primary_connection_string + container_name = azurerm_storage_container.test.name + https_only = true + + start = "2022-03-21" + expiry = "2027-03-21" + + permissions { + read = true + add = false + create = false + write = true + delete = false + list = true + } +} + +resource "azurerm_kusto_script" "script" { + name = "acctest-ks-%d" + database_id = azurerm_kusto_database.database.id + url = azurerm_storage_blob.test.id + sas_token = data.azurerm_storage_account_blob_container_sas.test.sas +} + +resource "azurerm_kusto_script" "script2" { + name = "acctest-ks-2-%d" + database_id = azurerm_kusto_database.database2.id + url = azurerm_storage_blob.test.id + sas_token = data.azurerm_storage_account_blob_container_sas.test.sas +} + +resource "azurerm_kusto_script" "script3" { + name = "acctest-ks-3-%d" + database_id = azurerm_kusto_database.database3.id + url = azurerm_storage_blob.test.id + sas_token = data.azurerm_storage_account_blob_container_sas.test.sas +} + +resource "azurerm_kusto_script" "script4" { + name = "acctest-ks-4-%d" + database_id = azurerm_kusto_database.database4.id + url = azurerm_storage_blob.test.id + sas_token = data.azurerm_storage_account_blob_container_sas.test.sas +} +`, data.RandomInteger, data.Locations.Primary, data.RandomString, data.RandomInteger) +} \ No newline at end of file From 18e431253658f5f2e9f9e313e443f424d538e9b2 Mon Sep 17 00:00:00 2001 From: Monika Date: Fri, 6 May 2022 09:41:39 -0700 Subject: [PATCH 2/9] Clean up tests --- .../kusto_database_script_resource_test.go | 23 +------------------ 1 file changed, 1 insertion(+), 22 deletions(-) diff --git a/internal/services/kusto/kusto_database_script_resource_test.go b/internal/services/kusto/kusto_database_script_resource_test.go index 93c9f960ab51..d0550b0638b1 100644 --- a/internal/services/kusto/kusto_database_script_resource_test.go +++ b/internal/services/kusto/kusto_database_script_resource_test.go @@ -229,28 +229,7 @@ resource "azurerm_kusto_script" "test" { `, template, data.RandomInteger) } -func (r KustoScriptResource) multiple(data acceptance.TestData) string { - template := r.template(data) - return fmt.Sprintf(` -%s - -resource "azurerm_kusto_script" "script" { - name = "acctest-ks-%d" - database_id = azurerm_kusto_database.test.id - url = azurerm_storage_blob.test.id - sas_token = data.azurerm_storage_account_blob_container_sas.test.sas -} - -resource "azurerm_kusto_script" "script2" { - name = "acctest-ks-2-%d" - database_id = azurerm_kusto_database.test.id - url = azurerm_storage_blob.test.id - sas_token = data.azurerm_storage_account_blob_container_sas.test.sas -} -`, template, data.RandomInteger, data.RandomInteger) -} - -func (KustoDatabaseResource) multiple_diff(data acceptance.TestData) string { +func (KustoDatabaseResource) multiple(data acceptance.TestData) string { return fmt.Sprintf(` provider "azurerm" { features {} From 2abbb36eb4e8d527611b41aa27f688d70b97a562 Mon Sep 17 00:00:00 2001 From: Monika Date: Fri, 6 May 2022 09:56:22 -0700 Subject: [PATCH 3/9] Remove lock on database resource --- internal/services/kusto/kusto_database_resource.go | 4 ---- 1 file changed, 4 deletions(-) diff --git a/internal/services/kusto/kusto_database_resource.go b/internal/services/kusto/kusto_database_resource.go index e30ad4e97290..3f59d7d2bdb1 100644 --- a/internal/services/kusto/kusto_database_resource.go +++ b/internal/services/kusto/kusto_database_resource.go @@ -10,7 +10,6 @@ import ( "github.com/hashicorp/terraform-provider-azurerm/helpers/tf" "github.com/hashicorp/terraform-provider-azurerm/helpers/validate" "github.com/hashicorp/terraform-provider-azurerm/internal/clients" - "github.com/hashicorp/terraform-provider-azurerm/internal/locks" "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" @@ -96,9 +95,6 @@ func resourceKustoDatabaseCreateUpdate(d *pluginsdk.ResourceData, meta interface } } - locks.ByID(id.ID()) - defer locks.UnlockByID(id.ID()) - databaseProperties := expandKustoDatabaseProperties(d) readWriteDatabase := kusto.ReadWriteDatabase{ From 7762e36cf16d577e555e408a416c693614d1fbae Mon Sep 17 00:00:00 2001 From: Monika Date: Fri, 6 May 2022 10:00:32 -0700 Subject: [PATCH 4/9] Resolve merge --- internal/services/kusto/kusto_cluster_resource.go | 1 - 1 file changed, 1 deletion(-) diff --git a/internal/services/kusto/kusto_cluster_resource.go b/internal/services/kusto/kusto_cluster_resource.go index 78c6012cd778..5e0dee9326a9 100644 --- a/internal/services/kusto/kusto_cluster_resource.go +++ b/internal/services/kusto/kusto_cluster_resource.go @@ -17,7 +17,6 @@ import ( "github.com/hashicorp/terraform-provider-azurerm/helpers/azure" "github.com/hashicorp/terraform-provider-azurerm/helpers/tf" "github.com/hashicorp/terraform-provider-azurerm/internal/clients" - "github.com/hashicorp/terraform-provider-azurerm/internal/features" "github.com/hashicorp/terraform-provider-azurerm/internal/locks" "github.com/hashicorp/terraform-provider-azurerm/internal/services/kusto/parse" "github.com/hashicorp/terraform-provider-azurerm/internal/services/kusto/validate" From 55c8db3ab4296eed7cafaff68d64f0a97b3005ca Mon Sep 17 00:00:00 2001 From: Monika Date: Fri, 6 May 2022 13:40:21 -0700 Subject: [PATCH 5/9] Change lock to use name instead of id --- internal/services/kusto/kusto_cluster_resource.go | 4 ++-- internal/services/kusto/kusto_database_script_resource.go | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/internal/services/kusto/kusto_cluster_resource.go b/internal/services/kusto/kusto_cluster_resource.go index 5e0dee9326a9..d712be9ca945 100644 --- a/internal/services/kusto/kusto_cluster_resource.go +++ b/internal/services/kusto/kusto_cluster_resource.go @@ -262,8 +262,8 @@ func resourceKustoClusterCreateUpdate(d *pluginsdk.ResourceData, meta interface{ } } - locks.ByID(id.ID()) - defer locks.UnlockByID(id.ID()) + locks.ByID(id.Name()) + defer locks.UnlockByID(id.Name()) sku, err := expandKustoClusterSku(d.Get("sku").([]interface{})) if err != nil { diff --git a/internal/services/kusto/kusto_database_script_resource.go b/internal/services/kusto/kusto_database_script_resource.go index ea9c0d646631..32b5f804d47e 100644 --- a/internal/services/kusto/kusto_database_script_resource.go +++ b/internal/services/kusto/kusto_database_script_resource.go @@ -98,8 +98,8 @@ func resourceKustoDatabaseScriptCreateUpdate(d *pluginsdk.ResourceData, meta int } } - locks.ByID(databaseId.ClusterName.ID()) - defer locks.UnlockByID(databaseId.ClusterName.ID()) + locks.ByID(id.ClusterName) + defer locks.UnlockByID(id.ClusterName) forceUpdateTag := d.Get("force_an_update_when_value_changed").(string) if len(forceUpdateTag) == 0 { From 119a4223d4392e2ae716f6abaa9ad1b53767e232 Mon Sep 17 00:00:00 2001 From: Monika Date: Mon, 9 May 2022 12:53:29 -0700 Subject: [PATCH 6/9] Fix lock field and acc test --- .../services/kusto/kusto_cluster_resource.go | 4 +- .../kusto_database_script_resource_test.go | 124 +++++------------- 2 files changed, 34 insertions(+), 94 deletions(-) diff --git a/internal/services/kusto/kusto_cluster_resource.go b/internal/services/kusto/kusto_cluster_resource.go index d712be9ca945..037139d56d4b 100644 --- a/internal/services/kusto/kusto_cluster_resource.go +++ b/internal/services/kusto/kusto_cluster_resource.go @@ -262,8 +262,8 @@ func resourceKustoClusterCreateUpdate(d *pluginsdk.ResourceData, meta interface{ } } - locks.ByID(id.Name()) - defer locks.UnlockByID(id.Name()) + locks.ByID(id.Name) + defer locks.UnlockByID(id.Name) sku, err := expandKustoClusterSku(d.Get("sku").([]interface{})) if err != nil { diff --git a/internal/services/kusto/kusto_database_script_resource_test.go b/internal/services/kusto/kusto_database_script_resource_test.go index d0550b0638b1..0574bd77d476 100644 --- a/internal/services/kusto/kusto_database_script_resource_test.go +++ b/internal/services/kusto/kusto_database_script_resource_test.go @@ -93,6 +93,9 @@ func TestAccKustoScript_multiple(t *testing.T) { Config: r.multiple(data), Check: acceptance.ComposeTestCheckFunc( check.That(data.ResourceName).ExistsInAzure(r), + check.That(fmt.Sprintf("%s%d", data.ResourceName, 2)).ExistsInAzure(r), + check.That(fmt.Sprintf("%s%d", data.ResourceName, 3)).ExistsInAzure(r), + check.That(fmt.Sprintf("%s%d", data.ResourceName, 4)).ExistsInAzure(r), ), }, data.ImportStep("sas_token"), @@ -229,122 +232,59 @@ resource "azurerm_kusto_script" "test" { `, template, data.RandomInteger) } -func (KustoDatabaseResource) multiple(data acceptance.TestData) string { +func (r KustoScriptResource) multiple(data acceptance.TestData) string { + template := r.template(data) 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" "database" { - name = "acctestkd-%d" - resource_group_name = azurerm_resource_group.rg.name - location = azurerm_resource_group.rg.location - cluster_name = azurerm_kusto_cluster.cluster.name -} - -resource "azurerm_kusto_database" "database2" { - name = "acctestkd-2-%d" - resource_group_name = azurerm_resource_group.rg.name - location = azurerm_resource_group.rg.location - cluster_name = azurerm_kusto_cluster.cluster.name -} - -resource "azurerm_kusto_database" "database3" { - name = "acctestkd-3-%d" - resource_group_name = azurerm_resource_group.rg.name - location = azurerm_resource_group.rg.location - cluster_name = azurerm_kusto_cluster.cluster.name -} - -resource "azurerm_kusto_database" "database4" { - name = "acctestkd-4-%d" - resource_group_name = azurerm_resource_group.rg.name - location = azurerm_resource_group.rg.location - cluster_name = azurerm_kusto_cluster.cluster.name -} - -resource "azurerm_storage_account" "test" { - name = "acctestsa%[1]d" - resource_group_name = azurerm_resource_group.test.name - location = azurerm_resource_group.test.location - account_tier = "Standard" - account_replication_type = "LRS" -} +%s -resource "azurerm_storage_container" "test" { - name = "setup-files" - storage_account_name = azurerm_storage_account.test.name - container_access_type = "private" +resource "azurerm_kusto_database" "test2" { + name = "acctest-kd-2-%d" + resource_group_name = azurerm_resource_group.test.name + location = azurerm_resource_group.test.location + cluster_name = azurerm_kusto_cluster.test.name } -resource "azurerm_storage_blob" "test" { - name = "script.txt" - storage_account_name = azurerm_storage_account.test.name - storage_container_name = azurerm_storage_container.test.name - type = "Block" - source_content = ".create table MyTable (Level:string, Timestamp:datetime, UserId:string, TraceId:string, Message:string, ProcessId:int32)" +resource "azurerm_kusto_database" "test3" { + name = "acctest-kd-3-%d" + resource_group_name = azurerm_resource_group.test.name + location = azurerm_resource_group.test.location + cluster_name = azurerm_kusto_cluster.test.name } -data "azurerm_storage_account_blob_container_sas" "test" { - connection_string = azurerm_storage_account.test.primary_connection_string - container_name = azurerm_storage_container.test.name - https_only = true - - start = "2022-03-21" - expiry = "2027-03-21" - - permissions { - read = true - add = false - create = false - write = true - delete = false - list = true - } +resource "azurerm_kusto_database" "test4" { + name = "acctest-kd-4-%d" + resource_group_name = azurerm_resource_group.test.name + location = azurerm_resource_group.test.location + cluster_name = azurerm_kusto_cluster.test.name } -resource "azurerm_kusto_script" "script" { +resource "azurerm_kusto_script" "test" { name = "acctest-ks-%d" - database_id = azurerm_kusto_database.database.id + database_id = azurerm_kusto_database.test.id url = azurerm_storage_blob.test.id sas_token = data.azurerm_storage_account_blob_container_sas.test.sas } -resource "azurerm_kusto_script" "script2" { +resource "azurerm_kusto_script" "test2" { name = "acctest-ks-2-%d" - database_id = azurerm_kusto_database.database2.id + database_id = azurerm_kusto_database.test2.id url = azurerm_storage_blob.test.id sas_token = data.azurerm_storage_account_blob_container_sas.test.sas } -resource "azurerm_kusto_script" "script3" { +resource "azurerm_kusto_script" "test3" { name = "acctest-ks-3-%d" - database_id = azurerm_kusto_database.database3.id + database_id = azurerm_kusto_database.test3.id url = azurerm_storage_blob.test.id sas_token = data.azurerm_storage_account_blob_container_sas.test.sas } -resource "azurerm_kusto_script" "script4" { +resource "azurerm_kusto_script" "test4" { name = "acctest-ks-4-%d" - database_id = azurerm_kusto_database.database4.id + database_id = azurerm_kusto_database.test4.id url = azurerm_storage_blob.test.id sas_token = data.azurerm_storage_account_blob_container_sas.test.sas } -`, data.RandomInteger, data.Locations.Primary, data.RandomString, data.RandomInteger) -} \ No newline at end of file +`, template, data.RandomInteger, data.RandomInteger, data.RandomInteger, data.RandomInteger, + data.RandomInteger, data.RandomInteger, data.RandomInteger) +} From 34e20c5dd017413d6f0741ee9b2751b9aae96e6b Mon Sep 17 00:00:00 2001 From: Monika Date: Wed, 11 May 2022 15:08:56 -0700 Subject: [PATCH 7/9] Change cluster name to use i --- internal/services/kusto/kusto_database_script_resource.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/internal/services/kusto/kusto_database_script_resource.go b/internal/services/kusto/kusto_database_script_resource.go index 32b5f804d47e..3c36ec7e80b5 100644 --- a/internal/services/kusto/kusto_database_script_resource.go +++ b/internal/services/kusto/kusto_database_script_resource.go @@ -98,8 +98,9 @@ func resourceKustoDatabaseScriptCreateUpdate(d *pluginsdk.ResourceData, meta int } } - locks.ByID(id.ClusterName) - defer locks.UnlockByID(id.ClusterName) + clusterId := parse.NewClusterId(databaseId.SubscriptionId, databaseId.ResourceGroup, databaseId.ClusterName) + locks.ByID(clusterId.ClusterName) + defer locks.UnlockByID(clusterId.ClusterName) forceUpdateTag := d.Get("force_an_update_when_value_changed").(string) if len(forceUpdateTag) == 0 { From 8a001895db1f74e1dbc0c651c74678a458bb687d Mon Sep 17 00:00:00 2001 From: Monika Date: Wed, 11 May 2022 16:58:58 -0700 Subject: [PATCH 8/9] Fix cluster name --- internal/services/kusto/kusto_database_script_resource.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/internal/services/kusto/kusto_database_script_resource.go b/internal/services/kusto/kusto_database_script_resource.go index 3c36ec7e80b5..47546b981433 100644 --- a/internal/services/kusto/kusto_database_script_resource.go +++ b/internal/services/kusto/kusto_database_script_resource.go @@ -98,9 +98,9 @@ func resourceKustoDatabaseScriptCreateUpdate(d *pluginsdk.ResourceData, meta int } } - clusterId := parse.NewClusterId(databaseId.SubscriptionId, databaseId.ResourceGroup, databaseId.ClusterName) - locks.ByID(clusterId.ClusterName) - defer locks.UnlockByID(clusterId.ClusterName) + clusterId := parse.NewClusterID(databaseId.SubscriptionId, databaseId.ResourceGroup, databaseId.ClusterName) + locks.ByID(clusterId.Name) + defer locks.UnlockByID(clusterId.Name) forceUpdateTag := d.Get("force_an_update_when_value_changed").(string) if len(forceUpdateTag) == 0 { From 9fa5e8e6740e1d0b95d9ad7f5f96b02b32c531cd Mon Sep 17 00:00:00 2001 From: Matthew Frahry Date: Wed, 11 May 2022 21:10:28 -0700 Subject: [PATCH 9/9] Update kusto_database_script_resource.go --- internal/services/kusto/kusto_database_script_resource.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/services/kusto/kusto_database_script_resource.go b/internal/services/kusto/kusto_database_script_resource.go index 47546b981433..fb373feda8bd 100644 --- a/internal/services/kusto/kusto_database_script_resource.go +++ b/internal/services/kusto/kusto_database_script_resource.go @@ -99,8 +99,8 @@ func resourceKustoDatabaseScriptCreateUpdate(d *pluginsdk.ResourceData, meta int } clusterId := parse.NewClusterID(databaseId.SubscriptionId, databaseId.ResourceGroup, databaseId.ClusterName) - locks.ByID(clusterId.Name) - defer locks.UnlockByID(clusterId.Name) + locks.ByID(clusterId.ID()) + defer locks.UnlockByID(clusterId.ID()) forceUpdateTag := d.Get("force_an_update_when_value_changed").(string) if len(forceUpdateTag) == 0 {