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_workspace: support data_collection_rule_id #23347

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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
19 changes: 19 additions & 0 deletions internal/services/loganalytics/log_analytics_workspace_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,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/commonschema"
"github.com/hashicorp/go-azure-sdk/resource-manager/insights/2022-06-01/datacollectionrules"
sharedKeyWorkspaces "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/workspaces"
"github.com/hashicorp/terraform-provider-azurerm/helpers/azure"
Expand Down Expand Up @@ -144,6 +145,12 @@ func resourceLogAnalyticsWorkspace() *pluginsdk.Resource {
ValidateFunc: validation.FloatAtLeast(-1.0),
},

"default_data_collection_rule_id": {
Copy link
Member

Choose a reason for hiding this comment

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

default is implied here since there isn't any other data collection rule ID property so can we rename this to

Suggested change
"default_data_collection_rule_id": {
"data_collection_rule_id": {

Type: pluginsdk.TypeString,
Optional: true,
ValidateFunc: datacollectionrules.ValidateDataCollectionRuleID,
},

"workspace_id": {
Type: pluginsdk.TypeString,
Computed: true,
Expand Down Expand Up @@ -311,6 +318,12 @@ func resourceLogAnalyticsWorkspaceCreateUpdate(d *pluginsdk.ResourceData, meta i
return err
}

// `default_data_collection_rule_id` also needs an additional update.
// error message: Default dcr is not applicable on workspace creation, please provide it on update.
if v, ok := d.GetOk("default_data_collection_rule_id"); ok {
parameters.Properties.DefaultDataCollectionRuleResourceId = pointer.To(v.(string))
}

// `allow_resource_only_permissions` needs an additional update, tacked on https://github.com/Azure/azure-rest-api-specs/issues/21591
err = client.CreateOrUpdateThenPoll(ctx, id, parameters)
if err != nil {
Expand Down Expand Up @@ -438,6 +451,12 @@ func resourceLogAnalyticsWorkspaceRead(d *pluginsdk.ResourceData, meta interface
d.Set("allow_resource_only_permissions", allowResourceOnlyPermissions)
d.Set("local_authentication_disabled", disableLocalAuth)

defaultDataCollectionRuleResourceId := ""
if props.DefaultDataCollectionRuleResourceId != nil {
defaultDataCollectionRuleResourceId = *props.DefaultDataCollectionRuleResourceId
Copy link
Member

Choose a reason for hiding this comment

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

We should parse this before setting to state

}
d.Set("default_data_collection_rule_id", defaultDataCollectionRuleResourceId)

sharedKeyId := sharedKeyWorkspaces.WorkspaceId{
SubscriptionId: id.SubscriptionId,
ResourceGroupName: id.ResourceGroupName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"
"testing"

"github.com/hashicorp/go-azure-sdk/resource-manager/insights/2022-06-01/datacollectionrules"
"github.com/hashicorp/go-azure-sdk/resource-manager/operationalinsights/2020-08-01/workspaces"
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance"
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance/check"
Expand Down Expand Up @@ -354,6 +355,30 @@ func TestAccLogAnalyticsWorkspace_updateSku(t *testing.T) {
})
}

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

// the default data collection rule could only be set during update,
// and to avoid the dependency cycle, we do an additional update here.
data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.withDataCollectionRule(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
{
Config: r.withDefaultDataCollectionRule(data, datacollectionrules.NewDataCollectionRuleID(data.Subscriptions.Primary, fmt.Sprintf("acctestRG-%d", data.RandomInteger), fmt.Sprintf("acctestmdcr-%d", data.RandomInteger)).ID()),
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 @@ -716,3 +741,82 @@ resource "azurerm_log_analytics_workspace" "test" {
}
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger, disableLocalAuth)
}

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

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

resource "azurerm_monitor_data_collection_rule" "test" {
name = "acctestmdcr-%[1]d"
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location

destinations {
log_analytics {
workspace_resource_id = azurerm_log_analytics_workspace.test.id
name = "test-destination"
}
}

data_flow {
streams = ["Microsoft-InsightsMetrics"]
destinations = ["test-destination"]
}
}

resource "azurerm_log_analytics_workspace" "test" {
name = "acctestLAW-%[1]d"
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
sku = "PerGB2018"
retention_in_days = 30
}
`, data.RandomInteger, data.Locations.Primary)
}

func (LogAnalyticsWorkspaceResource) withDefaultDataCollectionRule(data acceptance.TestData, ruleID string) string {
return fmt.Sprintf(`
provider "azurerm" {
features {}
}

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

resource "azurerm_monitor_data_collection_rule" "test" {
name = "acctestmdcr-%[1]d"
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location

destinations {
log_analytics {
workspace_resource_id = azurerm_log_analytics_workspace.test.id
name = "test-destination"
}
}

data_flow {
streams = ["Microsoft-InsightsMetrics"]
destinations = ["test-destination"]
}
}

resource "azurerm_log_analytics_workspace" "test" {
name = "acctestLAW-%[1]d"
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
sku = "PerGB2018"
retention_in_days = 30
default_data_collection_rule_id = "%[3]s"
}
`, data.RandomInteger, data.Locations.Primary, ruleID)
}
2 changes: 2 additions & 0 deletions website/docs/r/log_analytics_workspace.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ The following arguments are supported:

~> **NOTE:** `reservation_capacity_in_gb_per_day` can only be used when the `sku` is set to `CapacityReservation`.

* `default_data_collection_rule_id` - (Optional) The ID of the default Data Collection Rule to use for this workspace.

* `tags` - (Optional) A mapping of tags to assign to the resource.

~> **NOTE:** If a `azurerm_log_analytics_workspace` is connected to a `azurerm_log_analytics_cluster` via a `azurerm_log_analytics_linked_service` you will not be able to modify the workspaces `sku` field until the link between the workspace and the cluster has been broken by deleting the `azurerm_log_analytics_linked_service` resource. All other fields are modifiable while the workspace is linked to a cluster.
Expand Down