Skip to content

Commit

Permalink
fix: Data/Resource Referencing
Browse files Browse the repository at this point in the history
Links were not being generated correctly, this prepends the provider where
relevant.
  • Loading branch information
techman83 committed Nov 24, 2024
1 parent 1c4aece commit 1b1dd75
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 19 deletions.
14 changes: 12 additions & 2 deletions src/cally/cdk/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,10 @@ def __init__(self, tf_identifier: Optional[str] = None, **kwargs) -> None:
self.attributes = self._build_attributes(tf_identifier, **kwargs)

def __str__(self) -> str:
if self.tf_identifier:
if self.tf_identifier and self.tf_data_resource:
return f'${{{self.tf_resource}.{self.tf_identifier}.id}}'
if self.tf_identifier:
return f'${{{self.provider}_{self.tf_resource}.{self.tf_identifier}.id}}'
return self.__class__.__name__

def __getattr__(self, item: str) -> Optional[str]:
Expand All @@ -104,8 +106,12 @@ def __getattr__(self, item: str) -> Optional[str]:
return getattr(self._instantiated_resource, item)
if item in {'attributes', 'defaults', '_instantiated_resource'}:
return None
if self.tf_identifier:
if self.tf_identifier and self.tf_data_resource:
return f'${{{self.tf_resource}.{self.tf_identifier}.{item}}}'
if self.tf_identifier:
return (
f'${{{self.provider}_{self.tf_resource}.{self.tf_identifier}.{item}}}'
)
return None

def _get_attribute_default(self, name: str) -> Any:
Expand Down Expand Up @@ -134,6 +140,10 @@ def _build_attributes(
self._tf_identifier = tf_identifier
return cls(**kwargs)

@property
def tf_data_resource(self) -> bool:
return self.resource.startswith('data_')

@property
def tf_identifier(self) -> Optional[str]:
return self._tf_identifier
Expand Down
16 changes: 0 additions & 16 deletions tests/stacks/test_providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,3 @@ class Pet(CallyResource):
self.assertEqual(
result.get('provider', {}).get('random', {})[0].get('alias', ''), 'foo'
)

def test_resource_identifier(self):
class Pet(CallyResource):
provider = 'random'
resource = 'pet'

self.assertEqual('${pet.random-pet.id}', str(Pet('random-pet')))

def test_resource_identifier_underscore(self):
class PubsubTopic(CallyResource):
provider = 'google'
resource = 'pubsub_topic'

self.assertEqual(
'${pubsub_topic.random-topic.id}', str(PubsubTopic('random-topic'))
)
44 changes: 43 additions & 1 deletion tests/stacks/test_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ class StorageBucket(CallyResource):
],
)

def test_data_resource_reference(self):
def test_data_resource_reference_id(self):
class DataGoogleStorageBucket(CallyResource):
provider = 'google'
resource = 'data_google_storage_bucket'
Expand All @@ -124,3 +124,45 @@ class DataGoogleStorageBucket(CallyResource):
str(DataGoogleStorageBucket('bucketo', name='fish')),
'${data.google_storage_bucket.bucketo.id}',
)

def test_data_resource_reference(self):
class DataGoogleStorageBucket(CallyResource):
provider = 'google'
resource = 'data_google_storage_bucket'
defaults = MappingProxyType({'location': 'AUSTRALIA-SOUTHEAST1'})

self.assertEqual(
DataGoogleStorageBucket('bucketo', name='fish').name,
'${data.google_storage_bucket.bucketo.name}',
)

def test_resource_reference_id(self):
class StorageBucket(CallyResource):
provider = 'google'
resource = 'storage_bucket'
defaults = MappingProxyType({'location': 'AUSTRALIA-SOUTHEAST1'})

self.assertEqual(
str(StorageBucket('bucketo', name='fish')),
'${google_storage_bucket.bucketo.id}',
)

def test_resource_reference(self):
class StorageBucket(CallyResource):
provider = 'google'
resource = 'storage_bucket'
defaults = MappingProxyType({'location': 'AUSTRALIA-SOUTHEAST1'})

self.assertEqual(
StorageBucket('bucketo', name='fish').name,
'${google_storage_bucket.bucketo.name}',
)

def test_resource_reference_underscore(self):
class PubsubTopic(CallyResource):
provider = 'google'
resource = 'pubsub_topic'

self.assertEqual(
'${google_pubsub_topic.random-topic.id}', str(PubsubTopic('random-topic'))
)

0 comments on commit 1b1dd75

Please sign in to comment.