示例#1
0
文件: public.py 项目: kins299/mycloud
def public_meta(request, v_public):
    # Normal Response Codes: 204
    # Error Response Codes: internalServerError (500),
    #                       itemNotFound (404),
    #                       badRequest (400)

    request.user_uniq = None
    try:
        v_account, v_container, v_object = request.backend.get_public(
            request.user_uniq,
            v_public)
        meta = request.backend.get_object_meta(request.user_uniq, v_account,
                                               v_container, v_object, 'pithos')
        public = request.backend.get_object_public(
            request.user_uniq, v_account,
            v_container, v_object)
    except:
        raise faults.ItemNotFound('Object does not exist')

    if not public:
        raise faults.ItemNotFound('Object does not exist')
    update_manifest_meta(request, v_account, meta)

    response = HttpResponse(status=200)
    put_object_headers(response, meta, True)
    return response
示例#2
0
文件: util.py 项目: vgerak/synnefo
def get_rescue_image(properties=None, image_id=None):
    """
    Return a rescue image based on either a rescue image ID or
    VM specific properties.

    If properties are provided, the function will select the image based on the
    importance of each property. For example, a VM has properties
    OS-Family=Linux and OS=Debian, the system will attempt to find a Linux
    Debian rescue image, if it fails to do so, it will attempt to select a
    Linux image etc. If no image is suiting for the provided properties, a
    default image will be used.
    """
    if image_id is not None:
        try:
            return RescueImage.objects.get(id=image_id)
        except RescueImage.DoesNotExist:
            raise faults.ItemNotFound('Rescue image %d not found' % image_id)

    if properties is None:
        try:
            return RescueImage.objects.get(is_default=True, deleted=False)
        except RescueImage.DoesNotExist:
            raise faults.ItemNotFound('Rescue image not found')

    os_family = properties.os_family
    os = properties.os

    candidate_images = RescueImage.objects.filter(deleted=False)
    # Attempt to find an image that satisfies all properties
    if os_family is not None and os is not None:
        rescue_image = candidate_images.filter(
            target_os_family__iexact=os_family, target_os__iexact=os).first()
        if rescue_image is not None:
            return rescue_image

    # In case none are found, we should select based on the OS Family
    if os_family is not None:
        rescue_image = candidate_images.filter(
            target_os_family__iexact=os_family).first()
        if rescue_image is not None:
            return rescue_image
    try:
        # If we didn't find any images matching the criteria, fallback to
        # a default image
        return RescueImage.objects.get(is_default=True)
    except RescueImage.DoesNotExist:
        raise faults.ItemNotFound('Rescue image with properties: OS-Family %s '
                                  ' and OS %s not found' % (os_family, os))
示例#3
0
    def part_iterator(self):
        if self.length > 0:
            # Get the file for the current offset.
            file_size = self.sizes[self.file_index]
            while self.offset >= file_size:
                self.offset -= file_size
                self.file_index += 1
                file_size = self.sizes[self.file_index]

            # Get the block for the current position.
            self.block_index = int(self.offset / self.backend.block_size)
            if self.block_hash != \
                    self.hashmaps[self.file_index][self.block_index]:
                self.block_hash = self.hashmaps[
                    self.file_index][self.block_index]
                try:
                    self.block = self.backend.get_block(self.block_hash)
                except ItemNotExists:
                    raise faults.ItemNotFound('Block does not exist')

            # Get the data from the block.
            bo = self.offset % self.backend.block_size
            bs = self.backend.block_size
            if (self.block_index == len(self.hashmaps[self.file_index]) - 1 and
                    self.sizes[self.file_index] % self.backend.block_size):
                bs = self.sizes[self.file_index] % self.backend.block_size
            bl = min(self.length, bs - bo)
            data = self.block[bo:bo + bl]
            self.offset += bl
            self.length -= bl
            return data
        else:
            raise StopIteration
示例#4
0
文件: util.py 项目: vgerak/synnefo
def get_flavor(flavor_id,
               credentials,
               include_deleted=False,
               for_project=None,
               include_for_user=False):
    """Return a Flavor instance or raise ItemNotFound."""

    try:
        flavor_id = int(flavor_id)
        flavors = policy.FlavorPolicy\
            .filter_list(credentials, include_for_user=include_for_user)\
            .select_related("volume_type")
        if not include_deleted:
            flavors = flavors.filter(deleted=False)

        flavor = flavors.get(id=flavor_id)
        if not policy.FlavorPolicy\
           .has_access_to_flavor(flavor, credentials,
                                 project=for_project,
                                 include_for_user=include_for_user):
            raise faults.Forbidden("Insufficient access")
        return flavor
    except (ValueError, TypeError):
        raise faults.BadRequest("Invalid flavor ID '%s'" % flavor_id)
    except Flavor.DoesNotExist:
        raise faults.ItemNotFound('Flavor not found.')
示例#5
0
    def get_pithos_object(self, uuid, version=None, check_permissions=True,
                          check_image=True):
        """Get a Pithos object based on its UUID.

        If 'version' is not specified, the latest non-deleted version of this
        object will be retrieved.
        If 'check_permissions' is set to False, the Pithos backend will not
        check if the user has permissions to access this object.
        Finally, the 'check_image' option is used to check whether the Pithos
        object is an image or not.

        """
        if check_permissions:
            user = self.user
        else:
            user = None

        meta, permissions, path = self.backend.get_object_by_uuid(
            uuid=uuid, version=version, domain=PLANKTON_DOMAIN,
            user=user, check_permissions=check_permissions)
        account, container, path = path.split("/", 2)
        location = Location(account, container, path)

        if check_image and PLANKTON_PREFIX + "name" not in meta:
            # Check that object is an image by checking if it has an Image name
            # in Plankton metadata
            raise faults.ItemNotFound("Object '%s' does not exist." % uuid)

        return location, meta, permissions
示例#6
0
 def get_snapshot(self, snapshot_uuid, check_permissions=True):
     snap = self._get_image(snapshot_uuid,
                            check_permissions=check_permissions)
     if snap.get("is_snapshot", False) is False:
         raise faults.ItemNotFound("Snapshots '%s' does not exist" %
                                   snapshot_uuid)
     return snap
示例#7
0
def copy_or_move_object(request, src_account, src_container, src_name,
                        dest_account, dest_container, dest_name,
                        move=False, delimiter=None, listing_limit=None):
    """Copy or move an object."""

    if 'ignore_content_type' in request.GET and 'CONTENT_TYPE' in request.META:
        del(request.META['CONTENT_TYPE'])
    content_type, meta, permissions, public = get_object_headers(request)
    src_version = request.META.get('HTTP_X_SOURCE_VERSION')
    try:
        if move:
            version_id = request.backend.move_object(
                request.user_uniq, src_account, src_container, src_name,
                dest_account, dest_container, dest_name,
                content_type, 'pithos', meta, False, permissions, delimiter,
                listing_limit=listing_limit)
        else:
            version_id = request.backend.copy_object(
                request.user_uniq, src_account, src_container, src_name,
                dest_account, dest_container, dest_name,
                content_type, 'pithos', meta, False, permissions,
                src_version, delimiter, listing_limit=listing_limit)
    except NotAllowedError:
        raise faults.Forbidden('Not allowed')
    except (ItemNotExists, VersionNotExists):
        raise faults.ItemNotFound('Container or object does not exist')
    except ValueError:
        raise faults.BadRequest('Invalid sharing header')
    except QuotaError, e:
        raise faults.RequestEntityTooLarge('Quota error: %s' % e)
示例#8
0
文件: util.py 项目: kpelelis/synnefo
def get_vm(server_id, user_id, projects, for_update=False,
           non_deleted=False, non_suspended=False, prefetch_related=None):
    """Find a VirtualMachine instance based on ID and owner."""

    try:
        server_id = int(server_id)

        servers = VirtualMachine.objects.for_user(userid=user_id,
                                                  projects=projects)

        if for_update:
            servers = servers.select_for_update()
        if prefetch_related is not None:
            if isinstance(prefetch_related, list):
                servers = servers.prefetch_related(*prefetch_related)
            else:
                servers = servers.prefetch_related(prefetch_related)

        vm = servers.get(id=server_id)

        if non_deleted and vm.deleted:
            raise faults.BadRequest("Server has been deleted.")
        if non_suspended and vm.suspended:
            raise faults.Forbidden("Administratively Suspended VM")
        return vm
    except (ValueError, TypeError):
        raise faults.BadRequest('Invalid server ID.')
    except VirtualMachine.DoesNotExist:
        raise faults.ItemNotFound('Server not found.')
示例#9
0
def get_vm_meta(vm, key):
    """Return a VirtualMachineMetadata instance or raise ItemNotFound."""

    try:
        return VirtualMachineMetadata.objects.get(meta_key=key, vm=vm)
    except VirtualMachineMetadata.DoesNotExist:
        raise faults.ItemNotFound('Metadata key not found.')
示例#10
0
def get_image(image_id, user_id):
    """Return an Image instance or raise ItemNotFound."""

    with PlanktonBackend(user_id) as backend:
        try:
            return backend.get_image(image_id)
        except faults.ItemNotFound:
            raise faults.ItemNotFound("Image '%s' not found" % image_id)
示例#11
0
def get_vm_nic(vm, nic_id):
    """Get a VMs NIC by its ID."""
    try:
        nic_id = int(nic_id)
        return vm.nics.get(id=nic_id)
    except NetworkInterface.DoesNotExist:
        raise faults.ItemNotFound("NIC '%s' not found" % nic_id)
    except (ValueError, TypeError):
        raise faults.BadRequest("Invalid NIC ID '%s'" % nic_id)
示例#12
0
def get_floating_ip_by_address(userid, address, for_update=False):
    try:
        objects = IPAddress.objects
        if for_update:
            objects = objects.select_for_update()
        return objects.get(userid=userid, floating_ip=True,
                           address=address, deleted=False)
    except IPAddress.DoesNotExist:
        raise faults.ItemNotFound("Floating IP does not exist.")
示例#13
0
文件: util.py 项目: vgerak/synnefo
def get_floating_ip_by_address(credentials, address, for_update=False):
    try:
        objects = policy.IPAddressPolicy.filter_list(credentials).filter(
            floating_ip=True, deleted=False)
        if for_update:
            objects = objects.select_for_update()

        return objects.get(address=address)
    except IPAddress.DoesNotExist:
        raise faults.ItemNotFound("Floating IP does not exist.")
示例#14
0
def user_detail(request, user_id):
    admin_id = request.user_uniq
    logger.info('user_detail: %s user: %s', admin_id, user_id)
    try:
        user = AstakosUser.objects.get(uuid=user_id)
    except AstakosUser.DoesNotExist:
        raise faults.ItemNotFound("User not found")

    user_data = user_to_dict(user, detail=True)
    data = json.dumps({'user': user_data})
    return http.HttpResponse(data, status=200, content_type='application/json')
示例#15
0
def get_network(network_id, user_id, for_update=False):
    """Return a Network instance or raise ItemNotFound."""

    try:
        network_id = int(network_id)
        objects = Network.objects
        if for_update:
            objects = objects.select_for_update()
        return objects.get(Q(userid=user_id) | Q(public=True), id=network_id)
    except (ValueError, Network.DoesNotExist):
        raise faults.ItemNotFound('Network not found.')
示例#16
0
def get_subnet(subnet_id, user_id, for_update=False):
    """Return a Subnet instance or raise ItemNotFound."""

    try:
        objects = Subnet.objects
        subnet_id = int(subnet_id)
        return objects.get(Q(userid=user_id) | Q(public=True), id=subnet_id)
    except (ValueError, TypeError):
        raise faults.BadRequest("Invalid subnet ID '%s'" % subnet_id)
    except Subnet.DoesNotExist:
        raise faults.ItemNotFound("Subnet '%s' not found." % subnet_id)
示例#17
0
def get_flavor(flavor_id, include_deleted=False):
    """Return a Flavor instance or raise ItemNotFound."""

    try:
        flavor_id = int(flavor_id)
        if include_deleted:
            return Flavor.objects.get(id=flavor_id)
        else:
            return Flavor.objects.get(id=flavor_id, deleted=include_deleted)
    except (ValueError, Flavor.DoesNotExist):
        raise faults.ItemNotFound('Flavor not found.')
示例#18
0
def get_keypair(keypair_name, user_id, for_update=False):
    try:
        keypairs = PublicKeyPair.objects
        if for_update:
            keypairs = keypairs.select_for_update()
        keypair = keypairs.get(name=keypair_name, user=user_id)
        if keypair.deleted:
            raise faults.BadRequest("Keypair has been deleted.")
        return keypair
    except PublicKeyPair.DoesNotExist:
        raise faults.ItemNotFound('Keypair %s not found.' % keypair_name)
示例#19
0
def get_nic_from_index(vm, nic_index):
    """Returns the nic_index-th nic of a vm
       Error Response Codes: itemNotFound (404), badMediaType (415)
    """
    matching_nics = vm.nics.filter(index=nic_index)
    matching_nics_len = len(matching_nics)
    if matching_nics_len < 1:
        raise faults.ItemNotFound('NIC not found on VM')
    elif matching_nics_len > 1:
        raise faults.BadMediaType('NIC index conflict on VM')
    nic = matching_nics[0]
    return nic
示例#20
0
def get_subnet(subnet_id, user_id, user_projects, for_update=False):
    """Return a Subnet instance or raise ItemNotFound."""

    try:
        objects = Subnet.objects.for_user(user_id, user_projects, public=True)
        if for_update:
            objects.select_for_update()
        return objects.get(id=subnet_id)
    except (ValueError, TypeError):
        raise faults.BadRequest("Invalid subnet ID '%s'" % subnet_id)
    except Subnet.DoesNotExist:
        raise faults.ItemNotFound("Subnet '%s' not found." % subnet_id)
示例#21
0
def get_flavor(flavor_id, include_deleted=False):
    """Return a Flavor instance or raise ItemNotFound."""

    try:
        flavor_id = int(flavor_id)
        if include_deleted:
            return Flavor.objects.get(id=flavor_id)
        else:
            return Flavor.objects.get(id=flavor_id, deleted=include_deleted)
    except (ValueError, TypeError):
        raise faults.BadRequest("Invalid flavor ID '%s'" % flavor_id)
    except Flavor.DoesNotExist:
        raise faults.ItemNotFound('Flavor not found.')
示例#22
0
def get_floating_ip_by_id(userid, floating_ip_id, for_update=False):
    try:
        floating_ip_id = int(floating_ip_id)
        objects = IPAddress.objects
        if for_update:
            objects = objects.select_for_update()
        return objects.get(id=floating_ip_id, floating_ip=True,
                           userid=userid, deleted=False)
    except IPAddress.DoesNotExist:
        raise faults.ItemNotFound("Floating IP with ID %s does not exist." %
                                  floating_ip_id)
    except (ValueError, TypeError):
        raise faults.BadRequest("Invalid Floating IP ID %s" % floating_ip_id)
示例#23
0
def get_flavor(flavor_id, include_deleted=False):
    """Return a Flavor instance or raise ItemNotFound."""

    try:
        flavor_id = int(flavor_id)
        flavors = Flavor.objects.select_related("volume_type")
        if not include_deleted:
            flavors = flavors.filter(deleted=False)
        return flavors.get(id=flavor_id)
    except (ValueError, TypeError):
        raise faults.BadRequest("Invalid flavor ID '%s'" % flavor_id)
    except Flavor.DoesNotExist:
        raise faults.ItemNotFound('Flavor not found.')
示例#24
0
def user_update(request, user_id):
    admin_id = request.user_uniq
    req = api.utils.get_json_body(request)
    logger.info('user_update: %s user: %s request: %s', admin_id, user_id, req)

    user_data = req.get('user', {})

    try:
        user = AstakosUser.objects.select_for_update().get(uuid=user_id)
    except AstakosUser.DoesNotExist:
        raise faults.ItemNotFound("User not found")

    email = user_data.get('username', None)
    first_name = user_data.get('first_name', None)
    last_name = user_data.get('last_name', None)
    affiliation = user_data.get('affiliation', None)
    password = user_data.get('password', None)
    metadata = user_data.get('metadata', {})

    if 'password' in user_data:
        user.set_password(password)

    if 'username' in user_data:
        try:
            validate_email(email)
        except ValidationError:
            raise faults.BadRequest("Invalid username (email format required)")

        if AstakosUser.objects.verified_user_exists(email):
            raise faults.Conflict("User '%s' already exists" % email)

        user.email = email

    if 'first_name' in user_data:
        user.first_name = first_name

    if 'last_name' in user_data:
        user.last_name = last_name

    try:
        user.save()
        if 'metadata' in user_data:
            provider = user.auth_providers.get(auth_backend="astakos")
            provider.info = metadata
            if affiliation in user_data:
                provider.affiliation = affiliation
            provider.save()

    except Exception, e:
        raise faults.BadRequest(e.message)
示例#25
0
def grapher(request, graph_type, hostname):
    hostname = decrypt(uenc(hostname))
    fname = uenc(os.path.join(settings.RRD_PREFIX, hostname))
    if not os.path.isdir(fname):
        raise faults.ItemNotFound('No such instance')

    outfname = uenc(os.path.join(settings.GRAPH_PREFIX, hostname))
    draw_func = available_graph_types[graph_type]

    response = HttpResponse(draw_func(fname, outfname),
                            status=200,
                            content_type="image/png")
    response.override_serialization = True

    return response
示例#26
0
def get_port(port_id, user_id, for_update=False):
    """
    Return a NetworkInteface instance or raise ItemNotFound.
    """
    try:
        objects = NetworkInterface.objects.filter(userid=user_id)
        if for_update:
            objects = objects.select_for_update()
        # if (port.device_owner != "vm") and for_update:
        #     raise faults.BadRequest('Cannot update non vm port')
        return objects.get(id=port_id)
    except (ValueError, TypeError):
        raise faults.BadRequest("Invalid port ID '%s'" % port_id)
    except NetworkInterface.DoesNotExist:
        raise faults.ItemNotFound("Port '%s' not found." % port_id)
示例#27
0
def get_metadata_item(request, image_id, key):
    # Normal Response Codes: 200, 203
    # Error Response Codes: computeFault (400, 500),
    #                       serviceUnavailable (503),
    #                       unauthorized (401),
    #                       itemNotFound (404),
    #                       badRequest (400),
    #                       overLimit (413)

    log.debug('get_image_metadata_item %s %s', image_id, key)
    with backend.PlanktonBackend(request.user_uniq) as b:
        image = b.get_image(image_id)
    val = image['properties'].get(key)
    if val is None:
        raise faults.ItemNotFound('Metadata key not found.')
    return util.render_meta(request, {key: val}, status=200)
示例#28
0
文件: util.py 项目: vgerak/synnefo
def get_floating_ip_by_id(credentials, floating_ip_id, for_update=False):
    try:
        floating_ip_id = int(floating_ip_id)

        objects = policy.IPAddressPolicy.filter_list(credentials)\
                                        .filter(floating_ip=True,
                                                deleted=False)
        if for_update:
            objects = objects.select_for_update()

        return objects.get(id=floating_ip_id)
    except IPAddress.DoesNotExist:
        raise faults.ItemNotFound("Floating IP with ID %s does not exist." %
                                  floating_ip_id)
    except (ValueError, TypeError):
        raise faults.BadRequest("Invalid Floating IP ID %s" % floating_ip_id)
示例#29
0
文件: servers.py 项目: vgerak/synnefo
def get_server_password(request, server_id):
    # Normal Response Code: 200
    # Error Response Codes: computeFault (400, 500),
    #                       unauthorized (401),
    #                       itemNotFound (404),
    #                       badRequest (400),
    vm = util.get_vm(server_id, request.credentials)

    password = VM_PASSWORD_CACHE.get(str(vm.pk))

    if not password:
        raise faults.ItemNotFound()

    data = json.dumps({'password': password})

    return HttpResponse(data, status=200)
示例#30
0
def get_network(network_id, user_id, for_update=False, non_deleted=False):
    """Return a Network instance or raise ItemNotFound."""

    try:
        network_id = int(network_id)
        objects = Network.objects
        if for_update:
            objects = objects.select_for_update()
        network = objects.get(Q(userid=user_id) | Q(public=True),
                              id=network_id)
        if non_deleted and network.deleted:
            raise faults.BadRequest("Network has been deleted.")
        return network
    except (ValueError, TypeError):
        raise faults.BadRequest("Invalid network ID '%s'" % network_id)
    except Network.DoesNotExist:
        raise faults.ItemNotFound('Network %s not found.' % network_id)