def create(self, req, body=None): """Create a networkLink Creates a link between a server and a network. It could be fixed or floating IP. :param req: request object :param body: body request (not used) """ 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) pool = None if os_network.OSFloatingIPPool.scheme in obj["schemes"]: pool = obj["schemes"][os_network.OSFloatingIPPool.scheme][0] # Allocate public IP and associate it ot the server if net_id == os_helpers.PUBLIC_NETWORK: os_link = self.os_helper.assign_floating_ip(req, net_id, server_id, pool) else: # Allocate private network os_link = self.os_helper.create_port(req, net_id, server_id) occi_link = _get_network_link_resources([os_link]) return collection.Collection(resources=occi_link)
def test_get_id_no_kind_relative(self): req_url = '/foo' req = webob.Request.blank(req_url) res_url = "%s" % uuid.uuid4().hex r = helpers.get_id_with_kind(req, res_url) self.assertEqual('%s%s' % (req.application_url, req_url), r[0]) self.assertEqual(res_url, r[1])
def test_get_id_no_kind_absolute(self): req_url = '/foo' req = webob.Request.blank(req_url) res_id = uuid.uuid4().hex res_url = "/bar/%s" % res_id r = helpers.get_id_with_kind(req, res_url) self.assertEqual('%s/bar' % (req.application_url), r[0]) self.assertEqual(res_id, r[1])
def test_get_id_kind_matching(self): m = mock.MagicMock() m.location = "foo/" req_url = "/foo" req = webob.Request.blank(req_url) res_url = "%s" % uuid.uuid4().hex r = helpers.get_id_with_kind(req, res_url, m) self.assertEqual("%s%s" % (req.application_url, req_url), r[0]) self.assertEqual(res_url, r[1])
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 create(self, req, body=None): """Create a networkLink Creates a link between a server and a network. It could be fixed or floating IP. :param req: request object :param body: body request (not used) """ 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) pool = None if os_network.OSFloatingIPPool.scheme in obj["schemes"]: pool = (obj["schemes"][os_network.OSFloatingIPPool.scheme][0]) # Allocate public IP and associate it ot the server if net_id == os_helpers.PUBLIC_NETWORK: os_link = self.os_helper.assign_floating_ip( req, net_id, server_id, pool) else: # Allocate private network os_link = self.os_helper.create_port(req, net_id, server_id) occi_link = _get_network_link_resources([os_link]) return collection.Collection(resources=occi_link)