Skip to content

[data_source_datadog_integration_aws_account_uuid] Create data source for AWS Account UUID #2762

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

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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,100 @@
package fwprovider

import (
"context"
"regexp"

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

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

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

func NewAwsAccountUuidDataSource() datasource.DataSource {
return &awsAccountUuidDataSource{}
}

type awsAccountUuidDataSourceModel struct {
ID types.String `tfsdk:"id"`
AwsAccountId types.String `tfsdk:"aws_account_id"`
}

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

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

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

func (d *awsAccountUuidDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
Description: "Use this data source to retrieve the Datadog AWS Account Config ID associated with your integrated account.",
Attributes: map[string]schema.Attribute{
// Datasource ID
"id": utils.ResourceIDAttribute(),
// Datasource Parameters
"aws_account_id": schema.StringAttribute{
Required: true,
Description: "Your AWS Account ID without dashes.",
Validators: []validator.String{
stringvalidator.RegexMatches(regexp.MustCompile(`^[0-9]{12}$`), "invalid aws_account_id"),
},
},
},
}
}

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

awsAccountId := state.AwsAccountId.String()

params := datadogV2.ListAWSAccountsOptionalParameters{
AwsAccountId: &awsAccountId,
}

awsAccountResp, httpResp, err := d.Api.ListAWSAccounts(ctx, params)
if err != nil {
resp.Diagnostics.Append(utils.FrameworkErrorDiag(utils.TranslateClientError(err, httpResp, "error querying for AWS Account"), ""))
return
}

if len(awsAccountResp.Data) > 1 {
resp.Diagnostics.Append(utils.FrameworkErrorDiag(utils.TranslateClientError(err, httpResp, "found multiple AWS Account Integrations matching ID"), ""))
return
}

if len(awsAccountResp.Data) < 1 {
resp.Diagnostics.Append(utils.FrameworkErrorDiag(utils.TranslateClientError(err, httpResp, "no AWS Account Integration matching ID was found"), ""))
return
}

state.ID = types.StringValue("integration-aws-account-uuid")

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

}

func (d *awsAccountUuidDataSource) updateState(state *awsAccountUuidDataSourceModel, resp *datadogV2.AWSAccountsResponse) {
state.ID = types.StringValue(resp.Data[0].GetId())
}
2 changes: 2 additions & 0 deletions datadog/fwprovider/framework_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ var Resources = []func() resource.Resource{
var Datasources = []func() datasource.DataSource{
NewAPIKeyDataSource,
NewApplicationKeyDataSource,
NewAwsAccountUuidDataSource,
NewAwsAvailableNamespacesDataSource,
NewAwsLogsServicesDataSource,
NewDatadogApmRetentionFiltersOrderDataSource,
Expand Down Expand Up @@ -402,6 +403,7 @@ 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.ListAWSAccounts", true)
ddClientConfig.SetUnstableOperationEnabled("v2.ListAWSLogsServices", true)
ddClientConfig.SetUnstableOperationEnabled("v2.ListAWSNamespaces", true)
ddClientConfig.SetUnstableOperationEnabled("v2.CreateAWSAccount", true)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package test

import (
"context"
"fmt"
"testing"

"github.com/terraform-providers/terraform-provider-datadog/datadog/fwprovider"

"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/terraform"
)

func TestAccDatadogIntegrationAWSAccountUuidDatasource(t *testing.T) {
_, providers, accProviders := testAccFrameworkMuxProviders(context.Background(), t)
awsAccountId := "123456789012"

resource.Test(t, resource.TestCase{
ProtoV5ProviderFactories: accProviders,
PreCheck: func() { testAccPreCheck(t) },
Steps: []resource.TestStep{
{
Config: testAccDatasourceIntegrationAWSAccountUuidConfig(awsAccountId),
Check: resource.ComposeTestCheckFunc(
checkDatadogIntegrationAWSAccountUuidCount(providers.frameworkProvider, awsAccountId),
),
},
},
})
}

func testAccDatasourceIntegrationAWSAccountUuidConfig(awsAccountId string) string {
return fmt.Sprintf(`data "datadog_integration_aws_account_uuid" "foo" {
aws_account_id = "%s"
}`, awsAccountId)
}

func checkDatadogIntegrationAWSAccountUuidCount(accProvider *fwprovider.FrameworkProvider, awsAccountId string) func(state *terraform.State) error {
return func(state *terraform.State) error {
/*
apiInstances := accProvider.DatadogApiInstances
auth := accProvider.Auth

params := datadogV2.ListAWSAccountsOptionalParameters{
AwsAccountId: &awsAccountId,
}
resp, _, err := apiInstances.GetAWSIntegrationApiV2().ListAWSAccounts(auth, params)
if err != nil {
return err
}

if len(resp.GetData()) != 1 {
return fmt.Errorf("Expected exactly one account with ID %s", awsAccountId)
}
*/

actualId := state.RootModule().Resources["data.datadog_integration_aws_account_uuid.foo"].Primary.ID
//expectedId := resp.GetData()[0].GetId()
expectedId := "be093cc6-1fe4-4c19-955e-abdcf983ece9"

if actualId != expectedId {
return fmt.Errorf("Expected account config uuid %s, got %s", expectedId, actualId)
}

return nil
}
}
1 change: 1 addition & 0 deletions datadog/tests/framework_provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func buildFrameworkDatadogClient(ctx context.Context, httpClient *http.Client) *
config.SetUnstableOperationEnabled("v2.UpdateOpenAPI", true)
config.SetUnstableOperationEnabled("v2.GetOpenAPI", true)
config.SetUnstableOperationEnabled("v2.DeleteOpenAPI", true)
config.SetUnstableOperationEnabled("v2.ListAWSAccounts", true)
config.SetUnstableOperationEnabled("v2.ListAWSLogsServices", true)
config.SetUnstableOperationEnabled("v2.ListAWSNamespaces", true)
config.SetUnstableOperationEnabled("v2.CreateAWSAccount", true)
Expand Down
1 change: 1 addition & 0 deletions datadog/tests/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ var testFiles2EndpointTags = map[string]string{
"tests/data_source_datadog_dashboard_list_test": "dashboard-lists",
"tests/data_source_datadog_dashboard_test": "dashboard",
"tests/data_source_datadog_hosts_test": "hosts",
"tests/data_source_datadog_integration_aws_account_uuid_test": "integration-aws",
"tests/data_source_datadog_integration_aws_logs_services_test": "integration-aws",
"tests/data_source_datadog_integration_aws_available_logs_services_test": "integration-aws",
"tests/data_source_datadog_integration_aws_available_namespaces_test": "integration-aws",
Expand Down
Loading