Added seed code for access-management.
[ta/access-management.git] / src / access_management / rest-plugin / roles_users.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 RolesUsers(AMApiBase):
19
20     """
21     Role list users operations
22
23     .. :quickref: Roles users;Role list users operations
24
25     .. http:get:: /am/v1/roles/users
26
27     **Start Role list users**
28
29     **Example request**:
30
31     .. sourcecode:: http
32
33         GET am/v1/roles/permissions 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 role name to be searched in users.
41
42     **Example response**:
43
44     .. sourcecode:: http
45
46         HTTP/1.1 200 OK
47         {
48             "code": 0,
49             "description": "Resource added to role"
50             "data":
51             {
52                 "has_all":
53                 {
54                     "role_name": "has_all",
55                     "users": [user1, user2]
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 name and role owners
63     :> json string role_name: The role name.
64     :> json string users: The role owners.
65     """
66
67     endpoints = ['roles/users']
68     parser_arguments = ['role_name']
69
70     def get(self):
71         self.logger.info("Received a role users request!")
72         args = self.parse_args()
73         result=dict({})
74         state, message = self._role_users(args)
75
76         if state:
77             result.update({"role_name": args["role_name"]})
78             result.update({"users": message})
79             self.logger.info("The role users response done!")
80             return AMApiBase.embed_data({args["role_name"]:result}, 0, "These users have this role")
81         else:
82             self.logger.error("The {0} roles users list creation failed: {1}".format(args["role_name"], message))
83             return AMApiBase.embed_data({}, 1, message)
84
85     def _role_users(self, args):
86         state_open, message_open = self._open_db()
87         if state_open:
88             try:
89                 users=self.db.get_role_users(args["role_name"])
90             except Exception as ex:
91                 self.logger.error("Internal error: {0}".format(ex))
92                 return False, "Internal error: {0}".format(ex)
93             finally:
94                 state_close, message_close = self._close_db()
95                 if not state_close:
96                     self._close_db()
97             return True, users
98         else:
99             return False, message_open