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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
15 from am_api_base import *
16 from keystoneauth1 import exceptions
18 class UsersParameters(AMApiBase):
21 User set parameter operations
23 .. :quickref: User set parameter;User set parameter operations
25 .. http:post:: /am/v1/users/parameters
27 **Start User set parameter**
33 POST am/v1/users/parameters HTTP/1.1
34 Host: haproxyvip:61200
35 Accept: application/json
37 "user": <uuid> or <username>
38 "project_id: <project_id>
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.
53 "description": "User parameter modified."
56 :> json int code: the status code
57 :> json string description: the error description, present if code is non zero
60 endpoints = ['users/parameters']
61 parser_arguments = ['user',
67 self.logger.info("Received a set parameters request!")
68 args = self.parse_args()
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")
76 state, user_info = self.get_uuid_and_name(args["user"])
78 status, message = self._set_params(args, user_info)
81 self.logger.info("User parameter modified.")
82 return AMApiBase.embed_data({}, 0, "")
84 self.logger.error("Internal error in the keystone part: {0}".format(message))
85 return AMApiBase.embed_data({}, 1, message)
87 self.logger.error(user_info)
88 return AMApiBase.embed_data({}, 1, user_info)
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"])
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"])
108 self.logger.error("KS error removing project: {0}".format(message))
109 return False, "Keystone error, please try again."
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!"