-
Notifications
You must be signed in to change notification settings - Fork 5
CCM-13787: create supplier mock for testing #535
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Vlasis-Perdikidis
wants to merge
26
commits into
main
Choose a base branch
from
feature/CCM-13787-createSupplierMockForTesting
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
940bbdf
add supplier mock dummy lambda function
Vlasis-Perdikidis 72522b1
correct the name of the supplier_mock lambda
Vlasis-Perdikidis b709f6a
remove comment from tf file
Vlasis-Perdikidis 241808b
get dummy test to pass
Vlasis-Perdikidis 52da831
disable vale spelling from README
Vlasis-Perdikidis 577b851
correct lambda handler name
Vlasis-Perdikidis c3bde6b
restore changes
Vlasis-Perdikidis c22f094
call patch letter and add variables to parameter store
Vlasis-Perdikidis 5f4ea2a
merge main
Vlasis-Perdikidis c2c9675
resolve final conflicts
Vlasis-Perdikidis 98d5fb2
refresh package-lock file
Vlasis-Perdikidis e7a0433
correct lint and typecheck errors
Vlasis-Perdikidis b048bbf
resolve final conflicts
Vlasis-Perdikidis 92d4c60
re-add @pact-foundation/pact library in run-pact-tests.sh script
Vlasis-Perdikidis 510c828
install amd64 version of @pact-foundation if missing
Vlasis-Perdikidis 3199ce5
add ssm:GetParameter permission for supplierId parameter in supplier …
Vlasis-Perdikidis 3bd0da7
merge main
Vlasis-Perdikidis 3045c21
get package-lock in line with main
Vlasis-Perdikidis 6993b09
don't deploy the scheduler by default
Vlasis-Perdikidis 0bf202f
only deploy scheduler resources when flag is set to true
Vlasis-Perdikidis 994de51
deploy the schedule to test it
Vlasis-Perdikidis cfcdfef
add README with instructions on the supplier-mock lambda
Vlasis-Perdikidis 16651dc
add supplier-mock config in parameter store whith specificationId map…
Vlasis-Perdikidis da3ea16
fix typecheck
Vlasis-Perdikidis 343c992
remove comment
Vlasis-Perdikidis f7ac687
correct patch letter request body
Vlasis-Perdikidis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| name: Acceptance tests - component | ||
| description: "Run component acceptance tests for this repo" | ||
|
|
||
| inputs: | ||
| testType: | ||
| description: Type of test to run | ||
| required: true | ||
|
|
||
| targetEnvironment: | ||
| description: Name of the environment under test | ||
| required: true | ||
|
|
||
| targetComponent: | ||
| description: Name of the component under test | ||
| required: true | ||
|
|
||
| runs: | ||
| using: "composite" | ||
|
|
||
| steps: | ||
|
|
||
| - name: Repo setup | ||
| uses: ./.github/actions/node-install | ||
| with: | ||
| GITHUB_TOKEN: ${{ env.GITHUB_TOKEN }} | ||
|
|
||
| - name: Fetch terraform output | ||
| uses: actions/download-artifact@634f93cb2916e3fdff6788551b99b062d0335ce0 # v5 | ||
| with: | ||
| name: terraform-output-${{ inputs.targetComponent }} | ||
|
|
||
| - name: Get Node version | ||
| id: nodejs_version | ||
| shell: bash | ||
| run: | | ||
| echo "nodejs_version=$(grep "^nodejs\s" .tool-versions | cut -f2 -d' ')" >> $GITHUB_OUTPUT | ||
|
|
||
| - name: Run test - ${{ inputs.testType }} | ||
| shell: bash | ||
| env: | ||
| TARGET_ENVIRONMENT: ${{ inputs.targetEnvironment }} | ||
| run: | | ||
| make test-${{ inputs.testType }} |
This file contains hidden or 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
This file contains hidden or 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
105 changes: 105 additions & 0 deletions
105
infrastructure/terraform/components/api/module_lambda_supplier_mock.tf
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| module "supplier_mock" { | ||
| source = "https://github.com/NHSDigital/nhs-notify-shared-modules/releases/download/v2.0.29/terraform-lambda.zip" | ||
| count = var.deploy_supplier_mock_scheduler ? 1 : 0 | ||
|
|
||
| function_name = "supplier_mock" | ||
| description = "Mock the behaviour of a supplier" | ||
|
|
||
| aws_account_id = var.aws_account_id | ||
| component = var.component | ||
| environment = var.environment | ||
| project = var.project | ||
| region = var.region | ||
| group = var.group | ||
|
|
||
| log_retention_in_days = var.log_retention_in_days | ||
| kms_key_arn = module.kms.key_arn | ||
|
|
||
| iam_policy_document = { | ||
| body = data.aws_iam_policy_document.supplier_mock_lambda.json | ||
| } | ||
|
|
||
| function_s3_bucket = local.acct.s3_buckets["lambda_function_artefacts"]["id"] | ||
| function_code_base_path = local.aws_lambda_functions_dir_path | ||
| function_code_dir = "supplier-mock/dist" | ||
| function_include_common = true | ||
| handler_function_name = "supplierMockHandler" | ||
| runtime = "nodejs22.x" | ||
| memory = 512 | ||
| timeout = 29 | ||
| log_level = var.log_level | ||
|
|
||
| force_lambda_code_deploy = var.force_lambda_code_deploy | ||
| enable_lambda_insights = false | ||
|
|
||
| log_destination_arn = local.destination_arn | ||
| log_subscription_role_arn = local.acct.log_subscription_role_arn | ||
|
|
||
| lambda_env_vars = merge(local.common_lambda_env_vars, { | ||
| ENVIRONMENT = var.environment | ||
| GET_LETTERS_FUNCTION_NAME = module.get_letters.function_name | ||
| PATCH_LETTER_FUNCTION_NAME = module.patch_letter.function_name | ||
| SUPPLIER_MOCK_CONFIG_PARAM_NAME = aws_ssm_parameter.supplier_mock_config[0].name | ||
| }) | ||
| } | ||
|
|
||
| resource "aws_ssm_parameter" "supplier_mock_config" { | ||
| count = var.deploy_supplier_mock_scheduler ? 1 : 0 | ||
| name = format("/nhs/supapi/%s/supplier-mock/config", var.environment) | ||
| description = "JSON object containing supplier mock config for supplierID, getLetters limit, specificationId mapping, etc." | ||
| type = "String" | ||
| value = jsonencode({ | ||
| limit = "100" | ||
| supplier_id = "TestSupplier1" | ||
| specification_id_mapping = { | ||
| "test-specification-id-1" = "ACCEPTED" | ||
| } | ||
| }) | ||
|
|
||
| lifecycle { | ||
| ignore_changes = [value] | ||
| } | ||
| } | ||
|
|
||
| data "aws_iam_policy_document" "supplier_mock_lambda" { | ||
| statement { | ||
| sid = "KMSPermissions" | ||
| effect = "Allow" | ||
|
|
||
| actions = [ | ||
| "kms:Decrypt", | ||
| "kms:GenerateDataKey", | ||
| ] | ||
|
|
||
| resources = [ | ||
| module.kms.key_arn, ## Requires shared kms module | ||
| ] | ||
| } | ||
|
|
||
| statement { | ||
| sid = "AllowInvokeLambda" | ||
| effect = "Allow" | ||
|
|
||
| actions = [ | ||
| "lambda:InvokeFunction", | ||
| ] | ||
|
|
||
| resources = [ | ||
| module.get_letters.function_arn, | ||
| module.patch_letter.function_arn | ||
| ] | ||
| } | ||
|
|
||
| statement { | ||
| sid = "AllowReadSupplierMockConfigParameter" | ||
| effect = "Allow" | ||
|
|
||
| actions = [ | ||
| "ssm:GetParameter", | ||
| ] | ||
|
|
||
| resources = [ | ||
| aws_ssm_parameter.supplier_mock_config[0].arn | ||
| ] | ||
| } | ||
| } | ||
75 changes: 75 additions & 0 deletions
75
infrastructure/terraform/components/api/scheduler_supplier_mock.tf
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| data "aws_iam_policy_document" "supplier_mock_scheduler_trust_policy" { | ||
| statement { | ||
| sid = "AllowSchedulerAssumeRole" | ||
| effect = "Allow" | ||
|
|
||
| actions = [ | ||
| "sts:AssumeRole", | ||
| ] | ||
|
|
||
| principals { | ||
| type = "Service" | ||
|
|
||
| identifiers = [ | ||
| "scheduler.amazonaws.com", | ||
| ] | ||
| } | ||
| } | ||
| } | ||
|
|
||
| resource "aws_iam_role" "supplier_mock_scheduler" { | ||
| name = "${local.csi}-supplier-mock-scheduler" | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should the scheduler get created for all branches or just |
||
| description = "Allows EventBridge Scheduler to invoke supplier mock lambda" | ||
| assume_role_policy = data.aws_iam_policy_document.supplier_mock_scheduler_trust_policy.json | ||
| count = var.deploy_supplier_mock_scheduler ? 1 : 0 | ||
| } | ||
|
|
||
| data "aws_iam_policy_document" "supplier_mock_scheduler_invoke_policy" { | ||
| count = var.deploy_supplier_mock_scheduler ? 1 : 0 | ||
|
|
||
| statement { | ||
| sid = "AllowInvokeSupplierMockLambda" | ||
| effect = "Allow" | ||
|
|
||
| actions = [ | ||
| "lambda:InvokeFunction", | ||
| ] | ||
|
|
||
| resources = [ | ||
| module.supplier_mock[0].function_arn, | ||
| ] | ||
| } | ||
| } | ||
|
|
||
| resource "aws_iam_policy" "supplier_mock_scheduler_invoke_policy" { | ||
| count = var.deploy_supplier_mock_scheduler ? 1 : 0 | ||
| name = "${local.csi}-supplier-mock-scheduler-invoke" | ||
| policy = data.aws_iam_policy_document.supplier_mock_scheduler_invoke_policy[0].json | ||
| } | ||
|
|
||
| resource "aws_iam_role_policy_attachment" "supplier_mock_scheduler_invoke_policy" { | ||
| count = var.deploy_supplier_mock_scheduler ? 1 : 0 | ||
| role = aws_iam_role.supplier_mock_scheduler[0].name | ||
| policy_arn = aws_iam_policy.supplier_mock_scheduler_invoke_policy[0].arn | ||
| } | ||
|
|
||
| resource "aws_scheduler_schedule" "supplier_mock" { | ||
| count = var.deploy_supplier_mock_scheduler ? 1 : 0 | ||
| name = "${local.csi}-supplier-mock" | ||
| description = "Scheduled trigger for supplier mock lambda" | ||
| state = var.enable_supplier_mock_scheduler ? "ENABLED" : "DISABLED" | ||
|
|
||
| flexible_time_window { | ||
| mode = "OFF" | ||
| } | ||
|
|
||
| schedule_expression = var.supplier_mock_schedule_expression | ||
|
|
||
| target { | ||
| arn = module.supplier_mock[0].function_arn | ||
| role_arn = aws_iam_role.supplier_mock_scheduler[0].arn | ||
| input = jsonencode({ | ||
| source = "eventbridge-scheduler" | ||
| }) | ||
| } | ||
| } | ||
This file contains hidden or 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
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| coverage | ||
| node_modules | ||
| dist | ||
| .reports | ||
| .aws-sam |
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| <!-- vale off --> | ||
|
|
||
| # Supplier Mock | ||
|
|
||
| ## What is does | ||
|
|
||
| The supplier-mock lambda simulates the supplier's system behaviour so that downstream services can simulate supplier interaction without relying on a live supplier system. | ||
| The mock simulates the journey of a letter with the Supplier. It utilises the api-handler and retrieves pending letters by calling the `getLetters` lambda directly and then provides status updates for each letter by calling the `patchLetter` lambda. | ||
| Each time the mock is called it will call `getLetters` once with for the **supplierId** `TestSupplier1` and with a **limit** of `100` letters. These default values can be configured in the AWS Parameter Store as explained in [How to modify parameter store variables](#how-to-modify-default-variable-values-in-parameter-store) . It then loops through each retrieved letter and updates its status by calling `patchLetter`. | ||
|
|
||
| ## How to deploy the supapi-supplier-mock schedule | ||
|
|
||
| The AWS EventBridge Schedule that calls the supplier-mock lambda is created by the terraform file **/infrastructure/terraform/components/api/scheduler_supplier_mock.tf**, however, by default it's not deployed as it's not needed by every dynamic environment. | ||
| If you need to deploy the schedule in your environment you need to set the variable `deploy_supplier_mock_scheduler = true` in the file **/infrastructure/terraform/components/api/variables.tf**. | ||
|
|
||
| ## How to run the lambda | ||
|
|
||
| The supplier-mock lambda can be activated by manually enabling the `{environment}-supapi-supplier-mock` AWS EventBridge schedule from the AWS console. The schedule is configured to call the lambda once per minute. You need to manually disable the schedule from the AWS console to stop calling the supplier-mock lambda. | ||
| **NOTE: The schedule is not deployed by default** | ||
|
|
||
| ## How to modify default variable values in parameter store | ||
|
|
||
| As mentioned above the `limit` and `supplierId` default values are stored in the AWS Parameter Store and can be modified if needed by accessing the Parameter Store from the AWS Console. These store parameters are created in the **/infrastructure/terraform/components/api/module_lambda_supplier_mock.tf** module, but only if the `deploy_supplier_mock_scheduler` variable is set to `true`. The parameters are: | ||
|
|
||
| | Parameter | default value | | ||
| | --------------------------------------------------------- | ------------- | | ||
| | /nhs/supapi/supplier-mock/{environment}/get-letters-limit | 100 | | ||
| | /nhs/supapi/supplier-mock/{environment}/supplier-id | TestSupplier1 | | ||
|
|
||
| <!-- vale on --> |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know platform prefer resources such as this to have their own .tf file
Having to edit as my comment didn't save inintially and may have lost the line numbers
am talking about
the resource ssm parameter supplier_mock_config