Skip to content

Commit

Permalink
Merge branch 'issue_19_json_decoding'
Browse files Browse the repository at this point in the history
* issue_19_json_decoding:
  Test using the current version of the AWS CLI command as present in the Ubuntu image
  Use the correct parameter for find for tests
  Refactoring of some tests and handle invalid JSON results
  • Loading branch information
Richard Quadling authored and Richard Quadling committed Aug 6, 2024
2 parents 015eefd + bafcf68 commit 7995cee
Show file tree
Hide file tree
Showing 34 changed files with 103 additions and 82 deletions.
14 changes: 14 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,20 @@ jobs:
strategy:
matrix:
terraform_version:
- 1.9.3
- 1.9.2
- 1.9.1
- 1.9.0
- 1.8.5
- 1.8.4
- 1.8.3
- 1.8.2
- 1.8.1
- 1.8.0
- 1.7.5
- 1.7.4
- 1.7.3
- 1.7.2
- 1.7.1
- 1.7.0
- 1.6.6
Expand Down
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
# Changelog

# Unreleased - 2024/02/22
# v7.0.0 - 2024/08/06
- Fix a typo in the description for the `var.external_id`.
- Fix handling of invalid JSON returned from the AWS CLI. Thank you [홍수민 and horststumpf](https://github.com/digitickets/terraform-aws-cli/pull/19).
- Introduced 2 new outputs:
- `output.result_raw = string` - This will contain the raw output from the AWS CLI call.
- `output.result_was_decoded = bool` - This will indicated if the output from the AWS CLI call was successfully JSON decoded.

These were introduced as some of the results from the AWS CLI are not JSON decodable. For example `aws ec2 create-tags` returns nothing.

# v6.1.0 - 2024/01/31
- Added testing for Terraform 1.7+
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,9 @@ No modules.

| Name | Description |
|------|-------------|
| <a name="output_result"></a> [result](#output\_result) | The output of the AWS CLI command |
| <a name="output_result"></a> [result](#output\_result) | The output of the AWS CLI command, if it can be JSON decoded |
| <a name="output_result_raw"></a> [result\_raw](#output\_result\_raw) | The raw, non JSON decoded output of the AWS CLI command |
| <a name="output_result_was_decoded"></a> [result\_was\_decoded](#output\_result\_was\_decoded) | Can the output from the AWS CLI command can be JSON decoded |
<!-- END_TF_DOCS -->

# Docker
Expand Down
16 changes: 14 additions & 2 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,18 @@ data "local_file" "awscli_results_file" {

output "result" {
depends_on = [data.local_file.awscli_results_file]
description = "The output of the AWS CLI command"
value = jsondecode(data.local_file.awscli_results_file.content)
description = "The output of the AWS CLI command, if it can be JSON decoded"
value = try(jsondecode(data.local_file.awscli_results_file.content), "")
}

output "result_raw" {
depends_on = [data.local_file.awscli_results_file]
description = "The raw, non JSON decoded output of the AWS CLI command"
value = data.local_file.awscli_results_file.content
}

output "result_was_decoded" {
depends_on = [data.local_file.awscli_results_file]
description = "Can the output from the AWS CLI command can be JSON decoded"
value = can(jsondecode(data.local_file.awscli_results_file.content))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[
{
"variable": "data.local_file.awscli_results_file",
"problems": [
"An error occurred (NoSuchBucket) when calling the ListObjects operation: The specified bucket does not exist"
]
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ function test_setup() {
}

function test_teardown() {
echo "MODULE_TERRAFORM_AWS_CLI_RETAIN_LOGS = ${MODULE_TERRAFORM_AWS_CLI_RETAIN_LOGS}"
ROOT_DIRECTORY="test-reports/${TEST_NAME}/aws"

# Build all the paths for all the files.
Expand Down Expand Up @@ -46,6 +47,7 @@ function test_teardown() {
fi

if [ "${TEST_PASSED}" != "true" ]; then
echo "Failed : $TEST_PATH"
exit 1;
fi
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[
{
"variable": "data.local_file.awscli_results_file",
"problems": [
"An error occurred (NoSuchBucket) when calling the ListObjects operation: The specified bucket does not exist"
]
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ function test_teardown() {
# Not all the files are created as the test does not generate these errors
AWS_SCRIPT_FILENAMES=(
"${JQ_JSON}" "${JQ_ERROR_LOG}"
# "${AWS_STS_JSON}" "${AWS_STS_ERROR_LOG}"
"${AWS_STS_JSON}" "${AWS_STS_ERROR_LOG}"
"${AWS_CALL_JSON}" "${AWS_CALL_ERROR_LOG}"
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"update-detector",
"--finding-publishing-frequency",
"ONE_HOUR",
"--detector-id=01234567890123456789012345678901"
"--detector-id="
],
"aws_cli_query": "",
"external_id": "",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ aws_cli_commands = [
"update-detector",
"--finding-publishing-frequency",
"ONE_HOUR",
"--detector-id=01234567890123456789012345678901"
"--detector-id="
]
role_session_name = "empty_result"

Expand Down
13 changes: 0 additions & 13 deletions tests/script_removes_all_files/expected_apply_outputs.json

This file was deleted.

11 changes: 0 additions & 11 deletions tests/script_removes_all_files/expected_results.json

This file was deleted.

13 changes: 0 additions & 13 deletions tests/script_retains_all_files/expected_apply_outputs.json

This file was deleted.

11 changes: 0 additions & 11 deletions tests/script_retains_all_files/expected_results.json

This file was deleted.

Empty file.
Empty file.
13 changes: 0 additions & 13 deletions tests/test_data_retrieval_with_no_arn/expected_apply_outputs.json

This file was deleted.

11 changes: 0 additions & 11 deletions tests/test_data_retrieval_with_no_arn/expected_results.json

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[
{
"variable": "data.local_file.awscli_results_file",
"problems": [
"An error occurred (NoSuchBucket) when calling the ListObjects operation: The specified bucket does not exist"
]
}
]
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"alternative_path": "test-reports/test_data_retrieval_with_no_arn/aws",
"alternative_path": "test-reports/test_data_retrieval_with_no_role_arn_and_error/aws",
"assume_role_arn": "",
"aws_cli_commands": [
"s3api",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ aws_cli_commands = ["s3api", "list-objects", "--bucket=ryft-public-sample-data",
aws_cli_query = "max_by(Contents, &Size)"
region = "eu-west-1"

alternative_path = "test-reports/test_data_retrieval_with_no_arn/aws"
alternative_path = "test-reports/test_data_retrieval_with_no_role_arn_and_error/aws"
8 changes: 8 additions & 0 deletions tests/test_empty_command/expected_plan_errors.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[
{
"variable": "data.local_file.awscli_results_file",
"problems": [
"usage: aws [options] <command> <subcommand> [<subcommand> ...] [parameters]\nTo see help text, you can run:\n\n aws help\n aws <command> help\n aws <command> <subcommand> help\n\naws: error: the following arguments are required: command"
]
}
]
4 changes: 4 additions & 0 deletions tests/test_empty_command/terraform.tfvars
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
aws_cli_commands = []
region = "eu-west-1"

alternative_path = "test-reports/test_empty_command/aws"
5 changes: 5 additions & 0 deletions tests/test_empty_command/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env bash

export ALLOW_APPLY=true

. tests/common.sh $0
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"result": "",
"result_raw": "aws-cli/2.17.19 Python/3.11.9 Linux/6.5.0-1025-azure exe/x86_64.ubuntu.22\n",
"result_was_decoded": false
}
6 changes: 6 additions & 0 deletions tests/test_version_retrieval_will_error/terraform.tfvars
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
aws_cli_commands = [
"--version",
]
region = "eu-west-1"

alternative_path = "test-reports/test_version_retrieval_will_error/aws"
5 changes: 5 additions & 0 deletions tests/test_version_retrieval_will_error/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env bash

export ALLOW_APPLY=true

. tests/common.sh $0
2 changes: 1 addition & 1 deletion tests/tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ rm -rf test-reports

underline 'Running tests' =

find ./tests -type f -name test.sh | sort | xargs -L 1 bash
find ./tests -type f -name test.sh -maxdepth 2 | sort | xargs -L 1 bash

0 comments on commit 7995cee

Please sign in to comment.