def _resolve_get_param(s, params, transform=None): """ Resolve constructs of the form { get_param: my_param } """ def match_param_ref(key, value): return (key == 'get_param' and value is not None) def handle_param_ref(args): try: if not isinstance(args, list): args = [args] parameter = params[args[0]] try: for inner_param in args[1:]: if hasattr(parameter, str(inner_param)): parameter = getattr(parameter, inner_param) else: parameter = parameter[inner_param] return parameter except (KeyError, IndexError, TypeError): return '' except (KeyError, ValueError): raise exception.UserParameterMissing(key=args[0]) return template._resolve(match_param_ref, handle_param_ref, s, transform)
def resolve_replace(s): """ Resolve template string substitution via function str_replace Resolves the str_replace function of the form str_replace: template: <string template> params: <param dictionary> """ def handle_str_replace(args): if not isinstance(args, dict): raise TypeError('Arguments to "str_replace" must be a' "dictionary") try: template = args["template"] params = args["params"] except KeyError: example = """str_replace: template: This is $var1 template $var2 params: - var1: a - var2: string""" raise KeyError('"str_replace" syntax should be %s' % example) if not isinstance(template, basestring): raise TypeError('"template" parameter must be a string') if not isinstance(params, dict): raise TypeError('"params" parameter must be a dictionary') return Template(template).substitute(params) return template._resolve(lambda k, v: k == "str_replace", handle_str_replace, s)
def resolve_attributes(s, resources): """ Resolve constructs of the form { get_attr: [my_resource, my_attr] } """ def match_get_attr(key, value): return (key in ['get_attr', 'Fn::GetAtt'] and isinstance(value, list) and len(value) == 2 and None not in value and value[0] in resources) def handle_get_attr(args): resource, att = args try: r = resources[resource] if r.state in ( (r.CREATE, r.IN_PROGRESS), (r.CREATE, r.COMPLETE), (r.RESUME, r.IN_PROGRESS), (r.RESUME, r.COMPLETE), (r.UPDATE, r.IN_PROGRESS), (r.UPDATE, r.COMPLETE)): return r.FnGetAtt(att) except KeyError: raise exception.InvalidTemplateAttribute(resource=resource, key=att) return template._resolve(match_get_attr, handle_get_attr, s)
def resolve_attributes(s, resources, transform=None): """ Resolve constructs of the form { get_attr: [my_resource, my_attr] } """ def match_get_attr(key, value): return (key in ['get_attr'] and isinstance(value, list) and len(value) >= 2 and None not in value and value[0] in resources) def handle_get_attr(args): resource = args[0] try: r = resources[resource] if r.state in ((r.CREATE, r.IN_PROGRESS), (r.CREATE, r.COMPLETE), (r.RESUME, r.IN_PROGRESS), (r.RESUME, r.COMPLETE), (r.UPDATE, r.IN_PROGRESS), (r.UPDATE, r.COMPLETE)): rsrc_attr = args[1] attr = r.FnGetAtt(rsrc_attr) try: for inner_attr in args[2:]: if hasattr(attr, str(inner_attr)): attr = getattr(attr, inner_attr) else: attr = attr[inner_attr] return attr except (KeyError, IndexError, TypeError): return '' except (KeyError, IndexError): raise exception.InvalidTemplateAttribute(resource=resource, key=rsrc_attr) return template._resolve(match_get_attr, handle_get_attr, s, transform)
def resolve_attributes(s, resources, transform=None): """ Resolve constructs of the form { get_attr: [my_resource, my_attr] } """ def match_get_attr(key, value): return (key in ['get_attr', 'Fn::GetAtt'] and isinstance(value, list) and len(value) == 2 and None not in value and value[0] in resources) def handle_get_attr(args): resource, att = args try: r = resources[resource] if r.state in ((r.CREATE, r.IN_PROGRESS), (r.CREATE, r.COMPLETE), (r.RESUME, r.IN_PROGRESS), (r.RESUME, r.COMPLETE), (r.UPDATE, r.IN_PROGRESS), (r.UPDATE, r.COMPLETE)): return r.FnGetAtt(att) except KeyError: raise exception.InvalidTemplateAttribute(resource=resource, key=att) return template._resolve(match_get_attr, handle_get_attr, s, transform)
def resolve_resource_refs(s, resources): ''' Resolve constructs of the form { "get_resource" : "resource" } ''' def match_resource_ref(key, value): return key in ['get_resource', 'Ref'] and value in resources def handle_resource_ref(arg): return resources[arg].FnGetRefId() return template._resolve(match_resource_ref, handle_resource_ref, s)
def resolve_resource_refs(s, resources): """ Resolve constructs of the form { "get_resource" : "resource" } """ def match_resource_ref(key, value): return key == "get_resource" and value in resources def handle_resource_ref(arg): return resources[arg].FnGetRefId() return template._resolve(match_resource_ref, handle_resource_ref, s)
def resolve_param_refs(s, parameters): """ Resolve constructs of the form { get_param: my_param } """ def match_param_ref(key, value): return key == "get_param" and isinstance(value, basestring) and value in parameters def handle_param_ref(ref): try: return parameters[ref] except (KeyError, ValueError): raise exception.UserParameterMissing(key=ref) return template._resolve(match_param_ref, handle_param_ref, s)
def resolve_param_refs(s, parameters): """ Resolve constructs of the form { get_param: my_param } """ def match_param_ref(key, value): return (key in ['get_param', 'Ref'] and value is not None and value in parameters) def handle_param_ref(ref): try: return parameters[ref] except (KeyError, ValueError): raise exception.UserParameterMissing(key=ref) return template._resolve(match_param_ref, handle_param_ref, s)
def _resolve_ref(s, params, transform=None): """ Resolve constructs of the form { Ref: my_param } """ def match_param_ref(key, value): return (key == 'Ref' and value is not None and value in params) def handle_param_ref(ref): try: return params[ref] except (KeyError, ValueError): raise exception.UserParameterMissing(key=ref) return template._resolve(match_param_ref, handle_param_ref, s, transform)
def resolve_param_refs(s, parameters): """ Resolve constructs of the form { get_param: my_param } """ def match_param_ref(key, value): return (key == 'get_param' and isinstance(value, basestring) and value in parameters) def handle_param_ref(ref): try: return parameters[ref] except (KeyError, ValueError): raise exception.UserParameterMissing(key=ref) return template._resolve(match_param_ref, handle_param_ref, s)
def resolve_replace(s): """ Resolve template string substitution via function str_replace Resolves the str_replace function of the form str_replace: template: <string template> params: <param dictionary> """ def handle_str_replace(args): if not (isinstance(args, dict) or isinstance(args, list)): raise TypeError('Arguments to "str_replace" must be a' 'dictionary or a list') try: if isinstance(args, dict): text = args.get('template') params = args.get('params', {}) elif isinstance(args, list): params, text = args if text is None: raise KeyError() except KeyError: example = ('''str_replace: template: This is $var1 template $var2 params: var1: a var2: string''') raise KeyError('"str_replace" syntax should be %s' % example) if not hasattr(text, 'replace'): raise TypeError('"template" parameter must be a string') if not isinstance(params, dict): raise TypeError( '"params" parameter must be a dictionary') if isinstance(args, list): for key in params.iterkeys(): value = params.get(key, '') text = text.replace(key, value) return text return string.Template(text).safe_substitute(params) match_str_replace = lambda k, v: k in ['str_replace', 'Fn::Replace'] return template._resolve(match_str_replace, handle_str_replace, s)
def resolve_replace(s, transform=None): """ Resolve template string substitution via function str_replace Resolves the str_replace function of the form:: str_replace: template: <string template> params: <param dictionary> """ def handle_str_replace(args): if not (isinstance(args, dict) or isinstance(args, list)): raise TypeError( _('Arguments to "str_replace" must be a' 'dictionary or a list')) try: if isinstance(args, dict): text = args.get('template') params = args.get('params', {}) elif isinstance(args, list): params, text = args if text is None: raise KeyError() except KeyError: example = ('''str_replace: template: This is var1 template var2 params: var1: a var2: string''') raise KeyError( _('"str_replace" syntax should be %s') % example) if not hasattr(text, 'replace'): raise TypeError(_('"template" parameter must be a string')) if not isinstance(params, dict): raise TypeError(_('"params" parameter must be a dictionary')) for key in params.iterkeys(): value = params.get(key, '') or "" text = text.replace(key, str(value)) return text match_str_replace = lambda k, v: k in ['str_replace', 'Fn::Replace'] return template._resolve(match_str_replace, handle_str_replace, s, transform)
def resolve_attributes(s, resources, transform=None): """ Resolve constructs of the form { get_attr: [my_resource, my_attr] } """ def match_get_attr(key, value): return (key in ['get_attr'] and isinstance(value, list) and len(value) >= 2 and None not in value and value[0] in resources) def handle_get_attr(args): resource = args[0] try: r = resources[resource] if r.state in ( (r.CREATE, r.IN_PROGRESS), (r.CREATE, r.COMPLETE), (r.RESUME, r.IN_PROGRESS), (r.RESUME, r.COMPLETE), (r.UPDATE, r.IN_PROGRESS), (r.UPDATE, r.COMPLETE)): rsrc_attr = args[1] attr = r.FnGetAtt(rsrc_attr) try: for inner_attr in args[2:]: if hasattr(attr, str(inner_attr)): attr = getattr(attr, inner_attr) else: attr = attr[inner_attr] return attr except (KeyError, IndexError, TypeError): return '' except (KeyError, IndexError): raise exception.InvalidTemplateAttribute(resource=resource, key=rsrc_attr) return template._resolve(match_get_attr, handle_get_attr, s, transform)
def resolve_get_file(self, s, transform=None): """ Resolve file inclusion via function get_file. For any key provided the contents of the value in the template files dictionary will be substituted. Resolves the get_file function of the form:: get_file: <string key> """ def handle_get_file(args): if not (isinstance(args, basestring)): raise TypeError(_('Argument to "get_file" must be a string')) f = self.files.get(args) if f is None: raise ValueError( _('No content found in the "files" section ' 'for get_file path: %s') % args) return f match_get_file = lambda k, v: k == 'get_file' return template._resolve(match_get_file, handle_get_file, s, transform)
def resolve_replace(s): """ Resolve template string substitution via function str_replace Resolves the str_replace function of the form str_replace: template: <string template> params: <param dictionary> """ def handle_str_replace(args): if not isinstance(args, dict): raise TypeError('Arguments to "str_replace" must be a' 'dictionary') try: template = args['template'] params = args['params'] except KeyError: example = ('''str_replace: template: This is $var1 template $var2 params: - var1: a - var2: string''') raise KeyError('"str_replace" syntax should be %s' % example) if not isinstance(template, basestring): raise TypeError('"template" parameter must be a string') if not isinstance(params, dict): raise TypeError('"params" parameter must be a dictionary') return Template(template).substitute(params) return template._resolve(lambda k, v: k == 'str_replace', handle_str_replace, s)
def join(raw): def handle_join(args): delim, strs = args return delim.join(strs) return template._resolve(lambda k, v: k == 'Fn::Join', handle_join, raw)