Skip to content

Commit

Permalink
refactor: identifier -> tf_identifier
Browse files Browse the repository at this point in the history
At some point this will bring us unstuck. The risk of a resource having
'tf_identifier' as a property is much lower than 'identifier'. The examples
all show passing it as an arg, but it's technically a breaking change

BREAKING CHANGES:

Any resource instantiated with 'identifier=' will no longer work. It is
recommended that this be passed in as an argument, as it may change in
the future.

Before:

RandomPet(indentifier='random-pet-id')

After:

RandomPet('random-pet-id')
  • Loading branch information
techman83 committed Apr 10, 2024
1 parent 24bf313 commit f04e78b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 25 deletions.
34 changes: 17 additions & 17 deletions src/cally/cdk/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,21 +56,21 @@ def to_dict(self) -> dict:

class CallyResource:
_cdktf_resource: Any # This is probably a callable TerraformResource
_identifier: Optional[str]
_tf_identifier: Optional[str]
_instantiated_resource: TerraformResource
attributes: CallyResourceAttributes
provider: str
resource: str
defaults: Union[dict, MappingProxyType]

def __init__(self, identifier: Optional[str] = None, **kwargs) -> None:
def __init__(self, tf_identifier: Optional[str] = None, **kwargs) -> None:
module = import_module(f'cally.providers.{self.provider}.{self.resource}')
self._cdktf_resource = getattr(module, self.__class__.__name__)
self.attributes = self._build_attributes(identifier, **kwargs)
self.attributes = self._build_attributes(tf_identifier, **kwargs)

def __str__(self) -> str:
if self.identifier:
return f'${{{self.resource}.{self.identifier}.id}}'
if self.tf_identifier:
return f'${{{self.resource}.{self.tf_identifier}.id}}'
return self.__class__.__name__

def __getattr__(self, item: str) -> Optional[str]:
Expand All @@ -79,8 +79,8 @@ def __getattr__(self, item: str) -> Optional[str]:
return getattr(self._instantiated_resource, item)
if item in {'attributes', 'defaults', '_instantiated_resource'}:
return None
if self.identifier:
return f'${{{self.resource}.{self.identifier}.{item}}}'
if self.tf_identifier:
return f'${{{self.resource}.{self.tf_identifier}.{item}}}'
return None

def _get_attribute_default(self, name: str) -> Any:
Expand All @@ -89,7 +89,7 @@ def _get_attribute_default(self, name: str) -> Any:
return deepcopy(self.defaults.get(name, None))

def _build_attributes(
self, identifier: Optional[str] = None, **kwargs
self, tf_identifier: Optional[str] = None, **kwargs
) -> CallyResourceAttributes:
func = self._cdktf_resource.__init__ # type: ignore
parameters = inspect.signature(func).parameters
Expand All @@ -100,18 +100,18 @@ def _build_attributes(
]
name = f'{self.__class__.__name__}CallyAttributes'
cls = make_dataclass(name, fields, bases=(CallyResourceAttributes,))
if identifier:
if tf_identifier:
# Some newer provider releases appear to use 'id_'
id_field = 'id'
if 'id_' in parameters:
id_field = 'id_'
kwargs.update({id_field: identifier})
self._identifier = identifier
kwargs.update({id_field: tf_identifier})
self._tf_identifier = tf_identifier
return cls(**kwargs)

@property
def identifier(self) -> Optional[str]:
return self._identifier
def tf_identifier(self) -> Optional[str]:
return self._tf_identifier

def construct_resource(
self,
Expand Down Expand Up @@ -140,8 +140,8 @@ class CallyStack:
def __init__(self, service: 'CallyStackService') -> None:
self.service = service

def add_output(self, identifier: str, output: str) -> None:
self.outputs.append((identifier, output))
def add_output(self, tf_identifier: str, output: str) -> None:
self.outputs.append((tf_identifier, output))

def add_resource(self, resource: CallyResource) -> None:
self.resources.append(resource)
Expand Down Expand Up @@ -209,8 +209,8 @@ def __init__(self, scope: Construct) -> None:
self,
provider=stack.get_provider(self, resource.provider),
)
for identifier, value in stack.outputs:
TerraformOutput(self, identifier, value=value)
for tf_identifier, value in stack.outputs:
TerraformOutput(self, tf_identifier, value=value)
stack.get_backend()(self, **stack.service.backend_config)

app = App(outdir=outdir)
Expand Down
12 changes: 4 additions & 8 deletions tests/stacks/test_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,8 @@ class StorageBucket(CallyResource):
resource = 'storage_bucket'
defaults = MappingProxyType({'location': 'AUSTRALIA-SOUTHEAST1'})

bucket_chips = StorageBucket(identifier='bucket', name='chips')
bucket_fish = StorageBucket(
identifier='bucketo', name='fish', depends_on=[bucket_chips]
)
bucket_chips = StorageBucket('bucket', name='chips')
bucket_fish = StorageBucket('bucketo', name='fish', depends_on=[bucket_chips])
stack.add_resources([bucket_chips, bucket_fish])
result = self.synth_stack(stack)
self.assertEqual(
Expand All @@ -52,9 +50,7 @@ class StorageBucket(CallyResource):
defaults = MappingProxyType({'location': 'AUSTRALIA-SOUTHEAST1'})

stack.add_resource(
StorageBucket(
identifier='bucketo', name='fish', versioning=StorageBucketVersioning()
)
StorageBucket('bucketo', name='fish', versioning=StorageBucketVersioning())
)
result = self.synth_stack(stack)
self.assertDictEqual(
Expand Down Expand Up @@ -91,7 +87,7 @@ class StorageBucket(CallyResource):

stack.add_resource(
StorageBucket(
identifier='bucketo',
'bucketo',
name='fish',
lifecycle_rule=[
StorageBucketLifecycleRule(
Expand Down

0 comments on commit f04e78b

Please sign in to comment.