Skip to content
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

feat: Attach connectors to source #2772

Merged
merged 36 commits into from
Jan 24, 2023
Merged
Changes from 3 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
932ddbf
attach connector to its source
aaythapa Jan 4, 2023
45155b1
Merge branch 'aws:develop' into attach-connectors-to-source
aaythapa Jan 6, 2023
c76f6cd
error msg change
aaythapa Jan 6, 2023
672501c
address feedback
aaythapa Jan 10, 2023
3a0abe9
address feedback
aaythapa Jan 10, 2023
82d3083
Merge branch 'aws:develop' into attach-connectors-to-source
aaythapa Jan 17, 2023
a4746b9
fix error handling
aaythapa Jan 17, 2023
381e62b
Merge branch 'aws:develop' into attach-connectors-to-source
aaythapa Jan 17, 2023
3b9afa2
unit tests
aaythapa Jan 18, 2023
753afd3
unit tests
aaythapa Jan 18, 2023
4836603
schema update
aaythapa Jan 18, 2023
bf24177
type hints
aaythapa Jan 18, 2023
47f4e33
error unit tests
aaythapa Jan 18, 2023
b7e7f20
change transformed connector logical id to source_logical_id + connec…
aaythapa Jan 18, 2023
e1a2733
Merge branch 'aws:develop' into attach-connectors-to-source
aaythapa Jan 19, 2023
639f499
integration test + address feedback
aaythapa Jan 19, 2023
54df48f
Merge branch 'develop' into attach-connectors-to-source
aaythapa Jan 20, 2023
5009404
schema changes
aaythapa Jan 21, 2023
7048f80
schema changes
aaythapa Jan 21, 2023
7e6e034
Merge branch 'develop' into attach-connectors-to-source
aaythapa Jan 21, 2023
e841698
DependsOn unit test
aaythapa Jan 23, 2023
b593fd2
connector schema
aaythapa Jan 23, 2023
a2636a5
connector schema v2
aaythapa Jan 23, 2023
7a42e33
feat: Support RuntimeManagementConfig
aahung Jan 23, 2023
68becd7
import fix
aaythapa Jan 23, 2023
cf041b2
chore: bump version to 1.58.0
aws-sam-cli-bot Jan 23, 2023
c1eb8e4
Merge remote-tracking branch 'upstream/develop' into release-v1.58.0
aahung Jan 23, 2023
996ba0d
Merge pull request #2802 from aws/release-v1.58.0
aahung Jan 23, 2023
fd156fd
schema fix
aaythapa Jan 23, 2023
b78f897
Merge branch 'develop' into attach-connectors-to-source
aaythapa Jan 23, 2023
f2c1f57
error msg
aaythapa Jan 23, 2023
76c9189
Merge branch 'develop' into attach-connectors-to-source
aaythapa Jan 23, 2023
f711d0f
address feedback
aaythapa Jan 24, 2023
e92d625
address feedback
aaythapa Jan 24, 2023
802f4a2
address feedback
aaythapa Jan 24, 2023
f4055a6
additional tests
aaythapa Jan 24, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions samtranslator/translator/translator.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,9 @@ def translate(

self.sam_parser.parse(sam_template=sam_template, parameter_values=parameter_values, sam_plugins=sam_plugins)

updated_resources = add_embedded_connectors(sam_template=sam_template)
aaythapa marked this conversation as resolved.
Show resolved Hide resolved
sam_template["Resources"].update(updated_resources)

template = copy.deepcopy(sam_template)
macro_resolver = ResourceTypeResolver(sam_resources)
intrinsics_resolver = IntrinsicsResolver(parameter_values)
Expand Down Expand Up @@ -269,6 +272,64 @@ def _get_resources_to_iterate(
return functions + statemachines + apis + others + connectors


def make_error(msg: str) -> Exception:
aaythapa marked this conversation as resolved.
Show resolved Hide resolved
return InvalidDocumentException([InvalidTemplateException(msg)])


def add_embedded_connectors(sam_template: Dict[str, Any]) -> Any:
resources = sam_template.get("Resources", {})
connectors_list = []

# Loop through the resources in the template and see if any connectors have been attached
for source_logicalId, resource in resources.items():
aaythapa marked this conversation as resolved.
Show resolved Hide resolved
if "Connectors" in resource:
aaythapa marked this conversation as resolved.
Show resolved Hide resolved
if not isinstance(resource["Connectors"], dict):
raise make_error("'Connectors' must be a dict")
aaythapa marked this conversation as resolved.
Show resolved Hide resolved

# Go through each of the connectors that have been attached and create a Serverless Connector resource
for connector_logicalId, connector_dict in resource["Connectors"].items():
aaythapa marked this conversation as resolved.
Show resolved Hide resolved
if not isinstance(connector_dict, dict):
raise make_error("Invalid 'Connectors' type. Must be a dict.")

data = get_generated_connector(source_logicalId, connector_logicalId, connector_dict)
aaythapa marked this conversation as resolved.
Show resolved Hide resolved
# add to the list of generated connectors
connectors_list.append(data)

# delete connectors key, so it doesn't show up in the transformed template
del resource["Connectors"]

# go through the list of generated connectors and add it to resources dict
for connector_logicalId, connector_dict in connectors_list:
if connector_logicalId in resources:
raise make_error(
f"A resource with the id {connector_logicalId} already exists in the "
aaythapa marked this conversation as resolved.
Show resolved Hide resolved
f"resource. Please use a different id for that resource.",
)
aaythapa marked this conversation as resolved.
Show resolved Hide resolved
resources[connector_logicalId] = connector_dict

return resources


def get_generated_connector(
source_logicalId: str, connector_logicalId: str, connector_dict: Dict[str, Any]
) -> Tuple[str, Dict[str, Any]]:
connector = connector_dict
aaythapa marked this conversation as resolved.
Show resolved Hide resolved
connector["Type"] = "AWS::Serverless::Connector"
aaythapa marked this conversation as resolved.
Show resolved Hide resolved

# No need to raise an error for this instance as the error will be caught by the parser
if isinstance(connector["Properties"], dict):
aaythapa marked this conversation as resolved.
Show resolved Hide resolved
connector["Properties"]["Source"] = {"Id": source_logicalId}

if "SourceReference" in connector["Properties"]:
if not isinstance(connector["Properties"]["SourceReference"], dict):
raise make_error(f"The SourceReference property for {connector_logicalId} must be a dict")

connector["Properties"]["Source"].update(connector["Properties"]["SourceReference"])
del connector["Properties"]["SourceReference"]

return connector_logicalId, connector
aaythapa marked this conversation as resolved.
Show resolved Hide resolved


def prepare_plugins(plugins: List[Any], parameters: Optional[Dict[str, Any]] = None) -> SamPlugins:
"""
Creates & returns a plugins object with the given list of plugins installed. In addition to the given plugins,
Expand Down