3 O4Â\Á-ã@s@ddlmZmZmZmZdd„eeegDƒZGdd„deƒZdS)é)ÚActionÚ SubActionÚ RefActionÚ GetAttActioncCsi|]}|ƒ|j“qS©)Zintrinsic_name)Ú.0Úactionrrú‰/Users/sshvans/Documents/Work/Quickstart/python-virtual-environments/env/lib/python3.6/site-packages/samtranslator/intrinsics/resolver.pyú sr c@sheZdZefdd„Zdd„Zdd„Zdd„Zd d „Zd d „Z d d„Z dd„Z dd„Z dd„Z dd„ZdS)ÚIntrinsicsResolvercCsX|dkst|tƒ rtdƒ‚t|tƒ s@tdd„|jƒDƒƒ rHtdƒ‚||_||_dS)at Instantiate the resolver :param dict parameters: Map of parameter names to their values :param dict supported_intrinsics: Dictionary of intrinsic functions this class supports along with the Action class that can process this intrinsic :raises TypeError: If parameters or the supported_intrinsics arguments are invalid Nz%parameters must be a valid dictionarycSsg|]}t|tƒ‘qSr)Ú isinstancer)rÚvaluerrr ú sz/IntrinsicsResolver.__init__..zUsupported_intrinsics argument must be intrinsic names to corresponding Action classes)r ÚdictÚ TypeErrorÚallÚvaluesÚsupported_intrinsicsÚ parameters)Úselfrrrrr Ú__init__ s  zIntrinsicsResolver.__init__cCs|j||j|jƒS)ae Resolves references to parameters within the given dictionary recursively. Other intrinsic functions such as !GetAtt, !Sub or !Ref to non-parameters will be left untouched. Result is a dictionary where parameter values are inlined. Don't pass this dictionary directly into transform's output because it changes the template structure by inlining parameter values. :param input: Any primitive type (dict, array, string etc) whose values might contain intrinsic functions :return: A copy of a dictionary with parameter references replaced by actual value. )Ú _traverserÚ_try_resolve_parameter_refs)rÚinputrrr Úresolve_parameter_refss z)IntrinsicsResolver.resolve_parameter_refscCs|j|||jƒS)a5 Customers can provide a reference to a "derived" SAM resource such as Alias of a Function or Stage of an API resource. This method recursively walks the tree, converting all derived references to the real resource name, if it is present. Example: {"Ref": "MyFunction.Alias"} -> {"Ref": "MyFunctionAliasLive"} This method does not attempt to validate a reference. If it is invalid or non-resolvable, it skips the occurrence and continues with the rest. It is recommended that you have an external process that detects and surfaces invalid references. For first call, it is recommended that `template` is the entire CFN template in order to handle references in Mapping or Output sections. :param dict input: CFN template that needs resolution. This method will modify the input directly resolving references. In subsequent recursions, this will be a fragment of the CFN template. :param SupportedResourceReferences supported_resource_refs: Object that contains information about the resource references supported in this SAM template, along with the value they should resolve to. :return list errors: List of dictionary containing information about invalid reference. Empty list otherwise )rÚ_try_resolve_sam_resource_refs)rrÚsupported_resource_refsrrr Úresolve_sam_resource_refs+sz,IntrinsicsResolver.resolve_sam_resource_refscCs|j|||jƒS)a² Some SAM resources have their logical ids mutated from the original id that the customer writes in the template. This method recursively walks the tree and updates these logical ids from the old value to the new value that is generated by SAM. Example: {"Ref": "MyLayer"} -> {"Ref": "MyLayerABC123"} This method does not attempt to validate a reference. If it is invalid or non-resolvable, it skips the occurrence and continues with the rest. It is recommended that you have an external process that detects and surfaces invalid references. For first call, it is recommended that `template` is the entire CFN template in order to handle references in Mapping or Output sections. :param dict input: CFN template that needs resolution. This method will modify the input directly resolving references. In subsequent recursions, this will be a fragment of the CFN template. :param dict supported_resource_id_refs: Dictionary that maps old logical ids to new ones. :return list errors: List of dictionary containing information about invalid reference. Empty list otherwise )rÚ!_try_resolve_sam_resource_id_refs)rrÚsupported_resource_id_refsrrr Úresolve_sam_resource_id_refsCsz/IntrinsicsResolver.resolve_sam_resource_id_refscCsRt|ƒdkr|S|||ƒ}t|tƒr2|j|||ƒSt|tƒrJ|j|||ƒS|SdS)aÕ Driver method that performs the actual traversal of input and calls the appropriate `resolver_method` when to perform the resolution. :param input: Any primitive type (dict, array, string etc) whose value might contain an intrinsic function :param resolution_data: Data that will help with resolution. For example, when resolving parameter references, this object will contain a dictionary of parameter names and their values. :param resolver_method: Method that will be called to actually resolve an intrinsic function. This method is called with the parameters `(input, resolution_data)`. :return: Modified `input` with intrinsics resolved rN)Úlenr rÚ_traverse_dictÚlistÚ_traverse_list)rrÚresolution_dataÚresolver_methodrrr rZs    zIntrinsicsResolver._traversecCs,x&|jƒD]\}}|j|||ƒ||<q W|S)aƒ Traverse a dictionary to resolve intrinsic functions on every value :param input_dict: Input dictionary to traverse :param resolution_data: Data that the `resolver_method` needs to operate :param resolver_method: Method that can actually resolve an intrinsic function, if it detects one :return: Modified dictionary with values resolved )Úitemsr)rZ input_dictr%r&Úkeyr rrr r"†s z!IntrinsicsResolver._traverse_dictcCs,x&t|ƒD]\}}|j|||ƒ||<q W|S)aw Traverse a list to resolve intrinsic functions on every element :param input_list: List of input :param resolution_data: Data that the `resolver_method` needs to operate :param resolver_method: Method that can actually resolve an intrinsic function, if it detects one :return: Modified list with intrinsic functions resolved )Ú enumerater)rZ input_listr%r&Úindexr rrr r$”s z!IntrinsicsResolver._traverse_listcCs0|j|ƒs|St|jƒƒd}|j|j||ƒS)aö Try to resolve parameter references on the given input object. The object could be of any type. If the input is not in the format used by intrinsics (ie. dictionary with one key), input is returned unmodified. If the single key in dictionary is one of the supported intrinsic function types, go ahead and try to resolve it. :param input: Input object to resolve :param parameters: Parameter values used to for ref substitution :return: r)Ú_is_intrinsic_dictr#Úkeysrr)rrrÚ function_typerrr r¢s z.IntrinsicsResolver._try_resolve_parameter_refscCs0|j|ƒs|St|jƒƒd}|j|j||ƒS)aK Try to resolve SAM resource references on the given template. If the given object looks like one of the supported intrinsics, it calls the appropriate resolution on it. If not, this method returns the original input unmodified. :param dict input: Dictionary that may represent an intrinsic function :param SupportedResourceReferences supported_resource_refs: Object containing information about available resource references and the values they resolve to. :return: Modified input dictionary with references resolved r)r+r#r,rZresolve_resource_refs)rrrr-rrr r³s z1IntrinsicsResolver._try_resolve_sam_resource_refscCs0|j|ƒs|St|jƒƒd}|j|j||ƒS)a Try to resolve SAM resource id references on the given template. If the given object looks like one of the supported intrinsics, it calls the appropriate resolution on it. If not, this method returns the original input unmodified. :param dict input: Dictionary that may represent an intrinsic function :param dict supported_resource_id_refs: Dictionary that maps old logical ids to new ones. :return: Modified input dictionary with id references resolved r)r+r#r,rZresolve_resource_id_refs)rrrr-rrr rÄs z4IntrinsicsResolver._try_resolve_sam_resource_id_refscCs,t|tƒo*t|ƒdko*t|jƒƒd|jkS)zÐ Can the input represent an intrinsic function in it? :param input: Object to be checked :return: True, if the input contains a supported intrinsic function. False otherwise ér)r rr!r#r,r)rrrrr r+Ôs  z%IntrinsicsResolver._is_intrinsic_dictN)Ú__name__Ú __module__Ú __qualname__ÚDEFAULT_SUPPORTED_INTRINSICSrrrr rr"r$rrrr+rrrr r s  ,r N)Z samtranslator.intrinsics.actionsrrrrr2Úobjectr rrrr Ús