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

Resource - azurerm_log_analytics_workspace Fix: Allow for undeletion of LAW after attaching to Log Analytics Cluster #27522

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
9 changes: 9 additions & 0 deletions internal/services/loganalytics/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/hashicorp/go-azure-sdk/resource-manager/operationalinsights/2020-08-01/storageinsights"
"github.com/hashicorp/go-azure-sdk/resource-manager/operationalinsights/2020-08-01/workspaces"
"github.com/hashicorp/go-azure-sdk/resource-manager/operationalinsights/2022-10-01/clusters"
"github.com/hashicorp/go-azure-sdk/resource-manager/operationalinsights/2022-10-01/deletedworkspaces"
"github.com/hashicorp/go-azure-sdk/resource-manager/operationalinsights/2022-10-01/tables"
featureWorkspaces "github.com/hashicorp/go-azure-sdk/resource-manager/operationalinsights/2022-10-01/workspaces"
"github.com/hashicorp/go-azure-sdk/resource-manager/operationsmanagement/2015-11-01-preview/solution"
Expand All @@ -27,6 +28,7 @@ type Client struct {
DataExportClient *dataexport.DataExportClient
DataSourcesClient *datasources.DataSourcesClient
LinkedServicesClient *linkedservices.LinkedServicesClient
DeletedClient *deletedworkspaces.DeletedWorkspacesClient
LinkedStorageAccountClient *linkedstorageaccounts.LinkedStorageAccountsClient
QueryPacksClient *querypacks.QueryPacksClient
SavedSearchesClient *savedsearches.SavedSearchesClient
Expand Down Expand Up @@ -57,6 +59,12 @@ func NewClient(o *common.ClientOptions) (*Client, error) {
}
o.Configure(dataSourcesClient.Client, o.Authorizers.ResourceManager)

deletedClient, err := deletedworkspaces.NewDeletedWorkspacesClientWithBaseURI(o.Environment.ResourceManager)
if err != nil {
return nil, fmt.Errorf("building DeletedWorkspaces client: %+v", err)
}
o.Configure(deletedClient.Client, o.Authorizers.ResourceManager)

workspacesClient, err := workspaces.NewWorkspacesClientWithBaseURI(o.Environment.ResourceManager)
if err != nil {
return nil, fmt.Errorf("building Workspaces client: %+v", err)
Expand Down Expand Up @@ -131,5 +139,6 @@ func NewClient(o *common.ClientOptions) (*Client, error) {
SharedKeyWorkspacesClient: workspacesClient,
TablesClient: tablesClient,
WorkspaceClient: featureWorkspaceClient,
DeletedClient: deletedClient,
}, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

"github.com/hashicorp/go-azure-helpers/lang/pointer"
"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-sdk/resource-manager/insights/2022-06-01/datacollectionrules"
Expand Down Expand Up @@ -206,6 +207,7 @@ func resourceLogAnalyticsWorkspaceCustomDiff(ctx context.Context, d *pluginsdk.R

func resourceLogAnalyticsWorkspaceCreateUpdate(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).LogAnalytics.WorkspaceClient
deletedClient := meta.(*clients.Client).LogAnalytics.DeletedClient
subscriptionId := meta.(*clients.Client).Account.SubscriptionId
ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d)
defer cancel()
Expand All @@ -224,6 +226,23 @@ func resourceLogAnalyticsWorkspaceCreateUpdate(d *pluginsdk.ResourceData, meta i
}
}

id := commonids.NewSubscriptionID(subscriptionId)
deletedResp, err := deletedClient.List(ctx, id)
if err != nil {
return fmt.Errorf("checking for deleted Log Analytics Workspaces: %s", err)
}

if deletedModel := deletedResp.Model; deletedModel != nil && deletedModel.Value != nil {
for _, v := range *deletedModel.Value {
Copy link
Collaborator

Choose a reason for hiding this comment

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

we will need to nil check value

if v.Properties != nil && v.Properties.Sku != nil {
if pointer.From(v.Name) == name && strings.EqualFold(string(v.Properties.Sku.Name), string(workspaces.WorkspaceSkuNameEnumLACluster)) {
isLACluster = true
log.Printf("[INFO] Log Analytics Workspace %q: Soft-deleted resource is linked to Log Analytics Cluster", name)
}
}
}
}

if !response.WasNotFound(existing.HttpResponse) {
return tf.ImportAsExistsError("azurerm_log_analytics_workspace", id.ID())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,32 @@ func TestAccLogAnalyticsWorkspace_toggleIdentity(t *testing.T) {
})
}

func TestAccLogAnalyticsWorkspace_attachToLACDeleteThenRecreate(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_log_analytics_workspace", "test")
r := LogAnalyticsWorkspaceResource{}

data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.withLACLink(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
{
Config: r.withDeletedLAW(data),
},
data.ImportStep(),
{
Config: r.withRestoredLAW(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
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 {
Expand Down Expand Up @@ -989,3 +1015,109 @@ resource "azurerm_log_analytics_workspace" "test" {
}
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger, enable)
}

func (LogAnalyticsWorkspaceResource) withLACLink(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
features {
log_analytics_workspace {
permanently_delete_on_destroy = false
}
}
}

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

resource "azurerm_log_analytics_cluster" "test" {
name = "acctestLAC-%d"
location = "%s"
resource_group_name = azurerm_resource_group.test.name
identity {
type = "SystemAssigned"
}
size_gb = 100
}

resource "azurerm_log_analytics_linked_service" "example" {
resource_group_name = azurerm_resource_group.test.name
workspace_id = azurerm_log_analytics_workspace.test.id
write_access_id = azurerm_log_analytics_cluster.test.id
}

resource "azurerm_log_analytics_workspace" "test" {
name = "acctestLAW-%d"
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
}
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger, data.Locations.Primary, data.RandomInteger)
}

func (LogAnalyticsWorkspaceResource) withDeletedLAW(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
features {
log_analytics_workspace {
permanently_delete_on_destroy = false
}
}
}

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

resource "azurerm_log_analytics_cluster" "test" {
name = "acctestLAC-%d"
location = "%s"
resource_group_name = azurerm_resource_group.test.name
identity {
type = "SystemAssigned"
}
size_gb = 100
}

`, data.RandomInteger, data.Locations.Primary, data.RandomInteger, data.Locations.Primary)
}

func (LogAnalyticsWorkspaceResource) withRestoredLAW(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
features {
log_analytics_workspace {
permanently_delete_on_destroy = false
}
}
}

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

resource "azurerm_log_analytics_cluster" "test" {
name = "acctestLAC-%d"
location = "%s"
resource_group_name = azurerm_resource_group.test.name
identity {
type = "SystemAssigned"
}
size_gb = 100
}

resource "azurerm_log_analytics_linked_service" "example" {
resource_group_name = azurerm_resource_group.test.name
workspace_id = azurerm_log_analytics_workspace.test.id
write_access_id = azurerm_log_analytics_cluster.test.id
}

resource "azurerm_log_analytics_workspace" "test" {
name = "acctestLAW-%d"
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
}
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger, data.Locations.Primary, data.RandomInteger)
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading