From a03cc3fafc0514fb445934fdaaf7ae5c42b91fa0 Mon Sep 17 00:00:00 2001 From: ziyeqf <51212351+ziyeqf@users.noreply.github.com> Date: Mon, 7 Aug 2023 20:37:03 +0800 Subject: [PATCH] `azurerm_log_analytics_workspace`: allow changing `sku` between `CapacityReservation` and `PerGB2018` (#22597) * allow changing `sku` between `CapacityReservation` and `PerGB2018` Signed-off-by: ziyeqf <51212351+ziyeqf@users.noreply.github.com> * update per comment Signed-off-by: ziyeqf <51212351+ziyeqf@users.noreply.github.com> * update document Signed-off-by: ziyeqf <51212351+ziyeqf@users.noreply.github.com> --------- Signed-off-by: ziyeqf <51212351+ziyeqf@users.noreply.github.com> --- .../log_analytics_workspace_resource.go | 8 +++++-- .../log_analytics_workspace_resource_test.go | 23 +++++++++++++++++++ .../r/log_analytics_workspace.html.markdown | 4 +++- 3 files changed, 32 insertions(+), 3 deletions(-) diff --git a/internal/services/loganalytics/log_analytics_workspace_resource.go b/internal/services/loganalytics/log_analytics_workspace_resource.go index a2eedbe175f6..fea1bf511ce9 100644 --- a/internal/services/loganalytics/log_analytics_workspace_resource.go +++ b/internal/services/loganalytics/log_analytics_workspace_resource.go @@ -117,7 +117,6 @@ func resourceLogAnalyticsWorkspace() *pluginsdk.Resource { "reservation_capacity_in_gb_per_day": { Type: pluginsdk.TypeInt, Optional: true, - Computed: true, ValidateFunc: validation.IntInSlice([]int{ int(workspaces.CapacityReservationLevelOneHundred), int(workspaces.CapacityReservationLevelTwoHundred), @@ -178,7 +177,12 @@ func resourceLogAnalyticsWorkspaceCustomDiff(ctx context.Context, d *pluginsdk.R log.Printf("[INFO] Log Analytics Workspace SKU: OLD: %q, NEW: %q", old, new) // If the old value is not LACluster(e.g. "") return ForceNew because they are // really changing the sku... - if !strings.EqualFold(old.(string), string(workspaces.WorkspaceSkuNameEnumLACluster)) && !strings.EqualFold(old.(string), "") { + changingFromLACluster := strings.EqualFold(old.(string), string(workspaces.WorkspaceSkuNameEnumLACluster)) || strings.EqualFold(old.(string), "") + // changing from capacity reservation to perGB does not force new when the last sku update date is more than 31-days ago. + // to let users do the change, we do not force new in this case and let the API error out. + changingFromCapacityReservationToPerGB := strings.EqualFold(old.(string), string(workspaces.WorkspaceSkuNameEnumCapacityReservation)) && strings.EqualFold(new.(string), string(workspaces.WorkspaceSkuNameEnumPerGBTwoZeroOneEight)) + changingFromPerGBToCapacityReservation := strings.EqualFold(old.(string), string(workspaces.WorkspaceSkuNameEnumPerGBTwoZeroOneEight)) && strings.EqualFold(new.(string), string(workspaces.WorkspaceSkuNameEnumCapacityReservation)) + if !changingFromCapacityReservationToPerGB && !changingFromLACluster && !changingFromPerGBToCapacityReservation { d.ForceNew("sku") } } diff --git a/internal/services/loganalytics/log_analytics_workspace_resource_test.go b/internal/services/loganalytics/log_analytics_workspace_resource_test.go index baefdbce0ea4..d78fba1708ce 100644 --- a/internal/services/loganalytics/log_analytics_workspace_resource_test.go +++ b/internal/services/loganalytics/log_analytics_workspace_resource_test.go @@ -331,6 +331,29 @@ func TestAccLogAnalyticsWorkspace_ToggleDisableLocalAuth(t *testing.T) { }) } +func TestAccLogAnalyticsWorkspace_updateSku(t *testing.T) { + data := acceptance.BuildTestData(t, "azurerm_log_analytics_workspace", "test") + r := LogAnalyticsWorkspaceResource{} + + data.ResourceTest(t, r, []acceptance.TestStep{ + { + Config: r.basic(data), + Check: acceptance.ComposeTestCheckFunc( + check.That(data.ResourceName).ExistsInAzure(r), + ), + }, + data.ImportStep(), + { + Config: r.withCapacityReservationTypo(data, 100), + Check: acceptance.ComposeTestCheckFunc( + check.That(data.ResourceName).ExistsInAzure(r), + check.That(data.ResourceName).Key("reservation_capacity_in_gb_per_day").HasValue("100"), + ), + }, + data.ImportStep(), + }) +} + func (t LogAnalyticsWorkspaceResource) Exists(ctx context.Context, clients *clients.Client, state *pluginsdk.InstanceState) (*bool, error) { id, err := workspaces.ParseWorkspaceID(state.ID) if err != nil { diff --git a/website/docs/r/log_analytics_workspace.html.markdown b/website/docs/r/log_analytics_workspace.html.markdown index 5c7ef452826f..332f1b7b2e55 100644 --- a/website/docs/r/log_analytics_workspace.html.markdown +++ b/website/docs/r/log_analytics_workspace.html.markdown @@ -45,6 +45,8 @@ The following arguments are supported: ~> **NOTE:** A new pricing model took effect on `2018-04-03`, which requires the SKU `PerGB2018`. If you're provisioned resources before this date you have the option of remaining with the previous Pricing SKU and using the other SKUs defined above. More information about [the Pricing SKUs is available at the following URI](https://aka.ms/PricingTierWarning). +~> **NOTE:** Changing `sku` forces a new Log Analytics Workspace to be created, except when changing between `PerGB2018` and `CapacityReservation`. However, changing `sku` to `CapacityReservation` or changing `reservation_capacity_in_gb_per_day` to a higher tier will lead to a 31-days commitment period, during which the SKU cannot be changed to a lower one. Please refer to [official documentation](https://learn.microsoft.com/en-us/azure/azure-monitor/logs/cost-logs#commitment-tiers) for further information. + ~> **NOTE:** The `Free` SKU has a default `daily_quota_gb` value of `0.5` (GB). * `retention_in_days` - (Optional) The workspace data retention in days. Possible values are either 7 (Free Tier only) or range between 30 and 730. @@ -59,7 +61,7 @@ The following arguments are supported: * `internet_query_enabled` - (Optional) Should the Log Analytics Workspace support querying over the Public Internet? Defaults to `true`. -* `reservation_capacity_in_gb_per_day` - (Optional) The capacity reservation level in GB for this workspace. Must be in increments of 100 between 100 and 5000. +* `reservation_capacity_in_gb_per_day` - (Optional) The capacity reservation level in GB for this workspace. Possible values are `100`, `200`, `300`, `400`, `500`, `1000`, `2000` and `5000`. ~> **NOTE:** `reservation_capacity_in_gb_per_day` can only be used when the `sku` is set to `CapacityReservation`.