Skip to content

Commit

Permalink
fix: Data resources
Browse files Browse the repository at this point in the history
Data resources differ, in that the link is 'data.', stripping the
'data_' from the resource name. This fixes data refs
  • Loading branch information
techman83 committed Apr 10, 2024
1 parent f04e78b commit 0a1ff74
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/cally/cdk/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
Expand All @@ -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:
Expand Down Expand Up @@ -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,
Expand Down
11 changes: 11 additions & 0 deletions tests/stacks/test_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}',
)

0 comments on commit 0a1ff74

Please sign in to comment.