def test_osnetwork_interface(self): c = compute.ComputeResource("foo", summary="This is a summary", id=uuid.uuid4().hex) n = network.NetworkResource("bar", summary="This is a summary", id=uuid.uuid4().hex) i = os_network.OSNetworkInterface(c, n, "00:01:02:03:04:05", "127.0.0.1", pool="foo") self.assertEqual('_'.join([c.id, n.id, "127.0.0.1"]), i.id) self.assertEqual(i.address, "127.0.0.1") self.assertEqual(i.interface, "eth0") self.assertEqual(i.mac, "00:01:02:03:04:05") self.assertEqual(i.state, "active") self.assertIsNone(i.gateway) self.assertEqual(network_link.NetworkInterface.kind, i.kind) self.assertEqual(2, len(i.mixins)) self.assertIn(network_link.ip_network_interface, i.mixins) # FIXME(enolfc): this won't work without proper object comparison # self.assertIn(p, i.mixins) has_pool = False for m in i.mixins: if isinstance(m, os_network.OSFloatingIPPool): self.assertEqual(m.term, "foo") has_pool = True break self.assertTrue(has_pool) # contains kind and mixins attributes for att in network_link.NetworkInterface.kind.attributes: self.assertIn(att, i.attributes) for att in network_link.ip_network_interface.attributes: self.assertIn(att, i.attributes)
def create(self, req, body): parser = req.get_parser()(req.headers, req.body) scheme = {"category": network_link.NetworkInterface.kind} obj = parser.parse() validator = occi_validator.Validator(obj) validator.validate(scheme) attrs = obj.get("attributes", {}) server_id = attrs.get("occi.core.source") net_id = attrs.get("occi.core.target") # net_id is something like "fixed" or "floating/<pool_name>" if net_id == "fixed": raise exception.Invalid() try: _, pool_name = net_id.split("/", 1) except ValueError: raise exception.NetworkPoolFound(pool=net_id) # Allocate IP ip = self.os_helper.allocate_floating_ip(req, pool_name) # Add it to server self.os_helper.associate_floating_ip(req, server_id, ip["id"]) n = network.NetworkResource(title="network", id=net_id) c = compute.ComputeResource(title="Compute", id=server_id) l = os_network.OSNetworkInterface(c, n, "mac", ip["ip"]) return collection.Collection(resources=[l])
def _get_network_link_resources(link_list): """Create OCCI networkLink instances from json format :param link_list: provides by the cloud infrastructure """ occi_network_resources = [] if link_list: for l in link_list: compute_id = l['compute_id'] mac = l.get('mac', None) net_pool = l.get('pool', None) ip = l.get('ip', None) state = l.get('state', None) ip_id = l.get('ip_id', None) net_id = l['network_id'] n = network.NetworkResource(title="network", id=net_id) c = compute.ComputeResource(title="Compute", id=compute_id) iface = os_network.OSNetworkInterface(c, n, mac, ip, pool=net_pool, ip_id=ip_id, state=state) occi_network_resources.append(iface) return occi_network_resources
def _create_network_link(addr, comp): if addr["OS-EXT-IPS:type"] == "floating": net_id = network_api.FLOATING_PREFIX else: net_id = network_api.FIXED_PREFIX net = network.NetworkResource(title="network", id=net_id) return os_network.OSNetworkInterface(comp, net, addr["OS-EXT-IPS-MAC:mac_addr"], addr["addr"])
def _create_network_link(addr, comp, net_id, type_ip): if type_ip == "floating": net = ip_reservation.IPReservation(title="network", address=None, id=net_id) else: net = network.NetworkResource(title="network", id=net_id) return os_network.OSNetworkInterface(comp, net, addr["OS-EXT-IPS-MAC:mac_addr"], addr["addr"])
def index(self, req): floating_ips = self.os_helper.get_floating_ips(req) occi_link_resources = [] for ip in floating_ips: if ip["instance_id"]: net_id = "%s/%s" % (network_api.FLOATING_PREFIX, ip["pool"]) n = network.NetworkResource(title="network", id=net_id) c = compute.ComputeResource(title="Compute", id=ip["instance_id"]) # TODO(enolfc): get the MAC? iface = os_network.OSNetworkInterface(c, n, "mac", ip["ip"]) occi_link_resources.append(iface) return collection.Collection(resources=occi_link_resources)
def test_getters(self): c = compute.ComputeResource("foo", summary="This is a summary", id=uuid.uuid4().hex) n = network.NetworkResource("bar", summary="This is a summary", id=uuid.uuid4().hex) i = os_network.OSNetworkInterface(c, n, "00:01:02:03:04:05", "127.0.0.1") i.attributes["occi.networkinterface.address"].value = "192.163.1.2" self.assertEqual("192.163.1.2", i.address) i.attributes["occi.networkinterface.gateway"].value = "192.163.1.1" self.assertEqual("192.163.1.1", i.gateway) i.attributes["occi.networkinterface.allocation"].value = "static" self.assertEqual("static", i.allocation)
def _get_interface_from_id(self, req, id): try: server_id, server_addr = id.split('_', 1) except ValueError: raise exception.LinkNotFound(link_id=id) s = self.os_helper.get_server(req, server_id) addresses = s.get("addresses", {}) for addr_set in addresses.values(): for addr in addr_set: if addr["addr"] == server_addr: n, ip_id = self._get_os_network_ip(req, addr) c = compute.ComputeResource(title="Compute", id=server_id) # TODO(enolfc): get the MAC? return os_network.OSNetworkInterface(c, n, "mac", addr["addr"], ip_id) raise exception.LinkNotFound(link_id=id)
def _build_os_net_iface(self, req, server_id, addr): ip_id = pool = None if addr["OS-EXT-IPS:type"] == "fixed": net_id = network_api.FIXED_PREFIX else: net_id = network_api.FLOATING_PREFIX floating_ips = self.os_helper.get_floating_ips(req) for ip in floating_ips: if addr["addr"] == ip["ip"]: ip_id = ip["id"] pool = ip["pool"] break else: raise exception.NetworkNotFound(resource_id=addr) c = compute.ComputeResource(title="Compute", id=server_id) n = network.NetworkResource(title="network", id=net_id) # TODO(enolfc): get the MAC? return os_network.OSNetworkInterface(c, n, "mac", addr["addr"], ip_id, pool)
def create(self, req, body): parser = req.get_parser()(req.headers, req.body) scheme = { "category": network_link.NetworkInterface.kind, "optional_mixins": [ os_network.OSFloatingIPPool, ] } obj = parser.parse() validator = occi_validator.Validator(obj) validator.validate(scheme) attrs = obj.get("attributes", {}) _, net_id = helpers.get_id_with_kind( req, attrs.get("occi.core.target"), network.NetworkResource.kind) _, server_id = helpers.get_id_with_kind( req, attrs.get("occi.core.source"), compute.ComputeResource.kind) # net_id is something like "fixed" or "floating" if net_id == network_api.FIXED_PREFIX: raise exception.Invalid() elif net_id != network_api.FLOATING_PREFIX: raise exception.NetworkNotFound(resource_id=net_id) pool_name = None if os_network.OSFloatingIPPool.scheme in obj["schemes"]: pool_name = obj["schemes"][os_network.OSFloatingIPPool.scheme][0] # Allocate IP ip = self.os_helper.allocate_floating_ip(req, pool_name) # Add it to server self.os_helper.associate_floating_ip(req, server_id, ip["ip"]) n = network.NetworkResource(title="network", id=net_id) c = compute.ComputeResource(title="Compute", id=server_id) l = os_network.OSNetworkInterface(c, n, "mac", ip["ip"]) return collection.Collection(resources=[l])
def test_osnetwork_interface(self): c = compute.ComputeResource("foo", summary="This is a summary", id=uuid.uuid4().hex) n = network.NetworkResource("bar", summary="This is a summary", id=uuid.uuid4().hex) i = os_network.OSNetworkInterface(c, n, "00:01:02:03:04:05", "127.0.0.1") self.assertEqual('_'.join([c.id, "127.0.0.1"]), i.id) self.assertEqual(i.address, "127.0.0.1") self.assertEqual(i.interface, "eth0") self.assertEqual(i.mac, "00:01:02:03:04:05") self.assertEqual(i.state, "active") self.assertIsNone(i.gateway) self.assertEqual(network_link.NetworkInterface.kind, i.kind) self.assertIn(network_link.ip_network_interface, i.mixins) # contains kind and mixins attributes for att in network_link.NetworkInterface.kind.attributes: self.assertIn(att, i.attributes) for att in network_link.ip_network_interface.attributes: self.assertIn(att, i.attributes)
def test_get_interface_from_id(self, mock_get_server, mock_get_ip): server_id = uuid.uuid4().hex server_addr = "1.1.1.1" link_id = "%s_%s" % (server_id, server_addr) c = compute.ComputeResource(title="Compute", id=server_id) mock_get_server.return_value = { "addresses": { "foo": [{ "addr": server_addr }] } } mock_get_ip.return_value = ("bar", "baz") a = os_network.OSNetworkInterface(c, "bar", "mac", server_addr, "baz") ret = self.controller._get_interface_from_id(None, link_id) self.assertIsInstance(ret, os_network.OSNetworkInterface) self.assertEqual(a.ip_id, ret.ip_id) self.assertEqual(c.id, ret.source.id) mock_get_server.assert_called_with(None, server_id) mock_get_ip.assert_called_with(None, {"addr": server_addr})
def _create_network_link(addr, comp, net_id): net = network.NetworkResource(title="network", id=net_id) return os_network.OSNetworkInterface(comp, net, addr["OS-EXT-IPS-MAC:mac_addr"], addr["addr"])