diff --git a/internal/services/loganalytics/log_analytics_linked_storage_account_resource.go b/internal/services/loganalytics/log_analytics_linked_storage_account_resource.go index d3e2ba109ebd..f5e3878dd7e4 100644 --- a/internal/services/loganalytics/log_analytics_linked_storage_account_resource.go +++ b/internal/services/loganalytics/log_analytics_linked_storage_account_resource.go @@ -3,7 +3,6 @@ package loganalytics import ( "fmt" "log" - "strings" "time" "github.com/hashicorp/go-azure-helpers/lang/response" @@ -11,14 +10,16 @@ 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/tf/pluginsdk" + "github.com/hashicorp/terraform-provider-azurerm/internal/tf/suppress" "github.com/hashicorp/terraform-provider-azurerm/internal/tf/validation" "github.com/hashicorp/terraform-provider-azurerm/internal/timeouts" "github.com/hashicorp/terraform-provider-azurerm/utils" ) func resourceLogAnalyticsLinkedStorageAccount() *pluginsdk.Resource { - return &pluginsdk.Resource{ + resource := &pluginsdk.Resource{ Create: resourceLogAnalyticsLinkedStorageAccountCreateUpdate, Read: resourceLogAnalyticsLinkedStorageAccountRead, Update: resourceLogAnalyticsLinkedStorageAccountCreateUpdate, @@ -32,7 +33,7 @@ func resourceLogAnalyticsLinkedStorageAccount() *pluginsdk.Resource { }, Importer: pluginsdk.ImporterValidatingResourceId(func(id string) error { - _, err := linkedstorageaccounts.ParseDataSourceTypeIDInsensitively(id) // TODO remove insensitive parsing in 4.0 + _, err := linkedstorageaccounts.ParseDataSourceTypeID(id) return err }), @@ -42,13 +43,12 @@ func resourceLogAnalyticsLinkedStorageAccount() *pluginsdk.Resource { Required: true, ForceNew: true, ValidateFunc: validation.StringInSlice([]string{ - strings.ToLower(string(linkedstorageaccounts.DataSourceTypeCustomLogs)), - strings.ToLower(string(linkedstorageaccounts.DataSourceTypeAzureWatson)), - strings.ToLower(string(linkedstorageaccounts.DataSourceTypeQuery)), - strings.ToLower(string(linkedstorageaccounts.DataSourceTypeAlerts)), - // Value removed from enum in 2020-08-01, but effectively still works - "ingestion", - }, false), + string(linkedstorageaccounts.DataSourceTypeCustomLogs), + string(linkedstorageaccounts.DataSourceTypeAzureWatson), + string(linkedstorageaccounts.DataSourceTypeQuery), + string(linkedstorageaccounts.DataSourceTypeAlerts), + string(linkedstorageaccounts.DataSourceTypeIngestion), + }, !features.FourPointOhBeta()), }, "resource_group_name": azure.SchemaResourceGroupName(), @@ -71,6 +71,12 @@ func resourceLogAnalyticsLinkedStorageAccount() *pluginsdk.Resource { }, }, } + + if !features.FourPointOh() { + resource.Schema["data_source_type"].DiffSuppressFunc = suppress.CaseDifference + } + + return resource } func resourceLogAnalyticsLinkedStorageAccountCreateUpdate(d *pluginsdk.ResourceData, meta interface{}) error { @@ -114,7 +120,7 @@ func resourceLogAnalyticsLinkedStorageAccountRead(d *pluginsdk.ResourceData, met ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d) defer cancel() - id, err := linkedstorageaccounts.ParseDataSourceTypeIDInsensitively(d.Id()) // TODO remove insensitive parsing in 4.0 + id, err := linkedstorageaccounts.ParseDataSourceTypeID(d.Id()) if err != nil { return err } @@ -131,6 +137,7 @@ func resourceLogAnalyticsLinkedStorageAccountRead(d *pluginsdk.ResourceData, met d.Set("resource_group_name", id.ResourceGroupName) d.Set("workspace_resource_id", linkedstorageaccounts.NewWorkspaceID(id.SubscriptionId, id.ResourceGroupName, id.WorkspaceName).ID()) + d.Set("data_source_type", string(id.DataSourceType)) if model := resp.Model; model != nil { props := model.Properties @@ -139,13 +146,6 @@ func resourceLogAnalyticsLinkedStorageAccountRead(d *pluginsdk.ResourceData, met storageAccountIds = *props.StorageAccountIds } d.Set("storage_account_ids", storageAccountIds) - - dataSourceType := "" - if props.DataSourceType != nil { - dataSourceType = string(*props.DataSourceType) - } - d.Set("data_source_type", dataSourceType) - } return nil @@ -156,7 +156,7 @@ func resourceLogAnalyticsLinkedStorageAccountDelete(d *pluginsdk.ResourceData, m ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d) defer cancel() - id, err := linkedstorageaccounts.ParseDataSourceTypeIDInsensitively(d.Id()) // TODO remove insensitive parsing in 4.0 + id, err := linkedstorageaccounts.ParseDataSourceTypeID(d.Id()) if err != nil { return err } diff --git a/internal/services/loganalytics/log_analytics_linked_storage_account_resource_test.go b/internal/services/loganalytics/log_analytics_linked_storage_account_resource_test.go index 4c14ecf1a7e4..c97e23e4fe68 100644 --- a/internal/services/loganalytics/log_analytics_linked_storage_account_resource_test.go +++ b/internal/services/loganalytics/log_analytics_linked_storage_account_resource_test.go @@ -101,7 +101,7 @@ func TestAcclogAnalyticsLinkedStorageAccount_ingestion(t *testing.T) { } func (t LogAnalyticsLinkedStorageAccountResource) Exists(ctx context.Context, clients *clients.Client, state *pluginsdk.InstanceState) (*bool, error) { - id, err := linkedstorageaccounts.ParseDataSourceTypeIDInsensitively(state.ID) // TODO remove insensitive parsing in 4.0 + id, err := linkedstorageaccounts.ParseDataSourceTypeID(state.ID) if err != nil { return nil, err } @@ -147,7 +147,7 @@ func (r LogAnalyticsLinkedStorageAccountResource) basic(data acceptance.TestData %s resource "azurerm_log_analytics_linked_storage_account" "test" { - data_source_type = "customlogs" + data_source_type = "CustomLogs" resource_group_name = azurerm_resource_group.test.name workspace_resource_id = azurerm_log_analytics_workspace.test.id storage_account_ids = [azurerm_storage_account.test.id] @@ -181,7 +181,7 @@ resource "azurerm_storage_account" "test2" { } resource "azurerm_log_analytics_linked_storage_account" "test" { - data_source_type = "customlogs" + data_source_type = "CustomLogs" resource_group_name = azurerm_resource_group.test.name workspace_resource_id = azurerm_log_analytics_workspace.test.id storage_account_ids = [azurerm_storage_account.test.id, azurerm_storage_account.test2.id] @@ -194,7 +194,7 @@ func (r LogAnalyticsLinkedStorageAccountResource) ingestion(data acceptance.Test %s resource "azurerm_log_analytics_linked_storage_account" "test" { - data_source_type = "ingestion" + data_source_type = "Ingestion" resource_group_name = azurerm_resource_group.test.name workspace_resource_id = azurerm_log_analytics_workspace.test.id storage_account_ids = [azurerm_storage_account.test.id] diff --git a/internal/services/loganalytics/migration/linked_storage_account_v0_to_v1.go b/internal/services/loganalytics/migration/linked_storage_account_v0_to_v1.go new file mode 100644 index 000000000000..fc97088d6527 --- /dev/null +++ b/internal/services/loganalytics/migration/linked_storage_account_v0_to_v1.go @@ -0,0 +1,69 @@ +package migration + +import ( + "context" + + "github.com/hashicorp/go-azure-sdk/resource-manager/operationalinsights/2020-08-01/linkedstorageaccounts" + "github.com/hashicorp/terraform-provider-azurerm/helpers/azure" + "github.com/hashicorp/terraform-provider-azurerm/internal/features" + "github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk" + "github.com/hashicorp/terraform-provider-azurerm/internal/tf/suppress" + "github.com/hashicorp/terraform-provider-azurerm/internal/tf/validation" +) + +var _ pluginsdk.StateUpgrade = LinkedStorageAccountV0ToV1{} + +type LinkedStorageAccountV0ToV1 struct{} + +func (LinkedStorageAccountV0ToV1) UpgradeFunc() pluginsdk.StateUpgraderFunc { + return func(ctx context.Context, rawState map[string]interface{}, meta interface{}) (map[string]interface{}, error) { + oldId, err := linkedstorageaccounts.ParseDataSourceTypeIDInsensitively(rawState["id"].(string)) + if err != nil { + return rawState, err + } + + rawState["id"] = oldId.ID() + return rawState, nil + } +} + +func (LinkedStorageAccountV0ToV1) Schema() map[string]*pluginsdk.Schema { + schema := map[string]*pluginsdk.Schema{ + "data_source_type": { + Type: pluginsdk.TypeString, + Required: true, + ForceNew: true, + ValidateFunc: validation.StringInSlice([]string{ + string(linkedstorageaccounts.DataSourceTypeCustomLogs), + string(linkedstorageaccounts.DataSourceTypeAzureWatson), + string(linkedstorageaccounts.DataSourceTypeQuery), + string(linkedstorageaccounts.DataSourceTypeAlerts), + string(linkedstorageaccounts.DataSourceTypeIngestion), + }, !features.FourPointOhBeta()), + }, + + "resource_group_name": azure.SchemaResourceGroupName(), + + "workspace_resource_id": { + Type: pluginsdk.TypeString, + Required: true, + ForceNew: true, + ValidateFunc: linkedstorageaccounts.ValidateWorkspaceID, + }, + + "storage_account_ids": { + Type: pluginsdk.TypeSet, + Required: true, + MinItems: 1, + Elem: &pluginsdk.Schema{ + Type: pluginsdk.TypeString, + ValidateFunc: azure.ValidateResourceID, + }, + }, + } + + if !features.FourPointOh() { + schema["data_source_type"].DiffSuppressFunc = suppress.CaseDifference + } + return schema +} diff --git a/website/docs/r/log_analytics_linked_storage_account.html.markdown b/website/docs/r/log_analytics_linked_storage_account.html.markdown index edc88f8becf5..ac20a8e56e15 100644 --- a/website/docs/r/log_analytics_linked_storage_account.html.markdown +++ b/website/docs/r/log_analytics_linked_storage_account.html.markdown @@ -34,7 +34,7 @@ resource "azurerm_log_analytics_workspace" "example" { } resource "azurerm_log_analytics_linked_storage_account" "example" { - data_source_type = "customlogs" + data_source_type = "CustomLogs" resource_group_name = azurerm_resource_group.example.name workspace_resource_id = azurerm_log_analytics_workspace.example.id storage_account_ids = [azurerm_storage_account.example.id] @@ -45,7 +45,9 @@ resource "azurerm_log_analytics_linked_storage_account" "example" { The following arguments are supported: -* `data_source_type` - (Required) The data source type which should be used for this Log Analytics Linked Storage Account. Possible values are `customlogs`, `azurewatson`, `query`, `ingestion` and `alerts`. Changing this forces a new Log Analytics Linked Storage Account to be created. +* `data_source_type` - (Required) The data source type which should be used for this Log Analytics Linked Storage Account. Possible values are `CustomLogs`, `AzureWatson`, `Query`, `Ingestion` and `Alerts`. Changing this forces a new Log Analytics Linked Storage Account to be created. + +> **Note:** The `data_source_type` is case-insensitive in current 3.x version. And in 4.0 or later versions, Case-sensitivity will be required. * `resource_group_name` - (Required) The name of the Resource Group where the Log Analytics Linked Storage Account should exist. Changing this forces a new Log Analytics Linked Storage Account to be created.