def test_substitution_with_spaces(self): template = NetworkTemplate("a: <% a%>, b: <%b %>, " "c: <% c %>, d: <% d%>") substituted_string = template.safe_substitute(a='aaa', b='bbb', c='ccc', d='ddd') self.assertEqual(substituted_string, "a: aaa, b: bbb, c: ccc, d: ddd")
def test_substitution_with_extra_keys(self): template = NetworkTemplate("a: <%a%>") substituted_string = template.safe_substitute(a="aaa", b="bbb") self.assertEqual(substituted_string, "a: aaa") substituted_string = template.substitute(a="aaa", b="bbb") self.assertEqual(substituted_string, "a: aaa")
def test_substitution_with_missed_key(self): template = NetworkTemplate("a: <%a%>") substituted_string = template.safe_substitute(b='bbb') self.assertEqual(substituted_string, "a: <%a%>") self.assertRaises(KeyError, template.substitute, b='bbb')
def test_substitution_with_extra_keys(self): template = NetworkTemplate("a: <%a%>") substituted_string = template.safe_substitute(a='aaa', b='bbb') self.assertEqual(substituted_string, "a: aaa") substituted_string = template.substitute(a='aaa', b='bbb') self.assertEqual(substituted_string, "a: aaa")
def apply_network_template(cls, instance, template): if template is None: instance.network_template = None return template_body = template["adv_net_template"] # Get the correct nic_mapping for this node so we can # dynamically replace any interface references in any # template for this node. from nailgun.objects import NodeGroup node_group = NodeGroup.get_by_uid(instance.group_id).name if node_group not in template_body: node_group = "default" node_name = cls.get_slave_name(instance) nic_mapping = template_body[node_group]["nic_mapping"] if node_name not in nic_mapping: node_name = "default" nic_mapping = nic_mapping[node_name] # Replace interface references and re-parse JSON template_object = NetworkTemplate(jsonutils.dumps(template_body)) node_template = template_object.safe_substitute(nic_mapping) parsed_template = jsonutils.loads(node_template) output = parsed_template[node_group] output["templates"] = output.pop("network_scheme") output["roles"] = {} output["endpoints"] = {} for v in output["templates"].values(): for endpoint in v["endpoints"]: output["endpoints"][endpoint] = {} for role, ep in v["roles"].items(): output["roles"][role] = ep instance.network_template = output db().flush() if instance.cluster: nm = Cluster.get_network_manager(instance.cluster) nm.assign_networks_by_template(instance)
def apply_network_template(cls, instance, template): if template is None: instance.network_template = None return template_body = template['adv_net_template'] # Get the correct nic_mapping for this node so we can # dynamically replace any interface references in any # template for this node. from nailgun.objects import NodeGroup node_group = NodeGroup.get_by_uid(instance.group_id).name if node_group not in template_body: node_group = 'default' node_name = cls.get_slave_name(instance) nic_mapping = template_body[node_group]['nic_mapping'] if node_name not in nic_mapping: node_name = 'default' nic_mapping = nic_mapping[node_name] # Replace interface references and re-parse JSON template_object = NetworkTemplate(jsonutils.dumps(template_body)) node_template = template_object.safe_substitute(nic_mapping) parsed_template = jsonutils.loads(node_template) output = parsed_template[node_group] output['templates'] = output.pop('network_scheme') output['roles'] = {} output['endpoints'] = {} for v in output['templates'].values(): for endpoint in v['endpoints']: output['endpoints'][endpoint] = {} for role, ep in v['roles'].items(): output['roles'][role] = ep instance.network_template = output db().flush() if instance.cluster: nm = Cluster.get_network_manager(instance.cluster) nm.assign_networks_by_template(instance)
def apply_network_template(cls, instance, template): if template is None: instance.network_template = None return template_body = template['adv_net_template'] # Get the correct nic_mapping for this node so we can # dynamically replace any interface references in any # template for this node. from nailgun.objects import NodeGroup node_group = NodeGroup.get_by_uid(instance.group_id).name if node_group not in template_body: node_group = 'default' node_name = cls.get_slave_name(instance) nic_mapping = template_body[node_group]['nic_mapping'] if node_name not in nic_mapping: node_name = 'default' nic_mapping = nic_mapping[node_name] # Replace interface references and re-parse JSON template_object = NetworkTemplate(jsonutils.dumps(template_body)) node_template = template_object.safe_substitute(nic_mapping) parsed_template = jsonutils.loads(node_template) output = parsed_template[node_group] output['templates'] = output.pop('network_scheme') output['roles'] = {} output['endpoints'] = {} for v in output['templates'].values(): for endpoint in v['endpoints']: output['endpoints'][endpoint] = {} for role, ep in v['roles'].items(): output['roles'][role] = ep instance.network_template = output
def test_substitution_with_no_match(self): template = NetworkTemplate("a: <%a b: <% b % >") substituted_string = template.safe_substitute(a="aaa", b="bbb") self.assertEqual(substituted_string, "a: <%a b: <%b % >")
def test_substitution_with_spaces(self): template = NetworkTemplate("a: <% a%>, b: <%b %>, " "c: <% c %>, d: <% d%>") substituted_string = template.safe_substitute(a="aaa", b="bbb", c="ccc", d="ddd") self.assertEqual(substituted_string, "a: aaa, b: bbb, c: ccc, d: ddd")
def test_simple_substitution(self): template = NetworkTemplate("a: <%a%>, b: <%b%>") substituted_string = template.safe_substitute(a="aaa", b="bbb") self.assertEqual(substituted_string, "a: aaa, b: bbb")
def test_substitution_with_missed_key(self): template = NetworkTemplate("a: <%a%>") substituted_string = template.safe_substitute(b='bbb') self.assertEqual(substituted_string, "a: <%a") self.assertRaises(KeyError, template.substitute, dict(b='bbb'))
def test_substitution_with_no_match(self): template = NetworkTemplate("a: <%a b: <% b % >") substituted_string = template.safe_substitute(a='aaa', b='bbb') self.assertEqual(substituted_string, "a: <%a b: <%b % >")
def test_simple_substitution(self): template = NetworkTemplate("a: <%a%>, b: <%b%>") substituted_string = template.safe_substitute(a='aaa', b='bbb') self.assertEqual(substituted_string, "a: aaa, b: bbb")