22from copy import deepcopy
33from dataclasses import dataclass , field , make_dataclass
44from 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
78from cdktf import (
89 App ,
@@ -55,21 +56,21 @@ def to_dict(self) -> dict:
5556
5657class 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 )
0 commit comments