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

[AZINTS-2487] Update Azure integration terraform to support metric filitering #2738

Merged
merged 17 commits into from
Jan 6, 2025
Merged
85 changes: 74 additions & 11 deletions datadog/fwprovider/resource_datadog_azure_integration.go
Copy link
Contributor

Choose a reason for hiding this comment

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

do we want to add any additional validation about the ResourceProviderConfigs within terraform? or leave it all up to the API? im thinking probably the latter but wanna make sure we've considered it

Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"sync"

"github.com/DataDog/datadog-api-client-go/v2/api/datadogV1"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/diag"
frameworkPath "github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
Expand All @@ -29,18 +30,27 @@ type integrationAzureResource struct {
Auth context.Context
}

type ResourceProviderConfigModel struct {
Namespace types.String `tfsdk:"namespace"`
MetricsEnabled types.Bool `tfsdk:"metrics_enabled"`
}

type integrationAzureModel struct {
ID types.String `tfsdk:"id"`
AppServicePlanFilters types.String `tfsdk:"app_service_plan_filters"`
Automute types.Bool `tfsdk:"automute"`
ClientId types.String `tfsdk:"client_id"`
ClientSecret types.String `tfsdk:"client_secret"`
ContainerAppFilters types.String `tfsdk:"container_app_filters"`
ResourceCollectionEnabled types.Bool `tfsdk:"resource_collection_enabled"`
CspmEnabled types.Bool `tfsdk:"cspm_enabled"`
CustomMetricsEnabled types.Bool `tfsdk:"custom_metrics_enabled"`
HostFilters types.String `tfsdk:"host_filters"`
TenantName types.String `tfsdk:"tenant_name"`
ID types.String `tfsdk:"id"`
AppServicePlanFilters types.String `tfsdk:"app_service_plan_filters"`
Automute types.Bool `tfsdk:"automute"`
ClientId types.String `tfsdk:"client_id"`
ClientSecret types.String `tfsdk:"client_secret"`
ContainerAppFilters types.String `tfsdk:"container_app_filters"`
ResourceCollectionEnabled types.Bool `tfsdk:"resource_collection_enabled"`
CspmEnabled types.Bool `tfsdk:"cspm_enabled"`
CustomMetricsEnabled types.Bool `tfsdk:"custom_metrics_enabled"`
HostFilters types.String `tfsdk:"host_filters"`
TenantName types.String `tfsdk:"tenant_name"`
MetricsEnabled types.Bool `tfsdk:"metrics_enabled"`
MetricsEnabledDefault types.Bool `tfsdk:"metrics_enabled_default"`
UsageMetricsEnabled types.Bool `tfsdk:"usage_metrics_enabled"`
ResourceProviderConfigs []*ResourceProviderConfigModel `tfsdk:"resource_provider_configs"`
}

func NewIntegrationAzureResource() resource.Resource {
Expand Down Expand Up @@ -115,6 +125,35 @@ func (r *integrationAzureResource) Schema(_ context.Context, _ resource.SchemaRe
Description: "This comma-separated list of tags (in the form `key:value,key:value`) defines a filter that Datadog uses when collecting metrics from Azure App Service Plans. Only App Service Plans that match one of the defined tags are imported into Datadog. The rest, including the apps and functions running on them, are ignored. This also filters the metrics for any App or Function running on the App Service Plan(s).",
Default: stringdefault.StaticString(""),
},
"metrics_enabled": schema.BoolAttribute{
Computed: true,
Default: booldefault.StaticBool(true),
Optional: true,
Description: "Enable Azure metrics for your organization.",
},
"metrics_enabled_default": schema.BoolAttribute{
Computed: true,
Default: booldefault.StaticBool(true),
Optional: true,
Description: "Enable Azure metrics for your organization for resource providers where no resource provider config is specified.",
},
"usage_metrics_enabled": schema.BoolAttribute{
Computed: true,
Default: booldefault.StaticBool(true),
Optional: true,
Description: "Enable azure.usage metrics for your organization.",
},
"resource_provider_configs": schema.ListAttribute{
Computed: true,
Optional: true,
Description: "Configuration settings applied to resources from the specified Azure resource providers.",
ElementType: types.ObjectType{
AttrTypes: map[string]attr.Type{
"namespace": types.StringType,
"metrics_enabled": types.BoolType,
},
},
},
"id": utils.ResourceIDAttribute(),
},
}
Expand Down Expand Up @@ -257,6 +296,18 @@ func (r *integrationAzureResource) updateState(ctx context.Context, state *integ
state.ResourceCollectionEnabled = types.BoolValue(account.GetResourceCollectionEnabled())
state.CspmEnabled = types.BoolValue(account.GetCspmEnabled())
state.CustomMetricsEnabled = types.BoolValue(account.GetCustomMetricsEnabled())
state.MetricsEnabled = types.BoolValue(account.GetMetricsEnabled())
state.MetricsEnabledDefault = types.BoolValue(account.GetMetricsEnabledDefault())
state.UsageMetricsEnabled = types.BoolValue(account.GetUsageMetricsEnabled())

resourceProviderConfigs := account.GetResourceProviderConfigs()
state.ResourceProviderConfigs = make([]*ResourceProviderConfigModel, len(resourceProviderConfigs))
for i, resourceProviderConfig := range resourceProviderConfigs {
state.ResourceProviderConfigs[i] = &ResourceProviderConfigModel{
Namespace: types.StringValue(resourceProviderConfig.GetNamespace()),
MetricsEnabled: types.BoolValue(resourceProviderConfig.GetMetricsEnabled()),
}
}
Comment on lines +304 to +310
Copy link
Contributor

Choose a reason for hiding this comment

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

go best practices would be to do this as:

Suggested change
state.ResourceProviderConfigs = make([]*ResourceProviderConfigModel, len(resourceProviderConfigs))
for i, resourceProviderConfig := range resourceProviderConfigs {
state.ResourceProviderConfigs[i] = &ResourceProviderConfigModel{
Namespace: types.StringValue(resourceProviderConfig.GetNamespace()),
MetricsEnabled: types.BoolValue(resourceProviderConfig.GetMetricsEnabled()),
}
}
state.ResourceProviderConfigs = make([]*ResourceProviderConfigModel, 0, len(resourceProviderConfigs))
for i, resourceProviderConfig := range resourceProviderConfigs {
state.ResourceProviderConfigs = append(state.ResourceProviderConfigs, &ResourceProviderConfigModel{
Namespace: types.StringValue(resourceProviderConfig.GetNamespace()),
MetricsEnabled: types.BoolValue(resourceProviderConfig.GetMetricsEnabled()),
}
}


hostFilters, exists := account.GetHostFiltersOk()
if exists {
Expand Down Expand Up @@ -316,6 +367,18 @@ func (r *integrationAzureResource) buildIntegrationAzureRequestBody(ctx context.
}
datadogDefinition.SetCspmEnabled(state.CspmEnabled.ValueBool())
datadogDefinition.SetCustomMetricsEnabled(state.CustomMetricsEnabled.ValueBool())
datadogDefinition.SetMetricsEnabled(state.MetricsEnabled.ValueBool())
datadogDefinition.SetMetricsEnabledDefault(state.MetricsEnabledDefault.ValueBool())
datadogDefinition.SetUsageMetricsEnabled(state.UsageMetricsEnabled.ValueBool())

resourceProviderConfigsPayload := make([]datadogV1.ResourceProviderConfig, len(state.ResourceProviderConfigs))
Copy link
Contributor

Choose a reason for hiding this comment

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

same comment here as above

for _, resourceProviderConfig := range state.ResourceProviderConfigs {
resourceProviderConfigsPayload = append(resourceProviderConfigsPayload, datadogV1.ResourceProviderConfig{
Namespace: resourceProviderConfig.Namespace.ValueStringPointer(),
MetricsEnabled: resourceProviderConfig.MetricsEnabled.ValueBoolPointer(),
})
}
datadogDefinition.SetResourceProviderConfigs(resourceProviderConfigsPayload)

if !state.ClientSecret.IsNull() {
datadogDefinition.SetClientSecret(state.ClientSecret.ValueString())
Expand Down
12 changes: 12 additions & 0 deletions docs/resources/integration_azure.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,24 @@ resource "datadog_integration_azure" "sandbox" {
Note: This requires `resource_collection_enabled` to be set to true. Defaults to `false`.
- `custom_metrics_enabled` (Boolean) Enable custom metrics for your organization. Defaults to `false`.
- `host_filters` (String) String of host tag(s) (in the form `key:value,key:value`) defines a filter that Datadog will use when collecting metrics from Azure. Limit the Azure instances that are pulled into Datadog by using tags. Only hosts that match one of the defined tags are imported into Datadog. e.x. `env:production,deploymentgroup:red` Defaults to `""`.
- `metrics_enabled` (Boolean) Enable Azure metrics for your organization. Defaults to `true`.
- `metrics_enabled_default` (Boolean) Enable Azure metrics for your organization for resource providers where no resource provider config is specified. Defaults to `true`.
- `resource_collection_enabled` (Boolean) When enabled, Datadog collects metadata and configuration info from cloud resources (such as compute instances, databases, and load balancers) monitored by this app registration.
- `resource_provider_configs` (List of Object) Configuration settings applied to resources from the specified Azure resource providers. (see [below for nested schema](#nestedatt--resource_provider_configs))
- `usage_metrics_enabled` (Boolean) Enable azure.usage metrics for your organization. Defaults to `true`.

### Read-Only

- `id` (String) The ID of this resource.

<a id="nestedatt--resource_provider_configs"></a>
### Nested Schema for `resource_provider_configs`

Optional:
Copy link
Contributor

Choose a reason for hiding this comment

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

should these be optional? and could we add an example usage to the example terraform?


- `metrics_enabled` (Boolean)
- `namespace` (String)

## Import

Import is supported using the following syntax:
Expand Down
Loading