Skip to content

Commit

Permalink
[datadog_integration_aws_available_namespaces] Create AWS Integration…
Browse files Browse the repository at this point in the history
…s V2 datasources `datadog_integration_aws_available_logs_services/namespaces` (#2709)

* Create v2 data_source_datadog_integration_aws_logs_services

* Rename resource

* Fix datasource typename and set state

* Add datadog_integration_aws_available_namespaces datasource and test

* make docs

* Reference data sources from account resource

* typo

* Update docs

---------

Co-authored-by: Anika Maskara <anika.maskara@datadoghq.com>
  • Loading branch information
ktmq and amaskara-dd authored Dec 17, 2024
1 parent 993b0b2 commit 33b80ca
Show file tree
Hide file tree
Showing 16 changed files with 754 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package fwprovider

import (
"context"

"github.com/DataDog/datadog-api-client-go/v2/api/datadogV2"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"

"github.com/terraform-providers/terraform-provider-datadog/datadog/internal/utils"
)

var (
_ datasource.DataSource = &awsLogsServicesDataSource{}
)

func NewAwsLogsServicesDataSource() datasource.DataSource {
return &awsLogsServicesDataSource{}
}

type awsLogsServicesDataSourceModel struct {
ID types.String `tfsdk:"id"`
LogsServices []string `tfsdk:"aws_logs_services"`
}

type awsLogsServicesDataSource struct {
Api *datadogV2.AWSLogsIntegrationApi
Auth context.Context
}

func (r *awsLogsServicesDataSource) Configure(_ context.Context, request datasource.ConfigureRequest, response *datasource.ConfigureResponse) {
providerData, _ := request.ProviderData.(*FrameworkProvider)
r.Api = providerData.DatadogApiInstances.GetAWSLogsIntegrationApiV2()
r.Auth = providerData.Auth
}

func (d *awsLogsServicesDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = "integration_aws_available_logs_services"
}

func (d *awsLogsServicesDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
Description: "Use this data source to retrieve all AWS log ready services. This is the list of allowed values for `logs_config.lambda_forwarder.sources` in [`datadog_integration_aws_account` resource](https://registry.terraform.io/providers/DataDog/datadog/latest/docs/resources/integration_aws_account).",
Attributes: map[string]schema.Attribute{
// Datasource ID
"id": utils.ResourceIDAttribute(),
// Datasource Parameters
"aws_logs_services": schema.ListAttribute{
Description: "List of AWS log ready services.",
ElementType: types.StringType,
Computed: true,
},
},
}
}

func (d *awsLogsServicesDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var state awsLogsServicesDataSourceModel
if resp.Diagnostics.HasError() {
return
}

awsLogsServicesResp, httpResp, err := d.Api.ListAWSLogsServices(d.Auth)
if err != nil {
resp.Diagnostics.Append(utils.FrameworkErrorDiag(utils.TranslateClientError(err, httpResp, "error querying AWS Logs Services"), ""))
return
}

state.ID = types.StringValue("integration-aws-available-logs-services")

d.updateState(&state, &awsLogsServicesResp)
resp.Diagnostics.Append(resp.State.Set(ctx, &state)...)

}

func (d *awsLogsServicesDataSource) updateState(state *awsLogsServicesDataSourceModel, resp *datadogV2.AWSLogsServicesResponse) {
logsServicesDd := resp.Data.GetAttributes().LogsServices
state.LogsServices = append(state.LogsServices, logsServicesDd...)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package fwprovider

import (
"context"

"github.com/DataDog/datadog-api-client-go/v2/api/datadogV2"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"

"github.com/terraform-providers/terraform-provider-datadog/datadog/internal/utils"
)

var (
_ datasource.DataSource = &awsAvailableNamespacesDataSource{}
)

func NewAwsAvailableNamespacesDataSource() datasource.DataSource {
return &awsAvailableNamespacesDataSource{}
}

type awsAvailableNamespacesDataSourceModel struct {
ID types.String `tfsdk:"id"`
AvailableNamespaces []string `tfsdk:"aws_namespaces"`
}

type awsAvailableNamespacesDataSource struct {
Api *datadogV2.AWSIntegrationApi
Auth context.Context
}

func (r *awsAvailableNamespacesDataSource) Configure(_ context.Context, request datasource.ConfigureRequest, response *datasource.ConfigureResponse) {
providerData, _ := request.ProviderData.(*FrameworkProvider)
r.Api = providerData.DatadogApiInstances.GetAWSIntegrationApiV2()
r.Auth = providerData.Auth
}

func (d *awsAvailableNamespacesDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = "integration_aws_available_namespaces"
}

func (d *awsAvailableNamespacesDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
Description: "Use this data source to retrieve all available AWS namespaces. This is the list of allowed values for `metrics_config.namespace_filters` `include_only` or `exclude_only` in [`datadog_integration_aws_account` resource](https://registry.terraform.io/providers/DataDog/datadog/latest/docs/resources/integration_aws_account).",
Attributes: map[string]schema.Attribute{
// Datasource ID
"id": utils.ResourceIDAttribute(),
// Datasource Parameters
"aws_namespaces": schema.ListAttribute{
Description: "List of available AWS namespaces.",
ElementType: types.StringType,
Computed: true,
},
},
}
}

func (d *awsAvailableNamespacesDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var state awsAvailableNamespacesDataSourceModel
if resp.Diagnostics.HasError() {
return
}

awsAvailableNamespacesResp, httpResp, err := d.Api.ListAWSNamespaces(d.Auth)
if err != nil {
resp.Diagnostics.Append(utils.FrameworkErrorDiag(utils.TranslateClientError(err, httpResp, "error querying AWS Namespaces"), ""))
return
}

state.ID = types.StringValue("integration-aws-available-namespaces")

d.updateState(&state, &awsAvailableNamespacesResp)
resp.Diagnostics.Append(resp.State.Set(ctx, &state)...)

}

func (d *awsAvailableNamespacesDataSource) updateState(state *awsAvailableNamespacesDataSourceModel, resp *datadogV2.AWSNamespacesResponse) {
namespacesDd := resp.Data.GetAttributes().Namespaces
state.AvailableNamespaces = append(state.AvailableNamespaces, namespacesDd...)
}
4 changes: 4 additions & 0 deletions datadog/fwprovider/framework_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ var Resources = []func() resource.Resource{
var Datasources = []func() datasource.DataSource{
NewAPIKeyDataSource,
NewApplicationKeyDataSource,
NewAwsAvailableNamespacesDataSource,
NewAwsLogsServicesDataSource,
NewDatadogApmRetentionFiltersOrderDataSource,
NewDatadogDashboardListDataSource,
NewDatadogIntegrationAWSNamespaceRulesDatasource,
Expand Down Expand Up @@ -399,6 +401,8 @@ func defaultConfigureFunc(p *FrameworkProvider, request *provider.ConfigureReque
ddClientConfig.SetUnstableOperationEnabled("v2.UpdateOpenAPI", true)
ddClientConfig.SetUnstableOperationEnabled("v2.GetOpenAPI", true)
ddClientConfig.SetUnstableOperationEnabled("v2.DeleteOpenAPI", true)
ddClientConfig.SetUnstableOperationEnabled("v2.ListAWSLogsServices", true)
ddClientConfig.SetUnstableOperationEnabled("v2.ListAWSNamespaces", true)
ddClientConfig.SetUnstableOperationEnabled("v2.CreateAWSAccount", true)
ddClientConfig.SetUnstableOperationEnabled("v2.UpdateAWSAccount", true)
ddClientConfig.SetUnstableOperationEnabled("v2.DeleteAWSAccount", true)
Expand Down
22 changes: 14 additions & 8 deletions datadog/fwprovider/resource_datadog_integration_aws_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,9 +289,9 @@ func (r *integrationAwsAccountResource) Schema(_ context.Context, _ resource.Sch
"sources": schema.ListAttribute{
Optional: true,
Computed: true,
Description: "List of service IDs set to enable automatic log collection. " +
"Discover the list of available services with the " +
"[Get list of AWS log ready services](https://docs.datadoghq.com/api/latest/aws-logs-integration/#get-list-of-aws-log-ready-services) endpoint.",
Description: "List of service IDs set to enable automatic log collection. Use " +
"[`datadog_integration_aws_available_logs_services` data source](https://registry.terraform.io/providers/DataDog/datadog/latest/docs/data-sources/integration_aws_available_logs_services) " +
"to get allowed values.",
ElementType: types.StringType,
Default: listdefault.StaticValue(types.ListValueMust(types.StringType, []attr.Value{})),
},
Expand Down Expand Up @@ -353,9 +353,13 @@ func (r *integrationAwsAccountResource) Schema(_ context.Context, _ resource.Sch
"namespace_filters": schema.SingleNestedBlock{
Attributes: map[string]schema.Attribute{
"exclude_only": schema.ListAttribute{
Optional: true,
Computed: true,
Description: "Exclude only these namespaces from metrics collection. Defaults to `[\"AWS/SQS\", \"AWS/ElasticMapReduce\"]`. `AWS/SQS` and `AWS/ElasticMapReduce` are excluded by default to reduce your AWS CloudWatch costs from `GetMetricData` API calls.",
Optional: true,
Computed: true,
Description: "Exclude only these namespaces from metrics collection. Use " +
"[`datadog_integration_aws_available_namespaces` data source](https://registry.terraform.io/providers/DataDog/datadog/latest/docs/data-sources/integration_aws_available_namespaces) " +
"to get allowed values. Defaults to `[\"AWS/SQS\", \"AWS/ElasticMapReduce\"]`. " +
"`AWS/SQS` and `AWS/ElasticMapReduce` are excluded by default to reduce your AWS " +
"CloudWatch costs from `GetMetricData` API calls.",
ElementType: types.StringType,
Default: listdefault.StaticValue(types.ListValueMust(
types.StringType, []attr.Value{
Expand All @@ -365,8 +369,10 @@ func (r *integrationAwsAccountResource) Schema(_ context.Context, _ resource.Sch
),
},
"include_only": schema.ListAttribute{
Optional: true,
Description: "Include only these namespaces for metrics collection.",
Optional: true,
Description: "Include only these namespaces for metrics collection. Use " +
"[`datadog_integration_aws_available_namespaces` data source](https://registry.terraform.io/providers/DataDog/datadog/latest/docs/data-sources/integration_aws_available_namespaces) " +
"to get allowed values.",
ElementType: types.StringType,
},
},
Expand Down
9 changes: 9 additions & 0 deletions datadog/internal/utils/api_instances_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ type ApiInstances struct {
auditApiV2 *datadogV2.AuditApi
authNMappingsApiV2 *datadogV2.AuthNMappingsApi
awsIntegrationApiV2 *datadogV2.AWSIntegrationApi
awsLogsIntegrationApiV2 *datadogV2.AWSLogsIntegrationApi
cloudflareIntegrationApiV2 *datadogV2.CloudflareIntegrationApi
csmThreatsApiV2 *datadogV2.CSMThreatsApi
confluentCloudApiV2 *datadogV2.ConfluentCloudApi
Expand Down Expand Up @@ -120,6 +121,14 @@ func (i *ApiInstances) GetAWSLogsIntegrationApiV1() *datadogV1.AWSLogsIntegratio
return i.awsLogsIntegrationApiV1
}

// GetAWSLogsIntegrationApiV2 get instance of AwsLogsIntegrationApi
func (i *ApiInstances) GetAWSLogsIntegrationApiV2() *datadogV2.AWSLogsIntegrationApi {
if i.awsLogsIntegrationApiV2 == nil {
i.awsLogsIntegrationApiV2 = datadogV2.NewAWSLogsIntegrationApi(i.HttpClient)
}
return i.awsLogsIntegrationApiV2
}

// GetAzureIntegrationApiV1 get instance of AzureIntegrationApi
func (i *ApiInstances) GetAzureIntegrationApiV1() *datadogV1.AzureIntegrationApi {
if i.azureIntegrationApiV1 == nil {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2024-12-09T10:54:13.353546-05:00
Loading

0 comments on commit 33b80ca

Please sign in to comment.