예제 #1
0
    def patch(self, request, project_id):
        """Update a single project quota data.

        The PATCH data should be an application/json object with the
        attributes to set to new quota values.

        This method returns HTTP 204 (no content) on success.
        """
        # Filters only neutron quota fields
        disabled_quotas = quotas.get_disabled_quotas(request)

        if api.base.is_service_enabled(request, 'network') and \
                api.neutron.is_extension_supported(request, 'quotas'):
            neutron_data = {
                key: request.DATA[key] for key in quotas.NEUTRON_QUOTA_FIELDS
                if key not in disabled_quotas
            }

            api.neutron.tenant_quota_update(request,
                                            project_id,
                                            **neutron_data)
        else:
            message = _('Service Neutron is disabled or quotas extension not '
                        'available.')
            raise rest_utils.AjaxError(501, message)
예제 #2
0
    def get(self, request):
        """Get the values for Nova specific quotas

        Example GET:
        http://localhost/api/nova/quota-sets/defaults/
        """
        if api.base.is_service_enabled(request, 'compute'):
            quota_set = api.nova.default_quota_get(request,
                                                   request.user.tenant_id)

            disabled_quotas = quotas.get_disabled_quotas(request)

            filtered_quotas = [quota for quota in quota_set
                               if quota.name not in disabled_quotas]

            result = [{
                'display_name': quotas.QUOTA_NAMES.get(
                    quota.name,
                    quota.name.replace("_", " ").title()
                ) + '',
                'name': quota.name,
                'limit': quota.limit
            } for quota in filtered_quotas]

            return {'items': result}
        else:
            raise rest_utils.AjaxError(501, _('Service Nova is disabled.'))
예제 #3
0
def create_image_metadata(data):
    try:
        """Use the given dict of image form data to generate the metadata used for
        creating the image in glance.
        """

        meta = {
            'protected': data.get('protected'),
            'min_disk': data.get('min_disk', 0),
            'min_ram': data.get('min_ram', 0),
            'name': data.get('name'),
            'disk_format': data.get('disk_format'),
            'container_format': data.get('container_format'),
            'properties': {}
        }

        # 'architecture' will be directly mapped
        # into the .properties by the handle_unknown_properties function.
        # 'kernel' and 'ramdisk' need to get specifically mapped for backwards
        # compatibility.
        props = data.get('properties')
        if props and props.get('description'):
            meta['properties']['description'] = props.get('description')
        if data.get('kernel'):
            meta['properties']['kernel_id'] = data.get('kernel')
        if data.get('ramdisk'):
            meta['properties']['ramdisk_id'] = data.get('ramdisk')
        handle_unknown_properties(data, meta)
        handle_visibility(data.get('visibility'), meta)

    except KeyError as e:
        raise rest_utils.AjaxError(400,
                                   'missing required parameter %s' % e.args[0])
    return meta
예제 #4
0
 def get(self, request):
     """Get a list of heat services.
     """
     if api.base.is_service_enabled(request, 'orchestration'):
         result = api.heat.service_list(request)
         return {'items': [u.to_dict() for u in result]}
     else:
         raise rest_utils.AjaxError(501, '')
예제 #5
0
 def get(self, request):
     """Get a list of agents
     """
     if api.base.is_service_enabled(request, 'network') and \
        api.neutron.is_extension_supported(request, 'agent'):
         result = api.neutron.agent_list(request, **request.GET)
         return {'items': [n.to_dict() for n in result]}
     else:
         raise rest_utils.AjaxError(501, '')
예제 #6
0
 def get(self, request):
     """Get a list of nova services.
     Will return HTTP 501 status code if the service_list extension is
     not supported.
     """
     if api.base.is_service_enabled(request, 'compute') \
        and api.nova.extension_supported('Services', request):
         result = api.nova.service_list(request)
         return {'items': [u.to_dict() for u in result]}
     else:
         raise rest_utils.AjaxError(501, '')
예제 #7
0
def handle_visibility(visibility, meta):
    # The following expects a 'visibility' parameter to be passed via
    # the AJAX call, then translates this to a Glance API v1 is_public
    # parameter.  In the future, if the 'visibility' param is exposed on the
    # glance API, you can check for version, e.g.:
    #   if float(api.glance.get_version()) < 2.0:
    mapping_to_v1 = {'public': True, 'private': False, 'shared': False}
    # note: presence of 'visibility' previously checked for in general call
    try:
        meta['is_public'] = mapping_to_v1[visibility]
    except KeyError as e:
        raise rest_utils.AjaxError(400,
                                   'invalid visibility option: %s' % e.args[0])
예제 #8
0
    def patch(self, request):
        """Update the values for Cinder specific quotas

        This method returns HTTP 204 (no content) on success.
        """
        if api.cinder.is_volume_service_enabled():
            cinder_data = {
                key: request.DATA[key]
                for key in quotas.CINDER_QUOTA_FIELDS
            }

            api.cinder.default_quota_update(request, **cinder_data)
        else:
            raise rest_utils.AjaxError(501, _('Service Cinder is disabled.'))
예제 #9
0
    def post(self, request):
        """Create a server.

        Create a server using the parameters supplied in the POST
        application/json object. The required parameters as specified by
        the underlying novaclient are:

        :param name: The new server name.
        :param source_id: The ID of the image to use.
        :param flavor_id: The ID of the flavor to use.
        :param key_name: (optional extension) name of previously created
                      keypair to inject into the instance.
        :param user_data: user data to pass to be exposed by the metadata
                      server this can be a file type object as well or a
                      string.
        :param security_groups: An array of one or more objects with a "name"
            attribute.

        Other parameters are accepted as per the underlying novaclient:
        "block_device_mapping", "block_device_mapping_v2", "nics", "meta",
        "availability_zone", "instance_count", "admin_pass", "disk_config",
        "config_drive"

        This returns the new server object on success.
        """
        try:
            args = (
                request,
                request.DATA['name'],
                request.DATA['source_id'],
                request.DATA['flavor_id'],
                request.DATA['key_name'],
                request.DATA['user_data'],
                request.DATA['security_groups'],
            )
        except KeyError as e:
            raise rest_utils.AjaxError(400, 'missing required parameter '
                                            "'%s'" % e.args[0])
        kw = {}
        for name in self._optional_create:
            if name in request.DATA:
                kw[name] = request.DATA[name]

        new = api.nova.server_create(*args, **kw)
        return rest_utils.CreatedResponse(
            '/api/nova/servers/%s' % utils_http.urlquote(new.id),
            new.to_dict()
        )
예제 #10
0
    def get(self, request):
        if api.base.is_service_enabled(request, 'network'):
            quota_set = api.neutron.tenant_quota_get(
                request, request.user.tenant_id)

            result = [{
                'display_name': quotas.QUOTA_NAMES.get(
                    quota.name,
                    quota.name.replace('_', ' ').title()
                ) + '',
                'name': quota.name,
                'limit': quota.limit
            } for quota in quota_set]

            return {'items': result}
        else:
            raise rest_utils.AjaxError(501, _('Service Neutron is disabled.'))
예제 #11
0
    def patch(self, request, project_id):
        """Update a single project quota data.

        The PATCH data should be an application/json object with the
        attributes to set to new quota values.

        This method returns HTTP 204 (no content) on success.
        """
        disabled_quotas = quotas.get_disabled_quotas(request)

        if api.base.is_service_enabled(request, 'compute'):
            nova_data = {
                key: request.DATA[key] for key in quotas.NOVA_QUOTA_FIELDS
                if key not in disabled_quotas
            }

            api.nova.tenant_quota_update(request, project_id, **nova_data)
        else:
            raise rest_utils.AjaxError(501, _('Service Nova is disabled.'))
예제 #12
0
    def post(self, request):
        """Create a project (tenant).

        Create a project using parameters supplied in the POST
        application/json object. The "name" (string) parameter is required,
        others are optional: "description" (string), "domain_id" (string) and
        "enabled" (boolean, defaults to true). Additional, undefined
        parameters may also be provided, but you'll have to look deep into
        keystone to figure out what they might be.

        This method returns the new project object on success.
        """
        kwargs = _tenant_kwargs_from_DATA(request.DATA)
        if not kwargs['name']:
            raise rest_utils.AjaxError(400, '"name" is required')
        new_project = api.keystone.tenant_create(request, kwargs.pop('name'),
                                                 **kwargs)
        return rest_utils.CreatedResponse(
            '/api/keystone/projects/%s' % new_project.id,
            new_project.to_dict())
예제 #13
0
    def patch(self, request, project_id):
        """Update a single project quota data.

        The PATCH data should be an application/json object with the
        attributes to set to new quota values.

        This method returns HTTP 204 (no content) on success.
        """
        # Filters cinder quota fields
        disabled_quotas = quotas.get_disabled_quotas(request)

        if api.cinder.is_volume_service_enabled():
            cinder_data = {
                key: request.DATA[key]
                for key in quotas.CINDER_QUOTA_FIELDS
                if key not in disabled_quotas
            }

            api.cinder.tenant_quota_update(request, project_id, **cinder_data)
        else:
            raise rest_utils.AjaxError(501, _('Service Cinder is disabled.'))
예제 #14
0
 def get(self, request):
     """Get a list of cinder services.
     Will return HTTP 501 status code if the service_list extension is
     not supported.
     """
     if api.base.is_service_enabled(request, 'volume') and \
        api.cinder.extension_supported(request, 'Services'):
         result = api.cinder.service_list(request)
         return {
             'items': [{
                 'binary': u.binary,
                 'host': u.host,
                 'zone': u.zone,
                 'updated_at': u.updated_at,
                 'status': u.status,
                 'state': u.state,
                 'id': idx + 1
             } for idx, u in enumerate(result)]
         }
     else:
         raise rest_utils.AjaxError(501, '')
예제 #15
0
    def post(self, request):
        '''Check policy rules.

        Check the group of policy rules supplied in the POST
        application/json object. The policy target, if specified will also be
        passed in to the policy check method as well.

        The action returns an object with one key: "allowed" and the value
        is the result of the policy check, True or False.
        '''

        rules = []
        try:
            rules_in = request.DATA['rules']
            rules = tuple([tuple(rule) for rule in rules_in])
        except Exception:
            raise rest_utils.AjaxError(400, 'unexpected parameter format')

        policy_target = request.DATA.get('target') or {}

        result = policy.check(rules, request, policy_target)

        return {"allowed": result}
예제 #16
0
    def patch(self, request):
        """Update the values for Nova specific quotas

        This method returns HTTP 204 (no content) on success.
        """
        if api.base.is_service_enabled(request, 'compute'):
            disabled_quotas = quotas.get_disabled_quotas(request)

            all_quotas = quotas.NOVA_QUOTA_FIELDS + quotas.MISSING_QUOTA_FIELDS

            filtered_quotas = [quota for quota in all_quotas
                               if quota not in disabled_quotas]

            request_data = {
                key: request.DATA.get(key, None) for key in filtered_quotas
            }

            nova_data = {key: value for key, value in request_data.items()
                         if value is not None}

            api.nova.default_quota_update(request, **nova_data)
        else:
            raise rest_utils.AjaxError(501, _('Service Nova is disabled.'))
예제 #17
0
    def get(self, request):
        """Get the values for Cinder specific quotas

        Example GET:
        http://localhost/api/cinder/quota-sets/defaults/
        """
        if api.cinder.is_volume_service_enabled():
            quota_set = api.cinder.default_quota_get(request,
                                                     request.user.tenant_id)

            result = [{
                'display_name':
                quotas.QUOTA_NAMES.get(quota.name,
                                       quota.name.replace("_", " ").title()) +
                '',
                'name':
                quota.name,
                'limit':
                quota.limit
            } for quota in quota_set]
            return {'items': result}
        else:
            raise rest_utils.AjaxError(501, _('Service Cinder is disabled.'))
예제 #18
0
    def post(self, request, container, object_name):
        """Create a new object or pseudo-folder

        :param request:
        :param container:
        :param object_name:

        If the object_name (ie. POST path) ends in a '/' then a folder is
        created, rather than an object. Any file content passed along with
        the request will be ignored in that case.

        POST parameter:

        :param file: the file data for the upload.

        :return:
        """
        form = UploadObjectForm(request.POST, request.FILES)
        if not form.is_valid():
            raise rest_utils.AjaxError(500, 'Invalid request')

        data = form.clean()

        try:
            if object_name[-1] == '/':
                result = api.swift.swift_create_pseudo_folder(
                    request, container, object_name)
            else:
                result = api.swift.swift_upload_object(request, container,
                                                       object_name,
                                                       data['file'])
        except exceptions.AlreadyExists as e:
            # 409 Conflict
            return rest_utils.JSONResponse(str(e), 409)

        return rest_utils.CreatedResponse(
            u'/api/swift/containers/%s/object/%s' % (container, result.name))