| 1 |
# Copyright 2015 Huawei Technologies India Pvt Ltd. |
| 2 |
# All Rights Reserved |
| 3 |
# |
| 4 |
# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 5 |
# not use this file except in compliance with the License. You may obtain |
| 6 |
# a copy of the License at |
| 7 |
# |
| 8 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
# |
| 10 |
# Unless required by applicable law or agreed to in writing, software |
| 11 |
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 |
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 |
# License for the specific language governing permissions and limitations |
| 14 |
# under the License. |
| 15 |
|
| 16 |
from neutronclient._i18n import _ |
| 17 |
from neutronclient.common import utils |
| 18 |
from neutronclient.neutron import v2_0 as neutronV20 |
| 19 |
|
| 20 |
# key=object_type: value={key=resource, value=cmd_resource} |
| 21 |
RBAC_OBJECTS = {'network': {'network': 'network'}, |
| 22 |
'qos-policy': {'policy': 'qos_policy'}} |
| 23 |
|
| 24 |
|
| 25 |
def _get_cmd_resource(obj_type): |
| 26 |
resource = list(RBAC_OBJECTS[obj_type])[0] |
| 27 |
cmd_resource = RBAC_OBJECTS[obj_type][resource] |
| 28 |
return resource, cmd_resource |
| 29 |
|
| 30 |
|
| 31 |
def get_rbac_obj_params(client, obj_type, obj_id_or_name): |
| 32 |
resource, cmd_resource = _get_cmd_resource(obj_type) |
| 33 |
obj_id = neutronV20.find_resourceid_by_name_or_id( |
| 34 |
client=client, resource=resource, name_or_id=obj_id_or_name, |
| 35 |
cmd_resource=cmd_resource) |
| 36 |
|
| 37 |
return obj_id, cmd_resource |
| 38 |
|
| 39 |
|
| 40 |
class ListRBACPolicy(neutronV20.ListCommand): |
| 41 |
"""List RBAC policies that belong to a given tenant.""" |
| 42 |
|
| 43 |
resource = 'rbac_policy' |
| 44 |
list_columns = ['id', 'object_type', 'object_id'] |
| 45 |
pagination_support = True |
| 46 |
sorting_support = True |
| 47 |
allow_names = False |
| 48 |
|
| 49 |
|
| 50 |
class ShowRBACPolicy(neutronV20.ShowCommand): |
| 51 |
"""Show information of a given RBAC policy.""" |
| 52 |
|
| 53 |
resource = 'rbac_policy' |
| 54 |
allow_names = False |
| 55 |
|
| 56 |
|
| 57 |
class CreateRBACPolicy(neutronV20.CreateCommand): |
| 58 |
"""Create a RBAC policy for a given tenant.""" |
| 59 |
|
| 60 |
resource = 'rbac_policy' |
| 61 |
|
| 62 |
def add_known_arguments(self, parser): |
| 63 |
parser.add_argument( |
| 64 |
'name', |
| 65 |
metavar='RBAC_OBJECT', |
| 66 |
help=_('ID or name of the RBAC object.')) |
| 67 |
parser.add_argument( |
| 68 |
'--type', choices=RBAC_OBJECTS.keys(), |
| 69 |
required=True, |
| 70 |
type=utils.convert_to_lowercase, |
| 71 |
help=_('Type of the object that RBAC policy affects.')) |
| 72 |
parser.add_argument( |
| 73 |
'--target-tenant', |
| 74 |
default='*', |
| 75 |
help=_('ID of the tenant to which the RBAC ' |
| 76 |
'policy will be enforced.')) |
| 77 |
parser.add_argument( |
| 78 |
'--action', choices=['access_as_external', 'access_as_shared'], |
| 79 |
type=utils.convert_to_lowercase, |
| 80 |
required=True, |
| 81 |
help=_('Action for the RBAC policy.')) |
| 82 |
|
| 83 |
def args2body(self, parsed_args): |
| 84 |
neutron_client = self.get_client() |
| 85 |
_object_id, _object_type = get_rbac_obj_params(neutron_client, |
| 86 |
parsed_args.type, |
| 87 |
parsed_args.name) |
| 88 |
body = { |
| 89 |
'object_id': _object_id, |
| 90 |
'object_type': _object_type, |
| 91 |
'target_tenant': parsed_args.target_tenant, |
| 92 |
'action': parsed_args.action, |
| 93 |
} |
| 94 |
return {self.resource: body} |
| 95 |
|
| 96 |
|
| 97 |
class UpdateRBACPolicy(neutronV20.UpdateCommand): |
| 98 |
"""Update RBAC policy for given tenant.""" |
| 99 |
|
| 100 |
resource = 'rbac_policy' |
| 101 |
allow_names = False |
| 102 |
|
| 103 |
def add_known_arguments(self, parser): |
| 104 |
parser.add_argument( |
| 105 |
'--target-tenant', |
| 106 |
help=_('ID of the tenant to which the RBAC ' |
| 107 |
'policy will be enforced.')) |
| 108 |
|
| 109 |
def args2body(self, parsed_args): |
| 110 |
body = {'target_tenant': parsed_args.target_tenant} |
| 111 |
return {self.resource: body} |
| 112 |
|
| 113 |
|
| 114 |
class DeleteRBACPolicy(neutronV20.DeleteCommand): |
| 115 |
"""Delete a RBAC policy.""" |
| 116 |
|
| 117 |
resource = 'rbac_policy' |
| 118 |
allow_names = False |