-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add role and policy creation to dw acceptance test
Use the default role to create an environment and DW cluster. The AWS policy is queried via CLI.
- Loading branch information
Showing
2 changed files
with
148 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 = <<EOF | ||
{ | ||
"Version": "2012-10-17", | ||
"Statement": [ | ||
{ | ||
"Sid": "Statement1", | ||
"Effect": "Allow", | ||
"Principal": { | ||
"AWS": "arn:aws:iam::%[2]s:root" | ||
}, | ||
"Action": "sts:AssumeRole", | ||
"Condition": { | ||
"StringEquals": { | ||
"sts:ExternalId": %[3]q | ||
} | ||
} | ||
} | ||
] | ||
} | ||
EOF | ||
tags = { | ||
owner = "[email protected]" | ||
} | ||
} | ||
resource "aws_iam_policy" "cdp_test_policy" { | ||
name = "%[1]s-policy" | ||
description = "DefaultCBPolicy for CDP, replace the static file with a CLI call" | ||
policy = <<EOF | ||
%[4]s | ||
EOF | ||
} | ||
resource "aws_iam_policy_attachment" "test-attach" { | ||
name = "test_attachment" | ||
roles = [aws_iam_role.cdp_test_role.name] | ||
policy_arn = aws_iam_policy.cdp_test_policy.arn | ||
} | ||
`, p.Name, p.AccountID, p.ExternalID, p.DefaultPolicy) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters