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

Add table azure_monitor_log_profile Closes #712 #717

Merged
merged 2 commits into from
Feb 2, 2024
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
@@ -0,0 +1,7 @@
[
{
"id": "{{ output.resource_id_lower.value }}",
"name": "{{ resourceName }}",
"storage_account_id": "{{ output.storage_account_id.value }}"
}
]
8 changes: 8 additions & 0 deletions azure-test/tests/azure_monitor_log_profile/test-get-query.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
select
name,
id,
storage_account_id
from
azure.azure_monitor_log_profile
where
name = '{{ resourceName }}'
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[
{
"id": "{{ output.resource_id_lower.value }}",
"name": "{{ resourceName }}"
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
select
name,
id
from
azure.azure_monitor_log_profile
where
id = '{{ output.resource_id_lower.value }}';
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
select
name,
id
from
azure.azure_monitor_log_profile
where
name = 'dummy-{{ resourceName }}';
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[
{
"name": "{{ resourceName }}",
"subscription_id": "{{ output.subscription_id.value }}",
"title": "{{ resourceName }}"
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
select
name,
subscription_id,
title
from
azure.azure_monitor_log_profile
where
name = '{{ resourceName }}';
1 change: 1 addition & 0 deletions azure-test/tests/azure_monitor_log_profile/variables.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
106 changes: 106 additions & 0 deletions azure-test/tests/azure_monitor_log_profile/vatiables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
variable "resource_name" {
type = string
default = "turbot-test-20200125-create-update"
description = "Name of the resource used throughout the test."
}

variable "azure_environment" {
type = string
default = "public"
description = "Azure environment used for the test."
}

variable "azure_subscription" {
type = string
default = "3510ae4d-530b-497d-8f30-53b9616fc6c1"
description = "Azure subscription used for the test."
}

provider "azurerm" {
# Cannot be passed as a variable
environment = var.azure_environment
subscription_id = var.azure_subscription
features {}
}

data "azurerm_client_config" "current" {}

data "null_data_source" "resource" {
inputs = {
scope = "azure:///subscriptions/${data.azurerm_client_config.current.subscription_id}"
}
}

resource "azurerm_resource_group" "named_test_resource" {
name = var.resource_name
location = "East US"
}

resource "azurerm_storage_account" "named_test_resource" {
name = var.resource_name
resource_group_name = azurerm_resource_group.named_test_resource.name
location = azurerm_resource_group.named_test_resource.location
account_tier = "Standard"
account_replication_type = "GRS"
}

resource "azurerm_eventhub_namespace" "named_test_resource" {
name = var.resource_name
location = azurerm_resource_group.named_test_resource.location
resource_group_name = azurerm_resource_group.named_test_resource.name
sku = "Standard"
capacity = 2
}

resource "azurerm_monitor_log_profile" "named_test_resource" {
name = var.resource_name

categories = [
"Action",
"Delete",
"Write",
]

locations = [
"eastus",
"global",
]

# RootManageSharedAccessKey is created by default with listen, send, manage permissions
servicebus_rule_id = "${azurerm_eventhub_namespace.named_test_resource.id}/authorizationrules/RootManageSharedAccessKey"
storage_account_id = azurerm_storage_account.named_test_resource.id

retention_policy {
enabled = true
days = 7
}
}

output "resource_aka" {
value = "azure://${azurerm_monitor_log_profile.named_test_resource.id}"
}

output "resource_aka_lower" {
value = "azure://${lower(azurerm_monitor_log_profile.named_test_resource.id)}"
}

output "resource_name" {
value = var.resource_name
}

output "resource_id_lower" {
value = lower(azurerm_monitor_log_profile.named_test_resource.id)
}

output "storage_account_id" {
value = azurerm_storage_account.named_test_resource.id
}

output "subscription_id" {
value = var.azure_subscription
}

output "location" {
value = azurerm_resource_group.named_test_resource.location
}

1 change: 1 addition & 0 deletions azure/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ func Plugin(ctx context.Context) *plugin.Plugin {
"azure_management_lock": tableAzureManagementLock(ctx),
"azure_mariadb_server": tableAzureMariaDBServer(ctx),
"azure_monitor_activity_log_event": tableAzureMonitorActivityLogEvent(ctx),
"azure_monitor_log_profile": tableAzureMonitorLogProfile(ctx),
"azure_mssql_elasticpool": tableAzureMSSQLElasticPool(ctx),
"azure_mssql_managed_instance": tableAzureMSSQLManagedInstance(ctx),
"azure_mssql_virtual_machine": tableAzureMSSQLVirtualMachine(ctx),
Expand Down
166 changes: 166 additions & 0 deletions azure/table_azure_monitor_log_profile.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
package azure

import (
"context"

"github.com/turbot/steampipe-plugin-sdk/v5/grpc/proto"
"github.com/turbot/steampipe-plugin-sdk/v5/plugin"
"github.com/turbot/steampipe-plugin-sdk/v5/plugin/transform"

"github.com/Azure/azure-sdk-for-go/profiles/2020-09-01/monitor/mgmt/insights"
)

//// TABLE DEFINITION

func tableAzureMonitorLogProfile(_ context.Context) *plugin.Table {
return &plugin.Table{
Name: "azure_monitor_log_profile",
Description: "Azure Monitor Log Profile",
Get: &plugin.GetConfig{
KeyColumns: plugin.AllColumns([]string{"name"}),
Hydrate: getMonitorLogProfile,
IgnoreConfig: &plugin.IgnoreConfig{
ShouldIgnoreErrorFunc: isNotFoundError([]string{"ResourceNotFound", "404"}),
},
},
List: &plugin.ListConfig{
Hydrate: listMonitorLogProfiles,
},
Columns: azureColumns([]*plugin.Column{
{
Name: "id",
Description: "Azure resource Id.",
Type: proto.ColumnType_STRING,
Transform: transform.FromGo(),
},
{
Name: "name",
Description: "Azure resource name.",
Type: proto.ColumnType_STRING,
},
{
Name: "type",
Description: "Azure resource type.",
Type: proto.ColumnType_STRING,
},
{
Name: "location",
Description: "The resource location.",
Type: proto.ColumnType_STRING,
},
{
Name: "storage_account_id",
Description: "The resource id of the storage account to which you would like to send the Activity Log.",
Type: proto.ColumnType_STRING,
Transform: transform.FromField("LogProfileProperties.StorageAccountID"),
},
{
Name: "service_bus_rule_id",
Description: "The service bus rule ID of the service bus namespace in which you would like to have Event Hubs created for streaming the Activity Log.",
Type: proto.ColumnType_STRING,
Transform: transform.FromField("LogProfileProperties.ServiceBusRuleID"),
},
{
Name: "locations",
Description: "List of regions for which Activity Log events should be stored or streamed. It is a comma separated list of valid ARM locations including the 'global' location.",
Type: proto.ColumnType_JSON,
Transform: transform.FromField("LogProfileProperties.Locations"),
},
{
Name: "categories",
Description: "The categories of the logs. These categories are created as is convenient to the user.",
Type: proto.ColumnType_JSON,
Transform: transform.FromField("LogProfileProperties.Categories"),
},
{
Name: "retention_policy",
Description: "The retention policy for the events in the log.",
Type: proto.ColumnType_JSON,
Transform: transform.FromField("LogProfileProperties.RetentionPolicy"),
},

// Steampipe standard columns
{
Name: "title",
Description: ColumnDescriptionTitle,
Type: proto.ColumnType_STRING,
Transform: transform.FromField("Name"),
},
{
Name: "tags",
Description: ColumnDescriptionTags,
Type: proto.ColumnType_JSON,
},
{
Name: "akas",
Description: ColumnDescriptionAkas,
Type: proto.ColumnType_STRING,
Transform: transform.FromField("ID").Transform(idToAkas),
},
}),
}
}

//// LIST FUNCTION

func listMonitorLogProfiles(ctx context.Context, d *plugin.QueryData, _ *plugin.HydrateData) (interface{}, error) {
session, err := GetNewSession(ctx, d, "MANAGEMENT")
if err != nil {
plugin.Logger(ctx).Error("azure_monitor_log_profile.listMonitorLogProfiles", "session_error", err)
return nil, err
}
subscriptionID := session.SubscriptionID

client := insights.NewLogProfilesClientWithBaseURI(session.ResourceManagerEndpoint, subscriptionID)
client.Authorizer = session.Authorizer

// API doesn't support pagination
result, err := client.List(ctx)
if err != nil {
plugin.Logger(ctx).Error("azure_monitor_log_profile.listMonitorLogProfiles", "api_error", err)
return nil, err
}

for _, profile := range *result.Value {
d.StreamListItem(ctx, profile)

// Check if context has been cancelled or if the limit has been hit (if specified)
// if there is a limit, it will return the number of rows required to reach this limit
if d.RowsRemaining(ctx) == 0 {
return nil, nil
}
}

return nil, err
}

//// HYDRATE FUNCTIONS

func getMonitorLogProfile(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {

name := d.EqualsQuals["name"].GetStringValue()

session, err := GetNewSession(ctx, d, "MANAGEMENT")
if err != nil {
plugin.Logger(ctx).Error("azure_monitor_log_profile.getMonitorLogProfile", "session_error", err)
return nil, err
}
subscriptionID := session.SubscriptionID

client := insights.NewLogProfilesClientWithBaseURI(session.ResourceManagerEndpoint, subscriptionID)
client.Authorizer = session.Authorizer

op, err := client.Get(ctx, name)
if err != nil {
plugin.Logger(ctx).Error("azure_monitor_log_profile.getMonitorLogProfile", "api_error", err)
return nil, err
}

// In some cases resource does not give any notFound error
// instead of notFound error, it returns empty data
if op.ID != nil {
return op, nil
}

return nil, nil
}
Loading