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

azurerm_kusto_cluster - update optimized_auto_scale after sku has been updated #24906

Merged
merged 2 commits into from
Feb 16, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 26 additions & 25 deletions internal/services/kusto/kusto_cluster_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,30 +301,6 @@ func resourceKustoClusterCreateUpdate(d *pluginsdk.ResourceData, meta interface{
return err
}

optimizedAutoScale := expandOptimizedAutoScale(d.Get("optimized_auto_scale").([]interface{}))

if optimizedAutoScale != nil && optimizedAutoScale.IsEnabled {
if sku.Capacity == nil {
return fmt.Errorf("sku.capacity could not be empty")
}
// Ensure that requested Capcity is always between min and max to support updating to not overlapping autoscale ranges
if *sku.Capacity < optimizedAutoScale.Minimum {
sku.Capacity = utils.Int64(optimizedAutoScale.Minimum)
}
if *sku.Capacity > optimizedAutoScale.Maximum {
sku.Capacity = utils.Int64(optimizedAutoScale.Maximum)
}

// Capacity must be set for the initial creation when using OptimizedAutoScaling but cannot be updated
if d.HasChange("sku.0.capacity") && !d.IsNewResource() {
return fmt.Errorf("cannot change `sku.capacity` when `optimized_auto_scaling.enabled` is set to `true`")
}

if optimizedAutoScale.Minimum > optimizedAutoScale.Maximum {
return fmt.Errorf("`optimized_auto_scaling.maximum_instances` must be >= `optimized_auto_scaling.minimum_instances`")
}
}

publicNetworkAccess := clusters.PublicNetworkAccessEnabled
if !d.Get("public_network_access_enabled").(bool) {
publicNetworkAccess = clusters.PublicNetworkAccessDisabled
Expand All @@ -333,7 +309,6 @@ func resourceKustoClusterCreateUpdate(d *pluginsdk.ResourceData, meta interface{
publicIPType := clusters.PublicIPType(d.Get("public_ip_type").(string))

clusterProperties := clusters.ClusterProperties{
OptimizedAutoscale: optimizedAutoScale,
EnableAutoStop: utils.Bool(d.Get("auto_stop_enabled").(bool)),
EnableDiskEncryption: utils.Bool(d.Get("disk_encryption_enabled").(bool)),
EnableDoubleEncryption: utils.Bool(d.Get("double_encryption_enabled").(bool)),
Expand Down Expand Up @@ -398,6 +373,32 @@ func resourceKustoClusterCreateUpdate(d *pluginsdk.ResourceData, meta interface{

d.SetId(id.ID())

// optimized_auto_scale can't be updated when the sku is also being updated so we'll update it separately
// todo rework this when we split create/update
optimizedAutoScale := expandOptimizedAutoScale(d.Get("optimized_auto_scale").([]interface{}))
if optimizedAutoScale != nil && optimizedAutoScale.IsEnabled {
if sku.Capacity == nil {
return fmt.Errorf("sku.capacity could not be empty")
}
// Ensure that requested Capcity is always between min and max to support updating to not overlapping autoscale ranges
if *sku.Capacity < optimizedAutoScale.Minimum {
sku.Capacity = utils.Int64(optimizedAutoScale.Minimum)
}
if *sku.Capacity > optimizedAutoScale.Maximum {
sku.Capacity = utils.Int64(optimizedAutoScale.Maximum)
}

if optimizedAutoScale.Minimum > optimizedAutoScale.Maximum {
return fmt.Errorf("`optimized_auto_scaling.maximum_instances` must be >= `optimized_auto_scaling.minimum_instances`")
}

clusterProperties.OptimizedAutoscale = optimizedAutoScale

if err := client.CreateOrUpdateThenPoll(ctx, id, kustoCluster, clusters.CreateOrUpdateOperationOptions{}); err != nil {
return fmt.Errorf("creating/updating %s: %+v", id, err)
}
}

return resourceKustoClusterRead(d, meta)
}

Expand Down
58 changes: 56 additions & 2 deletions internal/services/kusto/kusto_cluster_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,35 @@ func TestAccKustoCluster_optimizedAutoScale(t *testing.T) {
})
}

func TestAccKustoCluster_updateSkuAndOptimizedAutoScale(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_kusto_cluster", "test")
r := KustoClusterResource{}

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

func TestAccKustoCluster_trustedExternalTenants(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_kusto_cluster", "test")
r := KustoClusterResource{}
Expand Down Expand Up @@ -806,6 +835,30 @@ resource "azurerm_kusto_cluster" "test" {
`, data.RandomInteger, data.Locations.Primary, data.RandomString)
}

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

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

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

sku {
name = "Dev(No SLA)_Standard_E2a_v4"
capacity = 1
}
}
`, data.RandomInteger, data.Locations.Primary, data.RandomString)
}

func (KustoClusterResource) optimizedAutoScale(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
Expand All @@ -823,12 +876,13 @@ resource "azurerm_kusto_cluster" "test" {
resource_group_name = azurerm_resource_group.test.name

sku {
name = "Standard_D11_v2"
name = "Standard_L8s_v3"
capacity = 2
}

optimized_auto_scale {
minimum_instances = 2
maximum_instances = 3
maximum_instances = 2
}
}
`, data.RandomInteger, data.Locations.Primary, data.RandomString)
Expand Down
Loading