diff --git a/cdpacctest/acctest.go b/cdpacctest/acctest.go index 6a016bc1..768c0085 100644 --- a/cdpacctest/acctest.go +++ b/cdpacctest/acctest.go @@ -11,7 +11,11 @@ package cdpacctest import ( + "encoding/base64" "fmt" + environmentoperations "github.com/cloudera/terraform-provider-cdp/cdp-sdk-go/gen/environments/client/operations" + environmentsmodels "github.com/cloudera/terraform-provider-cdp/cdp-sdk-go/gen/environments/models" + "github.com/stretchr/testify/assert" "math/rand" "os" "regexp" @@ -50,6 +54,11 @@ var ( VersionConstraint: "~> 3.4", }, } + TimeExternalProvider = map[string]resource.ExternalProvider{ + "time": { + Source: "hashicorp/time", + }, + } cdpClientOnce sync.Once cdpClient *cdp.Client @@ -95,11 +104,25 @@ provider "cdp" { ` } -func TestAccAwsProviderConfig() string { - return ` -provider "aws" { +type awsProvider struct { + Profile string + Region string } -` + +func NewAwsProvider(profile, region string) awsProvider { + return awsProvider{ + Profile: profile, + Region: region, + } +} + +func TestAccAwsProviderConfig(p awsProvider) string { + return fmt.Sprintf(` + provider "aws" { + profile = %[1]q + region = %[2]q + } +`, p.Profile, p.Region) } // CheckCrn Checks whether the value is set and is a properly formatted CRN @@ -126,3 +149,97 @@ func GetCdpClientForAccTest() *cdp.Client { }) return cdpClient } + +type awsAccountCredentials struct { + Name string + AccountID string + ExternalID string + DefaultPolicy string +} + +func NewAwsAccountCredentials(name string) awsAccountCredentials { + return awsAccountCredentials{ + Name: name, + } +} + +func getEnvironmentPrerequisites(t *testing.T, cloudPlatform string) *environmentsmodels.GetCredentialPrerequisitesResponse { + params := environmentoperations.NewGetCredentialPrerequisitesParams() + params.WithInput(&environmentsmodels.GetCredentialPrerequisitesRequest{ + CloudPlatform: &cloudPlatform, + }) + client := GetCdpClientForAccTest() + response, err := client.Environments.Operations.GetCredentialPrerequisites(params) + assert.Nil(t, err) + payload := response.GetPayload() + assert.NotNil(t, payload) + return payload +} + +func (a *awsAccountCredentials) WithPolicy(t *testing.T) { + payload := getEnvironmentPrerequisites(t, "AWS") + assert.NotNil(t, payload) + decodedBytes, err := base64.StdEncoding.DecodeString(*payload.Aws.PolicyJSON) + assert.Nil(t, err) + a.DefaultPolicy = string(decodedBytes) +} + +func (a *awsAccountCredentials) WithExternalID(t *testing.T) { + payload := getEnvironmentPrerequisites(t, "AWS") + assert.NotNil(t, payload) + a.ExternalID = *payload.Aws.ExternalID +} + +func (a *awsAccountCredentials) WithAccountID(t *testing.T) { + payload := getEnvironmentPrerequisites(t, "AWS") + assert.NotNil(t, payload) + a.AccountID = payload.AccountID +} + +func CreateDefaultRoleAndPolicy(p awsAccountCredentials) string { + return fmt.Sprintf(` + resource "aws_iam_role" "cdp_test_role" { + name = "%[1]s-role" + + assume_role_policy = <