Skip to content

Commit c5641f3

Browse files
authored
Merge pull request #22 from CallyCo-io/fix/various
Fix Data Refs
2 parents 42cd96f + 0a1ff74 commit c5641f3

2 files changed

Lines changed: 41 additions & 27 deletions

File tree

src/cally/cdk/__init__.py

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
from copy import deepcopy
33
from dataclasses import dataclass, field, make_dataclass
44
from importlib import import_module
5-
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Tuple
5+
from types import MappingProxyType
6+
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Tuple, Union
67

78
from cdktf import (
89
App,
@@ -55,21 +56,21 @@ def to_dict(self) -> dict:
5556

5657
class CallyResource:
5758
_cdktf_resource: Any # This is probably a callable TerraformResource
58-
_identifier: Optional[str]
59+
_tf_identifier: Optional[str]
5960
_instantiated_resource: TerraformResource
6061
attributes: CallyResourceAttributes
6162
provider: str
6263
resource: str
63-
defaults: dict
64+
defaults: Union[dict, MappingProxyType]
6465

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

7071
def __str__(self) -> str:
71-
if self.identifier:
72-
return f'${{{self.resource}.{self.identifier}.id}}'
72+
if self.tf_identifier:
73+
return f'${{{self.tf_resource}.{self.tf_identifier}.id}}'
7374
return self.__class__.__name__
7475

7576
def __getattr__(self, item: str) -> Optional[str]:
@@ -78,8 +79,8 @@ def __getattr__(self, item: str) -> Optional[str]:
7879
return getattr(self._instantiated_resource, item)
7980
if item in {'attributes', 'defaults', '_instantiated_resource'}:
8081
return None
81-
if self.identifier:
82-
return f'${{{self.resource}.{self.identifier}.{item}}}'
82+
if self.tf_identifier:
83+
return f'${{{self.tf_resource}.{self.tf_identifier}.{item}}}'
8384
return None
8485

8586
def _get_attribute_default(self, name: str) -> Any:
@@ -88,7 +89,7 @@ def _get_attribute_default(self, name: str) -> Any:
8889
return deepcopy(self.defaults.get(name, None))
8990

9091
def _build_attributes(
91-
self, identifier: Optional[str] = None, **kwargs
92+
self, tf_identifier: Optional[str] = None, **kwargs
9293
) -> CallyResourceAttributes:
9394
func = self._cdktf_resource.__init__ # type: ignore
9495
parameters = inspect.signature(func).parameters
@@ -99,18 +100,24 @@ def _build_attributes(
99100
]
100101
name = f'{self.__class__.__name__}CallyAttributes'
101102
cls = make_dataclass(name, fields, bases=(CallyResourceAttributes,))
102-
if identifier:
103+
if tf_identifier:
103104
# Some newer provider releases appear to use 'id_'
104105
id_field = 'id'
105106
if 'id_' in parameters:
106107
id_field = 'id_'
107-
kwargs.update({id_field: identifier})
108-
self._identifier = identifier
108+
kwargs.update({id_field: tf_identifier})
109+
self._tf_identifier = tf_identifier
109110
return cls(**kwargs)
110111

111112
@property
112-
def identifier(self) -> Optional[str]:
113-
return self._identifier
113+
def tf_identifier(self) -> Optional[str]:
114+
return self._tf_identifier
115+
116+
@property
117+
def tf_resource(self) -> Optional[str]:
118+
if self.resource.startswith('data_'):
119+
return f'data.{self.resource[5:]}'
120+
return self.resource
114121

115122
def construct_resource(
116123
self,
@@ -139,8 +146,8 @@ class CallyStack:
139146
def __init__(self, service: 'CallyStackService') -> None:
140147
self.service = service
141148

142-
def add_output(self, identifier: str, output: str) -> None:
143-
self.outputs.append((identifier, output))
149+
def add_output(self, tf_identifier: str, output: str) -> None:
150+
self.outputs.append((tf_identifier, output))
144151

145152
def add_resource(self, resource: CallyResource) -> None:
146153
self.resources.append(resource)
@@ -208,8 +215,8 @@ def __init__(self, scope: Construct) -> None:
208215
self,
209216
provider=stack.get_provider(self, resource.provider),
210217
)
211-
for identifier, value in stack.outputs:
212-
TerraformOutput(self, identifier, value=value)
218+
for tf_identifier, value in stack.outputs:
219+
TerraformOutput(self, tf_identifier, value=value)
213220
stack.get_backend()(self, **stack.service.backend_config)
214221

215222
app = App(outdir=outdir)

tests/stacks/test_resources.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,8 @@ class StorageBucket(CallyResource):
2424
resource = 'storage_bucket'
2525
defaults = MappingProxyType({'location': 'AUSTRALIA-SOUTHEAST1'})
2626

27-
bucket_chips = StorageBucket(identifier='bucket', name='chips')
28-
bucket_fish = StorageBucket(
29-
identifier='bucketo', name='fish', depends_on=[bucket_chips]
30-
)
27+
bucket_chips = StorageBucket('bucket', name='chips')
28+
bucket_fish = StorageBucket('bucketo', name='fish', depends_on=[bucket_chips])
3129
stack.add_resources([bucket_chips, bucket_fish])
3230
result = self.synth_stack(stack)
3331
self.assertEqual(
@@ -52,9 +50,7 @@ class StorageBucket(CallyResource):
5250
defaults = MappingProxyType({'location': 'AUSTRALIA-SOUTHEAST1'})
5351

5452
stack.add_resource(
55-
StorageBucket(
56-
identifier='bucketo', name='fish', versioning=StorageBucketVersioning()
57-
)
53+
StorageBucket('bucketo', name='fish', versioning=StorageBucketVersioning())
5854
)
5955
result = self.synth_stack(stack)
6056
self.assertDictEqual(
@@ -91,7 +87,7 @@ class StorageBucket(CallyResource):
9187

9288
stack.add_resource(
9389
StorageBucket(
94-
identifier='bucketo',
90+
'bucketo',
9591
name='fish',
9692
lifecycle_rule=[
9793
StorageBucketLifecycleRule(
@@ -117,3 +113,14 @@ class StorageBucket(CallyResource):
117113
}
118114
],
119115
)
116+
117+
def test_data_resource_reference(self):
118+
class DataGoogleStorageBucket(CallyResource):
119+
provider = 'google'
120+
resource = 'data_google_storage_bucket'
121+
defaults = MappingProxyType({'location': 'AUSTRALIA-SOUTHEAST1'})
122+
123+
self.assertEqual(
124+
str(DataGoogleStorageBucket('bucketo', name='fish')),
125+
'${data.google_storage_bucket.bucketo.id}',
126+
)

0 commit comments

Comments
 (0)