Skip to content

Commit

Permalink
azurerm_monitor_diagnostic_setting: adding support for `category_gr…
Browse files Browse the repository at this point in the history
…oup` (#16367)

`azurerm_monitor_diagnostic_setting`: adding support for `category_group`
  • Loading branch information
xiaxyi authored Aug 30, 2022
1 parent 2c805cf commit b85c257
Show file tree
Hide file tree
Showing 32 changed files with 1,322 additions and 202 deletions.
14 changes: 6 additions & 8 deletions internal/services/monitor/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
newActionGroupClient "github.com/Azure/azure-sdk-for-go/services/preview/monitor/mgmt/2021-09-01-preview/insights"
"github.com/hashicorp/go-azure-sdk/resource-manager/insights/2021-04-01/datacollectionendpoints"
"github.com/hashicorp/go-azure-sdk/resource-manager/insights/2021-04-01/datacollectionrules"
diagnosticSettingClient "github.com/hashicorp/go-azure-sdk/resource-manager/insights/2021-05-01-preview/diagnosticsettings"
diagnosticCategoryClient "github.com/hashicorp/go-azure-sdk/resource-manager/insights/2021-05-01-preview/diagnosticsettingscategories"
"github.com/hashicorp/go-azure-sdk/resource-manager/insights/2021-08-01/scheduledqueryrules"
"github.com/hashicorp/terraform-provider-azurerm/internal/common"
)
Expand All @@ -29,8 +31,8 @@ type Client struct {
AlertRulesClient *classic.AlertRulesClient
DataCollectionRulesClient *datacollectionrules.DataCollectionRulesClient
DataCollectionEndpointsClient *datacollectionendpoints.DataCollectionEndpointsClient
DiagnosticSettingsClient *classic.DiagnosticSettingsClient
DiagnosticSettingsCategoryClient *classic.DiagnosticSettingsCategoryClient
DiagnosticSettingsClient *diagnosticSettingClient.DiagnosticSettingsClient
DiagnosticSettingsCategoryClient *diagnosticCategoryClient.DiagnosticSettingsCategoriesClient
LogProfilesClient *classic.LogProfilesClient
MetricAlertsClient *classic.MetricAlertsClient
PrivateLinkScopesClient *classic.PrivateLinkScopesClient
Expand Down Expand Up @@ -61,16 +63,13 @@ func NewClient(o *common.ClientOptions) *Client {
AlertRulesClient := classic.NewAlertRulesClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&AlertRulesClient.Client, o.ResourceManagerAuthorizer)

DataCollectionEndpointsClient := datacollectionendpoints.NewDataCollectionEndpointsClientWithBaseURI(o.ResourceManagerEndpoint)
o.ConfigureClient(&DataCollectionEndpointsClient.Client, o.ResourceManagerAuthorizer)

DataCollectionRulesClient := datacollectionrules.NewDataCollectionRulesClientWithBaseURI(o.ResourceManagerEndpoint)
o.ConfigureClient(&DataCollectionRulesClient.Client, o.ResourceManagerAuthorizer)

DiagnosticSettingsClient := classic.NewDiagnosticSettingsClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
DiagnosticSettingsClient := diagnosticSettingClient.NewDiagnosticSettingsClientWithBaseURI(o.ResourceManagerEndpoint)
o.ConfigureClient(&DiagnosticSettingsClient.Client, o.ResourceManagerAuthorizer)

DiagnosticSettingsCategoryClient := classic.NewDiagnosticSettingsCategoryClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
DiagnosticSettingsCategoryClient := diagnosticCategoryClient.NewDiagnosticSettingsCategoriesClientWithBaseURI(o.ResourceManagerEndpoint)
o.ConfigureClient(&DiagnosticSettingsCategoryClient.Client, o.ResourceManagerAuthorizer)

LogProfilesClient := classic.NewLogProfilesClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
Expand Down Expand Up @@ -100,7 +99,6 @@ func NewClient(o *common.ClientOptions) *Client {
ActivityLogAlertsClient: &ActivityLogAlertsClient,
AlertRulesClient: &AlertRulesClient,
DataCollectionRulesClient: &DataCollectionRulesClient,
DataCollectionEndpointsClient: &DataCollectionEndpointsClient,
DiagnosticSettingsClient: &DiagnosticSettingsClient,
DiagnosticSettingsCategoryClient: &DiagnosticSettingsCategoryClient,
LogProfilesClient: &LogProfilesClient,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@ import (
"strings"
"time"

"github.com/Azure/azure-sdk-for-go/services/preview/monitor/mgmt/2021-07-01-preview/insights"
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonids"
"github.com/hashicorp/go-azure-sdk/resource-manager/insights/2021-05-01-preview/diagnosticsettingscategories"
"github.com/hashicorp/terraform-provider-azurerm/helpers/azure"
"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/timeouts"
)

func dataSourceMonitorDiagnosticCategories() *pluginsdk.Resource {
return &pluginsdk.Resource{
resource := &pluginsdk.Resource{
Read: dataSourceMonitorDiagnosticCategoriesRead,

Timeouts: &pluginsdk.ResourceTimeout{
Expand All @@ -27,7 +29,14 @@ func dataSourceMonitorDiagnosticCategories() *pluginsdk.Resource {
ValidateFunc: azure.ValidateResourceID,
},

"logs": {
"log_category_types": {
Type: pluginsdk.TypeSet,
Elem: &pluginsdk.Schema{Type: pluginsdk.TypeString},
Set: pluginsdk.HashString,
Computed: true,
},

"log_category_groups": {
Type: pluginsdk.TypeSet,
Elem: &pluginsdk.Schema{Type: pluginsdk.TypeString},
Set: pluginsdk.HashString,
Expand All @@ -42,57 +51,90 @@ func dataSourceMonitorDiagnosticCategories() *pluginsdk.Resource {
},
},
}

if !features.FourPointOhBeta() {
resource.Schema["logs"] = &pluginsdk.Schema{
Type: pluginsdk.TypeSet,
Elem: &pluginsdk.Schema{Type: pluginsdk.TypeString},
Set: pluginsdk.HashString,
Computed: true,
Deprecated: "`logs` will be removed in favour of the property `log_category_types` in version 4.0 of the AzureRM Provider.",
}
}

return resource
}

func dataSourceMonitorDiagnosticCategoriesRead(d *pluginsdk.ResourceData, meta interface{}) error {
categoriesClient := meta.(*clients.Client).Monitor.DiagnosticSettingsCategoryClient
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()

actualResourceId := d.Get("resource_id").(string)
actualResourceId := commonids.NewScopeID(d.Get("resource_id").(string))
// trim off the leading `/` since the CheckExistenceByID / List methods don't expect it
resourceId := strings.TrimPrefix(actualResourceId, "/")
resourceId := strings.TrimPrefix(actualResourceId.Scope, "/")
resourceIdToList, err := commonids.ParseScopeID(resourceId)
if err != nil {
return fmt.Errorf("parsing resource id error: %+v", err)
}

// then retrieve the possible Diagnostics Categories for this Resource
categories, err := categoriesClient.List(ctx, resourceId)
categories, err := categoriesClient.DiagnosticSettingsCategoryList(ctx, *resourceIdToList)
if err != nil {
return fmt.Errorf("retrieving Diagnostics Categories for Resource %q: %+v", actualResourceId, err)
}

if categories.Value == nil {
if categories.Model == nil && categories.Model.Value == nil {
return fmt.Errorf("retrieving Diagnostics Categories for Resource %q: `categories.Value` was nil", actualResourceId)
}

d.SetId(actualResourceId)
val := *categories.Value
d.SetId(actualResourceId.ID())
val := *categories.Model.Value

metrics := make([]string, 0)
logs := make([]string, 0)
categoryGroups := make([]string, 0)

for _, v := range val {
if v.Name == nil {
continue
}

if category := v.DiagnosticSettingsCategory; category != nil {
switch category.CategoryType {
case insights.CategoryTypeLogs:
logs = append(logs, *v.Name)
case insights.CategoryTypeMetrics:
metrics = append(metrics, *v.Name)
default:
return fmt.Errorf("Unsupported category type %q", string(category.CategoryType))
if category := v.Properties; category != nil {
if category.CategoryGroups != nil {
for _, item := range *category.CategoryGroups {
categoryGroups = append(categoryGroups, item)
}
}
if category.CategoryType != nil {
switch *category.CategoryType {
case diagnosticsettingscategories.CategoryTypeLogs:
logs = append(logs, *v.Name)
case diagnosticsettingscategories.CategoryTypeMetrics:
metrics = append(metrics, *v.Name)
default:
return fmt.Errorf("Unsupported category type %q", string(*category.CategoryType))
}
}
}
}

if err := d.Set("logs", logs); err != nil {
return fmt.Errorf("setting `logs`: %+v", err)
if err := d.Set("log_category_types", logs); err != nil {
return fmt.Errorf("setting `log_category_types`: %+v", err)
}

if !features.FourPointOhBeta() {
if err := d.Set("logs", logs); err != nil {
return fmt.Errorf("setting `log`: %+v", err)
}
}

if err := d.Set("metrics", metrics); err != nil {
return fmt.Errorf("setting `metrics`: %+v", err)
}

if err := d.Set("log_category_groups", categoryGroups); err != nil {
return fmt.Errorf("setting `log_category_groups`: %+v", err)
}
return nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ func TestAccDataSourceMonitorDiagnosticCategories_appService(t *testing.T) {
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).Key("metrics.#").Exists(),
check.That(data.ResourceName).Key("logs.#").Exists(),
check.That(data.ResourceName).Key("log_category_types.#").Exists(),
check.That(data.ResourceName).Key("log_category_groups.#").Exists(),
),
},
})
Expand All @@ -35,6 +37,8 @@ func TestAccDataSourceMonitorDiagnosticCategories_storageAccount(t *testing.T) {
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).Key("metrics.#").Exists(),
check.That(data.ResourceName).Key("logs.#").Exists(),
check.That(data.ResourceName).Key("log_category_types.#").Exists(),
check.That(data.ResourceName).Key("log_category_groups.#").Exists(),
),
},
})
Expand Down
Loading

0 comments on commit b85c257

Please sign in to comment.