Added seed code for access-management.
[ta/access-management.git] / src / access_management / rest-plugin / users_parameters.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 from keystoneauth1 import exceptions
17
18 class UsersParameters(AMApiBase):
19
20     """
21     User set parameter operations
22
23     .. :quickref: User set parameter;User set parameter operations
24
25     .. http:post:: /am/v1/users/parameters
26
27     **Start User set parameter**
28
29     **Example request**:
30
31     .. sourcecode:: http
32
33         POST am/v1/users/parameters HTTP/1.1
34         Host: haproxyvip:61200
35         Accept: application/json
36         {
37             "user": <uuid> or <username>
38             "project_id: <project_id>
39             "email": <email>
40         }
41
42     :> json string user: The user's id or name.
43     :> json string project_id: The user's default project id.
44     :> json string email: The user's email.
45
46     **Example response**:
47
48     .. sourcecode:: http
49
50         HTTP/1.1 200 OK
51         {
52             "code": 0,
53             "description": "User parameter modified."
54         }
55
56     :> json int code: the status code
57     :> json string description: the error description, present if code is non zero
58     """
59
60     endpoints = ['users/parameters']
61     parser_arguments = ['user',
62                         'project_id',
63                         'email',
64                         'description']
65
66     def post(self):
67         self.logger.info("Received a set parameters request!")
68         args = self.parse_args()
69
70         if args["project_id"] is not None:
71             projectidstate = self.id_validator(args["project_id"])
72             if projectidstate == False:
73                 self.logger.error("Project id validation failed")
74                 return AMApiBase.embed_data({}, 1, "Project id validation failed")
75
76         state, user_info = self.get_uuid_and_name(args["user"])
77         if state:
78             status, message = self._set_params(args, user_info)
79
80             if status:
81                 self.logger.info("User parameter modified.")
82                 return AMApiBase.embed_data({}, 0, "")
83             else:
84                 self.logger.error("Internal error in the keystone part: {0}".format(message))
85                 return AMApiBase.embed_data({}, 1, message)
86         else:
87             self.logger.error(user_info)
88             return AMApiBase.embed_data({}, 1, user_info)
89
90     def _set_params(self, args, user_info):
91         if args["project_id"] is not None:
92             um_proj_id = self.get_project_id(defaults.PROJECT_NAME)
93             if um_proj_id is None:
94                 self.logger.error("The user management project is not found!")
95                 return False, "Keystone error, please try again."
96             ks_member_roleid = self.get_role_id(defaults.KS_MEMBER_NAME)
97             if ks_member_roleid is None:
98                 self.logger.error("Member user role not found!")
99                 return False, "Keystone error, please try again."
100             if args["project_id"] != um_proj_id and user_info["project"] != args["project_id"]:
101                 state, message = self.send_role_request_and_check_response(ks_member_roleid, user_info["id"], "put", args["project_id"])
102                 if not state:
103                     self.logger.error("KS error adding project: {0}".format(message))
104                     return False, "Keystone error, please try again."
105             if user_info["project"] and user_info["project"] != um_proj_id and user_info["project"] != args["project_id"]:
106                 state, message = self.send_role_request_and_check_response(ks_member_roleid, user_info["id"], "delete", user_info["project"])
107                 if not state:
108                     self.logger.error("KS error removing project: {0}".format(message))
109                     return False, "Keystone error, please try again."
110         try:
111             self.keystone.users.update(user_info["id"], email=args["email"], default_project=args["project_id"])
112         except exceptions.http.NotFound as ex:
113             self.logger.error("KS NotFound error: {0}".format(ex))
114             return False, "This user does not exist in the keystone!"
115         except Exception as ex:
116             self.logger.error("KS general error: {0}".format(ex))
117             return False, "{0}".format(ex)
118         return True, "Updated!"