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

Adding metadata property for unsupported connector profile #2879

Merged
merged 6 commits into from
Feb 9, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
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
2 changes: 2 additions & 0 deletions samtranslator/model/connector/connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
],
)

UNSUPPORTED_CONNECTOR_PROFILE_TYPE = "UNSUPPORTED_CONNECTOR_PROFILE_TYPE"


class ConnectorResourceError(Exception):
"""
Expand Down
28 changes: 26 additions & 2 deletions samtranslator/model/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from abc import ABC, abstractmethod
from collections import defaultdict
from enum import Enum
from typing import List, Optional, Sequence, Union
from typing import Any, Dict, List, Optional, Sequence, Union


class ExpectedType(Enum):
Expand All @@ -16,12 +17,17 @@ class ExceptionWithMessage(ABC, Exception):
def message(self) -> str:
"""Return the exception message."""

@property
xazhao marked this conversation as resolved.
Show resolved Hide resolved
def metadata(self) -> Optional[Dict[str, Any]]:
"""Return the exception metadata."""


class InvalidDocumentException(ExceptionWithMessage):
"""Exception raised when the given document is invalid and cannot be transformed.

Attributes:
message -- explanation of the error
metadata -- a dictionary of metadata (key, value pair)
causes -- list of errors which caused this document to be invalid
"""

Expand All @@ -37,6 +43,17 @@ def message(self) -> str:
len(self.causes)
)

@property
GavinZZ marked this conversation as resolved.
Show resolved Hide resolved
def metadata(self) -> Dict[str, List[Any]]:
# Merge metadata in each exception to one single metadata dictionary
metadata_dict = defaultdict(list)
for cause in self.causes:
if not cause.metadata:
continue
for k, v in cause.metadata.items():
metadata_dict[k].append(v)
return metadata_dict

@property
def causes(self) -> Sequence[ExceptionWithMessage]:
return self._causes
Expand Down Expand Up @@ -86,9 +103,12 @@ class InvalidResourceException(ExceptionWithMessage):
message -- explanation of the error
"""

def __init__(self, logical_id: Union[str, List[str]], message: str) -> None:
def __init__(
self, logical_id: Union[str, List[str]], message: str, metadata: Optional[Dict[str, Any]] = None
) -> None:
self._logical_id = logical_id
self._message = message
self._metadata = metadata

def __lt__(self, other): # type: ignore[no-untyped-def]
return self._logical_id < other._logical_id
Expand All @@ -97,6 +117,10 @@ def __lt__(self, other): # type: ignore[no-untyped-def]
def message(self) -> str:
return "Resource with id [{}] is invalid. {}".format(self._logical_id, self._message)

@property
def metadata(self) -> Optional[Dict[str, Any]]:
return self._metadata


class InvalidResourcePropertyTypeException(InvalidResourceException):
def __init__(
Expand Down
2 changes: 2 additions & 0 deletions samtranslator/model/sam_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
from samtranslator.model.architecture import ARM64, X86_64
from samtranslator.model.cloudformation import NestedStack
from samtranslator.model.connector.connector import (
UNSUPPORTED_CONNECTOR_PROFILE_TYPE,
ConnectorResourceError,
ConnectorResourceReference,
add_depends_on,
Expand Down Expand Up @@ -1861,6 +1862,7 @@ def generate_resources(
raise InvalidResourceException(
self.logical_id,
f"Unable to create connector from {source.resource_type} to {destination.resource_type}; it's not supported or the template is invalid.",
{UNSUPPORTED_CONNECTOR_PROFILE_TYPE: {source.resource_type: destination.resource_type}},
)

# removing duplicate permissions
Expand Down