Added seed code for access-management.
[ta/access-management.git] / src / access_management / rest-plugin / roles_details.py
1 # Copyright 2019 Nokia
2
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 #     http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 from am_api_base import *
16
17
18 class RolesDetails(AMApiBase):
19
20     """
21     Role details operations
22
23     .. :quickref: Roles details;Role details operations
24
25     .. http:get:: /am/v1/roles/details
26
27     **Start Role details**
28
29     **Example request**:
30
31     .. sourcecode:: http
32
33         GET am/v1/roles/details HTTP/1.1
34         Host: haproxyvip:61200
35         Accept: application/json
36         {
37             "role_name": "test_role"
38         }
39
40     :> json string role_name: The showed role name.
41
42     **Example response**:
43
44     .. sourcecode:: http
45
46         HTTP/1.1 200 OK
47         {
48             "code": 0,
49             "description": "Role details."
50             "data":
51             {
52                 "has":
53                 {
54                     "permission_name": "has",
55                     "resources": ["GET", "POST"]
56                  }
57             }
58         }
59
60     :> json int code: the status code
61     :> json string description: the error description, present if code is non zero
62     :> json object data: a dictionary with the role's details
63     :> json string permission_name: The permission's name.
64     :> json string resources: The permission resource's.
65     """
66
67     endpoints = ['roles/details']
68     parser_arguments = ['role_name']
69
70     def get(self):
71         self.logger.info("Received a role show request!")
72         role_details=dict({})
73         args = self.parse_args()
74
75         state, details = self._role_show(args)
76
77         if state:
78             if len(details) == 0:
79                 role_details.update({"None":{"permission_name": "No permissions","resources": "None"}})
80             else:
81                 for perm in details:
82                     role_details.update({perm: {"permission_name": perm,"resources": details[perm]}})
83             self.logger.info("The {0} role show response done!".format(args["role_name"]))
84             return AMApiBase.embed_data(role_details, 0)
85         else:
86             self.logger.error("The {0} role show failed: {1}".format(args["role_name"], details))
87             return AMApiBase.construct_error_response(1, details)
88
89     def _role_show(self,args):
90         state_open, message_open = self._open_db()
91         if state_open:
92             try:
93                 details = self.db.get_role_resources(args["role_name"])
94                 roles = self.db.get_role_table()
95                 for role in roles:
96                     if role["name"] == args["role_name"]:
97                         break
98             except Exception as ex:
99                 self.logger.error("Internal error: {0}".format(ex))
100                 return False, "Internal error: {0}".format(ex)
101             finally:
102                 self.db.close()
103             return True, details
104         else:
105             return False, message_open