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(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 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 _get_compute_resources(self, servers): occi_compute_resources = [] if servers: for s in servers: s = compute.ComputeResource(title=s["name"], id=s["id"]) occi_compute_resources.append(s) return occi_compute_resources
def show(self, req, id): # get info from server s = self.os_helper.get_server(req, id) # get info from flavor flavor = self.os_helper.get_flavor(req, s["flavor"]["id"]) res_tpl = templates.OpenStackResourceTemplate(flavor["id"], flavor["name"], flavor["vcpus"], flavor["ram"], flavor["disk"]) # get info from image img_id = s["image"]["id"] try: image = self.os_helper.get_image(req, img_id) except webob.exc.HTTPNotFound: image = { "id": img_id, "name": "None (Image with ID '%s' not found)" % img_id, } os_tpl = templates.OpenStackOSTemplate(image["id"], image["name"]) # build the compute object comp = compute.ComputeResource(title=s["name"], id=s["id"], cores=flavor["vcpus"], hostname=s["name"], memory=flavor["ram"], state=helpers.vm_state(s["status"]), mixins=[os_tpl, res_tpl]) # storage links vols = self.os_helper.get_server_volumes_link(req, s["id"]) for v in vols: st = storage.StorageResource(title="storage", id=v["volumeId"]) comp.add_link( storage_link.StorageLink(comp, st, deviceid=v["device"])) # network links addresses = s.get("addresses", {}) if addresses: for addr_set in addresses.values(): for addr in addr_set: # TODO(jorgesece): add pool information if addr["OS-EXT-IPS:type"] == "floating": net_id = helpers.PUBLIC_NETWORK else: try: net_id = self.os_helper.get_network_id( req, addr['OS-EXT-IPS-MAC:mac_addr'], id) except webob.exc.HTTPNotFound: net_id = "FIXED" comp.add_link(_create_network_link(addr, comp, net_id)) return [comp]
def test_setters(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) l = network_link.NetworkInterface([], c, n) l.mac = "00:00:00:00:00:00" self.assertEqual("00:00:00:00:00:00", l.attributes["occi.networkinterface.mac"].value)
def test_compute(self): id = uuid.uuid4().hex c = compute.ComputeResource("foo", summary="This is a summary", id=id) self.assertEqual("foo", c.title) self.assertEqual(id, c.id) self.assertEqual("This is a summary", c.summary) self.assertIsNone(c.architecture) self.assertIsNone(c.cores) self.assertIsNone(c.hostname) self.assertIsNone(c.memory) self.assertIsNone(c.speed)
def test_setters(self): c = compute.ComputeResource("foo") c.architecture = "bar" self.assertEqual("bar", c.attributes["occi.compute.architecture"].value) c.cores = 5 self.assertEqual(5, c.attributes["occi.compute.cores"].value) c.hostname = "foobar" self.assertEqual("foobar", c.attributes["occi.compute.hostname"].value) c.speed = 8 self.assertEqual(8, c.attributes["occi.compute.speed"].value) c.memory = 4 self.assertEqual(4, c.attributes["occi.compute.memory"].value)
def test_getters(self): c = compute.ComputeResource("foo", state="baz") self.assertEqual("baz", c.state) c.attributes["occi.compute.architecture"].value = "bar" self.assertEqual("bar", c.architecture) c.attributes["occi.compute.cores"].value = 5 self.assertEqual(5, c.cores) c.attributes["occi.compute.hostname"].value = "foobar" self.assertEqual("foobar", c.hostname) c.attributes["occi.compute.speed"].value = 8 self.assertEqual(8, c.speed) c.attributes["occi.compute.memory"].value = 9 self.assertEqual(9, c.memory)
def test_networkinterface(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) l = network_link.NetworkInterface([], c, n) self.assertEqual(c, l.source) self.assertEqual(n, l.target) self.assertIsNone(l.interface) self.assertIsNone(l.mac) self.assertIsNone(l.state)
def test_setters(self): c = compute.ComputeResource("foo", summary="This is a summary", id=uuid.uuid4().hex) s = storage.StorageResource("bar", summary="This is a summary", id=uuid.uuid4().hex) l = storage_link.StorageLink(c, s) l.deviceid = "/dev/vdc" self.assertEqual("/dev/vdc", l.attributes["occi.storagelink.deviceid"].value) l.mountpoint = "/mnt" self.assertEqual("/mnt", l.attributes["occi.storagelink.mountpoint"].value)
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) s = storage.StorageResource("bar", summary="This is a summary", id=uuid.uuid4().hex) l = storage_link.StorageLink(c, s, deviceid="/dev/vdc", mountpoint="/mnt", state="foobar") self.assertEqual("/dev/vdc", l.deviceid) self.assertEqual("/mnt", l.mountpoint) self.assertEqual("foobar", l.state)
def test_storagelink(self): server_id = uuid.uuid4().hex c = compute.ComputeResource("foo", summary="This is a summary", id=server_id) vol_id = uuid.uuid4().hex s = storage.StorageResource("bar", summary="This is a summary", id=vol_id) l = storage_link.StorageLink(c, s) link_id = '%s_%s' % (server_id, vol_id) self.assertEqual(link_id, l.id) self.assertIsNone(l.deviceid) self.assertIsNone(l.mountpoint) self.assertIsNone(l.state)
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 index(self, req): volumes = self.os_helper.get_volumes(req) occi_link_resources = [] for v in volumes: for attach in v["attachments"]: if attach: c = compute.ComputeResource(title="Compute", id=attach["serverId"]) s = storage.StorageResource(title="Storage", id=v["id"]) l = storage_link.StorageLink(c, s, deviceid=attach["device"]) occi_link_resources.append(l) 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) l = network_link.NetworkInterface([], c, n, interface="eth1", mac="00:01:02:03:04:05", state="foo") self.assertEqual("eth1", l.interface) self.assertEqual("00:01:02:03:04:05", l.mac) self.assertEqual("foo", l.state)
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": storage_link.StorageLink.kind} obj = parser.parse() validator = occi_validator.Validator(obj) validator.validate(scheme) attrs = obj.get("attributes", {}) vol_id = attrs.get("occi.core.target") server_id = attrs.get("occi.core.source") device = attrs.get("occi.storagelink.deviceid", None) attachment = self.os_helper.create_server_volumes_link(req, server_id, vol_id, dev=device) c = compute.ComputeResource(title="Compute", id=server_id) s = storage.StorageResource(title="Storage", id=vol_id) l = storage_link.StorageLink(c, s, deviceid=attachment["device"]) return collection.Collection(resources=[l])
def _get_security_link_resources(link_list): """Create OCCI security group instances from a list :param link_list: provides by the cloud infrastructure """ occi_secgropu_resources = [] if link_list: for l in link_list: compute_id = l['compute_id'] sec = l['securitygroup'] sec_id = sec.get("id") sec_name = sec.get("title", "") sec_rules = sec.get("rules", []) s = securitygroup.SecurityGroupResource(title=sec_name, id=sec_id, rules=sec_rules) c = compute.ComputeResource(title="Compute", id=compute_id) link = securitygroup_link.SecurityGroupLink(source=c, target=s) occi_secgropu_resources.append(link) return occi_secgropu_resources
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 show(self, req, id): # get info from server s = self.os_helper.get_server(req, id) # get info from flavor flavor = self.os_helper.get_flavor(req, s["flavor"]["id"]) res_tpl = templates.OpenStackResourceTemplate(flavor["id"], flavor["name"], flavor["vcpus"], flavor["ram"], flavor["disk"]) # get info from image image = self.os_helper.get_image(req, s["image"]["id"]) os_tpl = templates.OpenStackOSTemplate(image["id"], image["name"]) # build the compute object comp = compute.ComputeResource(title=s["name"], id=s["id"], cores=flavor["vcpus"], hostname=s["name"], memory=flavor["ram"], state=helpers.vm_state(s["status"]), mixins=[os_tpl, res_tpl]) # storage links vols = self.os_helper.get_server_volumes_link(req, s["id"]) for v in vols: st = storage.StorageResource(title="storage", id=v["volumeId"]) comp.add_link( storage_link.StorageLink(comp, st, deviceid=v["device"])) # network links addresses = s.get("addresses", {}) if addresses: for addr_set in addresses.values(): for addr in addr_set: comp.add_link(_create_network_link(addr, comp)) return [comp]
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 show(self, req, id): v = self._get_attachment_from_id(req, id) c = compute.ComputeResource(title="Compute", id=v["serverId"]) s = storage.StorageResource(title="Storage", id=v["volumeId"]) return storage_link.StorageLink(c, s, deviceid=v.get("device", None))