Skip to content

Commit

Permalink
working data source test wow
Browse files Browse the repository at this point in the history
  • Loading branch information
embeaken committed Jan 24, 2025
1 parent fa8ca6b commit cc64a2e
Showing 1 changed file with 45 additions and 14 deletions.
59 changes: 45 additions & 14 deletions datadog/tests/data_source_datadog_connection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,47 +6,78 @@ import (
"testing"

"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/terraform"
"github.com/terraform-providers/terraform-provider-datadog/datadog/fwprovider"
)

func TestConns(t *testing.T) {
var (
testAWSAccountID = "123456789012"
testAWSRole = "role"
)

func TestAccDatadogConnectionDatasource_AWS_AssumeRole(t *testing.T) {
t.Parallel()

ctx, _, accProviders := testAccFrameworkMuxProviders(context.Background(), t)
ctx, providers, accProviders := testAccFrameworkMuxProviders(context.Background(), t)

connectionID := uniqueEntityName(ctx, t)
connectionName := uniqueEntityName(ctx, t)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProtoV5ProviderFactories: accProviders,
CheckDestroy: testAccCheckDatadogConnectionDestroy(providers.frameworkProvider),
Steps: []resource.TestStep{
{
Config: testConnectionDataSourceConfig(connectionID),
Config: testConnectionDataSourceConfig(connectionName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("datadog_connection.conn", "name", "test"),
resource.TestCheckResourceAttr("datadog_connection.conn", "name", connectionName),
resource.TestCheckResourceAttr("datadog_connection.conn", "aws.assume_role.account_id", testAWSAccountID),
resource.TestCheckResourceAttr("datadog_connection.conn", "aws.assume_role.role", testAWSRole),
resource.TestCheckResourceAttrSet("datadog_connection.conn", "aws.assume_role.principal_id"),
resource.TestCheckResourceAttrSet("datadog_connection.conn", "aws.assume_role.external_id"),
),
},
},
})
}

func testConnectionDataSourceConfig(uniq string) string {
func testConnectionDataSourceConfig(name string) string {
return fmt.Sprintf(`
%s
data "datadog_connection" "conn" {
id = "%s"
}`, testConnectionResourceConfig(), uniq)
id = datadog_connection.conn.id
depends_on = [datadog_connection.conn]
}`, testConnectionResourceConfig(name))
}

func testConnectionResourceConfig() string {
return `
func testConnectionResourceConfig(name string) string {
return fmt.Sprintf(`
resource "datadog_connection" "conn" {
name = "test"
name = "%s"
aws {
assume_role {
account_id = "123"
role = "role"
account_id = "%s"
role = "%s"
}
}
}`
}`, name, testAWSAccountID, testAWSRole)
}

func testAccCheckDatadogConnectionDestroy(accProvider *fwprovider.FrameworkProvider) func(*terraform.State) error {
return func(s *terraform.State) error {
apiInstances := accProvider.DatadogApiInstances
auth := accProvider.Auth

resource := s.RootModule().Resources["datadog_connection.conn"]
_, httpRes, err := apiInstances.GetActionConnectionApiV2().GetActionConnection(auth, resource.Primary.ID)
if err != nil {
if httpRes.StatusCode == 404 {
return nil
}
return err
}

return fmt.Errorf("connection destroy check failed")
}
}

0 comments on commit cc64a2e

Please sign in to comment.