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_log_analytics_linked_storage_account - fix data_source_type case sensitivity #18116

Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,23 @@ package loganalytics
import (
"fmt"
"log"
"strings"
"time"

"github.com/hashicorp/go-azure-helpers/lang/response"
"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/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,
Expand All @@ -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
}),

Expand All @@ -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(),
Expand All @@ -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 {
Expand Down Expand Up @@ -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
}
Expand All @@ -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))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since this is part of the id, we might want to do a state migration so we can remove the parse insensitively above?


if model := resp.Model; model != nil {
props := model.Properties
Expand All @@ -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
Expand All @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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]
Expand All @@ -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]
Expand Down
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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.

Expand Down