def create_project(request): if request.is_ajax() and request.method == 'POST': token = request.session.get('passToken') auth_url = request.session.get('auth_url') project = json.loads(request.POST.get("project")) data = { "project": { "description": project.get("description"), "domain_id": project.get("domain_id"), "name": project.get("name"), "enabled": project.get("enabled"), "is_domain": False, } } keystone = KeystoneRestAPI(auth_url, token) result = keystone.create_project(data) if result.get("success"): project_id = result["success"]["project"].get("id") assign = json.loads(request.POST.get("assignList")) assign_users = assign.get("users") for assign_user in assign_users: user_id = assign_user.get("user_id") role_id_list = assign_user.get("role_id_list") for role_id in role_id_list: assign_result = keystone.assign_role_to_user_on_projects( project_id, user_id, role_id) if assign_result.get("error"): if not result.get("error"): result["error"] = [] result["error"].append(assign_result["error"]) assign_groups = assign.get("groups") for assign_group in assign_groups: group_id = assign_group.get("group_id") role_id_list = assign_group.get("role_id_list") for role_id in role_id_list: assign_result = keystone.assign_role_to_group_on_projects( project_id, group_id, role_id) if assign_result.get("error"): if not result.get("error"): result["error"] = [] result["error"].append(assign_result["error"]) return JsonResponse(result)
def create_user(request): if request.is_ajax() and request.method == 'POST': token = request.session.get('passToken') auth_url = request.session.get('auth_url') keystone = KeystoneRestAPI(auth_url, token) user = json.loads(request.POST.get("user")) data = { "user": { k: v for k, v in user.items() if v is not None and k not in ["role_id", "domain_name"] } } result = keystone.create_user(data) if result.get("success") and user.get("default_project_id"): created_user = result["success"].get("user") a_result = keystone.assign_role_to_user_on_projects(user.get("default_project_id"), created_user.get("id"), user.get("role_id")) if a_result.get("error"): result["error"] = a_result["error"] return JsonResponse(result)
def update_project(request, project_id): if request.is_ajax() and request.method == 'POST': token = request.session.get('passToken') auth_url = request.session.get('auth_url') project = json.loads(request.POST.get("project")) quota = json.loads(request.POST.get("quotas")) data = { "project": { "description": project.get("description"), "domain_id": project.get("domain_id"), "name": project.get("name"), "enabled": project.get("enabled"), "is_domain": False, } } keystone = KeystoneRestAPI(auth_url, token) result = keystone.update_project(project_id, data) err_msg_list = [] if result.get("success"): assign_list = json.loads(request.POST.get("assignList")) unassign_list = json.loads(request.POST.get("unassignList")) # 사용자 역할 할당 for assign in assign_list.get("users"): user_id = assign.get("user_id") role_id_list = assign.get("role_id_list") for role_id in role_id_list: assign_result = keystone.assign_role_to_user_on_projects( project_id, user_id, role_id) if assign_result.get("error"): if not result.get("error"): result["error"] = [] result["error"].append(assign_result["error"]) # 사용자 역할 제거 for unassign in unassign_list.get("users"): user_id = unassign.get("user_id") role_id_list = unassign.get("role_id_list") for role_id in role_id_list: unassign_result = keystone.unassign_role_to_user_on_projects( project_id, user_id, role_id) if unassign_result.get("error"): if not result.get("error"): result["error"] = [] result["error"].append(unassign_result["error"]) # 그룹 역할 할당 for assign in assign_list.get("groups"): group_id = assign.get("group_id") role_id_list = assign.get("role_id_list") for role_id in role_id_list: assign_result = keystone.assign_role_to_group_on_projects( project_id, group_id, role_id) if assign_result.get("error"): if not result.get("error"): result["error"] = [] result["error"].append(assign_result["error"]) # 그룹 역할 제거 for unassign in unassign_list.get("groups"): group_id = unassign.get("group_id") role_id_list = unassign.get("role_id_list") for role_id in role_id_list: unassign_result = keystone.unassign_role_to_group_on_projects( project_id, group_id, role_id) if unassign_result.get("error"): if not result.get("error"): result["error"] = [] result["error"].append(unassign_result["error"]) if quota["nova"].get("quota_set"): nova = NovaRestAPI(auth_url, token) result = nova.update_quotas(project_id, quota["nova"]) if result.get("error"): err_msg_list.append(result["error"]) if quota["neutron"].get("quota"): neutron = NeutronRestAPI(auth_url, token) result = neutron.update_quotas(project_id, quota["neutron"]) if result.get("error"): err_msg_list.append(result["error"]) if quota["cinder"].get("quota_set"): cinder = CinderRestAPI(auth_url, token) result = cinder.update_quotas(project_id, quota["cinder"]) if result.get("error"): err_msg_list.append(result["error"]) return JsonResponse(result)