def test_resource_base(self, mock_session): mock_session.configure_mock(base_url=self.BASE) c = RootCollection() self.assertEqual(c.uuid, '') self.assertEqual(c.fq_name, FQName()) self.assertEqual(c.href, self.BASE + '/') c = Collection('foo') self.assertEqual(c.uuid, '') self.assertEqual(c.fq_name, FQName()) self.assertEqual(c.href, self.BASE + '/foos') r = Resource('foo', uuid='x') self.assertEqual(r.uuid, 'x') self.assertEqual(r.fq_name, FQName()) self.assertEqual(r.href, self.BASE + '/foo/x') r = Resource('foo', fq_name='foo:bar') self.assertEqual(r.uuid, '') self.assertEqual(r.fq_name, FQName('foo:bar')) self.assertEqual(r.href, self.BASE + '/foos') r = Resource('foo', to='foo:bar') self.assertEqual(r.uuid, '') self.assertEqual(r.fq_name, FQName('foo:bar')) self.assertEqual(r.href, self.BASE + '/foos') with self.assertRaises(AssertionError): r = Resource('bar')
def test_resource_collection(self, mock_session): mock_session.get_json.return_value = { "foos": [{ "href": self.BASE + "/foo/ec1afeaa-8930-43b0-a60a-939f23a50724", "uuid": "ec1afeaa-8930-43b0-a60a-939f23a50724" }, { "href": self.BASE + "/foo/c2588045-d6fb-4f37-9f46-9451f653fb6a", "uuid": "c2588045-d6fb-4f37-9f46-9451f653fb6a" }] } collection = Collection('foo', fetch=True) self.assertTrue(collection.href.endswith('s')) self.assertEqual(collection.fq_name, FQName()) expected_collection = Collection('foo') expected_collection.data = [ Resource('foo', href=self.BASE + "/foo/ec1afeaa-8930-43b0-a60a-939f23a50724", uuid="ec1afeaa-8930-43b0-a60a-939f23a50724"), Resource('foo', href=self.BASE + "/foo/c2588045-d6fb-4f37-9f46-9451f653fb6a", uuid="c2588045-d6fb-4f37-9f46-9451f653fb6a") ] self.assertEqual(collection, expected_collection)
def test_resource_long_ls(self, mock_session): mock_session.id_to_fqname.return_value = { 'type': 'foo', 'fq_name': FQName('default-project:foo:ec1afeaa-8930-43b0-a60a-939f23a50724') } mock_session.get_json.return_value = { 'foo': { 'href': BASE + '/foo/ec1afeaa-8930-43b0-a60a-939f23a50724', 'uuid': 'ec1afeaa-8930-43b0-a60a-939f23a50724', 'fq_name': ['default-project', 'foo', 'ec1afeaa-8930-43b0-a60a-939f23a50724'], 'prop': { 'foo': False, 'bar': [1, 2, 3] } } } ShellContext.current_path = Path('/') result = self.mgr.get('ls')(paths=['foo/ec1afeaa-8930-43b0-a60a-939f23a50724'], long=True) expected_result = "foo/ec1afeaa-8930-43b0-a60a-939f23a50724 default-project:foo:ec1afeaa-8930-43b0-a60a-939f23a50724" self.assertEqual(result, expected_result) ShellContext.current_path = Path('/foo') result = self.mgr.get('ls')(paths=['ec1afeaa-8930-43b0-a60a-939f23a50724'], long=True, fields=['prop']) expected_results = ["ec1afeaa-8930-43b0-a60a-939f23a50724 foo=False|bar=1,2,3", "ec1afeaa-8930-43b0-a60a-939f23a50724 bar=1,2,3|foo=False"] self.assertTrue(any([result == r for r in expected_results]))
def _migrate_vmi(self, old_vmi, new_vm, si): itf_type = old_vmi['virtual_machine_interface_properties'][ 'service_interface_type'] if itf_type == 'right': vmi_index = str(1) else: vmi_index = str(2) new_fq_name = list(si.parent.fq_name) + [ str(new_vm.fq_name) + '__' + itf_type + '__' + vmi_index ] vmi = copy.deepcopy(old_vmi) del vmi['uuid'] vmi['fq_name'] = FQName(new_fq_name) vmi['display_name'] = new_fq_name[-1] vmi['name'] = new_fq_name[-1] self._create_res(vmi) for old_iip in old_vmi.get('instance_ip_back_refs', []): self._remove_back_ref(old_vmi, old_iip) new_iip = self._migrate_iip(old_iip, si, itf_type) self._add_back_ref(vmi, new_iip) for fip in old_vmi.get('floating_ip_back_refs', []): self._remove_back_ref(old_vmi, fip) self._add_back_ref(vmi, fip) self._delete_res(old_vmi) return vmi
def __call__(self, paths=None, yes=False, **kwargs): super(FixSg, self).__call__(**kwargs) if (not yes and not self.dry_run and not self.check and not continue_prompt( "Some SGs will be deleted. Are you sure to continue?")): raise CommandError("Exiting.") bad_sg_exists = False for r in self.resources: r.fetch() fq_name = r.fq_name if r.children.security_group: bad_sg = [] good_sg = [] for sg in r.children.security_group: if sg.fq_name[2] == 'default': if not FQName(sg.fq_name[0:2]) == fq_name: bad_sg.append(sg) else: good_sg.append(sg) if bad_sg != []: bad_sg_exists = True print "Tenant %s %s" % (r.uuid, r.fq_name) for sg in bad_sg: self._handle_sg("Bad ", sg) for sg in good_sg: self._handle_sg("Good", sg, delete=False) if self.check and bad_sg_exists: raise CommandError()
def _check(self, project): if project.fq_name == FQName('default-domain:default-project'): return keystone_uuid = project.uuid.replace('-', '') try: self.kclient.tenants.get(keystone_uuid) except HttpError as e: if e.http_status == 404: printo(self.current_path(project)) else: raise
def __call__(self, project_fqname=None): project = Resource('project', fq_name=project_fqname, check=True) vns = Collection('virtual-network', parent_uuid=project.uuid, fetch=True, recursive=2) return json.dumps([{ "virtual_network_name": vn.fq_name[-1], "project_fqname": str(FQName(vn.fq_name[0:-1])), "shared": vn.get('is_shared', False), "external": vn.get('router_external', False), } for vn in vns], indent=2)
def _migrate_iip(self, old_iip, si, itf_type): new_fq_name = '%s__%s-%s' % (str(si.parent.fq_name).replace( ':', '__'), si.fq_name[-1], itf_type) # there is 2 left itf so migrate the iip only once if str(old_iip.fq_name) == new_fq_name: printo('Already migrated %s. Skip' % old_iip.fq_name) return old_iip self._delete_res(old_iip) iip = copy.deepcopy(old_iip) del iip['uuid'] iip['fq_name'] = FQName([new_fq_name]) iip['display_name'] = new_fq_name iip['name'] = new_fq_name self._create_res(iip) return iip
def test_resource_check(self, mock_session): mock_session.configure_mock(base_url=BASE) mock_session.id_to_fqname.side_effect = HttpError() r = Resource('bar', uuid='57ef609c-6c9b-4b91-a542-26c61420c37b') self.assertFalse(r.exists) mock_session.id_to_fqname.side_effect = [FQName('domain:bar')] r = Resource('bar', uuid='57ef609c-6c9b-4b91-a542-26c61420c37b') self.assertTrue(r.exists) mock_session.fqname_to_id.side_effect = HttpError() r = Resource('bar', fq_name='domain:foo') self.assertFalse(r.exists) mock_session.fqname_to_id.side_effect = [ '588e1a17-ae50-4b67-8078-95f061d833ca' ] self.assertTrue(r.exists)
def __call__(self, router_name=None, router_ip=None, router_port=None, router_asn=None, router_address_families=[], router_type=None, router_md5=None): default_ri = Resource('routing-instance', fq_name=DEFAULT_RI_FQ_NAME, check=True) router_fq_name = DEFAULT_RI_FQ_NAME + [router_name] bgp_router = Resource('bgp-router', fq_name=router_fq_name) if bgp_router.exists: raise CommandError("The BGP router %s already exists" % FQName(router_fq_name)) if router_type != 'contrail' and 'erm-vpn' in router_address_families: router_address_families.remove('erm-vpn') auth_data = None if router_md5: auth_data = { 'key_items': [{ 'key': router_md5, 'key_id': 0, }], 'key_type': 'md5', } router_parameters = { 'address': router_ip, 'address_families': { 'family': router_address_families, }, 'autonomous_system': router_asn, 'identifier': router_ip, 'port': router_port, 'vendor': router_type, 'auth_data': auth_data, } # full-mesh with existing BGP routers bgp_router_refs = [] for bgp_router_neighbor in Collection('bgp-router', parent_uuid=default_ri.uuid, fetch=True): bgp_router_refs.append({ 'to': bgp_router_neighbor.fq_name, 'attr': { 'session': [{ 'attributes': [{ 'address_families': { 'family': router_address_families, }, 'auth_data': auth_data, }], }], }, }) bgp_router = Resource('bgp-router', fq_name=router_fq_name, parent=default_ri, bgp_router_parameters=router_parameters, bgp_router_refs=bgp_router_refs) bgp_router.save()