def validate_template(self, cnxt, template, params=None): """ The validate_template method uses the stack parser to check the validity of a template. :param cnxt: RPC context. :param template: Template of stack you want to create. :param params: Stack Input Params """ LOG.info(_('validate_template')) if template is None: msg = _("No Template provided.") return webob.exc.HTTPBadRequest(explanation=msg) tmpl = parser.Template(template) # validate overall template try: tmpl.validate() except Exception as ex: return {'Error': six.text_type(ex)} # validate resource classes tmpl_resources = tmpl[tmpl.RESOURCES] env = environment.Environment(params) for res in tmpl_resources.values(): ResourceClass = env.get_class(res['Type']) if ResourceClass == resources.template_resource.TemplateResource: # we can't validate a TemplateResource unless we instantiate # it as we need to download the template and convert the # paramerters into properties_schema. continue props = properties.Properties(ResourceClass.properties_schema, res.get('Properties', {}), context=cnxt) try: ResourceClass.validate_deletion_policy(res) props.validate(with_value=False) except Exception as ex: return {'Error': six.text_type(ex)} # validate parameters tmpl_params = tmpl.parameters(None, {}) tmpl_params.validate(validate_value=False, context=cnxt) is_real_param = lambda p: p.name not in tmpl_params.PSEUDO_PARAMETERS params = tmpl_params.map(api.format_validate_parameter, is_real_param) param_groups = parameter_groups.ParameterGroups(tmpl) result = { 'Description': tmpl.get('Description', ''), 'Parameters': params, } if param_groups.parameter_groups: result['ParameterGroups'] = param_groups.parameter_groups return result
def validate(self): ''' Validates the template. ''' # TODO(sdake) Should return line number of invalid reference # validate overall template (top-level structure) self.t.validate() # Validate parameters self.parameters.validate(context=self.context, validate_value=self.strict_validate) # Validate Parameter Groups parameter_groups = param_groups.ParameterGroups(self.t) parameter_groups.validate() # Check duplicate names between parameters and resources dup_names = set(self.parameters.keys()) & set(self.keys()) if dup_names: LOG.debug("Duplicate names %s" % dup_names) raise exception.StackValidationFailed( message=_("Duplicate names %s") % dup_names) for res in self.dependencies: try: result = res.validate() except exception.HeatException as ex: LOG.info(ex) raise ex except Exception as ex: LOG.exception(ex) raise exception.StackValidationFailed( message=encodeutils.safe_decode(six.text_type(ex))) if result: raise exception.StackValidationFailed(message=result) for val in self.outputs.values(): try: if not val or not val.get('Value'): message = _('Each Output must contain ' 'a Value key.') raise exception.StackValidationFailed(message=message) function.validate(val.get('Value')) except AttributeError: message = _('Output validation error: ' 'Outputs must contain Output. ' 'Found a [%s] instead') % type(val) raise exception.StackValidationFailed(message=message) except Exception as ex: reason = _('Output validation error: ' '%s') % six.text_type(ex) raise exception.StackValidationFailed(message=reason)
def validate_template(self, cnxt, template, params=None): """ The validate_template method uses the stack parser to check the validity of a template. :param cnxt: RPC context. :param template: Template of stack you want to create. :param params: Stack Input Params """ logger.info(_('validate_template')) if template is None: msg = _("No Template provided.") return webob.exc.HTTPBadRequest(explanation=msg) tmpl = parser.Template(template) try: tmpl_resources = tmpl['Resources'] except KeyError as ex: return {'Error': str(ex)} # validate overall template (top-level structure) tmpl.validate() if not tmpl_resources: return {'Error': 'At least one Resources member must be defined.'} env = environment.Environment(params) for res in tmpl_resources.values(): try: if not res.get('Type'): return { 'Error': 'Every Resource object must ' 'contain a Type member.' } except AttributeError: type_res = type(res) if isinstance(res, unicode): type_res = "string" return { 'Error': 'Resources must contain Resource. ' 'Found a [%s] instead' % type_res } ResourceClass = env.get_class(res['Type']) if ResourceClass == resources.template_resource.TemplateResource: # we can't validate a TemplateResource unless we instantiate # it as we need to download the template and convert the # paramerters into properties_schema. continue props = properties.Properties(ResourceClass.properties_schema, res.get('Properties', {}), context=cnxt) try: ResourceClass.validate_deletion_policy(res) props.validate(with_value=False) except Exception as ex: return {'Error': str(ex)} tmpl_params = tmpl.parameters(None, {}, validate_value=False) is_real_param = lambda p: p.name not in tmpl_params.PSEUDO_PARAMETERS params = tmpl_params.map(api.format_validate_parameter, is_real_param) param_groups = parameter_groups.ParameterGroups(tmpl) result = { 'Description': tmpl.get('Description', ''), 'Parameters': params, } if param_groups.parameter_groups: result['ParameterGroups'] = param_groups.parameter_groups return result