Skip to content

Commit

Permalink
fix: bucket creation and parsing lists in recipe (#159)
Browse files Browse the repository at this point in the history
  • Loading branch information
uhinze authored Apr 13, 2023
1 parent 409df59 commit d248e58
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 14 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ Configure AWS CLI with your credentials as shown here - https://docs.aws.amazon.
"gdk_version": "1.0.0"
}
```
2. Replace `<PLACEHOLDER_AUTHOR>` with your name, `<PLACEHOLDER_BUCKET>` with a s3 bucket name and `<PLACEHOLDER_REGION>` with an aws region.
2. Replace `<PLACEHOLDER_AUTHOR>` with your name, `<PLACEHOLDER_BUCKET>` with a prefix for an Amazon S3 bucket name and `<PLACEHOLDER_REGION>` with an AWS region. The specified bucket will be created in the specified region if it doesn't exist (name format: `{PLACEHOLDER_BUCKET}-{PLACEHOLDER_REGION}-{account_number}`).
3. After replace these value the `gdk-config.json` file should look similar to:
```json
{
Expand Down
4 changes: 2 additions & 2 deletions gdk/aws_clients/S3Client.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,10 @@ def valid_bucket_for_artifacts_exists(self, bucket, region) -> bool:
)
except ClientError as exc:
error_code = exc.response["Error"]["Code"]
if error_code != "403" and error_code != "404":
if error_code != "AccessDenied" and error_code != "NoSuchBucket":
logging.error("Could not verify if the bucket '%s' exists in the region '%s'.", bucket, region)
raise
elif error_code == "403":
elif error_code == "AccessDenied":
logging.error(
"Bucket '%s' already exists and is not owned by you. Please provide a different name for the"
" bucket in the configuration.",
Expand Down
9 changes: 6 additions & 3 deletions gdk/common/CaseInsensitive.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import json
import yaml
from pathlib import Path

import yaml
from requests.structures import CaseInsensitiveDict as _CaseInsensitiveDict


Expand All @@ -27,7 +27,9 @@ def _convert_nested_dict(self, case_insensitive_dict: _CaseInsensitiveDict) -> N
if isinstance(value, dict):
case_insensitive_dict.update({key: CaseInsensitiveDict(value)})
elif isinstance(value, list):
case_insensitive_dict.update({key: [CaseInsensitiveDict(val) for val in value if isinstance(val, dict)]})
case_insensitive_dict.update(
{key: [CaseInsensitiveDict(val) if isinstance(val, dict) else val for val in value]}
)

def _convert_nested_case_insensitive_dict(self, dictObj: dict) -> dict:
for key, value in dictObj.items():
Expand All @@ -38,8 +40,9 @@ def _convert_nested_case_insensitive_dict(self, dictObj: dict) -> dict:
{
key: [
self._convert_nested_case_insensitive_dict(dict(val))
for val in value
if isinstance(val, CaseInsensitiveDict)
else val
for val in value
]
}
)
Expand Down
6 changes: 3 additions & 3 deletions tests/gdk/aws_clients/test_S3Client.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ def test_valid_bucket_for_artifacts_exists_owned_by_someone(self):

def throw_err(*args, **kwargs):
ex = boto3.client("s3").exceptions.ClientError(
{"Error": {"Code": "403", "Message": "Forbidden"}}, "GetBucketLocation"
{"Error": {"Code": "AccessDenied", "Message": "Forbidden"}}, "GetBucketLocation"
)
raise ex

Expand All @@ -146,15 +146,15 @@ def throw_err(*args, **kwargs):
s3_client_utils.valid_bucket_for_artifacts_exists(bucket, region)

assert mock_get_bucket_location.call_args_list == [call(Bucket=bucket)]
assert "An error occurred (403) when calling the GetBucketLocation" in str(e.value.args[0])
assert "An error occurred (AccessDenied) when calling the GetBucketLocation" in str(e.value.args[0])

def test_valid_bucket_for_artifacts_exists_not_exists(self):
bucket = "test-bucket"
region = "region"

def throw_err(*args, **kwargs):
ex = boto3.client("s3").exceptions.ClientError(
{"Error": {"Code": "404", "Message": "Not Found"}}, "GetBucketLocation"
{"Error": {"Code": "NoSuchBucket", "Message": "Not Found"}}, "GetBucketLocation"
)
raise ex

Expand Down
12 changes: 8 additions & 4 deletions tests/gdk/common/test_CaseInsensitive.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import json
import tempfile
from pathlib import Path
from unittest import TestCase
import tempfile

import pytest
import yaml
from gdk.common.CaseInsensitive import CaseInsensitiveRecipeFile
import json
from gdk.common.CaseInsensitive import CaseInsensitiveDict

from gdk.common.CaseInsensitive import (CaseInsensitiveDict,
CaseInsensitiveRecipeFile)


class CaseInsensitiveRecipeFileTest(TestCase):
Expand Down Expand Up @@ -79,11 +81,13 @@ def test_convert_dict_to_CaseInsensitiveDict(self):
"key1": "value1",
"key2": [{"key21": "value21"}, {"key22": "value22"}],
"key3": {"key31": {"key311": "key312"}},
"key4": ["entry1", "entry2"]
}
cis = CaseInsensitiveDict(dictionary)
assert "KEY1" in cis
assert cis["KEY2"][0]["KEy21"] == "value21"
assert cis["KEy3"]["key31"]["KeY311"] == "key312"
assert len(cis["key4"]) == 2
assert cis.to_dict() == dictionary

def test_when_update_value_then_key_not_changed(self):
Expand Down
30 changes: 29 additions & 1 deletion tests/gdk/static/project_utils/valid_component_recipe.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,35 @@
"ComponentPublisher": "Amazon",
"ComponentConfiguration": {
"DefaultConfiguration": {
"Message": "world"
"Message": "world",
"SampleList": [
"1",
"2",
"3"
],
"SampleNestedList": [
[
"1"
],
[
"2"
],
[
"3"
]
],
"SampleMap": {
"key1": "value1",
"key2": {
"key3": [
"value2",
"value3"
],
"key4": {
"key41": "value4"
}
}
}
}
},
"Manifests": [
Expand Down
16 changes: 16 additions & 0 deletions tests/gdk/static/project_utils/valid_component_recipe.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,22 @@ ComponentPublisher: Amazon
ComponentConfiguration:
DefaultConfiguration:
Message: world
SampleList:
- '1'
- '2'
- '3'
SampleNestedList:
- - '1'
- - '2'
- - '3'
SampleMap:
key1: value1
key2:
key3:
- value2
- value3
key4:
key41: value4
Manifests:
- Platform:
os: linux
Expand Down

0 comments on commit d248e58

Please sign in to comment.