diff --git a/.github/workflows/contract-testing.yaml b/.github/workflows/contract-testing.yaml index 47e4fd44e..22a61387a 100644 --- a/.github/workflows/contract-testing.yaml +++ b/.github/workflows/contract-testing.yaml @@ -30,6 +30,7 @@ jobs: serverless-private-endpoint: ${{ steps.filter.outputs.serverless-private-endpoint }} stream-connection: ${{ steps.filter.outputs.stream-connection }} stream-instance: ${{ steps.filter.outputs.stream-instance }} + trigger: ${{ steps.filter.outputs.trigger }} steps: - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 if: ${{ github.event_name == 'push' }} @@ -75,6 +76,8 @@ jobs: - 'cfn-resources/stream-connection/**' stream-instance: - 'cfn-resources/stream-instance/**' + trigger: + - 'cfn-resources/trigger/**' access-list-api-key: needs: change-detection if: ${{ needs.change-detection.outputs.access-list-api-key == 'true' }} @@ -853,4 +856,51 @@ jobs: make create-test-resources cat inputs/inputs_1_create.json make run-contract-testing - make delete-test-resources \ No newline at end of file + make delete-test-resources + trigger: + needs: change-detection + if: ${{ needs.change-detection.outputs.trigger == 'true' }} + runs-on: ubuntu-latest + env: + MONGODB_ATLAS_PUBLIC_KEY: ${{ secrets.CLOUD_DEV_PUBLIC_KEY }} + MONGODB_ATLAS_PRIVATE_KEY: ${{ secrets.CLOUD_DEV_PRIVATE_KEY }} + MONGODB_ATLAS_ORG_ID: ${{ secrets.CLOUD_DEV_ORG_ID }} + MONGODB_ATLAS_BASE_URL: ${{ vars.MONGODB_ATLAS_BASE_URL }} + + FOLDER_PATH: cfn-resources/trigger + # Atlas Init CLI Terminal formatting + COLUMNS: 140 + FORCE_COLOR: "yes" + TF_INPUT: "false" + NON_INTERACTIVE: "true" + # Atlas Init CLI Variables + ATLAS_INIT_CFN_PROFILE: profile-${{ github.run_id }} + ATLAS_INIT_CFN_REGION: "us-east-1" + ATLAS_INIT_CFN_USE_KMS_KEY: "false" + ATLAS_INIT_PROFILE: ${{ format('profile-{0}',github.run_id) }} + ATLAS_INIT_PROJECT_NAME: ${{ format('gh-ci-{0}', github.run_id) }} + steps: + - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 + - uses: aws-actions/setup-sam@2360ef6d90015369947b45b496193ab9976a9b04 + with: + use-installer: true + - uses: aws-actions/configure-aws-credentials@e3dd6a429d7300a6a4c196c26e071d42e0343502 + with: + aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID_TEST_ENV }} + aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY_TEST_ENV }} + aws-region: us-east-1 + - name: setup-atlas-init + uses: "EspenAlbert/atlas-init/.github/templates/external-setup@6ec12c3992b57f62e400bd8cdf98697fe244b532" + with: + version: "0.4.1" + tf-version: "1.10.2" + extra-pypi-packages: "cloudformation-cli cloudformation-cli-go-plugin setuptools" + go-version-file: 'cfn-resources/go.mod' + - name: run-atlas-init + uses: "EspenAlbert/atlas-init/.github/templates/external-run-with-tf@6ec12c3992b57f62e400bd8cdf98697fe244b532" + with: + cli-command: "cfn contract-test" + step-name: contract-test + cwd: ${{ env.FOLDER_PATH}} + profile-name: ${{ env.ATLAS_INIT_PROFILE }} + dry-run-first: "true" diff --git a/.gitignore b/.gitignore index ddb453079..137373470 100644 --- a/.gitignore +++ b/.gitignore @@ -27,6 +27,7 @@ update.json **/functions/packages **/taskcat_outputs/ **/inputs/** +**/samples/** pytestdebug.log !/cdk/**/.* *.DS_Store diff --git a/cfn-resources/trigger/README.md b/cfn-resources/trigger/README.md index 8e035014c..1d9b70f20 100644 --- a/cfn-resources/trigger/README.md +++ b/cfn-resources/trigger/README.md @@ -15,3 +15,13 @@ See the [resource docs](docs/README.md). ## Cloudformation Examples See the examples [CFN Template](../../examples/trigger/trigger.json) for example resource. + +## Development +```shell +uvx atlas-init@0.4.1 +atlas-init init +cd cfn-resources/trigger +atlas-init apply +atlas-init cfn contract-test # use --help to see more options +atlas-init destroy +``` diff --git a/cfn-resources/trigger/cmd/resource/resource.go b/cfn-resources/trigger/cmd/resource/resource.go index 183e988fc..eece2e28b 100644 --- a/cfn-resources/trigger/cmd/resource/resource.go +++ b/cfn-resources/trigger/cmd/resource/resource.go @@ -229,28 +229,26 @@ func newEventTrigger(model *Model) (*realm.EventTriggerRequest, error) { conf.Database = *model.DatabaseTrigger.Database } dTrigger := model.DatabaseTrigger - - if dTrigger.Match != nil { - jsonData := []byte(*dTrigger.Match) - // convert the JSON string to a map - var m interface{} - if err := json.Unmarshal(jsonData, &m); err != nil { - return nil, errors.New("error unmarshalling Match field - " + err.Error()) + if dTrigger != nil { + if dTrigger.Match != nil { + jsonData := []byte(*dTrigger.Match) + // convert the JSON string to a map + var m interface{} + if err := json.Unmarshal(jsonData, &m); err != nil { + return nil, errors.New("error unmarshalling Match field - " + err.Error()) + } + conf.Match = m } - conf.Match = m - } - if dTrigger.Project != nil { - jsonData := []byte(*dTrigger.Project) - // convert the JSON string to a map - var m interface{} - if err := json.Unmarshal(jsonData, &m); err != nil { - return nil, errors.New("error unmarshalling Project field - " + err.Error()) + if dTrigger.Project != nil { + jsonData := []byte(*dTrigger.Project) + // convert the JSON string to a map + var m interface{} + if err := json.Unmarshal(jsonData, &m); err != nil { + return nil, errors.New("error unmarshalling Project field - " + err.Error()) + } + conf.Project = m } - conf.Project = m - } - - if dTrigger != nil { conf.Collection = aws.StringValue(dTrigger.Collection) conf.ServiceID = aws.StringValue(dTrigger.ServiceId) conf.OperationTypes = dTrigger.OperationTypes diff --git a/cfn-resources/trigger/index.html b/cfn-resources/trigger/index.html deleted file mode 100644 index 4b73c5abd..000000000 --- a/cfn-resources/trigger/index.html +++ /dev/null @@ -1,25 +0,0 @@ -MongoDB Documentation — MongoDB Atlas
Loading
Give Feedback
© 2022 MongoDB, Inc.

About

  • Careers
  • Investor Relations
  • Legal Notices
  • Privacy Notices
  • Security Information
  • Trust Center
© 2022 MongoDB, Inc.
\ No newline at end of file diff --git a/cfn-resources/trigger/test/inputs_1_create.template.json b/cfn-resources/trigger/test/inputs_1_create.template.json index 100237fe3..13c7c7e01 100644 --- a/cfn-resources/trigger/test/inputs_1_create.template.json +++ b/cfn-resources/trigger/test/inputs_1_create.template.json @@ -1,14 +1,14 @@ { - "Name": "materializeMonthlyProductSales46", + "Name": "cfn-test-trigger-${RANDOM_INT_100K}", "Type": "DATABASE", - "Profile": "default", + "Profile": "${MONGODB_ATLAS_PROFILE}", "DatabaseTrigger": { "OperationTypes": [ "INSERT" ], "Database": "store", "Collection": "sales", - "ServiceId": "6387aee08659af5254b0a51e", + "ServiceId": "${MONGODB_REALM_SERVICE_ID}", "FullDocument": "true", "FullDocumentBeforeChange": "false", "Unordered": "false", @@ -19,14 +19,14 @@ "EventProcessors": { "FUNCTION": { "FuncConfig": { - "FunctionName": "cfn_func", - "FunctionId": "63862553ac0702272aa701ba" + "FunctionName": "${MONGODB_REALM_FUNCTION_NAME}", + "FunctionId": "${MONGODB_REALM_FUNCTION_ID}" } }, "AWSEVENTBRIDGE": { "AWSConfig": {} } }, - "AppId": "638624a5167f5659feb75971", - "ProjectId": "625454459c4e6108393d650d" + "AppId": "${MONGODB_REALM_APP_ID}", + "ProjectId": "${MONGODB_ATLAS_PROJECT_ID}" } diff --git a/cfn-resources/trigger/test/inputs_1_update.template.json b/cfn-resources/trigger/test/inputs_1_update.template.json index 100237fe3..13c7c7e01 100644 --- a/cfn-resources/trigger/test/inputs_1_update.template.json +++ b/cfn-resources/trigger/test/inputs_1_update.template.json @@ -1,14 +1,14 @@ { - "Name": "materializeMonthlyProductSales46", + "Name": "cfn-test-trigger-${RANDOM_INT_100K}", "Type": "DATABASE", - "Profile": "default", + "Profile": "${MONGODB_ATLAS_PROFILE}", "DatabaseTrigger": { "OperationTypes": [ "INSERT" ], "Database": "store", "Collection": "sales", - "ServiceId": "6387aee08659af5254b0a51e", + "ServiceId": "${MONGODB_REALM_SERVICE_ID}", "FullDocument": "true", "FullDocumentBeforeChange": "false", "Unordered": "false", @@ -19,14 +19,14 @@ "EventProcessors": { "FUNCTION": { "FuncConfig": { - "FunctionName": "cfn_func", - "FunctionId": "63862553ac0702272aa701ba" + "FunctionName": "${MONGODB_REALM_FUNCTION_NAME}", + "FunctionId": "${MONGODB_REALM_FUNCTION_ID}" } }, "AWSEVENTBRIDGE": { "AWSConfig": {} } }, - "AppId": "638624a5167f5659feb75971", - "ProjectId": "625454459c4e6108393d650d" + "AppId": "${MONGODB_REALM_APP_ID}", + "ProjectId": "${MONGODB_ATLAS_PROJECT_ID}" } diff --git a/cfn-resources/util/util.go b/cfn-resources/util/util.go index 0b430e0ec..07197a511 100644 --- a/cfn-resources/util/util.go +++ b/cfn-resources/util/util.go @@ -20,6 +20,7 @@ import ( "fmt" "log" "net/http" + "net/url" "os" "runtime" "strconv" @@ -109,6 +110,15 @@ func GetRealmClient(ctx context.Context, req handler.Request, profileName *strin optsRealm := []realm.ClientOpt{realm.SetUserAgent(userAgent)} authConfig := realmAuth.NewConfig(nil) + + if p.BaseURL != "" { + if strings.Contains(p.BaseURL, "-dev.") { + adminURL := "https://services.cloud-dev.mongodb.com/api/admin/v3.0/" + optsRealm = append(optsRealm, realm.SetBaseURL(adminURL)) + authConfig.AuthURL, _ = url.Parse(adminURL + "auth/providers/mongodb-cloud/login") + } + } + token, err := authConfig.NewTokenFromCredentials(ctx, p.PublicKey, p.PrivateKey) if err != nil { return nil, err