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

Updating Eventhub as the log analytics destination #17668

Merged
merged 3 commits into from
Aug 2, 2022
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,9 +3,12 @@ package loganalytics
import (
"fmt"
"log"
"strings"
"time"

"github.com/Azure/azure-sdk-for-go/services/operationalinsights/mgmt/2020-08-01/operationalinsights"
"github.com/hashicorp/go-azure-sdk/resource-manager/eventhub/2021-11-01/eventhubs"
"github.com/hashicorp/go-azure-sdk/resource-manager/eventhub/2021-11-01/namespaces"
"github.com/hashicorp/terraform-provider-azurerm/helpers/azure"
"github.com/hashicorp/terraform-provider-azurerm/helpers/tf"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
Expand Down Expand Up @@ -107,16 +110,30 @@ func resourceOperationalinsightsDataExportCreateUpdate(d *pluginsdk.ResourceData
}
}

destinationId := d.Get("destination_resource_id").(string)

parameters := operationalinsights.DataExport{
DataExportProperties: &operationalinsights.DataExportProperties{
Destination: &operationalinsights.Destination{
ResourceID: utils.String(d.Get("destination_resource_id").(string)),
ResourceID: utils.String(destinationId),
},
TableNames: utils.ExpandStringSlice(d.Get("table_names").(*pluginsdk.Set).List()),
Enable: utils.Bool(d.Get("enabled").(bool)),
},
}

if strings.Contains(destinationId, "Microsoft.EventHub") {
eventhubId, err := eventhubs.ParseEventhubID(destinationId)
if err != nil {
return fmt.Errorf("parsing destination eventhub id error: %+v", err)
}
destinationId = namespaces.NewNamespaceID(eventhubId.SubscriptionId, eventhubId.ResourceGroupName, eventhubId.NamespaceName).ID()
parameters.DataExportProperties.Destination.ResourceID = utils.String(destinationId)
parameters.DataExportProperties.Destination.DestinationMetaData = &operationalinsights.DestinationMetaData{
EventHubName: utils.String(eventhubId.EventHubName),
}
}

if _, err := client.CreateOrUpdate(ctx, id.ResourceGroup, id.WorkspaceName, id.DataexportName, parameters); err != nil {
return fmt.Errorf("creating/updating %s: %+v", id, err)
}
Expand Down Expand Up @@ -149,7 +166,12 @@ func resourceOperationalinsightsDataExportRead(d *pluginsdk.ResourceData, meta i
d.Set("workspace_resource_id", parse.NewLogAnalyticsWorkspaceID(id.SubscriptionId, id.ResourceGroup, id.WorkspaceName).ID())
if props := resp.DataExportProperties; props != nil {
d.Set("export_rule_id", props.DataExportID)
d.Set("destination_resource_id", flattenDataExportDestination(props.Destination))

destinationId, err := flattenDataExportDestination(props.Destination)
if err != nil {
return fmt.Errorf("flattening destination ID error: %+v", err)
}
d.Set("destination_resource_id", destinationId)
d.Set("enabled", props.Enable)
d.Set("table_names", utils.FlattenStringSlice(props.TableNames))
}
Expand All @@ -172,15 +194,26 @@ func resourceOperationalinsightsDataExportDelete(d *pluginsdk.ResourceData, meta
return nil
}

func flattenDataExportDestination(input *operationalinsights.Destination) string {
func flattenDataExportDestination(input *operationalinsights.Destination) (string, error) {
if input == nil {
return ""
return "", nil
}

var resourceID string
if input.ResourceID != nil {
resourceID = *input.ResourceID
if input.Type == operationalinsights.TypeEventHub {
if input.DestinationMetaData != nil && input.DestinationMetaData.EventHubName != nil {
eventhubName := *input.DestinationMetaData.EventHubName
eventhubNamespaceId, err := eventhubs.ParseNamespaceID(resourceID)
eventhubId := eventhubs.NewEventhubID(eventhubNamespaceId.SubscriptionId, eventhubNamespaceId.ResourceGroupName, eventhubNamespaceId.NamespaceName, eventhubName)
if err != nil {
return "", fmt.Errorf("parsing destination eventhub namespace ID error")
}
resourceID = eventhubId.ID()
}
}
}

return resourceID
return resourceID, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,21 @@ func TestAccLogAnalyticsDataExportRule_complete(t *testing.T) {
})
}

func TestAccLogAnalyticsDataExportRule_toEventhubs(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_log_analytics_data_export_rule", "test")
r := LogAnalyticsDataExportRuleResource{}

data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.toEventHub(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
})
}

func (t LogAnalyticsDataExportRuleResource) Exists(ctx context.Context, clients *clients.Client, state *pluginsdk.InstanceState) (*bool, error) {
id, err := parse.LogAnalyticsDataExportID(state.ID)
if err != nil {
Expand Down Expand Up @@ -125,7 +140,22 @@ resource "azurerm_storage_account" "test" {
account_tier = "Standard"
account_replication_type = "LRS"
}
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger, data.RandomString)

resource "azurerm_eventhub_namespace" "test" {
name = "acctest-EHN-%d"
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
sku = "Standard"
}

resource "azurerm_eventhub" "test" {
name = "acctest-EH-%d"
namespace_name = azurerm_eventhub_namespace.test.name
resource_group_name = azurerm_resource_group.test.name
partition_count = 2
message_retention = 7
}
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger, data.RandomString, data.RandomInteger, data.RandomInteger)
}

func (r LogAnalyticsDataExportRuleResource) basic(data acceptance.TestData) string {
Expand Down Expand Up @@ -184,3 +214,18 @@ resource "azurerm_log_analytics_data_export_rule" "test" {
}
`, r.template(data), data.RandomInteger)
}

func (r LogAnalyticsDataExportRuleResource) toEventHub(data acceptance.TestData) string {
return fmt.Sprintf(`
%s

resource "azurerm_log_analytics_data_export_rule" "test" {
name = "acctest-DER-%d"
resource_group_name = azurerm_resource_group.test.name
workspace_resource_id = azurerm_log_analytics_workspace.test.id
destination_resource_id = azurerm_eventhub.test.id
table_names = ["Heartbeat"]
enabled = true
}
`, r.template(data), data.RandomInteger)
}

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