From 0a1ff74d69c6e872a5bf5be9f39a593ecc0efd3d Mon Sep 17 00:00:00 2001 From: Leon Wright Date: Wed, 10 Apr 2024 16:55:30 +0800 Subject: [PATCH] fix: Data resources Data resources differ, in that the link is 'data.', stripping the 'data_' from the resource name. This fixes data refs --- src/cally/cdk/__init__.py | 10 ++++++++-- tests/stacks/test_resources.py | 11 +++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/cally/cdk/__init__.py b/src/cally/cdk/__init__.py index 697ffb3..aff3787 100644 --- a/src/cally/cdk/__init__.py +++ b/src/cally/cdk/__init__.py @@ -70,7 +70,7 @@ def __init__(self, tf_identifier: Optional[str] = None, **kwargs) -> None: def __str__(self) -> str: if self.tf_identifier: - return f'${{{self.resource}.{self.tf_identifier}.id}}' + return f'${{{self.tf_resource}.{self.tf_identifier}.id}}' return self.__class__.__name__ def __getattr__(self, item: str) -> Optional[str]: @@ -80,7 +80,7 @@ def __getattr__(self, item: str) -> Optional[str]: if item in {'attributes', 'defaults', '_instantiated_resource'}: return None if self.tf_identifier: - return f'${{{self.resource}.{self.tf_identifier}.{item}}}' + return f'${{{self.tf_resource}.{self.tf_identifier}.{item}}}' return None def _get_attribute_default(self, name: str) -> Any: @@ -113,6 +113,12 @@ def _build_attributes( def tf_identifier(self) -> Optional[str]: return self._tf_identifier + @property + def tf_resource(self) -> Optional[str]: + if self.resource.startswith('data_'): + return f'data.{self.resource[5:]}' + return self.resource + def construct_resource( self, scope: Optional[Construct] = None, diff --git a/tests/stacks/test_resources.py b/tests/stacks/test_resources.py index 6c0f8f0..6692569 100644 --- a/tests/stacks/test_resources.py +++ b/tests/stacks/test_resources.py @@ -113,3 +113,14 @@ class StorageBucket(CallyResource): } ], ) + + def test_data_resource_reference(self): + class DataGoogleStorageBucket(CallyResource): + provider = 'google' + resource = 'data_google_storage_bucket' + defaults = MappingProxyType({'location': 'AUSTRALIA-SOUTHEAST1'}) + + self.assertEqual( + str(DataGoogleStorageBucket('bucketo', name='fish')), + '${data.google_storage_bucket.bucketo.id}', + )