Added seed code for access-management.
[ta/access-management.git] / src / access_management / rest-plugin / permissions.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 Permissions(AMApiBase):
19
20     """
21     Permission list operations
22
23     .. :quickref: Permissions;Permission list operations
24
25     .. http:get:: /am/v1/permissions
26
27     **Start Permission list**
28
29     **Example request**:
30
31     .. sourcecode:: http
32
33         GET am/v1/permissions HTTP/1.1
34         Host: haproxyvip:61200
35         Accept: application/json
36
37     **Example response**:
38
39     .. sourcecode:: http
40
41         HTTP/1.1 200 OK
42         {
43             "code": 0,
44             "description": ""
45             "data":
46             {
47                 "am/permissions":
48                 {
49                     "permission_name": "am/permissions",
50                     "resources": ["GET"]
51                 },
52                 "am/permissions/details":
53                 {
54                     "permission_name": "am/permissions/details",
55                     "resources": ["GET"]
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 permissions elements
63     :> json string permission_name: Permission name
64     :> json string resources: permissions resources
65     """
66
67     endpoints = ['permissions']
68
69     def get(self):
70         self.logger.info("Received a permission list request!")
71         permissions_lis=dict({})
72         state, permissions = self._permission_list()
73
74         if state:
75             for element in permissions:
76                 value = dict({})
77                 value.update({"permission_name": element, "resources": permissions[element]})
78                 permissions_lis.update({element: value})
79             self.logger.info("The permission list response done!")
80             return AMApiBase.embed_data(permissions_lis, 0, "")
81         else:
82             return AMApiBase.construct_error_response(1, permissions)
83
84     def _permission_list(self):
85         state_open, message_open = self._open_db()
86         if state_open:
87             try:
88                 permissions = self.db.get_resources()
89             except Exception as ex:
90                 self.logger.error("Internal error: {0}".format(ex))
91                 return False, "{0}".format(ex)
92             finally:
93                 state_close, message_close = self._close_db()
94                 if not state_close:
95                     self._close_db()
96             return True, permissions
97         else:
98             return False, message_open