Skip to content

Commit

Permalink
refactor: updating to account for Kusto Cluster / Database becoming a…
Browse files Browse the repository at this point in the history
… Common ID
  • Loading branch information
tombuildsstuff committed Jan 11, 2024
1 parent 4c254a2 commit 6f3dbd4
Show file tree
Hide file tree
Showing 22 changed files with 88 additions and 88 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import (
"time"

"github.com/hashicorp/go-azure-helpers/lang/response"
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonids"
"github.com/hashicorp/go-azure-sdk/resource-manager/datashare/2019-11-01/dataset"
"github.com/hashicorp/go-azure-sdk/resource-manager/datashare/2019-11-01/share"
"github.com/hashicorp/go-azure-sdk/resource-manager/kusto/2023-08-15/clusters"
"github.com/hashicorp/terraform-provider-azurerm/helpers/tf"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/datashare/validate"
Expand Down Expand Up @@ -55,7 +55,7 @@ func resourceDataShareDataSetKustoCluster() *pluginsdk.Resource {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: clusters.ValidateClusterID,
ValidateFunc: commonids.ValidateKustoClusterID,
},

"display_name": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import (
"time"

"github.com/hashicorp/go-azure-helpers/lang/response"
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonids"
"github.com/hashicorp/go-azure-sdk/resource-manager/datashare/2019-11-01/dataset"
"github.com/hashicorp/go-azure-sdk/resource-manager/datashare/2019-11-01/share"
"github.com/hashicorp/go-azure-sdk/resource-manager/kusto/2023-08-15/databases"
"github.com/hashicorp/terraform-provider-azurerm/helpers/tf"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/datashare/validate"
Expand Down Expand Up @@ -55,7 +55,7 @@ func resourceDataShareDataSetKustoDatabase() *pluginsdk.Resource {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: databases.ValidateDatabaseID,
ValidateFunc: commonids.ValidateKustoDatabaseID,
},

"display_name": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import (
"time"

"github.com/hashicorp/go-azure-helpers/lang/response"
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonids"
"github.com/hashicorp/go-azure-sdk/resource-manager/digitaltwins/2023-01-31/timeseriesdatabaseconnections"
"github.com/hashicorp/go-azure-sdk/resource-manager/eventhub/2021-11-01/eventhubs"
"github.com/hashicorp/go-azure-sdk/resource-manager/kusto/2023-08-15/clusters"
"github.com/hashicorp/terraform-provider-azurerm/internal/sdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/digitaltwins/validate"
eventhubValidate "github.com/hashicorp/terraform-provider-azurerm/internal/services/eventhub/validate"
Expand Down Expand Up @@ -77,7 +77,7 @@ func (m TimeSeriesDatabaseConnectionResource) Arguments() map[string]*pluginsdk.
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: clusters.ValidateClusterID,
ValidateFunc: commonids.ValidateKustoClusterID,
},

"kusto_cluster_uri": {
Expand Down Expand Up @@ -216,7 +216,7 @@ func (m TimeSeriesDatabaseConnectionResource) Read() sdk.ResourceFunc {
output.EventhubNamespaceEndpointUri = properties.EventHubEndpointUri
output.EventhubNamespaceId = properties.EventHubNamespaceResourceId

kustoClusterId, err := clusters.ParseClusterIDInsensitively(properties.AdxResourceId)
kustoClusterId, err := commonids.ParseKustoClusterIDInsensitively(properties.AdxResourceId)
if err != nil {
return fmt.Errorf("parsing `kusto_cluster_uri`: %+v", err)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import (
"time"

"github.com/hashicorp/go-azure-helpers/lang/response"
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonids"
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonschema"
"github.com/hashicorp/go-azure-helpers/resourcemanager/location"
"github.com/hashicorp/go-azure-sdk/resource-manager/kusto/2023-08-15/attacheddatabaseconfigurations"
"github.com/hashicorp/go-azure-sdk/resource-manager/kusto/2023-08-15/clusters"
"github.com/hashicorp/terraform-provider-azurerm/helpers/tf"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/locks"
Expand Down Expand Up @@ -78,7 +78,7 @@ func resourceKustoAttachedDatabaseConfiguration() *pluginsdk.Resource {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: clusters.ValidateClusterID,
ValidateFunc: commonids.ValidateKustoClusterID,
},

"attached_database_names": {
Expand Down Expand Up @@ -219,7 +219,7 @@ func resourceKustoAttachedDatabaseConfigurationRead(d *pluginsdk.ResourceData, m
d.Set("location", location.NormalizeNilable(model.Location))

if props := model.Properties; props != nil {
clusterResourceId, parseErr := clusters.ParseClusterIDInsensitively(props.ClusterResourceId)
clusterResourceId, parseErr := commonids.ParseKustoClusterIDInsensitively(props.ClusterResourceId)
if parseErr != nil {
return parseErr
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func resourceKustoClusterCustomerManagedKey() *pluginsdk.Resource {
}),

Importer: pluginsdk.ImporterValidatingResourceId(func(id string) error {
_, err := clusters.ParseClusterID(id)
_, err := commonids.ParseKustoClusterID(id)
return err
}),

Expand All @@ -51,7 +51,7 @@ func resourceKustoClusterCustomerManagedKey() *pluginsdk.Resource {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: clusters.ValidateClusterID,
ValidateFunc: commonids.ValidateKustoClusterID,
},

"key_vault_id": commonschema.ResourceIDReferenceRequired(commonids.KeyVaultId{}),
Expand Down Expand Up @@ -85,20 +85,20 @@ func resourceKustoClusterCustomerManagedKeyCreateUpdate(d *pluginsdk.ResourceDat
defer cancel()

clusterIDRaw := d.Get("cluster_id").(string)
clusterID, err := clusters.ParseClusterID(clusterIDRaw)
clusterID, err := commonids.ParseKustoClusterID(clusterIDRaw)
if err != nil {
return err
}

locks.ByName(clusterID.ClusterName, "azurerm_kusto_cluster")
defer locks.UnlockByName(clusterID.ClusterName, "azurerm_kusto_cluster")
locks.ByName(clusterID.KustoClusterName, "azurerm_kusto_cluster")
defer locks.UnlockByName(clusterID.KustoClusterName, "azurerm_kusto_cluster")

cluster, err := clusterClient.Get(ctx, *clusterID)
if err != nil {
return fmt.Errorf("retrieving Kusto Cluster %q (Resource Group %q): %+v", clusterID.ClusterName, clusterID.ResourceGroupName, err)
return fmt.Errorf("retrieving %s: %+v", *clusterID, err)
}
if cluster.Model.Properties == nil {
return fmt.Errorf("retrieving Kusto Cluster %q (Resource Group %q): `ClusterProperties` was nil", clusterID.ClusterName, clusterID.ResourceGroupName)
return fmt.Errorf("retrieving %s: `ClusterProperties` was nil", *clusterID)
}

// since we're mutating the kusto cluster here, we can use that as the ID
Expand Down Expand Up @@ -160,7 +160,7 @@ func resourceKustoClusterCustomerManagedKeyCreateUpdate(d *pluginsdk.ResourceDat

err = clusterClient.UpdateThenPoll(ctx, *clusterID, props, clusters.UpdateOperationOptions{})
if err != nil {
return fmt.Errorf("updating Customer Managed Key for Kusto Cluster %q (Resource Group %q): %+v", clusterID.ClusterName, clusterID.ResourceGroupName, err)
return fmt.Errorf("updating Customer Managed Key for %s: %+v", *clusterID, err)
}

d.SetId(resourceID)
Expand All @@ -174,26 +174,26 @@ func resourceKustoClusterCustomerManagedKeyRead(d *pluginsdk.ResourceData, meta
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()

id, err := clusters.ParseClusterID(d.Id())
id, err := commonids.ParseKustoClusterID(d.Id())
if err != nil {
return err
}

cluster, err := clusterClient.Get(ctx, *id)
if err != nil {
if !response.WasNotFound(cluster.HttpResponse) {
log.Printf("[DEBUG] Kusto Cluster %q could not be found in Resource Group %q - removing from state!", id.ClusterName, id.ResourceGroupName)
log.Printf("[DEBUG] %s was not found - removing from state!", id)
d.SetId("")
return nil
}

return fmt.Errorf("retrieving Kusto Cluster %q (Resource Group %q): %+v", id.ClusterName, id.ResourceGroupName, err)
return fmt.Errorf("retrieving %s: %+v", id, err)
}
if cluster.Model == nil || cluster.Model.Properties == nil {
return fmt.Errorf("retrieving Kusto Cluster %q (Resource Group %q): `ClusterProperties` was nil", id.ClusterName, id.ResourceGroupName)
return fmt.Errorf("retrieving %s: `ClusterProperties` was nil", id)
}
if cluster.Model.Properties.KeyVaultProperties == nil {
log.Printf("[DEBUG] Customer Managed Key was not defined for Kusto Cluster %q (Resource Group %q) - removing from state!", id.ClusterName, id.ResourceGroupName)
log.Printf("[DEBUG] Customer Managed Key was not defined for %s - removing from state!", id)
d.SetId("")
return nil
}
Expand All @@ -220,7 +220,7 @@ func resourceKustoClusterCustomerManagedKeyRead(d *pluginsdk.ResourceData, meta
}

if keyVaultURI == "" {
return fmt.Errorf("retrieving Kusto Cluster %q (Resource Group %q): `properties.keyVaultProperties.keyVaultUri` was nil", id.ClusterName, id.ResourceGroupName)
return fmt.Errorf("retrieving %s: `properties.keyVaultProperties.keyVaultUri` was nil", id)
}

// now we have the key vault uri we can look up the ID
Expand All @@ -243,13 +243,13 @@ func resourceKustoClusterCustomerManagedKeyDelete(d *pluginsdk.ResourceData, met
ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d)
defer cancel()

clusterID, err := clusters.ParseClusterID(d.Id())
clusterID, err := commonids.ParseKustoClusterID(d.Id())
if err != nil {
return err
}

locks.ByName(clusterID.ClusterName, "azurerm_kusto_cluster")
defer locks.UnlockByName(clusterID.ClusterName, "azurerm_kusto_cluster")
locks.ByName(clusterID.KustoClusterName, "azurerm_kusto_cluster")
defer locks.UnlockByName(clusterID.KustoClusterName, "azurerm_kusto_cluster")

// confirm it still exists prior to trying to update it, else we'll get an error
cluster, err := client.Get(ctx, *clusterID)
Expand All @@ -258,7 +258,7 @@ func resourceKustoClusterCustomerManagedKeyDelete(d *pluginsdk.ResourceData, met
return nil
}

return fmt.Errorf("retrieving Kusto Cluster %q (Resource Group %q): %+v", clusterID.ClusterName, clusterID.ResourceGroupName, err)
return fmt.Errorf("retrieving %s: %+v", *clusterID, err)
}

// Since this isn't a real object, just modifying an existing object
Expand All @@ -271,9 +271,9 @@ func resourceKustoClusterCustomerManagedKeyDelete(d *pluginsdk.ResourceData, met
},
}

err = client.UpdateThenPoll(ctx, *clusterID, props, clusters.UpdateOperationOptions{})
err = client.UpdateThenPoll(ctx, *clusterID, props, clusters.DefaultUpdateOperationOptions())
if err != nil {
return fmt.Errorf("removing Customer Managed Key for Kusto Cluster %q (Resource Group %q): %+v", clusterID.ClusterName, clusterID.ResourceGroupName, err)
return fmt.Errorf("removing Customer Managed Key for %s: %+v", clusterID, err)
}

return nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"fmt"
"testing"

"github.com/hashicorp/go-azure-sdk/resource-manager/kusto/2023-08-15/clusters"
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonids"
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance"
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance/check"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
Expand Down Expand Up @@ -130,7 +130,7 @@ func TestAccKustoClusterCustomerManagedKey_userIdentity(t *testing.T) {
}

func (KustoClusterCustomerManagedKeyResource) Exists(ctx context.Context, clients *clients.Client, state *pluginsdk.InstanceState) (*bool, error) {
id, err := clusters.ParseClusterID(state.ID)
id, err := commonids.ParseKustoClusterID(state.ID)
if err != nil {
return nil, err
}
Expand Down
6 changes: 3 additions & 3 deletions internal/services/kusto/kusto_cluster_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import (
"time"

"github.com/hashicorp/go-azure-helpers/lang/response"
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonids"
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonschema"
"github.com/hashicorp/go-azure-helpers/resourcemanager/identity"
"github.com/hashicorp/go-azure-helpers/resourcemanager/location"
"github.com/hashicorp/go-azure-helpers/resourcemanager/tags"
"github.com/hashicorp/go-azure-sdk/resource-manager/kusto/2023-08-15/clusters"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/kusto/validate"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
Expand Down Expand Up @@ -61,7 +61,7 @@ func dataSourceKustoClusterRead(d *pluginsdk.ResourceData, meta interface{}) err
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()

id := clusters.NewClusterID(subscriptionId, d.Get("resource_group_name").(string), d.Get("name").(string))
id := commonids.NewKustoClusterID(subscriptionId, d.Get("resource_group_name").(string), d.Get("name").(string))
resp, err := client.Get(ctx, id)
if err != nil {
if !response.WasNotFound(resp.HttpResponse) {
Expand All @@ -73,7 +73,7 @@ func dataSourceKustoClusterRead(d *pluginsdk.ResourceData, meta interface{}) err

d.SetId(id.ID())

d.Set("name", id.ClusterName)
d.Set("name", id.KustoClusterName)
d.Set("resource_group_name", id.ResourceGroupName)

if model := resp.Model; model != nil {
Expand Down
15 changes: 7 additions & 8 deletions internal/services/kusto/kusto_cluster_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func resourceKustoCluster() *pluginsdk.Resource {
}),

Importer: pluginsdk.ImporterValidatingResourceId(func(id string) error {
_, err := clusters.ParseClusterID(id)
_, err := commonids.ParseKustoClusterID(id)
return err
}),

Expand Down Expand Up @@ -281,7 +281,7 @@ func resourceKustoClusterCreateUpdate(d *pluginsdk.ResourceData, meta interface{

log.Printf("[INFO] preparing arguments for Azure Kusto Cluster creation.")

id := clusters.NewClusterID(subscriptionId, d.Get("resource_group_name").(string), d.Get("name").(string))
id := commonids.NewKustoClusterID(subscriptionId, d.Get("resource_group_name").(string), d.Get("name").(string))
if d.IsNewResource() {
existing, err := client.Get(ctx, id)
if err != nil && !response.WasNotFound(existing.HttpResponse) {
Expand All @@ -293,8 +293,8 @@ func resourceKustoClusterCreateUpdate(d *pluginsdk.ResourceData, meta interface{
}
}

locks.ByName(id.ClusterName, "azurerm_kusto_cluster")
defer locks.UnlockByName(id.ClusterName, "azurerm_kusto_cluster")
locks.ByName(id.KustoClusterName, "azurerm_kusto_cluster")
defer locks.UnlockByName(id.KustoClusterName, "azurerm_kusto_cluster")

sku, err := expandKustoClusterSku(d.Get("sku").([]interface{}))
if err != nil {
Expand Down Expand Up @@ -380,7 +380,6 @@ func resourceKustoClusterCreateUpdate(d *pluginsdk.ResourceData, meta interface{
}

kustoCluster := clusters.Cluster{
Name: utils.String(id.ClusterName),
Location: location.Normalize(d.Get("location").(string)),
Identity: expandedIdentity,
Sku: *sku,
Expand All @@ -407,7 +406,7 @@ func resourceKustoClusterRead(d *pluginsdk.ResourceData, meta interface{}) error
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()

id, err := clusters.ParseClusterID(d.Id())
id, err := commonids.ParseKustoClusterID(d.Id())
if err != nil {
return err
}
Expand All @@ -421,7 +420,7 @@ func resourceKustoClusterRead(d *pluginsdk.ResourceData, meta interface{}) error
return fmt.Errorf("retrieving %s: %+v", *id, err)
}

d.Set("name", id.ClusterName)
d.Set("name", id.KustoClusterName)
d.Set("resource_group_name", id.ResourceGroupName)

if model := resp.Model; model != nil {
Expand Down Expand Up @@ -486,7 +485,7 @@ func resourceKustoClusterDelete(d *pluginsdk.ResourceData, meta interface{}) err
ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d)
defer cancel()

id, err := clusters.ParseClusterID(d.Id())
id, err := commonids.ParseKustoClusterID(d.Id())
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions internal/services/kusto/kusto_cluster_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"fmt"
"testing"

"github.com/hashicorp/go-azure-sdk/resource-manager/kusto/2023-08-15/clusters"
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonids"
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance"
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance/check"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
Expand Down Expand Up @@ -370,7 +370,7 @@ func TestAccKustoCluster_newSkus(t *testing.T) {
}

func (KustoClusterResource) Exists(ctx context.Context, clients *clients.Client, state *pluginsdk.InstanceState) (*bool, error) {
id, err := clusters.ParseClusterID(state.ID)
id, err := commonids.ParseKustoClusterID(state.ID)
if err != nil {
return nil, err
}
Expand Down
Loading

0 comments on commit 6f3dbd4

Please sign in to comment.