<commit_before>#   Copyright 2013 NetApp.
#
#   Licensed under the Apache License, Version 2.0 (the "License"); you may
#   not use this file except in compliance with the License. You may obtain
#   a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#   WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#   License for the specific language governing permissions and limitations
#   under the License.

import webob

from manila.api import extensions
from manila.api.openstack import wsgi
from manila import exception
from manila import share


authorize = extensions.extension_authorizer('share', 'services')


class ShareActionsController(wsgi.Controller):
    def __init__(self, *args, **kwargs):
        super(ShareActionsController, self).__init__(*args, **kwargs)
        self.share_api = share.API()

    @wsgi.action('os-allow_access')
    def _allow_access(self, req, id, body):
        """Add share access rule."""
        context = req.environ['manila.context']

        share = self.share_api.get(context, id)

        access_type = body['os-allow_access']['access_type']
        access_to = body['os-allow_access']['access_to']

        access = self.share_api.allow_access(
            context, share, access_type, access_to)
        return {'access': access}

    @wsgi.action('os-deny_access')
    def _deny_access(self, req, id, body):
        """Remove access rule."""
        context = req.environ['manila.context']

        access_id = body['os-deny_access']['access_id']

        try:
            access = self.share_api.access_get(context, access_id)
            if access.share_id != id:
                raise exception.NotFound()
            share = self.share_api.get(context, id)
        except exception.NotFound, error:
            raise webob.exc.HTTPNotFound(explanation=unicode(error))
        self.share_api.deny_access(context, share, access)
        return webob.Response(status_int=202)

    @wsgi.action('os-access_list')
    def _access_list(self, req, id, body):
        """list access rules."""
        context = req.environ['manila.context']

        share = self.share_api.get(context, id)
        access_list = self.share_api.access_get_all(context, share)
        return {'access_list': access_list}


# def create_resource():
#     return wsgi.Resource(ShareActionsController())


class Share_actions(extensions.ExtensionDescriptor):
    """Enable share actions."""

    name = 'ShareActions'<commit_msg>Added XML serialization for access actions<commit_after>#   Copyright 2013 NetApp.
#
#   Licensed under the Apache License, Version 2.0 (the "License"); you may
#   not use this file except in compliance with the License. You may obtain
#   a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#   WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#   License for the specific language governing permissions and limitations
#   under the License.

import webob

from manila.api import extensions
from manila.api.openstack import wsgi
from manila.api import xmlutil
from manila import exception
from manila import share


authorize = extensions.extension_authorizer('share', 'services')


class ShareAccessTemplate(xmlutil.TemplateBuilder):
    """XML Template for share access management methods."""

    def construct(self):
        root = xmlutil.TemplateElement('access',
                                       selector='access')
        root.set("share_id")
        root.set("deleted")
        root.set("created_at")
        root.set("updated_at")
        root.set("access_type")
        root.set("access_to")
        root.set("state")
        root.set("deleted_at")
        root.set("id")

        return xmlutil.MasterTemplate(root, 1)


class ShareAccessListTemplate(xmlutil.TemplateBuilder):
    """XML Template for share access list."""

    def construct(self):
        root = xmlutil.TemplateElement('access_list')
        elem = xmlutil.SubTemplateElement(root, 'share',
                                          selector='access_list')
        elem.set("state")
        elem.set("id")
        elem.set("access_type")
        elem.set("access_to")

        return xmlutil.MasterTemplate(root, 1)


class ShareActionsController(wsgi.Controller):
    def __init__(self, *args, **kwargs):
        super(ShareActionsController, self).__init__(*args, **kwargs)
        self.share_api = share.API()

    @wsgi.action('os-allow_access')
    @wsgi.serializers(xml=ShareAccessTemplate)
    def _allow_access(self, req, id, body):
        """Add share access rule."""
        context = req.environ['manila.context']

        share = self.share_api.get(context, id)

        access_type = body['os-allow_access']['access_type']
        access_to = body['os-allow_access']['access_to']

        access = self.share_api.allow_access(
            context, share, access_type, access_to)
        return {'access': access}

    @wsgi.action('os-deny_access')
    @wsgi.serializers(xml=ShareAccessTemplate)
    def _deny_access(self, req, id, body):
        """Remove access rule."""
        context = req.environ['manila.context']

        access_id = body['os-deny_access']['access_id']

        try:
            access = self.share_api.access_get(context, access_id)
            if access.share_id != id:
                raise exception.NotFound()
            share = self.share_api.get(context, id)
        except exception.NotFound, error:
            raise webob.exc.HTTPNotFound(explanation=unicode(error))
        self.share_api.deny_access(context, share, access)
        return webob.Response(status_int=202)

    @wsgi.action('os-access_list')
    @wsgi.serializers(xml=ShareAccessListTemplate)
    def _access_list(self, req, id, body):
        """list access rules."""
        context = req.environ['manila.context']

        share = self.share_api.get(context, id)
        access_list = self.share_api.access_get_all(context, share)
        return {'access_list': access_list}


# def create_resource():
#     return wsgi.Resource(ShareActionsController())


class Share_actions(extensions.ExtensionDescriptor):
    """Enable share actions."""

    name = 'ShareActions'
{'fraction_non_alphanumeric': 0.11116214974735875, 'fraction_numerical': 0.0036747818098300414, 'mean_word_length': 3.067247820672478, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 11}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1669174', 'n_tokens_mistral': 1882, 'n_tokens_neox': 1823, 'n_words': 476}