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 validate_attributes(self, required): """Validate required attributes :param required: required attributes """ attr = self.parsed_obj.get("attributes", {}) if required: for at in required: if at not in attr: raise exception.Invalid("Expecting %s attribute" % at)
def delete(self, req, id): iface = self._get_interface_from_id(req, id) if iface.target.id == "fixed": raise exception.Invalid() # remove floating IP server = iface.source.id self.os_helper.remove_floating_ip(req, server, iface.address) # release IP self.os_helper.release_floating_ip(req, iface.ip_id) return []
def get_id_with_kind(req, resource_url, kind=None): """Resolves the resource URL and tries to match it with the kind. :param req: current request :param resource_url: absolute or relative resource url :returns: a tuple with a base url and a resource id :raises ooi.exception.Invalid: if resource does not match kind """ res_base, res_id = _resolve_id(req.url, resource_url) if kind: kind_base, kind_id = _resolve_id(req.application_url, kind.location) if kind_base != res_base: raise exception.Invalid("Expecting %s resource" % kind_base) return res_base, res_id
def test_create_invalid_ids(self, mock_validator, mock_get_id): tenant = fakes.tenants["foo"] req = self._build_req(tenant["id"]) net_id = "foobarbaz" server_id = uuid.uuid4().hex obj = { "attributes": { "occi.core.target": net_id, "occi.core.source": server_id, } } req.get_parser = mock.MagicMock() mock_get_id.side_effect = exception.Invalid() req.get_parser.return_value.return_value.parse.return_value = obj mock_validator.validate.return_value = True self.assertRaises(exception.Invalid, self.controller.create, req, None)
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): parser = req.get_parser()(req.headers, req.body) scheme = {"category": storage.StorageResource.kind} obj = parser.parse() validator = occi_validator.Validator(obj) validator.validate(scheme) attrs = obj.get("attributes", {}) name = attrs.get("occi.core.title", "OCCI Volume") # TODO(enolfc): this should be handled by the validator try: size = attrs["occi.storage.size"] except KeyError: raise exception.Invalid() volume = self.os_helper.volume_create(req, name, size) st = storage.StorageResource(title=volume["displayName"], id=volume["id"], size=volume["size"], state=helpers.vol_state(volume["status"])) return collection.Collection(resources=[st])
def create(self, req, body=None): """Create a network instance in the cloud :param req: request object :param body: body request (not used) """ scheme = { "category": securitygroup.SecurityGroupResource.kind, } required = ["occi.core.title", "occi.securitygroup.rules"] attributes = process_parameters(req, scheme, required) name = attributes.get('occi.core.title') description = attributes.get("occi.core.summary", "") try: rules = attributes.get('occi.securitygroup.rules') except Exception: raise exception.Invalid( "Bad JSON format for occi.securitygroup.rules: %s" % attributes.get('occi.securitygroup.rules')) sec = self.os_helper.create_security_group(req, name, description, rules) occi_sec_resources = self._get_security_group_resources([sec]) return collection.Collection(resources=occi_sec_resources)