Skip to content

Commit

Permalink
chore: Updates federated_database_instance resource with new SDK (#1854)
Browse files Browse the repository at this point in the history
* remove NonEmptyToPtr

* PreCheckS3Bucket and remove SkipTestExtCred to run all tests

* change test name so they're run in GH

* pass s3 bucket variable to GH action

* rename resource methods

* pass aws credentials to GH action

* add aws_account_id

* assume role to access s3 bucket

* make all Optional attributes Computed so there are not unexpected plan changes

* no assume role

* pass AWS region

* change role name

* rename test helper methods

* rename federation roles

* create mig test

* configure mig tests

* fix migration config
  • Loading branch information
lantoli authored Jan 19, 2024
1 parent 12e57cc commit 831710a
Show file tree
Hide file tree
Showing 18 changed files with 234 additions and 119 deletions.
12 changes: 9 additions & 3 deletions .github/workflows/acceptance-tests-runner.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ on:
aws_secret_name:
type: string
required: true
aws_region_federation:
type: string
required: true
secrets: # all secrets are passed explicitly in this workflow
mongodb_atlas_public_key:
required: true
Expand All @@ -72,8 +75,8 @@ on:
required: true
aws_secret_access_key:
required: true

aws_s3_bucket_federation:
required: true

env:
# CI: true # GH does this automatically
Expand Down Expand Up @@ -428,7 +431,6 @@ jobs:
AWS_SECURITY_GROUP_2: ${{ vars.AWS_SECURITY_GROUP_2 }}
AWS_VPC_CIDR_BLOCK: ${{ vars.AWS_VPC_CIDR_BLOCK }}
AWS_VPC_ID: ${{ vars.AWS_VPC_ID }}

TEST_REGEX: "^TestAccProject"
run: make testacc
serverless:
Expand Down Expand Up @@ -504,6 +506,10 @@ jobs:
terraform_wrapper: false
- name: Acceptance Tests
env:
AWS_S3_BUCKET: ${{ secrets.aws_s3_bucket_federation }}
AWS_REGION: ${{ vars.aws_region_federation }}
AWS_ACCESS_KEY_ID: ${{ secrets.aws_access_key_id }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.aws_secret_access_key }}
MONGODB_ATLAS_PUBLIC_KEY: ${{ secrets.mongodb_atlas_public_key }}
MONGODB_ATLAS_PRIVATE_KEY: ${{ secrets.mongodb_atlas_private_key }}
MONGODB_ATLAS_ORG_ID: ${{ inputs.mongodb_atlas_org_id }}
Expand Down
4 changes: 3 additions & 1 deletion .github/workflows/acceptance-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,13 @@ jobs:
aws_account_id: ${{ secrets.AWS_ACCOUNT_ID }}
aws_access_key_id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws_secret_access_key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws_s3_bucket_federation: ${{ secrets.AWS_S3_BUCKET_FEDERATION }}
with:
terraform_version: ${{ inputs.terraform_version || vars.TF_VERSION_LATEST }}
ref: ${{ inputs.ref }}
test_group: ${{ inputs.test_group }}
atlas_cloud_env: ${{ inputs.atlas_cloud_env }}
aws_region_federation: ${{ vars.AWS_REGION_FEDERATION }}
mongodb_atlas_org_id: ${{ inputs.atlas_cloud_env == 'qa' && vars.MONGODB_ATLAS_ORG_ID_CLOUD_QA || vars.MONGODB_ATLAS_ORG_ID_CLOUD_DEV }}
mongodb_atlas_org_id_network: ${{ inputs.atlas_cloud_env == 'qa' && vars.MONGODB_ATLAS_ORG_ID_CLOUD_QA || vars.MONGODB_ATLAS_ORG_ID_CLOUD_DEV_NETWORK }}
mongodb_atlas_project_id_network: ${{ inputs.atlas_cloud_env == 'qa' && vars.MONGODB_ATLAS_PROJECT_ID_CLOUD_QA || vars.MONGODB_ATLAS_PROJECT_ID_CLOUD_DEV_NETWORK }}
Expand All @@ -65,4 +67,4 @@ jobs:
azure_atlas_app_id: ${{ inputs.atlas_cloud_env == 'qa' && vars.AZURE_ATLAS_APP_ID_QA || vars.AZURE_ATLAS_APP_ID }}
azure_service_principal_id: ${{ inputs.atlas_cloud_env == 'qa' && vars.AZURE_SERVICE_PRINCIPAL_ID_QA || vars.AZURE_SERVICE_PRINCIPAL_ID }}
azure_tenant_id: ${{ inputs.atlas_cloud_env == 'qa' && vars.AZURE_TENANT_ID_QA || vars.AZURE_TENANT_ID }}
aws_secret_name: ${{ inputs.atlas_cloud_env == 'qa' && vars.AWS_SECRET_NAME_QA || vars.AWS_SECRET_NAME }}
aws_secret_name: ${{ inputs.atlas_cloud_env == 'qa' && vars.AWS_SECRET_NAME_QA || vars.AWS_SECRET_NAME }}
33 changes: 32 additions & 1 deletion .github/workflows/migration-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ jobs:
advanced_cluster: ${{ steps.filter.outputs.advanced_cluster == 'true' || env.mustTrigger == 'true' }}
backup_online_archive: ${{ steps.filter.outputs.backup_online_archive == 'true' || env.mustTrigger == 'true' }}
stream: ${{ steps.filter.outputs.stream == 'true' || env.mustTrigger == 'true' }}
federation: ${{ steps.filter.outputs.federation == 'true' || env.mustTrigger == 'true' }}
steps:
- uses: actions/checkout@v4
- uses: dorny/paths-filter@v2
Expand All @@ -83,7 +84,9 @@ jobs:
stream:
- 'internal/service/streaminstance/*.go'
- 'internal/service/streamconnection/*.go'
federation:
- 'internal/service/federateddatabaseinstance/*.go'
project:
needs: [ change-detection, get-provider-version ]
if: ${{ needs.change-detection.outputs.project == 'true' || inputs.test_group == 'project' }}
Expand Down Expand Up @@ -225,3 +228,31 @@ jobs:
MONGODB_ATLAS_LAST_VERSION: ${{ needs.get-provider-version.outputs.provider_version }}
TEST_REGEX: "^TestAccMigrationStream"
run: make testacc
federation:
needs: [ change-detection, get-provider-version ]
if: ${{ needs.change-detection.outputs.federation == 'true' || inputs.test_group == 'federation' }}
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version-file: 'go.mod'
- uses: hashicorp/setup-terraform@v3
with:
terraform_version: ${{ env.terraform_version }}
terraform_wrapper: false
- name: Migration Tests
env:
AWS_S3_BUCKET: ${{ secrets.AWS_S3_BUCKET_FEDERATION }}
AWS_REGION: ${{ vars.AWS_REGION_FEDERATION }}
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
MONGODB_ATLAS_ORG_ID: ${{ vars.MONGODB_ATLAS_ORG_ID_CLOUD_DEV }}
MONGODB_ATLAS_BASE_URL: ${{ vars.MONGODB_ATLAS_BASE_URL }}
MONGODB_ATLAS_PUBLIC_KEY: ${{ secrets.MONGODB_ATLAS_PUBLIC_KEY_CLOUD_DEV }}
MONGODB_ATLAS_PRIVATE_KEY: ${{ secrets.MONGODB_ATLAS_PRIVATE_KEY_CLOUD_DEV }}
MONGODB_ATLAS_LAST_VERSION: ${{ needs.get-provider-version.outputs.provider_version }}
TEST_REGEX: "^TestAccMigrationFederatedDatabaseInstance"
run: make testacc
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func TestAccBackupDSCloudBackupSnapshotExportBucket_basic(t *testing.T) {
)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acc.PreCheck(t) },
PreCheck: func() { acc.PreCheck(t); acc.PreCheckS3Bucket(t) },
ProtoV6ProviderFactories: acc.TestAccProviderV6Factories,
Steps: []resource.TestStep{
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func TestAccBackupDSCloudBackupSnapshotExportBuckets_basic(t *testing.T) {
)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acc.PreCheck(t) },
PreCheck: func() { acc.PreCheck(t); acc.PreCheckS3Bucket(t) },
ProtoV6ProviderFactories: acc.TestAccProviderV6Factories,
Steps: []resource.TestStep{
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func TestAccBackupRSBackupSnapshotExportBucket_basic(t *testing.T) {
iamRoleID = os.Getenv("IAM_ROLE_ID")
)
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acc.PreCheck(t) },
PreCheck: func() { acc.PreCheck(t); acc.PreCheckS3Bucket(t) },
ProtoV6ProviderFactories: acc.TestAccProviderV6Factories,
CheckDestroy: testAccCheckMongoDBAtlasBackupSnapshotExportBucketDestroy,
Steps: []resource.TestStep{
Expand All @@ -51,7 +51,7 @@ func TestAccBackupRSBackupSnapshotExportBucket_importBasic(t *testing.T) {
)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acc.PreCheck(t) },
PreCheck: func() { acc.PreCheck(t); acc.PreCheckS3Bucket(t) },
ProtoV6ProviderFactories: acc.TestAccProviderV6Factories,
CheckDestroy: testAccCheckMongoDBAtlasBackupSnapshotExportBucketDestroy,
Steps: []resource.TestStep{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func TestAccBackupDSCloudBackupSnapshotExportJob_basic(t *testing.T) {
)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acc.PreCheck(t) },
PreCheck: func() { acc.PreCheck(t); acc.PreCheckS3Bucket(t) },
ProtoV6ProviderFactories: acc.TestAccProviderV6Factories,
Steps: []resource.TestStep{
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func TestAccBackupDSCloudBackupSnapshotExportJobs_basic(t *testing.T) {
)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acc.PreCheck(t) },
PreCheck: func() { acc.PreCheck(t); acc.PreCheckS3Bucket(t) },
ProtoV6ProviderFactories: acc.TestAccProviderV6Factories,
Steps: []resource.TestStep{
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func TestAccBackupRSBackupSnapshotExportJob_basic(t *testing.T) {
iamRoleID = os.Getenv("IAM_ROLE_ID")
)
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acc.PreCheck(t) },
PreCheck: func() { acc.PreCheck(t); acc.PreCheckS3Bucket(t) },
ProtoV6ProviderFactories: acc.TestAccProviderV6Factories,
CheckDestroy: testAccCheckMongoDBAtlasBackupSnapshotExportJobDestroy,
Steps: []resource.TestStep{
Expand All @@ -51,7 +51,7 @@ func TestAccBackupRSBackupSnapshotExportJob_importBasic(t *testing.T) {
)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acc.PreCheck(t) },
PreCheck: func() { acc.PreCheck(t); acc.PreCheckS3Bucket(t) },
ProtoV6ProviderFactories: acc.TestAccProviderV6Factories,
CheckDestroy: testAccCheckMongoDBAtlasBackupSnapshotExportJobDestroy,
Steps: []resource.TestStep{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
"go.mongodb.org/atlas-sdk/v20231115004/admin"
)

func TestAccDataSourceFederatedDatabaseInstance_basic(t *testing.T) {
func TestAccFederatedDatabaseInstanceDS_basic(t *testing.T) {
var (
resourceName = "data.mongodbatlas_federated_database_instance.test"
orgID = os.Getenv("MONGODB_ATLAS_ORG_ID")
Expand All @@ -32,10 +32,10 @@ func TestAccDataSourceFederatedDatabaseInstance_basic(t *testing.T) {
{
ExternalProviders: acc.ExternalProvidersOnlyAWS(),
ProtoV6ProviderFactories: acc.TestAccProviderV6Factories,
Config: testAccMongoDBAtlasFederatedDatabaseInstanceConfigDataSourceFirstSteps(name, projectName, orgID),
Config: configFirstStepsDS(name, projectName, orgID),
Check: resource.ComposeTestCheckFunc(
testAccCheckMongoDBAtlasFederatedDatabaseDataSourceInstanceExists(resourceName, &federatedInstance),
testAccCheckMongoDBAtlasFederatedDabaseInstanceAttributes(&federatedInstance, name),
checkExists(resourceName, &federatedInstance),
checkAttributes(&federatedInstance, name),
resource.TestCheckResourceAttrSet(resourceName, "project_id"),
resource.TestCheckResourceAttr(resourceName, "name", name),
resource.TestCheckResourceAttrSet(resourceName, "storage_stores.0.read_preference.0.tag_sets.#"),
Expand All @@ -47,31 +47,30 @@ func TestAccDataSourceFederatedDatabaseInstance_basic(t *testing.T) {
})
}

func TestAccDataSourceFederatedDatabaseInstance_S3Bucket(t *testing.T) {
acc.SkipTestExtCred(t)
func TestAccFederatedDatabaseInstanceDS_s3Bucket(t *testing.T) {
var (
resourceName = "data.mongodbatlas_federated_database_instance.test"
orgID = os.Getenv("MONGODB_ATLAS_ORG_ID")
projectName = acctest.RandomWithPrefix("test-acc")
name = acctest.RandomWithPrefix("test-acc")
policyName = acctest.RandomWithPrefix("test-acc")
roleName = acctest.RandomWithPrefix("test-acc")
roleName = acctest.RandomWithPrefix("mongodb-atlas-test-acc-fed")
testS3Bucket = os.Getenv("AWS_S3_BUCKET")
region = "VIRGINIA_USA"
federatedInstance = admin.DataLakeTenant{}
)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acc.PreCheckBasic(t) },
PreCheck: func() { acc.PreCheckBasic(t); acc.PreCheckS3Bucket(t) },
CheckDestroy: acc.CheckDestroyFederatedDatabaseInstance,
Steps: []resource.TestStep{
{
ExternalProviders: acc.ExternalProvidersOnlyAWS(),
ProtoV6ProviderFactories: acc.TestAccProviderV6Factories,
Config: testAccMongoDBAtlasFederatedDatabaseInstanceDataSourceConfigS3Bucket(policyName, roleName, projectName, orgID, name, testS3Bucket, region),
Config: configDSWithS3Bucket(policyName, roleName, projectName, orgID, name, testS3Bucket, region),
Check: resource.ComposeTestCheckFunc(
testAccCheckMongoDBAtlasFederatedDatabaseDataSourceInstanceExists(resourceName, &federatedInstance),
testAccCheckMongoDBAtlasFederatedDabaseInstanceAttributes(&federatedInstance, name),
checkExists(resourceName, &federatedInstance),
checkAttributes(&federatedInstance, name),
resource.TestCheckResourceAttrSet(resourceName, "project_id"),
resource.TestCheckResourceAttr(resourceName, "name", name),
),
Expand All @@ -80,7 +79,7 @@ func TestAccDataSourceFederatedDatabaseInstance_S3Bucket(t *testing.T) {
})
}

func testAccCheckMongoDBAtlasFederatedDatabaseDataSourceInstanceExists(resourceName string, dataFederatedInstance *admin.DataLakeTenant) resource.TestCheckFunc {
func checkExists(resourceName string, dataFederatedInstance *admin.DataLakeTenant) resource.TestCheckFunc {
return func(s *terraform.State) error {
connV2 := acc.TestAccProviderSdkV2.Meta().(*config.MongoDBClient).AtlasV2

Expand All @@ -103,7 +102,7 @@ func testAccCheckMongoDBAtlasFederatedDatabaseDataSourceInstanceExists(resourceN
}
}

func testAccCheckMongoDBAtlasFederatedDabaseInstanceAttributes(dataFederatedInstance *admin.DataLakeTenant, name string) resource.TestCheckFunc {
func checkAttributes(dataFederatedInstance *admin.DataLakeTenant, name string) resource.TestCheckFunc {
return func(s *terraform.State) error {
log.Printf("[DEBUG] difference dataFederatedInstance.Name: %s , username : %s", dataFederatedInstance.GetName(), name)
if dataFederatedInstance.GetName() != name {
Expand All @@ -113,8 +112,8 @@ func testAccCheckMongoDBAtlasFederatedDabaseInstanceAttributes(dataFederatedInst
}
}

func testAccMongoDBAtlasFederatedDatabaseInstanceDataSourceConfigS3Bucket(policyName, roleName, projectName, orgID, name, testS3Bucket, dataLakeRegion string) string {
stepConfig := testAccMongoDBAtlasFederatedDatabaseInstanceConfigDataSourceFirstStepS3Bucket(name, testS3Bucket)
func configDSWithS3Bucket(policyName, roleName, projectName, orgID, name, testS3Bucket, dataLakeRegion string) string {
stepConfig := configDSFirstStepS3Bucket(name, testS3Bucket)
return fmt.Sprintf(`
resource "aws_iam_role_policy" "test_policy" {
name = %[1]q
Expand Down Expand Up @@ -181,7 +180,7 @@ resource "mongodbatlas_cloud_provider_access_authorization" "auth_role" {
%s
`, policyName, roleName, projectName, orgID, stepConfig)
}
func testAccMongoDBAtlasFederatedDatabaseInstanceConfigDataSourceFirstStepS3Bucket(name, testS3Bucket string) string {
func configDSFirstStepS3Bucket(name, testS3Bucket string) string {
return fmt.Sprintf(`
resource "mongodbatlas_federated_database_instance" "test" {
project_id = mongodbatlas_project.test.id
Expand Down Expand Up @@ -273,7 +272,7 @@ data "mongodbatlas_federated_database_instance" "test" {
`, name, testS3Bucket)
}

func testAccMongoDBAtlasFederatedDatabaseInstanceConfigDataSourceFirstSteps(federatedInstanceName, projectName, orgID string) string {
func configFirstStepsDS(federatedInstanceName, projectName, orgID string) string {
return fmt.Sprintf(`
resource "mongodbatlas_project" "test" {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,27 @@ import (
"github.com/mongodb/terraform-provider-mongodbatlas/internal/testutil/acc"
)

func TestAccDataSourceFederatedDatabaseInstances_basic(t *testing.T) {
acc.SkipTestExtCred(t)
func TestAccFederatedDatabaseInstanceDSPlural_basic(t *testing.T) {
var (
resourceName = "data.mongodbatlas_federated_database_instances.test"
orgID = os.Getenv("MONGODB_ATLAS_ORG_ID")
projectName = acctest.RandomWithPrefix("test-acc")
firstName = acctest.RandomWithPrefix("test-acc")
secondName = acctest.RandomWithPrefix("test-acc")
policyName = acctest.RandomWithPrefix("test-acc")
roleName = acctest.RandomWithPrefix("test-acc")
roleName = acctest.RandomWithPrefix("mongodb-atlas-test-acc-fed")
testS3Bucket = os.Getenv("AWS_S3_BUCKET")
region = "VIRGINIA_USA"
)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acc.PreCheckBasic(t) },
PreCheck: func() { acc.PreCheckBasic(t); acc.PreCheckS3Bucket(t) },
CheckDestroy: acc.CheckDestroyFederatedDatabaseInstance,
Steps: []resource.TestStep{
{
ExternalProviders: acc.ExternalProvidersOnlyAWS(),
ProtoV6ProviderFactories: acc.TestAccProviderV6Factories,
Config: testAccMongoDBAtlasFederatedDatabaseInstancesDataSourceConfig(policyName, roleName, projectName, orgID, firstName, secondName, testS3Bucket, region),
Config: configDSPlural(policyName, roleName, projectName, orgID, firstName, secondName, testS3Bucket, region),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet(resourceName, "project_id"),
resource.TestCheckResourceAttrSet(resourceName, "results.#"),
Expand All @@ -41,8 +40,8 @@ func TestAccDataSourceFederatedDatabaseInstances_basic(t *testing.T) {
})
}

func testAccMongoDBAtlasFederatedDatabaseInstancesDataSourceConfig(policyName, roleName, projectName, orgID, firstName, secondName, testS3Bucket, dataLakeRegion string) string {
stepConfig := testAccMongoDBAtlasFederatedDatabaseInstancesConfigDataSourceFirstStep(firstName, secondName, testS3Bucket)
func configDSPlural(policyName, roleName, projectName, orgID, firstName, secondName, testS3Bucket, dataLakeRegion string) string {
stepConfig := configDSPluralFirstStep(firstName, secondName, testS3Bucket)
return fmt.Sprintf(`
resource "aws_iam_role_policy" "test_policy" {
name = %[1]q
Expand Down Expand Up @@ -109,7 +108,7 @@ resource "mongodbatlas_cloud_provider_access_authorization" "auth_role" {
%s
`, policyName, roleName, projectName, orgID, stepConfig)
}
func testAccMongoDBAtlasFederatedDatabaseInstancesConfigDataSourceFirstStep(firstName, secondName, testS3Bucket string) string {
func configDSPluralFirstStep(firstName, secondName, testS3Bucket string) string {
return fmt.Sprintf(`
resource "mongodbatlas_federated_database_instance" "test" {
project_id = mongodbatlas_project.test.id
Expand Down
Loading

0 comments on commit 831710a

Please sign in to comment.