def _single_node_template_content_test(self, tpl_snippet): nodetemplates = (toscaparser.utils.yamlparser.simple_ordered_parse( tpl_snippet))['node_templates'] name = list(nodetemplates.keys())[0] nodetemplate = NodeTemplate(name, nodetemplates, self._custom_types()) nodetemplate.validate() nodetemplate.requirements nodetemplate.get_capabilities_objects() nodetemplate.get_properties_objects() nodetemplate.interfaces
def _single_node_template_content_test(self, tpl_snippet): nodetemplates = (toscaparser.utils.yamlparser. simple_ordered_parse(tpl_snippet))['node_templates'] name = list(nodetemplates.keys())[0] nodetemplate = NodeTemplate(name, nodetemplates, self._custom_types()) nodetemplate.validate() nodetemplate.requirements nodetemplate.get_capabilities_objects() nodetemplate.get_properties_objects() nodetemplate.interfaces
def test_invalid_scalar_unit(self): tpl_snippet = ''' server: type: tosca.my.nodes.Compute properties: cpu_frequency: 50.3.6 GHZ disk_size: MB mem_size: 1 QB ''' nodetemplates = yamlparser.simple_parse(tpl_snippet) nodetemplate = NodeTemplate('server', nodetemplates, self.custom_def) for p in nodetemplate.get_properties_objects(): self.assertRaises(ValueError, p.validate)
def test_invalid_scalar_unit(self): tpl_snippet = ''' server: type: tosca.my.nodes.Compute properties: cpu_frequency: 50.3.6 GHZ disk_size: MB mem_size: 1 QB ''' nodetemplates = yamlparser.simple_parse(tpl_snippet) nodetemplate = NodeTemplate('server', nodetemplates, self.custom_def) for p in nodetemplate.get_properties_objects(): self.assertRaises(ValueError, p.validate)
def build_node_fact(node_tpl: NodeTemplate) -> str: def build_node_property(prop: Property) -> str: if type(prop.value) in [int, float]: prop_val_str = str(prop.value) elif type(prop.value) is str: prop_val_str = '"' + prop.value + '"' # TODO: escaping elif type(prop.value) is bool: prop_val_str = "true" if prop.value else "false" elif type(prop.value) is GetInput: prop_val_str = f"get_input({prop.value.args[0]})" else: raise ValueError(f"Property type {type(prop.value)} not handled.") return f"property({prop.name}, {prop_val_str})" properties = "[" \ + ", ".join([build_node_property(prop) for prop in node_tpl.get_properties_objects()]) \ + "]" def build_node_capability(cap: Capability): properties = "[" \ + ", ".join([build_node_property(prop) for prop in cap.get_properties_objects()]) \ + "]" return f"capability({cap.name}, {properties})" capabilities = "[" \ + ", ".join([build_node_capability(cap) for cap in node_tpl.get_capabilities_objects()]) \ + "]" def build_node_requirement(req): req_name = list(req)[0] return f"requirement({req_name}, {req[req_name]})" requirements = "[" \ + ", ".join([build_node_requirement(req) for req in node_tpl.requirements]) \ + "]" return textwrap.dedent(f""" node( {node_tpl.name}, '{node_tpl.type}', {properties}, {capabilities}, {requirements} )""")