From 913b04b7f90214558cfc68728f3630c8354517cc Mon Sep 17 00:00:00 2001 From: Lei Lei Date: Fri, 30 Jan 2015 23:24:14 -0800 Subject: [PATCH] Passing user variable as kwarg. Change-Id: Idc8f995715c032b4a57a763e336ac512afce3c81 --- bin/clean_installers.py | 2 +- bin/delete_clusters.py | 4 +- bin/manage_db.py | 4 +- bin/poll_switch.py | 2 +- compass/actions/poll_switch.py | 10 +- compass/actions/update_progress.py | 31 +- compass/actions/util.py | 48 +- compass/api/api.py | 261 ++++---- compass/db/api/adapter_holder.py | 4 +- compass/db/api/cluster.py | 234 ++++--- compass/db/api/database.py | 4 +- compass/db/api/host.py | 126 ++-- compass/db/api/machine.py | 28 +- compass/db/api/network.py | 14 +- compass/db/api/permission.py | 6 +- compass/db/api/switch.py | 91 +-- compass/db/api/user.py | 93 +-- compass/db/api/user_log.py | 10 +- .../update_progress/test_update_progress.py | 60 +- compass/tests/api/test_api.py | 5 +- compass/tests/api/test_health_check_api.py | 2 +- compass/tests/db/api/test_adapter_holder.py | 8 +- compass/tests/db/api/test_cluster.py | 608 +++++++++--------- compass/tests/db/api/test_host.py | 396 ++++++------ compass/tests/db/api/test_machine.py | 50 +- compass/tests/db/api/test_network.py | 88 ++- compass/tests/db/api/test_permission.py | 6 +- compass/tests/db/api/test_switch.py | 433 +++++++------ compass/tests/db/api/test_user.py | 120 ++-- compass/tests/db/api/test_user_log.py | 43 +- 30 files changed, 1425 insertions(+), 1366 deletions(-) diff --git a/bin/clean_installers.py b/bin/clean_installers.py index a2fef6fe..6b1ae3ed 100755 --- a/bin/clean_installers.py +++ b/bin/clean_installers.py @@ -61,7 +61,7 @@ def clean_installers(): if package_installer ] user = user_api.get_user_object(setting.COMPASS_ADMIN_EMAIL) - adapters = adapter_api.list_adapters(user) + adapters = adapter_api.list_adapters(user=user) filtered_os_installers = {} filtered_package_installers = {} for adapter in adapters: diff --git a/bin/delete_clusters.py b/bin/delete_clusters.py index 37a24bb5..fddec179 100755 --- a/bin/delete_clusters.py +++ b/bin/delete_clusters.py @@ -56,13 +56,13 @@ def delete_clusters(): if clusternames: list_cluster_args['name'] = clusternames clusters = cluster_api.list_clusters( - user, **list_cluster_args + user=user, **list_cluster_args ) delete_underlying_host = flags.OPTIONS.delete_hosts for cluster in clusters: cluster_id = cluster['id'] cluster_api.del_cluster( - user, cluster_id, True, False, delete_underlying_host + cluster_id, True, False, delete_underlying_host, user=user ) diff --git a/bin/manage_db.py b/bin/manage_db.py index 108593ea..5ab5b1db 100755 --- a/bin/manage_db.py +++ b/bin/manage_db.py @@ -148,7 +148,7 @@ def set_switch_machines(): switch_mapping = {} for switch in switches: added_switch = switch_api.add_switch( - user, False, **switch + False, user=user, **switch ) switch_mapping[switch['ip']] = added_switch['id'] for switch_ip, machines in switch_machines.items(): @@ -158,7 +158,7 @@ def set_switch_machines(): switch_id = switch_mapping[switch_ip] for machine in machines: switch_api.add_switch_machine( - user, switch_id, False, **machine + switch_id, False, user=user, **machine ) diff --git a/bin/poll_switch.py b/bin/poll_switch.py index 779fa1b8..c61e1dd2 100755 --- a/bin/poll_switch.py +++ b/bin/poll_switch.py @@ -62,7 +62,7 @@ def pollswitches(switch_ips): poll_switches = [] all_switches = dict([ (switch['ip'], switch['credentials']) - for switch in switch_api.list_switches(user) + for switch in switch_api.list_switches(user=user) ]) if switch_ips: poll_switches = dict([ diff --git a/compass/actions/poll_switch.py b/compass/actions/poll_switch.py index dafb125b..0e7f2850 100644 --- a/compass/actions/poll_switch.py +++ b/compass/actions/poll_switch.py @@ -128,7 +128,7 @@ def poll_switch(poller_email, ip_addr, credentials, switch_dict, machine_dicts = _poll_switch( ip_addr, credentials, req_obj=req_obj, oper=oper ) - switches = switch_api.list_switches(poller, ip_int=ip_int) + switches = switch_api.list_switches(ip_int=ip_int, user=poller) if not switches: logging.error('no switch found for %s', ip_addr) return @@ -137,6 +137,10 @@ def poll_switch(poller_email, ip_addr, credentials, for machine_dict in machine_dicts: logging.debug('add machine: %s', machine_dict) switch_api.add_switch_machine( - poller, switch['id'], False, **machine_dict + switch['id'], False, user=poller, **machine_dict + ) + switch_api.update_switch( + switch['id'], + user=poller, + **switch_dict ) - switch_api.update_switch(poller, switch['id'], **switch_dict) diff --git a/compass/actions/update_progress.py b/compass/actions/update_progress.py index cb7aa577..5581f102 100644 --- a/compass/actions/update_progress.py +++ b/compass/actions/update_progress.py @@ -55,7 +55,7 @@ def update_progress(): logging.info('update installing progress') user = user_api.get_user_object(setting.COMPASS_ADMIN_EMAIL) - hosts = host_api.list_hosts(user) + hosts = host_api.list_hosts(user=user) host_mapping = {} for host in hosts: if 'id' not in host: @@ -74,13 +74,13 @@ def update_progress(): '%s is not in host %s', host_dirname, host ) continue - host_state = host_api.get_host_state(user, host_id) + host_state = host_api.get_host_state(host_id, user=user) if 'state' not in host_state: logging.error('state is not in host state %s', host_state) continue if host_state['state'] == 'INSTALLING': host_log_histories = host_api.get_host_log_histories( - user, host_id + host_id, user=user ) host_log_history_mapping = {} for host_log_history in host_log_histories: @@ -101,7 +101,7 @@ def update_progress(): 'ignore host state %s since it is not in installing', host_state ) - adapters = adapter_api.list_adapters(user) + adapters = adapter_api.list_adapters(user=user) adapter_mapping = {} for adapter in adapters: if 'id' not in adapter: @@ -116,7 +116,7 @@ def update_progress(): continue adapter_id = adapter['id'] adapter_mapping[adapter_id] = adapter - clusters = cluster_api.list_clusters(user) + clusters = cluster_api.list_clusters(user=user) cluster_mapping = {} for cluster in clusters: if 'id' not in cluster: @@ -129,12 +129,15 @@ def update_progress(): cluster ) continue - cluster_state = cluster_api.get_cluster_state(user, cluster_id) + cluster_state = cluster_api.get_cluster_state( + cluster_id, + user=user + ) if 'state' not in cluster_state: logging.error('state not in cluster state %s', cluster_state) continue cluster_mapping[cluster_id] = (cluster, cluster_state) - clusterhosts = cluster_api.list_clusterhosts(user) + clusterhosts = cluster_api.list_clusterhosts(user=user) clusterhost_mapping = {} for clusterhost in clusterhosts: if 'clusterhost_id' not in clusterhost: @@ -194,7 +197,7 @@ def update_progress(): package_installer = adapter['package_installer'] clusterhost['package_installer'] = package_installer clusterhost_state = cluster_api.get_clusterhost_self_state( - user, clusterhost_id + clusterhost_id, user=user ) if 'state' not in clusterhost_state: logging.error( @@ -205,7 +208,7 @@ def update_progress(): if clusterhost_state['state'] == 'INSTALLING': clusterhost_log_histories = ( cluster_api.get_clusterhost_log_histories( - user, clusterhost_id + clusterhost_id, user=user ) ) clusterhost_log_history_mapping = {} @@ -236,7 +239,7 @@ def update_progress(): host_mapping.items() ): host_api.update_host_state( - user, host_id, + host_id, user=user, percentage=host_state.get('percentage', 0), message=host_state.get('message', ''), severity=host_state.get('severity', 'INFO') @@ -245,7 +248,7 @@ def update_progress(): host_log_history_mapping.items() ): host_api.add_host_log_history( - user, host_id, filename=filename, + host_id, filename=filename, user=user, position=host_log_history.get('position', 0), percentage=host_log_history.get('percentage', 0), partial_line=host_log_history.get('partial_line', ''), @@ -264,7 +267,7 @@ def update_progress(): clusterhost_mapping.items() ): cluster_api.update_clusterhost_state( - user, clusterhost_id, + clusterhost_id, user=user, percentage=clusterhost_state.get('percentage', 0), message=clusterhost_state.get('message', ''), severity=clusterhost_state.get('severity', 'INFO') @@ -273,7 +276,7 @@ def update_progress(): clusterhost_log_history_mapping.items() ): cluster_api.add_clusterhost_log_history( - user, clusterhost_id, filename=filename, + clusterhost_id, user=user, filename=filename, position=clusterhost_log_history.get('position', 0), percentage=clusterhost_log_history.get('percentage', 0), partial_line=clusterhost_log_history.get( @@ -290,5 +293,5 @@ def update_progress(): cluster_mapping) for cluster_id, (cluster, cluster_state) in cluster_mapping.items(): cluster_api.update_cluster_state( - user, cluster_id + cluster_id, user=user ) diff --git a/compass/actions/util.py b/compass/actions/util.py index 054fe9b7..683575b0 100644 --- a/compass/actions/util.py +++ b/compass/actions/util.py @@ -94,8 +94,8 @@ class ActionHelper(object): } To view a complete output, please refer to backend doc. """ - adapter_info = adapter_db.get_adapter(user, adapter_id) - metadata = cluster_db.get_cluster_metadata(user, cluster_id) + adapter_info = adapter_db.get_adapter(adapter_id, user=user) + metadata = cluster_db.get_cluster_metadata(cluster_id, user=user) adapter_info.update({const.METADATA: metadata}) for flavor_info in adapter_info[const.FLAVORS]: @@ -128,7 +128,7 @@ class ActionHelper(object): "owner": "xxx" } """ - cluster_info = cluster_db.get_cluster(user, cluster_id) + cluster_info = cluster_db.get_cluster(cluster_id, user=user) # convert roles retrieved from db into a list of role names roles_info = cluster_info.setdefault( @@ -137,11 +137,11 @@ class ActionHelper(object): ActionHelper._get_role_names(roles_info) # get cluster config info - cluster_config = cluster_db.get_cluster_config(user, cluster_id) + cluster_config = cluster_db.get_cluster_config(cluster_id, user=user) cluster_info.update(cluster_config) - deploy_config = cluster_db.get_cluster_deployed_config(user, - cluster_id) + deploy_config = cluster_db.get_cluster_deployed_config(cluster_id, + user=user) cluster_info.update(deploy_config) return cluster_info @@ -179,7 +179,7 @@ class ActionHelper(object): """ hosts_info = {} for host_id in hosts_id_list: - info = cluster_db.get_cluster_host(user, cluster_id, host_id) + info = cluster_db.get_cluster_host(cluster_id, host_id, user=user) logging.debug("checking on info %r %r" % (host_id, info)) info[const.ROLES] = ActionHelper._get_role_names(info[const.ROLES]) @@ -187,9 +187,9 @@ class ActionHelper(object): # TODO(grace): Is following line necessary?? info.setdefault(const.ROLES, []) - config = cluster_db.get_cluster_host_config(user, - cluster_id, - host_id) + config = cluster_db.get_cluster_host_config(cluster_id, + host_id, + user=user) info.update(config) networks = info[const.NETWORKS] @@ -220,26 +220,34 @@ class ActionHelper(object): cluster_id = cluster_config[const.ID] del cluster_config[const.ID] - cluster_db.update_cluster_deployed_config(user, cluster_id, + cluster_db.update_cluster_deployed_config(cluster_id, user=user, **cluster_config) hosts_id_list = deployed_config[const.HOSTS].keys() for host_id in hosts_id_list: config = deployed_config[const.HOSTS][host_id] - cluster_db.update_cluster_host_deployed_config(user, - cluster_id, + cluster_db.update_cluster_host_deployed_config(cluster_id, host_id, + user=user, **config) @staticmethod def update_state(cluster_id, host_id_list, user): # update all clusterhosts state for host_id in host_id_list: - cluster_db.update_cluster_host_state(user, cluster_id, host_id, - state='INSTALLING') + cluster_db.update_cluster_host_state( + cluster_id, + host_id, + user=user, + state='INSTALLING' + ) # update cluster state - cluster_db.update_cluster_state(user, cluster_id, state='INSTALLING') + cluster_db.update_cluster_state( + cluster_id, + user=user, + state='INSTALLING' + ) @staticmethod def delete_cluster( @@ -251,7 +259,7 @@ class ActionHelper(object): user, host_id, True, True ) cluster_db.del_cluster( - user, cluster_id, True, True + cluster_id, True, True, user=user ) @staticmethod @@ -260,16 +268,16 @@ class ActionHelper(object): ): if delete_underlying_host: host_db.del_host( - user, host_id, True, True + host_id, True, True, user=user ) cluster_db.del_cluster_host( - user, cluster_id, host_id, True, True + cluster_id, host_id, True, True, user=user ) @staticmethod def delete_host(host_id, user): host_db.del_host( - user, host_id, True, True + host_id, True, True, user=user ) @staticmethod diff --git a/compass/api/api.py b/compass/api/api.py index 9ad8a929..2e0011cc 100644 --- a/compass/api/api.py +++ b/compass/api/api.py @@ -66,7 +66,7 @@ def update_user_token(func): datetime.datetime.now() + app.config['REMEMBER_COOKIE_DURATION'] ) user_api.record_user_token( - current_user, current_user.token, expire_timestamp + current_user.token, expire_timestamp, user=current_user ) return response return decorated_api @@ -237,7 +237,7 @@ def _login(use_cookie): user_log_api.log_user_action(user.id, request.path) response_data = user_api.record_user_token( - user, user.token, user.expire_timestamp + user.token, user.expire_timestamp, user=user ) return utils.make_json_response(200, response_data) @@ -260,7 +260,7 @@ def logout(): """User logout.""" user_log_api.log_user_action(current_user.id, request.path) response_data = user_api.clean_user_token( - current_user, current_user.token + current_user.token, user=current_user ) logout_user() return utils.make_json_response(200, response_data) @@ -277,7 +277,7 @@ def list_users(): active=_bool_converter ) return utils.make_json_response( - 200, user_api.list_users(current_user, **data) + 200, user_api.list_users(user=current_user, **data) ) @@ -288,7 +288,7 @@ def list_users(): def add_user(): """add user.""" data = _get_request_data() - user_dict = user_api.add_user(current_user, **data) + user_dict = user_api.add_user(user=current_user, **data) return utils.make_json_response( 200, user_dict ) @@ -302,7 +302,7 @@ def show_user(user_id): """Get user.""" data = _get_request_args() return utils.make_json_response( - 200, user_api.get_user(current_user, user_id, **data) + 200, user_api.get_user(user_id, user=current_user, **data) ) @@ -314,7 +314,7 @@ def show_current_user(): """Get user.""" data = _get_request_args() return utils.make_json_response( - 200, user_api.get_current_user(current_user, **data) + 200, user_api.get_current_user(user=current_user, **data) ) @@ -328,8 +328,8 @@ def update_user(user_id): return utils.make_json_response( 200, user_api.update_user( - current_user, user_id, + user=current_user, **data ) ) @@ -345,7 +345,7 @@ def delete_user(user_id): return utils.make_json_response( 200, user_api.del_user( - current_user, user_id, **data + user_id, user=current_user, **data ) ) @@ -358,7 +358,7 @@ def list_user_permissions(user_id): """Get user permissions.""" data = _get_request_args() return utils.make_json_response( - 200, user_api.get_permissions(current_user, user_id, **data) + 200, user_api.get_permissions(user_id, user=current_user, **data) ) @@ -371,14 +371,14 @@ def take_user_action(user_id): data = _get_request_data() update_permissions_func = _wrap_response( functools.partial( - user_api.update_permissions, current_user, user_id + user_api.update_permissions, user_id, user=current_user, ), 200 ) def disable_user(disable_user=None): return user_api.update_user( - current_user, user_id, active=False + user_id, user=current_user, active=False ) disable_user_func = _wrap_response( @@ -388,7 +388,7 @@ def take_user_action(user_id): def enable_user(enable_user=None): return user_api.update_user( - current_user, user_id, active=True + user_id, user=current_user, active=True ) enable_user_func = _wrap_response( @@ -418,7 +418,7 @@ def show_user_permission(user_id, permission_id): return utils.make_json_response( 200, user_api.get_permission( - current_user, user_id, permission_id, + user_id, permission_id, user=current_user, **data ) ) @@ -434,7 +434,7 @@ def add_user_permission(user_id): return utils.make_json_response( 200, user_api.add_permission( - current_user, user_id, + user_id, user=current_user, **data ) ) @@ -453,7 +453,7 @@ def delete_user_permission(user_id, permission_id): return utils.make_json_response( 200, user_api.del_permission( - current_user, user_id, permission_id, + user_id, permission_id, user=current_user, **data ) ) @@ -468,7 +468,7 @@ def list_permissions(): data = _get_request_args() return utils.make_json_response( 200, - permission_api.list_permissions(current_user, **data) + permission_api.list_permissions(user=current_user, **data) ) @@ -481,7 +481,7 @@ def show_permission(permission_id): data = _get_request_args() return utils.make_json_response( 200, - permission_api.get_permission(current_user, permission_id, **data) + permission_api.get_permission(permission_id, user=current_user, **data) ) @@ -526,7 +526,7 @@ def list_all_user_actions(): return utils.make_json_response( 200, user_log_api.list_actions( - current_user, **data + user=current_user, **data ) ) @@ -542,7 +542,7 @@ def list_user_actions(user_id): return utils.make_json_response( 200, user_log_api.list_user_actions( - current_user, user_id, **data + user_id, user=current_user, **data ) ) @@ -557,7 +557,7 @@ def delete_all_user_actions(): return utils.make_json_response( 200, user_log_api.del_actions( - current_user, **data + user=current_user, **data ) ) @@ -572,7 +572,7 @@ def delete_user_actions(user_id): return utils.make_json_response( 200, user_log_api.del_user_actions( - current_user, user_id, **data + user_id, user=current_user, **data ) ) @@ -627,7 +627,7 @@ def list_switches(): return utils.make_json_response( 200, switch_api.list_switches( - current_user, **data + user=current_user, **data ) ) @@ -640,7 +640,7 @@ def show_switch(switch_id): """Get switch.""" data = _get_request_args() return utils.make_json_response( - 200, switch_api.get_switch(current_user, switch_id, **data) + 200, switch_api.get_switch(switch_id, user=current_user, **data) ) @@ -653,7 +653,7 @@ def add_switch(): data = _get_request_data() return utils.make_json_response( 200, - switch_api.add_switch(current_user, **data) + switch_api.add_switch(user=current_user, **data) ) @@ -666,7 +666,7 @@ def update_switch(switch_id): data = _get_request_data() return utils.make_json_response( 200, - switch_api.update_switch(current_user, switch_id, **data) + switch_api.update_switch(switch_id, user=current_user, **data) ) @@ -679,7 +679,7 @@ def patch_switch(switch_id): data = _get_request_data() return utils.make_json_response( 200, - switch_api.patch_switch(current_user, switch_id, **data) + switch_api.patch_switch(switch_id, user=current_user, **data) ) @@ -692,7 +692,7 @@ def delete_switch(switch_id): data = _get_request_data() return utils.make_json_response( 200, - switch_api.del_switch(current_user, switch_id, **data) + switch_api.del_switch(switch_id, user=current_user, **data) ) @@ -707,7 +707,7 @@ def list_switch_filters(): return utils.make_json_response( 200, switch_api.list_switch_filters( - current_user, **data + user=current_user, **data ) ) @@ -720,7 +720,8 @@ def show_switch_filters(switch_id): """Get switch filters.""" data = _get_request_args() return utils.make_json_response( - 200, switch_api.get_switch_filters(current_user, switch_id, **data) + 200, + switch_api.get_switch_filters(switch_id, user=current_user, **data) ) @@ -733,7 +734,7 @@ def update_switch_filters(switch_id): data = _get_request_data() return utils.make_json_response( 200, - switch_api.update_switch_filters(current_user, switch_id, **data) + switch_api.update_switch_filters(switch_id, user=current_user, **data) ) @@ -746,7 +747,7 @@ def patch_switch_filters(switch_id): data = _get_request_data() return utils.make_json_response( 200, - switch_api.patch_switch_filter(current_user, switch_id, **data) + switch_api.patch_switch_filter(switch_id, user=current_user, **data) ) @@ -832,7 +833,7 @@ def list_switch_machines(switch_id): return utils.make_json_response( 200, switch_api.list_switch_machines( - current_user, switch_id, **data + switch_id, user=current_user, **data ) ) @@ -853,7 +854,7 @@ def list_switch_machines_hosts(switch_id): return utils.make_json_response( 200, switch_api.list_switch_machines_hosts( - current_user, switch_id, **data + switch_id, user=current_user, **data ) ) @@ -867,7 +868,7 @@ def add_switch_machine(switch_id): data = _get_request_data() return utils.make_json_response( 200, - switch_api.add_switch_machine(current_user, switch_id, **data) + switch_api.add_switch_machine(switch_id, user=current_user, **data) ) @@ -884,7 +885,7 @@ def show_switch_machine(switch_id, machine_id): return utils.make_json_response( 200, switch_api.get_switch_machine( - current_user, switch_id, machine_id, **data + switch_id, machine_id, user=current_user, **data ) ) @@ -902,7 +903,7 @@ def update_switch_machine(switch_id, machine_id): return utils.make_json_response( 200, switch_api.update_switch_machine( - current_user, switch_id, machine_id, **data + switch_id, machine_id, user=current_user, **data ) ) @@ -938,7 +939,7 @@ def delete_switch_machine(switch_id, machine_id): return utils.make_json_response( 200, switch_api.del_switch_machine( - current_user, switch_id, machine_id, **data + switch_id, machine_id, user=current_user, **data ) ) @@ -952,13 +953,13 @@ def take_switch_action(switch_id): data = _get_request_data() poll_switch_machines_func = _wrap_response( functools.partial( - switch_api.poll_switch_machines, current_user, switch_id + switch_api.poll_switch_machines, switch_id, user=current_user, ), 202 ) update_switch_machines_func = _wrap_response( functools.partial( - switch_api.update_switch_machines, current_user, switch_id + switch_api.update_switch_machines, switch_id, user=current_user, ), 200 ) @@ -980,25 +981,25 @@ def take_machine_action(machine_id): data = _get_request_data() tag_func = _wrap_response( functools.partial( - machine_api.update_machine, current_user, machine_id + machine_api.update_machine, machine_id, user=current_user, ), 200 ) poweron_func = _wrap_response( functools.partial( - machine_api.poweron_machine, current_user, machine_id + machine_api.poweron_machine, machine_id, user=current_user, ), 202 ) poweroff_func = _wrap_response( functools.partial( - machine_api.poweroff_machine, current_user, machine_id + machine_api.poweroff_machine, machine_id, user=current_user, ), 202 ) reset_func = _wrap_response( functools.partial( - machine_api.reset_machine, current_user, machine_id + machine_api.reset_machine, machine_id, user=current_user, ), 202 ) @@ -1026,7 +1027,7 @@ def list_switchmachines(): return utils.make_json_response( 200, switch_api.list_switchmachines( - current_user, **data + user=current_user, **data ) ) @@ -1048,7 +1049,7 @@ def list_switchmachines_hosts(): return utils.make_json_response( 200, switch_api.list_switchmachines_hosts( - current_user, **data + user=current_user, **data ) ) @@ -1066,7 +1067,7 @@ def show_switchmachine(switch_machine_id): return utils.make_json_response( 200, switch_api.get_switchmachine( - current_user, switch_machine_id, **data + switch_machine_id, user=current_user, **data ) ) @@ -1084,7 +1085,7 @@ def update_switchmachine(switch_machine_id): return utils.make_json_response( 200, switch_api.update_switchmachine( - current_user, switch_machine_id, **data + switch_machine_id, user=current_user, **data ) ) @@ -1099,7 +1100,7 @@ def patch_switchmachine(switch_machine_id): return utils.make_json_response( 200, switch_api.patch_switchmachine( - current_user, switch_machine_id, **data + switch_machine_id, user=current_user, **data ) ) @@ -1114,7 +1115,7 @@ def delete_switchmachine(switch_machine_id): return utils.make_json_response( 200, switch_api.del_switchmachine( - current_user, switch_machine_id, **data + switch_machine_id, user=current_user, **data ) ) @@ -1131,7 +1132,7 @@ def list_machines(): return utils.make_json_response( 200, machine_api.list_machines( - current_user, **data + user=current_user, **data ) ) @@ -1146,7 +1147,7 @@ def show_machine(machine_id): return utils.make_json_response( 200, machine_api.get_machine( - current_user, machine_id, **data + machine_id, user=current_user, **data ) ) @@ -1161,7 +1162,7 @@ def update_machine(machine_id): return utils.make_json_response( 200, machine_api.update_machine( - current_user, machine_id, **data + machine_id, user=current_user, **data ) ) @@ -1176,7 +1177,7 @@ def patch_machine(machine_id): return utils.make_json_response( 200, machine_api.patch_machine( - current_user, machine_id, **data + machine_id, user=current_user, **data ) ) @@ -1191,7 +1192,7 @@ def delete_machine(machine_id): return utils.make_json_response( 200, machine_api.del_machine( - current_user, machine_id, **data + machine_id, user=current_user, **data ) ) @@ -1206,7 +1207,7 @@ def list_subnets(): return utils.make_json_response( 200, network_api.list_subnets( - current_user, **data + user=current_user, **data ) ) @@ -1221,7 +1222,7 @@ def show_subnet(subnet_id): return utils.make_json_response( 200, network_api.get_subnet( - current_user, subnet_id, **data + subnet_id, user=current_user, **data ) ) @@ -1235,7 +1236,7 @@ def add_subnet(): data = _get_request_data() return utils.make_json_response( 200, - network_api.add_subnet(current_user, **data) + network_api.add_subnet(user=current_user, **data) ) @@ -1249,7 +1250,7 @@ def update_subnet(subnet_id): return utils.make_json_response( 200, network_api.update_subnet( - current_user, subnet_id, **data + subnet_id, user=current_user, **data ) ) @@ -1264,7 +1265,7 @@ def delete_subnet(subnet_id): return utils.make_json_response( 200, network_api.del_subnet( - current_user, subnet_id, **data + subnet_id, user=current_user, **data ) ) @@ -1283,7 +1284,7 @@ def list_adapters(): return utils.make_json_response( 200, adapter_api.list_adapters( - current_user, **data + user=current_user, **data ) ) @@ -1298,7 +1299,7 @@ def show_adapter(adapter_id): return utils.make_json_response( 200, adapter_api.get_adapter( - current_user, adapter_id, **data + adapter_id, user=current_user, **data ) ) @@ -1313,7 +1314,7 @@ def show_adapter_metadata(adapter_id): return utils.make_json_response( 200, metadata_api.get_package_metadata( - current_user, adapter_id, **data + adapter_id, user=current_user, **data ) ) @@ -1328,7 +1329,7 @@ def show_os_metadata(os_id): return utils.make_json_response( 200, metadata_api.get_os_metadata( - current_user, os_id, **data + os_id, user=current_user, **data ) ) @@ -1346,7 +1347,7 @@ def show_adapter_os_metadata(adapter_id, os_id): return utils.make_json_response( 200, metadata_api.get_package_os_metadata( - current_user, adapter_id, os_id, **data + adapter_id, os_id, user=current_user, **data ) ) @@ -1361,7 +1362,7 @@ def list_clusters(): return utils.make_json_response( 200, cluster_api.list_clusters( - current_user, **data + user=current_user, **data ) ) @@ -1376,7 +1377,7 @@ def show_cluster(cluster_id): return utils.make_json_response( 200, cluster_api.get_cluster( - current_user, cluster_id, **data + cluster_id, user=current_user, **data ) ) @@ -1390,7 +1391,7 @@ def add_cluster(): data = _get_request_data() return utils.make_json_response( 200, - cluster_api.add_cluster(current_user, **data) + cluster_api.add_cluster(user=current_user, **data) ) @@ -1404,7 +1405,7 @@ def update_cluster(cluster_id): return utils.make_json_response( 200, cluster_api.update_cluster( - current_user, cluster_id, **data + cluster_id, user=current_user, **data ) ) @@ -1417,7 +1418,7 @@ def delete_cluster(cluster_id): """Delete cluster.""" data = _get_request_data() response = cluster_api.del_cluster( - current_user, cluster_id, **data + cluster_id, user=current_user, **data ) if 'status' in response: return utils.make_json_response( @@ -1439,7 +1440,7 @@ def show_cluster_config(cluster_id): return utils.make_json_response( 200, cluster_api.get_cluster_config( - current_user, cluster_id, **data + cluster_id, user=current_user, **data ) ) @@ -1454,7 +1455,7 @@ def show_cluster_metadata(cluster_id): return utils.make_json_response( 200, cluster_api.get_cluster_metadata( - current_user, cluster_id, **data + cluster_id, user=current_user, **data ) ) @@ -1468,7 +1469,9 @@ def update_cluster_config(cluster_id): data = _get_request_data() return utils.make_json_response( 200, - cluster_api.update_cluster_config(current_user, cluster_id, **data) + cluster_api.update_cluster_config( + cluster_id, user=current_user, **data + ) ) @@ -1481,7 +1484,7 @@ def patch_cluster_config(cluster_id): data = _get_request_data() return utils.make_json_response( 200, - cluster_api.patch_cluster_config(current_user, cluster_id, **data) + cluster_api.patch_cluster_config(cluster_id, user=current_user, **data) ) @@ -1495,7 +1498,7 @@ def delete_cluster_config(cluster_id): return utils.make_json_response( 200, cluster_api.del_cluster_config( - current_user, cluster_id, **data + cluster_id, user=current_user, **data ) ) @@ -1511,19 +1514,19 @@ def take_cluster_action(cluster_id): update_cluster_hosts_func = _wrap_response( functools.partial( - cluster_api.update_cluster_hosts, current_user, cluster_id + cluster_api.update_cluster_hosts, cluster_id, user=current_user, ), 200 ) review_cluster_func = _wrap_response( functools.partial( - cluster_api.review_cluster, current_user, cluster_id + cluster_api.review_cluster, cluster_id, user=current_user, ), 200 ) deploy_cluster_func = _wrap_response( functools.partial( - cluster_api.deploy_cluster, current_user, cluster_id + cluster_api.deploy_cluster, cluster_id, user=current_user, ), 202 ) @@ -1556,7 +1559,7 @@ def get_cluster_state(cluster_id): return utils.make_json_response( 200, cluster_api.get_cluster_state( - current_user, cluster_id, **data + cluster_id, user=current_user, **data ) ) @@ -1638,7 +1641,7 @@ def list_cluster_hosts(cluster_id): return utils.make_json_response( 200, _reformat_host(cluster_api.list_cluster_hosts( - current_user, cluster_id, **data + cluster_id, user=current_user, **data )) ) @@ -1653,7 +1656,7 @@ def list_clusterhosts(): return utils.make_json_response( 200, _reformat_host(cluster_api.list_clusterhosts( - current_user, **data + user=current_user, **data )) ) @@ -1668,7 +1671,7 @@ def show_cluster_host(cluster_id, host_id): return utils.make_json_response( 200, _reformat_host(cluster_api.get_cluster_host( - current_user, cluster_id, host_id, **data + cluster_id, host_id, user=current_user, **data )) ) @@ -1683,7 +1686,7 @@ def show_clusterhost(clusterhost_id): return utils.make_json_response( 200, _reformat_host(cluster_api.get_clusterhost( - current_user, clusterhost_id, **data + clusterhost_id, user=current_user, **data )) ) @@ -1697,7 +1700,7 @@ def add_cluster_host(cluster_id): data = _get_request_data() return utils.make_json_response( 200, - cluster_api.add_cluster_host(current_user, cluster_id, **data) + cluster_api.add_cluster_host(cluster_id, user=current_user, **data) ) @@ -1714,7 +1717,7 @@ def update_cluster_host(cluster_id, host_id): return utils.make_json_response( 200, cluster_api.update_cluster_host( - current_user, cluster_id, host_id, **data + cluster_id, host_id, user=current_user, **data ) ) @@ -1732,7 +1735,7 @@ def update_clusterhost(clusterhost_id): return utils.make_json_response( 200, cluster_api.update_clusterhost( - current_user, clusterhost_id, **data + clusterhost_id, user=current_user, **data ) ) @@ -1750,7 +1753,7 @@ def patch_cluster_host(cluster_id, host_id): return utils.make_json_response( 200, cluster_api.patch_cluster_host( - current_user, cluster_id, host_id, **data + cluster_id, host_id, user=current_user, **data ) ) @@ -1768,7 +1771,7 @@ def patch_clusterhost(clusterhost_id): return utils.make_json_response( 200, cluster_api.patch_clusterhost( - current_user, clusterhost_id, **data + clusterhost_id, user=current_user, **data ) ) @@ -1784,7 +1787,7 @@ def delete_cluster_host(cluster_id, host_id): """Delete cluster host.""" data = _get_request_data() response = cluster_api.del_cluster_host( - current_user, cluster_id, host_id, **data + cluster_id, host_id, user=current_user, **data ) if 'status' in response: return utils.make_json_response( @@ -1807,7 +1810,7 @@ def delete_clusterhost(clusterhost_id): """Delete cluster host.""" data = _get_request_data() response = cluster_api.del_clusterhost( - current_user, clusterhost_id, **data + clusterhost_id, user=current_user, **data ) if 'status' in response: return utils.make_json_response( @@ -1832,7 +1835,7 @@ def show_cluster_host_config(cluster_id, host_id): return utils.make_json_response( 200, cluster_api.get_cluster_host_config( - current_user, cluster_id, host_id, **data + cluster_id, host_id, user=current_user, **data ) ) @@ -1847,7 +1850,7 @@ def show_clusterhost_config(clusterhost_id): return utils.make_json_response( 200, cluster_api.get_clusterhost_config( - current_user, clusterhost_id, **data + clusterhost_id, user=current_user, **data ) ) @@ -1865,7 +1868,7 @@ def update_cluster_host_config(cluster_id, host_id): return utils.make_json_response( 200, cluster_api.update_cluster_host_config( - current_user, cluster_id, host_id, **data + cluster_id, host_id, user=current_user, **data ) ) @@ -1880,7 +1883,7 @@ def update_clusterhost_config(clusterhost_id): return utils.make_json_response( 200, cluster_api.update_clusterhost_config( - current_user, clusterhost_id, **data + clusterhost_id, user=current_user, **data ) ) @@ -1898,7 +1901,7 @@ def patch_cluster_host_config(cluster_id, host_id): return utils.make_json_response( 200, cluster_api.patch_cluster_host_config( - current_user, cluster_id, host_id, **data + cluster_id, host_id, user=current_user, **data ) ) @@ -1913,7 +1916,7 @@ def patch_clusterhost_config(clusterhost_id): return utils.make_json_response( 200, cluster_api.patch_clusterhost_config( - current_user, clusterhost_id, **data + clusterhost_id, user=current_user, **data ) ) @@ -1931,7 +1934,7 @@ def delete_cluster_host_config(cluster_id, host_id): return utils.make_json_response( 200, cluster_api.del_clusterhost_config( - current_user, cluster_id, host_id, **data + cluster_id, host_id, user=current_user, **data ) ) @@ -1946,7 +1949,7 @@ def delete_clusterhost_config(clusterhost_id): return utils.make_json_response( 200, cluster_api.del_clusterhost_config( - current_user, clusterhost_id, **data + clusterhost_id, user=current_user, **data ) ) @@ -1964,7 +1967,7 @@ def show_cluster_host_state(cluster_id, host_id): return utils.make_json_response( 200, cluster_api.get_cluster_host_state( - current_user, cluster_id, host_id, **data + cluster_id, host_id, user=current_user, **data ) ) @@ -1979,7 +1982,7 @@ def show_clusterhost_state(clusterhost_id): return utils.make_json_response( 200, cluster_api.get_clusterhost_state( - current_user, clusterhost_id, **data + clusterhost_id, user=current_user, **data ) ) @@ -1997,7 +2000,7 @@ def update_cluster_host_state(cluster_id, host_id): return utils.make_json_response( 200, cluster_api.update_clusterhost_state( - current_user, cluster_id, host_id, **data + cluster_id, host_id, user=current_user, **data ) ) @@ -2012,7 +2015,7 @@ def update_clusterhost_state(clusterhost_id): return utils.make_json_response( 200, cluster_api.update_clusterhost_state( - current_user, clusterhost_id, **data + clusterhost_id, user=current_user, **data ) ) @@ -2027,7 +2030,7 @@ def list_hosts(): return utils.make_json_response( 200, _reformat_host(host_api.list_hosts( - current_user, **data + user=current_user, **data )) ) @@ -2042,7 +2045,7 @@ def show_host(host_id): return utils.make_json_response( 200, _reformat_host(host_api.get_host( - current_user, host_id, **data + host_id, user=current_user, **data )) ) @@ -2061,7 +2064,7 @@ def list_machines_or_hosts(): return utils.make_json_response( 200, _reformat_host(host_api.list_machines_or_hosts( - current_user, **data + user=current_user, **data )) ) @@ -2076,7 +2079,7 @@ def show_machine_or_host(host_id): return utils.make_json_response( 200, _reformat_host(host_api.get_machine_or_host( - current_user, host_id, **data + host_id, user=current_user, **data )) ) @@ -2091,7 +2094,7 @@ def update_host(host_id): return utils.make_json_response( 200, host_api.update_host( - current_user, host_id, **data + host_id, user=current_user, **data ) ) @@ -2106,7 +2109,7 @@ def update_hosts(): return utils.make_json_response( 200, host_api.update_hosts( - current_user, data + data, user=current_user, ) ) @@ -2119,7 +2122,7 @@ def delete_host(host_id): """Delete host.""" data = _get_request_data() response = host_api.del_host( - current_user, host_id, **data + host_id, user=current_user, **data ) if 'status' in response: return utils.make_json_response( @@ -2141,7 +2144,7 @@ def get_host_clusters(host_id): return utils.make_json_response( 200, host_api.get_host_clusters( - current_user, host_id, **data + host_id, user=current_user, **data ) ) @@ -2156,7 +2159,7 @@ def show_host_config(host_id): return utils.make_json_response( 200, host_api.get_host_config( - current_user, host_id, **data + host_id, user=current_user, **data ) ) @@ -2170,7 +2173,7 @@ def update_host_config(host_id): data = _get_request_data() return utils.make_json_response( 200, - host_api.update_host_config(current_user, host_id, **data) + host_api.update_host_config(host_id, user=current_user, **data) ) @@ -2183,7 +2186,7 @@ def patch_host_config(host_id): data = _get_request_data() return utils.make_json_response( 200, - host_api.patch_host_config(current_user, host_id, **data) + host_api.patch_host_config(host_id, user=current_user, **data) ) @@ -2197,7 +2200,7 @@ def delete_host_config(host_id): return utils.make_json_response( 200, host_api.del_host_config( - current_user, host_id, **data + host_id, user=current_user, **data ) ) @@ -2213,7 +2216,7 @@ def list_host_networks(host_id): 200, _reformat_host_networks( host_api.list_host_networks( - current_user, host_id, **data + host_id, user=current_user, **data ) ) ) @@ -2232,7 +2235,7 @@ def list_hostnetworks(): return utils.make_json_response( 200, _reformat_host_networks( - host_api.list_hostnetworks(current_user, **data) + host_api.list_hostnetworks(user=current_user, **data) ) ) @@ -2250,7 +2253,7 @@ def show_host_network(host_id, host_network_id): return utils.make_json_response( 200, host_api.get_host_network( - current_user, host_id, host_network_id, **data + host_id, host_network_id, user=current_user, **data ) ) @@ -2265,7 +2268,7 @@ def show_hostnetwork(host_network_id): return utils.make_json_response( 200, host_api.get_hostnetwork( - current_user, host_network_id, **data + host_network_id, user=current_user, **data ) ) @@ -2278,7 +2281,7 @@ def add_host_network(host_id): """add host network.""" data = _get_request_data() return utils.make_json_response( - 200, host_api.add_host_network(current_user, host_id, **data) + 200, host_api.add_host_network(host_id, user=current_user, **data) ) @@ -2291,7 +2294,7 @@ def update_host_networks(): data = _get_request_data_as_list() return utils.make_json_response( 200, host_api.add_host_networks( - current_user, data) + data, user=current_user,) ) @@ -2308,7 +2311,7 @@ def update_host_network(host_id, host_network_id): return utils.make_json_response( 200, host_api.update_host_network( - current_user, host_id, host_network_id, **data + host_id, host_network_id, user=current_user, **data ) ) @@ -2323,7 +2326,7 @@ def update_hostnetwork(host_network_id): return utils.make_json_response( 200, host_api.update_hostnetwork( - current_user, host_network_id, **data + host_network_id, user=current_user, **data ) ) @@ -2341,7 +2344,7 @@ def delete_host_network(host_id, host_network_id): return utils.make_json_response( 200, host_api.del_host_network( - current_user, host_id, host_network_id, **data + host_id, host_network_id, user=current_user, **data ) ) @@ -2356,7 +2359,7 @@ def delete_hostnetwork(host_network_id): return utils.make_json_response( 200, host_api.del_hostnetwork( - current_user, host_network_id, **data + host_network_id, user=current_user, **data ) ) @@ -2371,7 +2374,7 @@ def show_host_state(host_id): return utils.make_json_response( 200, host_api.get_host_state( - current_user, host_id, **data + host_id, user=current_user, **data ) ) @@ -2386,7 +2389,7 @@ def update_host_state(host_id): return utils.make_json_response( 200, host_api.update_host_state( - current_user, host_id, **data + host_id, user=current_user, **data ) ) @@ -2427,19 +2430,19 @@ def take_host_action(host_id): data = _get_request_data() poweron_func = _wrap_response( functools.partial( - host_api.poweron_host, current_user, host_id + host_api.poweron_host, host_id, user=current_user, ), 202 ) poweroff_func = _wrap_response( functools.partial( - host_api.poweroff_host, current_user, host_id + host_api.poweroff_host, host_id, user=current_user, ), 202 ) reset_func = _wrap_response( functools.partial( - host_api.reset_host, current_user, host_id + host_api.reset_host, host_id, user=current_user, ) ) return _group_data_action( diff --git a/compass/db/api/adapter_holder.py b/compass/db/api/adapter_holder.py index f8b44bc4..1125d0d1 100644 --- a/compass/db/api/adapter_holder.py +++ b/compass/db/api/adapter_holder.py @@ -95,7 +95,7 @@ def _filter_adapters(adapter_config, filter_name, filter_value): roles=RESP_ROLES_FIELDS, flavors=RESP_FLAVORS_FIELDS ) -def list_adapters(lister, session=None, **filters): +def list_adapters(user=None, session=None, **filters): """list adapters.""" if not ADAPTER_MAPPING: load_adapters_internal(session) @@ -125,6 +125,6 @@ def get_adapter_internal(session, adapter_id): roles=RESP_ROLES_FIELDS, flavors=RESP_FLAVORS_FIELDS ) -def get_adapter(getter, adapter_id, session=None, **kwargs): +def get_adapter(adapter_id, user=None, session=None, **kwargs): """get adapter.""" return get_adapter_internal(session, adapter_id) diff --git a/compass/db/api/cluster.py b/compass/db/api/cluster.py index 8b8fff33..1690788b 100644 --- a/compass/db/api/cluster.py +++ b/compass/db/api/cluster.py @@ -154,7 +154,7 @@ UPDATED_CLUSTERHOST_LOG_FIELDS = [ permission.PERMISSION_LIST_CLUSTERS ) @utils.wrap_to_dict(RESP_FIELDS) -def list_clusters(lister, session=None, **filters): +def list_clusters(user=None, session=None, **filters): """List clusters.""" return utils.list_db_objects( session, models.Cluster, **filters @@ -168,8 +168,8 @@ def list_clusters(lister, session=None, **filters): ) @utils.wrap_to_dict(RESP_FIELDS) def get_cluster( - getter, cluster_id, - exception_when_missing=True, session=None, **kwargs + cluster_id, exception_when_missing=True, + user=None, session=None, **kwargs ): """Get cluster info.""" return utils.get_db_object( @@ -243,14 +243,13 @@ def is_cluster_editable( ) @utils.wrap_to_dict(RESP_FIELDS) def add_cluster( - creator, exception_when_existing=True, - name=None, session=None, **kwargs + name=None, user=None, session=None, **kwargs ): """Create a cluster.""" return utils.add_db_object( session, models.Cluster, exception_when_existing, - name, creator_id=creator.id, + name, creator_id=user.id, **kwargs ) @@ -265,13 +264,13 @@ def add_cluster( permission.PERMISSION_ADD_CLUSTER ) @utils.wrap_to_dict(RESP_FIELDS) -def update_cluster(updater, cluster_id, session=None, **kwargs): +def update_cluster(cluster_id, user=None, session=None, **kwargs): """Update a cluster.""" cluster = utils.get_db_object( session, models.Cluster, id=cluster_id ) is_cluster_editable( - session, cluster, updater, + session, cluster, user, reinstall_distributed_system_set=( kwargs.get('reinstall_distributed_system', False) ) @@ -301,9 +300,8 @@ def update_cluster(updater, cluster_id, session=None, **kwargs): hosts=RESP_CLUSTERHOST_FIELDS ) def del_cluster( - deleter, cluster_id, - force=False, from_database_only=False, - delete_underlying_host=False, session=None, **kwargs + cluster_id, force=False, from_database_only=False, + delete_underlying_host=False, user=None, session=None, **kwargs ): """Delete a cluster.""" cluster = utils.get_db_object( @@ -325,7 +323,7 @@ def del_cluster( cluster.state.state = 'ERROR' is_cluster_editable( - session, cluster, deleter, + session, cluster, user, reinstall_distributed_system_set=True ) @@ -333,7 +331,7 @@ def del_cluster( from compass.db.api import host as host_api host = clusterhost.host host_api.is_host_editable( - session, host, deleter, reinstall_os_set=True + session, host, user, reinstall_os_set=True ) if host.state.state == 'UNINITIALIZED' or from_database_only: utils.del_db_object( @@ -353,7 +351,7 @@ def del_cluster( celery_client.celery.send_task( 'compass.tasks.delete_cluster', ( - deleter.email, cluster_id, + user.email, cluster_id, [clusterhost.host_id for clusterhost in clusterhosts], delete_underlying_host ) @@ -371,7 +369,7 @@ def del_cluster( permission.PERMISSION_LIST_CLUSTER_CONFIG ) @utils.wrap_to_dict(RESP_CONFIG_FIELDS) -def get_cluster_config(getter, cluster_id, session=None, **kwargs): +def get_cluster_config(cluster_id, user=None, session=None, **kwargs): """Get cluster config.""" return utils.get_db_object( session, models.Cluster, id=cluster_id @@ -384,7 +382,7 @@ def get_cluster_config(getter, cluster_id, session=None, **kwargs): permission.PERMISSION_LIST_CLUSTER_CONFIG ) @utils.wrap_to_dict(RESP_DEPLOYED_CONFIG_FIELDS) -def get_cluster_deployed_config(getter, cluster_id, session=None, **kwargs): +def get_cluster_deployed_config(cluster_id, user=None, session=None, **kwargs): """Get cluster deployed config.""" return utils.get_db_object( session, models.Cluster, id=cluster_id @@ -397,7 +395,7 @@ def get_cluster_deployed_config(getter, cluster_id, session=None, **kwargs): permission.PERMISSION_LIST_METADATAS ) @utils.wrap_to_dict(RESP_METADATA_FIELDS) -def get_cluster_metadata(getter, cluster_id, session=None, **kwargs): +def get_cluster_metadata(cluster_id, user=None, session=None, **kwargs): """Get cluster metadata.""" cluster = utils.get_db_object( session, models.Cluster, id=cluster_id @@ -419,9 +417,9 @@ def get_cluster_metadata(getter, cluster_id, session=None, **kwargs): @utils.wrap_to_dict(RESP_CONFIG_FIELDS) -def _update_cluster_config(session, updater, cluster, **kwargs): +def _update_cluster_config(session, user, cluster, **kwargs): """Update a cluster config.""" - is_cluster_editable(session, cluster, updater) + is_cluster_editable(session, cluster, user) return utils.update_db_object( session, cluster, **kwargs ) @@ -441,13 +439,13 @@ def _update_cluster_config(session, updater, cluster, **kwargs): ) @utils.wrap_to_dict(RESP_DEPLOYED_CONFIG_FIELDS) def update_cluster_deployed_config( - updater, cluster_id, session=None, **kwargs + cluster_id, user=None, session=None, **kwargs ): """Update cluster deployed config.""" cluster = utils.get_db_object( session, models.Cluster, id=cluster_id ) - is_cluster_editable(session, cluster, updater) + is_cluster_editable(session, cluster, user) is_cluster_validated(session, cluster) return utils.update_db_object( session, cluster, **kwargs @@ -466,7 +464,7 @@ def update_cluster_deployed_config( @user_api.check_user_permission_in_session( permission.PERMISSION_ADD_CLUSTER_CONFIG ) -def update_cluster_config(updater, cluster_id, session=None, **kwargs): +def update_cluster_config(cluster_id, user=None, session=None, **kwargs): """Update cluster config.""" cluster = utils.get_db_object( session, models.Cluster, id=cluster_id @@ -490,7 +488,7 @@ def update_cluster_config(updater, cluster_id, session=None, **kwargs): cluster, **in_kwargs ): return _update_cluster_config( - session, updater, cluster, **in_kwargs + session, user, cluster, **in_kwargs ) return update_config_internal( @@ -510,7 +508,7 @@ def update_cluster_config(updater, cluster_id, session=None, **kwargs): @user_api.check_user_permission_in_session( permission.PERMISSION_ADD_CLUSTER_CONFIG ) -def patch_cluster_config(updater, cluster_id, session=None, **kwargs): +def patch_cluster_config(cluster_id, user=None, session=None, **kwargs): """patch cluster config.""" cluster = utils.get_db_object( session, models.Cluster, id=cluster_id @@ -532,7 +530,7 @@ def patch_cluster_config(updater, cluster_id, session=None, **kwargs): ) def update_config_internal(cluster, **in_kwargs): return _update_cluster_config( - session, updater, cluster, **in_kwargs + session, user, cluster, **in_kwargs ) return update_config_internal( @@ -546,12 +544,12 @@ def patch_cluster_config(updater, cluster_id, session=None, **kwargs): permission.PERMISSION_DEL_CLUSTER_CONFIG ) @utils.wrap_to_dict(RESP_CONFIG_FIELDS) -def del_cluster_config(deleter, cluster_id, session=None): +def del_cluster_config(cluster_id, user=None, session=None): """Delete a cluster config.""" cluster = utils.get_db_object( session, models.Cluster, id=cluster_id ) - is_cluster_editable(session, cluster, deleter) + is_cluster_editable(session, cluster, user) return utils.update_db_object( session, cluster, os_config={}, package_config={}, config_validated=False @@ -672,7 +670,7 @@ def _set_clusterhosts(session, cluster, machines): permission.PERMISSION_LIST_CLUSTERHOSTS ) @utils.wrap_to_dict(RESP_CLUSTERHOST_FIELDS) -def list_cluster_hosts(lister, cluster_id, session=None, **filters): +def list_cluster_hosts(cluster_id, user=None, session=None, **filters): """Get cluster host info.""" return utils.list_db_objects( session, models.ClusterHost, cluster_id=cluster_id, @@ -686,7 +684,7 @@ def list_cluster_hosts(lister, cluster_id, session=None, **filters): permission.PERMISSION_LIST_CLUSTERHOSTS ) @utils.wrap_to_dict(RESP_CLUSTERHOST_FIELDS) -def list_clusterhosts(lister, session=None, **filters): +def list_clusterhosts(user=None, session=None, **filters): """Get cluster host info.""" return utils.list_db_objects( session, models.ClusterHost, **filters @@ -700,8 +698,8 @@ def list_clusterhosts(lister, session=None, **filters): ) @utils.wrap_to_dict(RESP_CLUSTERHOST_FIELDS) def get_cluster_host( - getter, cluster_id, host_id, - exception_when_missing=True, session=None, **kwargs + cluster_id, host_id, exception_when_missing=True, + user=None, session=None, **kwargs ): """Get clusterhost info.""" return utils.get_db_object( @@ -718,8 +716,8 @@ def get_cluster_host( ) @utils.wrap_to_dict(RESP_CLUSTERHOST_FIELDS) def get_clusterhost( - getter, clusterhost_id, - exception_when_missing=True, session=None, **kwargs + clusterhost_id, exception_when_missing=True, + user=None, session=None, **kwargs ): """Get clusterhost info.""" return utils.get_db_object( @@ -735,14 +733,14 @@ def get_clusterhost( ) @utils.wrap_to_dict(RESP_CLUSTERHOST_FIELDS) def add_cluster_host( - creator, cluster_id, - exception_when_existing=True, session=None, **kwargs + cluster_id, exception_when_existing=True, + user=None, session=None, **kwargs ): """Add cluster host.""" cluster = utils.get_db_object( session, models.Cluster, id=cluster_id ) - is_cluster_editable(session, cluster, creator) + is_cluster_editable(session, cluster, user) return add_clusterhost_internal( session, cluster, exception_when_existing, **kwargs @@ -750,7 +748,7 @@ def add_cluster_host( @utils.wrap_to_dict(RESP_CLUSTERHOST_FIELDS) -def _update_clusterhost(session, updater, clusterhost, **kwargs): +def _update_clusterhost(session, user, clusterhost, **kwargs): clusterhost_dict = {} host_dict = {} for key, value in kwargs.items(): @@ -824,7 +822,7 @@ def _update_clusterhost(session, updater, clusterhost, **kwargs): session, clusterhost, **in_kwargs ) - is_cluster_editable(session, clusterhost.cluster, updater) + is_cluster_editable(session, clusterhost.cluster, user) return update_internal( clusterhost, **kwargs ) @@ -839,14 +837,14 @@ def _update_clusterhost(session, updater, clusterhost, **kwargs): permission.PERMISSION_UPDATE_CLUSTER_HOSTS ) def update_cluster_host( - updater, cluster_id, host_id, + cluster_id, host_id, user=None, session=None, **kwargs ): """Update cluster host.""" clusterhost = utils.get_db_object( session, models.ClusterHost, cluster_id=cluster_id, host_id=host_id ) - return _update_clusterhost(session, updater, clusterhost, **kwargs) + return _update_clusterhost(session, user, clusterhost, **kwargs) @utils.supported_filters( @@ -858,14 +856,14 @@ def update_cluster_host( permission.PERMISSION_UPDATE_CLUSTER_HOSTS ) def update_clusterhost( - updater, clusterhost_id, + clusterhost_id, user=None, session=None, **kwargs ): """Update cluster host.""" clusterhost = utils.get_db_object( session, models.ClusterHost, clusterhost_id=clusterhost_id ) - return _update_clusterhost(session, updater, clusterhost, **kwargs) + return _update_clusterhost(session, user, clusterhost, **kwargs) @utils.replace_filters( @@ -880,14 +878,14 @@ def update_clusterhost( permission.PERMISSION_UPDATE_CLUSTER_HOSTS ) def patch_cluster_host( - updater, cluster_id, host_id, session=None, - **kwargs + cluster_id, host_id, user=None, + session=None, **kwargs ): """Update cluster host.""" clusterhost = utils.get_db_object( session, models.ClusterHost, cluster_id=cluster_id, host_id=host_id ) - return _update_clusterhost(session, updater, clusterhost, **kwargs) + return _update_clusterhost(session, user, clusterhost, **kwargs) @utils.replace_filters( @@ -902,14 +900,14 @@ def patch_cluster_host( permission.PERMISSION_UPDATE_CLUSTER_HOSTS ) def patch_clusterhost( - updater, clusterhost_id, session=None, + clusterhost_id, user=None, session=None, **kwargs ): """Update cluster host.""" clusterhost = utils.get_db_object( session, models.ClusterHost, clusterhost_id=clusterhost_id ) - return _update_clusterhost(session, updater, clusterhost, **kwargs) + return _update_clusterhost(session, user, clusterhost, **kwargs) @utils.supported_filters([]) @@ -922,9 +920,9 @@ def patch_clusterhost( host=RESP_CLUSTERHOST_FIELDS ) def del_cluster_host( - deleter, cluster_id, host_id, + cluster_id, host_id, force=False, from_database_only=False, - delete_underlying_host=False, + delete_underlying_host=False, user=None, session=None, **kwargs ): """Delete cluster host.""" @@ -936,7 +934,7 @@ def del_cluster_host( clusterhost.state.state = 'ERROR' if not force: is_cluster_editable( - session, clusterhost.cluster, deleter, + session, clusterhost.cluster, user, reinstall_distributed_system_set=True ) else: @@ -949,7 +947,7 @@ def del_cluster_host( host.state.state = 'ERROR' import compass.db.api.host as host_api host_api.is_host_editable( - session, host, deleter, + session, host, user, reinstall_os_set=True ) if host.state.state == 'UNINITIALIZED' or from_database_only: @@ -970,7 +968,7 @@ def del_cluster_host( celery_client.celery.send_task( 'compass.tasks.delete_cluster_host', ( - deleter.email, cluster_id, host_id, + user.email, cluster_id, host_id, delete_underlying_host ) ) @@ -990,9 +988,9 @@ def del_cluster_host( host=RESP_CLUSTERHOST_FIELDS ) def del_clusterhost( - deleter, clusterhost_id, + clusterhost_id, force=False, from_database_only=False, - delete_underlying_host=False, + delete_underlying_host=False, user=None, session=None, **kwargs ): """Delete cluster host.""" @@ -1004,7 +1002,7 @@ def del_clusterhost( clusterhost.state.state = 'ERROR' if not force: is_cluster_editable( - session, clusterhost.cluster, deleter, + session, clusterhost.cluster, user, reinstall_distributed_system_set=True ) if delete_underlying_host: @@ -1013,7 +1011,7 @@ def del_clusterhost( host.state.state = 'ERROR' import compass.db.api.host as host_api host_api.is_host_editable( - session, host, deleter, + session, host, user, reinstall_os_set=True ) if host.state.state == 'UNINITIALIZED' or from_database_only: @@ -1034,7 +1032,7 @@ def del_clusterhost( celery_client.celery.send_task( 'compass.tasks.delete_cluster_host', ( - deleter.email, clusterhost.cluster_id, + user.email, clusterhost.cluster_id, clusterhost.host_id, delete_underlying_host ) @@ -1052,8 +1050,8 @@ def del_clusterhost( ) @utils.wrap_to_dict(RESP_CLUSTERHOST_CONFIG_FIELDS) def get_cluster_host_config( - getter, cluster_id, - host_id, session=None, **kwargs + cluster_id, host_id, user=None, + session=None, **kwargs ): """Get clusterhost config.""" return utils.get_db_object( @@ -1069,7 +1067,7 @@ def get_cluster_host_config( ) @utils.wrap_to_dict(RESP_CLUSTERHOST_DEPLOYED_CONFIG_FIELDS) def get_cluster_host_deployed_config( - getter, cluster_id, host_id, session=None, **kwargs + cluster_id, host_id, user=None, session=None, **kwargs ): """Get clusterhost deployed config.""" return utils.get_db_object( @@ -1084,7 +1082,7 @@ def get_cluster_host_deployed_config( permission.PERMISSION_LIST_CLUSTERHOST_CONFIG ) @utils.wrap_to_dict(RESP_CLUSTERHOST_CONFIG_FIELDS) -def get_clusterhost_config(getter, clusterhost_id, session=None, **kwargs): +def get_clusterhost_config(clusterhost_id, user=None, session=None, **kwargs): """Get clusterhost config.""" return utils.get_db_object( session, models.ClusterHost, clusterhost_id=clusterhost_id @@ -1098,7 +1096,7 @@ def get_clusterhost_config(getter, clusterhost_id, session=None, **kwargs): ) @utils.wrap_to_dict(RESP_CLUSTERHOST_DEPLOYED_CONFIG_FIELDS) def get_clusterhost_deployed_config( - getter, clusterhost_id, + clusterhost_id, user=None, session=None, **kwargs ): """Get clusterhost deployed config.""" @@ -1108,11 +1106,11 @@ def get_clusterhost_deployed_config( @utils.wrap_to_dict(RESP_CLUSTERHOST_CONFIG_FIELDS) -def _update_clusterhost_config(session, updater, clusterhost, **kwargs): +def _update_clusterhost_config(session, user, clusterhost, **kwargs): from compass.db.api import host as host_api ignore_keys = [] if not host_api.is_host_editable( - session, clusterhost.host, updater, + session, clusterhost.host, user, exception_when_not_editable=False ): ignore_keys.append('put_os_config') @@ -1124,7 +1122,7 @@ def _update_clusterhost_config(session, updater, clusterhost, **kwargs): def package_config_validates(package_config): cluster = clusterhost.cluster - is_cluster_editable(session, cluster, updater) + is_cluster_editable(session, cluster, user) metadata_api.validate_package_config( session, package_config, cluster.adapter_id ) @@ -1149,12 +1147,12 @@ def _update_clusterhost_config(session, updater, clusterhost, **kwargs): @utils.wrap_to_dict(RESP_CLUSTERHOST_DEPLOYED_CONFIG_FIELDS) def _update_clusterhost_deployed_config( - session, updater, clusterhost, **kwargs + session, user, clusterhost, **kwargs ): from compass.db.api import host as host_api ignore_keys = [] if not host_api.is_host_editable( - session, clusterhost.host, updater, + session, clusterhost.host, user, exception_when_not_editable=False ): ignore_keys.append('deployed_os_config') @@ -1165,7 +1163,7 @@ def _update_clusterhost_deployed_config( def package_config_validates(package_config): cluster = clusterhost.cluster - is_cluster_editable(session, cluster, updater) + is_cluster_editable(session, cluster, user) is_clusterhost_validated(session, clusterhost) @utils.supported_filters( @@ -1195,7 +1193,7 @@ def _update_clusterhost_deployed_config( permission.PERMISSION_ADD_CLUSTERHOST_CONFIG ) def update_cluster_host_config( - updater, cluster_id, host_id, session=None, **kwargs + cluster_id, host_id, user=None, session=None, **kwargs ): """Update clusterhost config.""" clusterhost = utils.get_db_object( @@ -1203,7 +1201,7 @@ def update_cluster_host_config( cluster_id=cluster_id, host_id=host_id ) return _update_clusterhost_config( - session, updater, clusterhost, **kwargs + session, user, clusterhost, **kwargs ) @@ -1216,7 +1214,7 @@ def update_cluster_host_config( permission.PERMISSION_ADD_CLUSTERHOST_CONFIG ) def update_cluster_host_deployed_config( - updater, cluster_id, host_id, session=None, **kwargs + cluster_id, host_id, user=None, session=None, **kwargs ): """Update clusterhost deployed config.""" clusterhost = utils.get_db_object( @@ -1224,7 +1222,7 @@ def update_cluster_host_deployed_config( cluster_id=cluster_id, host_id=host_id ) return _update_clusterhost_deployed_config( - session, updater, clusterhost, **kwargs + session, user, clusterhost, **kwargs ) @@ -1237,14 +1235,14 @@ def update_cluster_host_deployed_config( permission.PERMISSION_ADD_CLUSTERHOST_CONFIG ) def update_clusterhost_config( - updater, clusterhost_id, session=None, **kwargs + clusterhost_id, user=None, session=None, **kwargs ): """Update clusterhost config.""" clusterhost = utils.get_db_object( session, models.ClusterHost, clusterhost_id=clusterhost_id ) return _update_clusterhost_config( - session, updater, clusterhost, **kwargs + session, user, clusterhost, **kwargs ) @@ -1257,23 +1255,23 @@ def update_clusterhost_config( permission.PERMISSION_ADD_CLUSTERHOST_CONFIG ) def update_clusterhost_deployed_config( - updater, clusterhost_id, session=None, **kwargs + clusterhost_id, user=None, session=None, **kwargs ): """Update clusterhost deployed config.""" clusterhost = utils.get_db_object( session, models.ClusterHost, clusterhost_id=clusterhost_id ) return _update_clusterhost_deployed_config( - session, updater, clusterhost, **kwargs + session, user, clusterhost, **kwargs ) @utils.wrap_to_dict(RESP_CLUSTERHOST_CONFIG_FIELDS) -def _patch_clusterhost_config(session, updater, clusterhost, **kwargs): +def _patch_clusterhost_config(session, user, clusterhost, **kwargs): from compass.db.api import host as host_api ignore_keys = [] if not host_api.is_host_editable( - session, clusterhost.host, updater, + session, clusterhost.host, user, exception_when_not_editable=False ): ignore_keys.append('patched_os_config') @@ -1284,7 +1282,7 @@ def _patch_clusterhost_config(session, updater, clusterhost, **kwargs): def package_config_validates(package_config): cluster = clusterhost.cluster - is_cluster_editable(session, cluster, updater) + is_cluster_editable(session, cluster, user) metadata_api.validate_package_config( session, package_config, cluster.adapter_id ) @@ -1316,7 +1314,7 @@ def _patch_clusterhost_config(session, updater, clusterhost, **kwargs): permission.PERMISSION_ADD_CLUSTERHOST_CONFIG ) def patch_cluster_host_config( - updater, cluster_id, host_id, session=None, **kwargs + cluster_id, host_id, user=None, session=None, **kwargs ): """patch clusterhost config.""" clusterhost = utils.get_db_object( @@ -1324,7 +1322,7 @@ def patch_cluster_host_config( cluster_id=cluster_id, host_id=host_id ) return _patch_clusterhost_config( - session, updater, clusterhost, **kwargs + session, user, clusterhost, **kwargs ) @@ -1337,31 +1335,31 @@ def patch_cluster_host_config( permission.PERMISSION_ADD_CLUSTERHOST_CONFIG ) def patch_clusterhost_config( - updater, clusterhost_id, session=None, **kwargs + clusterhost_id, user=None, session=None, **kwargs ): """patch clusterhost config.""" clusterhost = utils.get_db_object( session, models.ClusterHost, clusterhost_id=clusterhost_id ) return _patch_clusterhost_config( - session, updater, clusterhost, **kwargs + session, user, clusterhost, **kwargs ) @utils.wrap_to_dict(RESP_CLUSTERHOST_CONFIG_FIELDS) def _delete_clusterhost_config( - session, deleter, clusterhost + session, user, clusterhost ): from compass.db.api import host as host_api ignore_keys = [] if not host_api.is_host_editable( - session, clusterhost.host, deleter, + session, clusterhost.host, user, exception_when_not_editable=False ): ignore_keys.append('os_config') def package_config_validates(package_config): - is_cluster_editable(session, clusterhost.cluster, deleter) + is_cluster_editable(session, clusterhost.cluster, user) @utils.supported_filters( optional_support_keys=['os_config', 'package_config'], @@ -1388,7 +1386,7 @@ def _delete_clusterhost_config( permission.PERMISSION_DEL_CLUSTERHOST_CONFIG ) def delete_cluster_host_config( - deleter, cluster_id, host_id, session=None + cluster_id, host_id, user=None, session=None ): """Delete a clusterhost config.""" clusterhost = utils.get_db_object( @@ -1396,7 +1394,7 @@ def delete_cluster_host_config( cluster_id=cluster_id, host_id=host_id ) return _delete_clusterhost_config( - session, deleter, clusterhost + session, user, clusterhost ) @@ -1406,13 +1404,13 @@ def delete_cluster_host_config( permission.PERMISSION_DEL_CLUSTERHOST_CONFIG ) @utils.wrap_to_dict(RESP_CLUSTERHOST_CONFIG_FIELDS) -def delete_clusterhost_config(deleter, clusterhost_id, session=None): +def delete_clusterhost_config(clusterhost_id, user=None, session=None): """Delet a clusterhost config.""" clusterhost = utils.get_db_object( session, models.ClusterHost, clusterhost_id=clusterhost_id ) return _delete_clusterhost_config( - session, deleter, clusterhost + session, user, clusterhost ) @@ -1428,14 +1426,14 @@ def delete_clusterhost_config(deleter, clusterhost_id, session=None): hosts=RESP_CLUSTERHOST_FIELDS ) def update_cluster_hosts( - updater, cluster_id, add_hosts={}, set_hosts=None, - remove_hosts={}, session=None + cluster_id, add_hosts={}, set_hosts=None, + remove_hosts={}, user=None, session=None ): """Update cluster hosts.""" cluster = utils.get_db_object( session, models.Cluster, id=cluster_id ) - is_cluster_editable(session, cluster, updater) + is_cluster_editable(session, cluster, user) if remove_hosts: _remove_clusterhosts(session, cluster, **remove_hosts) if add_hosts: @@ -1514,13 +1512,13 @@ def validate_cluster(session, cluster): cluster=RESP_CONFIG_FIELDS, hosts=RESP_CLUSTERHOST_CONFIG_FIELDS ) -def review_cluster(reviewer, cluster_id, review={}, session=None, **kwargs): +def review_cluster(cluster_id, review={}, user=None, session=None, **kwargs): """review cluster.""" from compass.db.api import host as host_api cluster = utils.get_db_object( session, models.Cluster, id=cluster_id ) - is_cluster_editable(session, cluster, reviewer) + is_cluster_editable(session, cluster, user) host_ids = review.get('hosts', []) clusterhost_ids = review.get('clusterhosts', []) clusterhosts = [] @@ -1542,7 +1540,7 @@ def review_cluster(reviewer, cluster_id, review={}, session=None, **kwargs): for clusterhost in clusterhosts: host = clusterhost.host if not host_api.is_host_editable( - session, host, reviewer, False + session, host, user, False ): logging.info( 'ignore update host %s config ' @@ -1616,7 +1614,7 @@ def review_cluster(reviewer, cluster_id, review={}, session=None, **kwargs): hosts=RESP_CLUSTERHOST_FIELDS ) def deploy_cluster( - deployer, cluster_id, deploy={}, session=None, **kwargs + cluster_id, deploy={}, user=None, session=None, **kwargs ): """deploy cluster.""" from compass.db.api import host as host_api @@ -1633,13 +1631,13 @@ def deploy_cluster( clusterhost.host_id in host_ids ): clusterhosts.append(clusterhost) - is_cluster_editable(session, cluster, deployer) + is_cluster_editable(session, cluster, user) is_cluster_validated(session, cluster) utils.update_db_object(session, cluster.state, state='INITIALIZED') for clusterhost in clusterhosts: host = clusterhost.host if host_api.is_host_editable( - session, host, deployer, + session, host, user, exception_when_not_editable=False ): host_api.is_host_validated( @@ -1655,7 +1653,7 @@ def deploy_cluster( celery_client.celery.send_task( 'compass.tasks.deploy_cluster', ( - deployer.email, cluster_id, + user.email, cluster_id, [clusterhost.host_id for clusterhost in clusterhosts] ) ) @@ -1672,7 +1670,7 @@ def deploy_cluster( permission.PERMISSION_GET_CLUSTER_STATE ) @utils.wrap_to_dict(RESP_STATE_FIELDS) -def get_cluster_state(getter, cluster_id, session=None, **kwargs): +def get_cluster_state(cluster_id, user=None, session=None, **kwargs): """Get cluster state info.""" return utils.get_db_object( session, models.Cluster, id=cluster_id @@ -1686,7 +1684,7 @@ def get_cluster_state(getter, cluster_id, session=None, **kwargs): ) @utils.wrap_to_dict(RESP_CLUSTERHOST_STATE_FIELDS) def get_cluster_host_state( - getter, cluster_id, host_id, session=None, **kwargs + cluster_id, host_id, user=None, session=None, **kwargs ): """Get clusterhost state info.""" return utils.get_db_object( @@ -1702,7 +1700,7 @@ def get_cluster_host_state( ) @utils.wrap_to_dict(RESP_CLUSTERHOST_STATE_FIELDS) def get_cluster_host_self_state( - getter, cluster_id, host_id, session=None, **kwargs + cluster_id, host_id, user=None, session=None, **kwargs ): """Get clusterhost state info.""" clusterhost = utils.get_db_object( @@ -1722,7 +1720,7 @@ def get_cluster_host_self_state( ) @utils.wrap_to_dict(RESP_CLUSTERHOST_STATE_FIELDS) def get_clusterhost_state( - getter, clusterhost_id, session=None, **kwargs + clusterhost_id, user=None, session=None, **kwargs ): """Get clusterhost state info.""" return utils.get_db_object( @@ -1738,7 +1736,7 @@ def get_clusterhost_state( ) @utils.wrap_to_dict(RESP_CLUSTERHOST_STATE_FIELDS) def get_clusterhost_self_state( - getter, clusterhost_id, session=None, **kwargs + clusterhost_id, user=None, session=None, **kwargs ): """Get clusterhost state info.""" return utils.get_db_object( @@ -1757,7 +1755,7 @@ def get_clusterhost_self_state( ) @utils.wrap_to_dict(RESP_CLUSTERHOST_STATE_FIELDS) def update_cluster_host_state( - updater, cluster_id, host_id, session=None, **kwargs + cluster_id, host_id, user=None, session=None, **kwargs ): """Update a clusterhost state.""" clusterhost = utils.get_db_object( @@ -1778,7 +1776,7 @@ def update_cluster_host_state( ) @utils.wrap_to_dict(RESP_CLUSTERHOST_STATE_FIELDS) def update_clusterhost_state( - updater, clusterhost_id, session=None, **kwargs + clusterhost_id, user=None, session=None, **kwargs ): """Update a clusterhost state.""" clusterhost = utils.get_db_object( @@ -1799,7 +1797,7 @@ def update_clusterhost_state( ) @utils.wrap_to_dict(RESP_STATE_FIELDS) def update_cluster_state( - updater, cluster_id, session=None, **kwargs + cluster_id, user=None, session=None, **kwargs ): """Update a cluster state.""" cluster = utils.get_db_object( @@ -1813,7 +1811,7 @@ def update_cluster_state( @database.run_in_session() @utils.wrap_to_dict(RESP_CLUSTERHOST_LOG_FIELDS) def get_cluster_host_log_histories( - getter, cluster_id, host_id, session=None, **kwargs + cluster_id, host_id, user=None, session=None, **kwargs ): """Get clusterhost log history.""" return utils.list_db_objects( @@ -1826,7 +1824,7 @@ def get_cluster_host_log_histories( @database.run_in_session() @utils.wrap_to_dict(RESP_CLUSTERHOST_LOG_FIELDS) def get_clusterhost_log_histories( - getter, clusterhost_id, + clusterhost_id, user=None, session=None, **kwargs ): """Get clusterhost log history.""" @@ -1839,7 +1837,7 @@ def get_clusterhost_log_histories( @database.run_in_session() @utils.wrap_to_dict(RESP_CLUSTERHOST_LOG_FIELDS) def get_cluster_host_log_history( - getter, cluster_id, host_id, filename, session=None, **kwargs + cluster_id, host_id, filename, user=None, session=None, **kwargs ): """Get clusterhost log history.""" return utils.get_db_object( @@ -1852,7 +1850,7 @@ def get_cluster_host_log_history( @database.run_in_session() @utils.wrap_to_dict(RESP_CLUSTERHOST_LOG_FIELDS) def get_clusterhost_log_history( - getter, clusterhost_id, filename, session=None, **kwargs + clusterhost_id, filename, user=None, session=None, **kwargs ): """Get host log history.""" return utils.get_db_object( @@ -1868,7 +1866,7 @@ def get_clusterhost_log_history( @database.run_in_session() @utils.wrap_to_dict(RESP_CLUSTERHOST_LOG_FIELDS) def update_cluster_host_log_history( - updater, cluster_id, host_id, filename, session=None, **kwargs + cluster_id, host_id, filename, user=None, session=None, **kwargs ): """Update a host log history.""" cluster_host_log_history = utils.get_db_object( @@ -1885,7 +1883,7 @@ def update_cluster_host_log_history( @database.run_in_session() @utils.wrap_to_dict(RESP_CLUSTERHOST_LOG_FIELDS) def update_clusterhost_log_history( - updater, clusterhost_id, filename, session=None, **kwargs + clusterhost_id, filename, user=None, session=None, **kwargs ): """Update a host log history.""" clusterhost_log_history = utils.get_db_object( @@ -1903,8 +1901,8 @@ def update_clusterhost_log_history( @database.run_in_session() @utils.wrap_to_dict(RESP_CLUSTERHOST_LOG_FIELDS) def add_clusterhost_log_history( - creator, clusterhost_id, exception_when_existing=False, - filename=None, session=None, **kwargs + clusterhost_id, exception_when_existing=False, + filename=None, user=None, session=None, **kwargs ): """add a host log history.""" return utils.add_db_object( @@ -1921,8 +1919,8 @@ def add_clusterhost_log_history( @database.run_in_session() @utils.wrap_to_dict(RESP_CLUSTERHOST_LOG_FIELDS) def add_cluster_host_log_history( - creator, cluster_id, host_id, exception_when_existing=False, - filename=None, session=None, **kwargs + cluster_id, host_id, exception_when_existing=False, + filename=None, user=None, session=None, **kwargs ): """add a host log history.""" clusterhost = utils.get_db_object( diff --git a/compass/db/api/database.py b/compass/db/api/database.py index fa8ad294..a909e6fc 100644 --- a/compass/db/api/database.py +++ b/compass/db/api/database.py @@ -149,9 +149,7 @@ def run_in_session(): def decorator(func): @functools.wraps(func) def wrapper(*args, **kwargs): - if args is not () and 'session' in str(args[-1]): - return func(*args, **kwargs) - elif 'session' in kwargs.keys(): + if 'session' in kwargs.keys(): return func(*args, **kwargs) else: with session() as my_session: diff --git a/compass/db/api/host.py b/compass/db/api/host.py index b5b8d543..0cb93a03 100644 --- a/compass/db/api/host.py +++ b/compass/db/api/host.py @@ -107,7 +107,7 @@ UPDATED_LOG_FIELDS = [ permission.PERMISSION_LIST_HOSTS ) @utils.wrap_to_dict(RESP_FIELDS) -def list_hosts(lister, session=None, **filters): +def list_hosts(user=None, session=None, **filters): """List hosts.""" return utils.list_db_objects( session, models.Host, **filters @@ -128,7 +128,7 @@ def list_hosts(lister, session=None, **filters): os_id=utils.general_filter_callback ) @utils.wrap_to_dict(RESP_FIELDS) -def list_machines_or_hosts(lister, session=None, **filters): +def list_machines_or_hosts(user=None, session=None, **filters): """List hosts.""" machines = utils.list_db_objects( session, models.Machine, **filters @@ -150,8 +150,8 @@ def list_machines_or_hosts(lister, session=None, **filters): ) @utils.wrap_to_dict(RESP_FIELDS) def get_host( - getter, host_id, - exception_when_missing=True, session=None, **kwargs + host_id, exception_when_missing=True, + user=None, session=None, **kwargs ): """get host info.""" return utils.get_db_object( @@ -167,8 +167,8 @@ def get_host( ) @utils.wrap_to_dict(RESP_FIELDS) def get_machine_or_host( - getter, host_id, - exception_when_missing=True, session=None, **kwargs + host_id, exception_when_missing=True, + user=None, session=None, **kwargs ): """get host info.""" machine = utils.get_db_object( @@ -190,7 +190,7 @@ def get_machine_or_host( permission.PERMISSION_LIST_HOST_CLUSTERS ) @utils.wrap_to_dict(RESP_CLUSTER_FIELDS) -def get_host_clusters(getter, host_id, session=None, **kwargs): +def get_host_clusters(host_id, user=None, session=None, **kwargs): """get host clusters.""" host = utils.get_db_object( session, models.Host, id=host_id @@ -276,13 +276,13 @@ def validate_host(session, host): ) @utils.input_validates(name=utils.check_name) @utils.wrap_to_dict(RESP_FIELDS) -def _update_host(session, updater, host_id, **kwargs): +def _update_host(session, user, host_id, **kwargs): """Update a host internal.""" host = utils.get_db_object( session, models.Host, id=host_id ) is_host_editable( - session, host, updater, + session, host, user, reinstall_os_set=kwargs.get('reinstall_os', False) ) if 'name' in kwargs: @@ -303,19 +303,19 @@ def _update_host(session, updater, host_id, **kwargs): @user_api.check_user_permission_in_session( permission.PERMISSION_UPDATE_HOST ) -def update_host(updater, host_id, session=None, **kwargs): +def update_host(host_id, user=None, session=None, **kwargs): """Update a host.""" - return _update_host(session, updater, host_id=host_id, **kwargs) + return _update_host(session, user, host_id=host_id, **kwargs) @database.run_in_session() @user_api.check_user_permission_in_session( permission.PERMISSION_UPDATE_HOST ) -def update_hosts(updater, data=[], session=None): +def update_hosts(data=[], user=None, session=None): hosts = [] for host_data in data: - hosts.append(_update_host(session, updater, **host_data)) + hosts.append(_update_host(session, user, **host_data)) return hosts @@ -329,8 +329,8 @@ def update_hosts(updater, data=[], session=None): host=RESP_FIELDS ) def del_host( - deleter, host_id, - force=False, from_database_only=False, session=None, **kwargs + host_id, force=False, from_database_only=False, + user=None, session=None, **kwargs ): """Delete a host.""" from compass.db.api import cluster as cluster_api @@ -340,7 +340,7 @@ def del_host( if host.state.state != 'UNINITIALIZED' and force: host.state.state = 'ERROR' is_host_editable( - session, host, deleter, + session, host, user, reinstall_os_set=True ) cluster_ids = [] @@ -348,7 +348,7 @@ def del_host( if clusterhost.state.state != 'UNINITIALIZED' and force: clusterhost.state.state = 'ERROR' cluster_api.is_cluster_editable( - session, clusterhost.cluster, deleter, + session, clusterhost.cluster, user, reinstall_distributed_system_set=True ) cluster_ids.append(clusterhost.cluster_id) @@ -363,7 +363,7 @@ def del_host( celery_client.celery.send_task( 'compass.tasks.delete_host', ( - deleter.email, host_id, cluster_ids + user.email, host_id, cluster_ids ) ) return { @@ -378,7 +378,7 @@ def del_host( permission.PERMISSION_LIST_HOST_CONFIG ) @utils.wrap_to_dict(RESP_CONFIG_FIELDS) -def get_host_config(getter, host_id, session=None, **kwargs): +def get_host_config(host_id, user=None, session=None, **kwargs): """Get host config.""" return utils.get_db_object( session, models.Host, id=host_id @@ -391,7 +391,7 @@ def get_host_config(getter, host_id, session=None, **kwargs): permission.PERMISSION_LIST_HOST_CONFIG ) @utils.wrap_to_dict(RESP_DEPLOYED_CONFIG_FIELDS) -def get_host_deployed_config(getter, host_id, session=None, **kwargs): +def get_host_deployed_config(host_id, user=None, session=None, **kwargs): """Get host deployed config.""" return utils.get_db_object( session, models.Host, id=host_id @@ -410,20 +410,20 @@ def get_host_deployed_config(getter, host_id, session=None, **kwargs): permission.PERMISSION_ADD_HOST_CONFIG ) @utils.wrap_to_dict(RESP_CONFIG_FIELDS) -def update_host_deployed_config(updater, host_id, session=None, **kwargs): +def update_host_deployed_config(host_id, user=None, session=None, **kwargs): """Update host deployed config.""" host = utils.get_db_object( session, models.Host, id=host_id ) - is_host_editable(session, host, updater) + is_host_editable(session, host, user) is_host_validated(session, host) return utils.update_db_object(session, host, **kwargs) @utils.wrap_to_dict(RESP_CONFIG_FIELDS) -def _update_host_config(session, updater, host, **kwargs): +def _update_host_config(session, user, host, **kwargs): """Update host config.""" - is_host_editable(session, host, updater) + is_host_editable(session, host, user) return utils.update_db_object(session, host, **kwargs) @@ -438,7 +438,7 @@ def _update_host_config(session, updater, host, **kwargs): @user_api.check_user_permission_in_session( permission.PERMISSION_ADD_HOST_CONFIG ) -def update_host_config(updater, host_id, session=None, **kwargs): +def update_host_config(host_id, user=None, session=None, **kwargs): host = utils.get_db_object( session, models.Host, id=host_id ) @@ -453,7 +453,7 @@ def update_host_config(updater, host_id, session=None, **kwargs): ) def update_config_internal(host, **in_kwargs): return _update_host_config( - session, updater, host, **kwargs + session, user, host, **kwargs ) return update_config_internal( @@ -472,7 +472,7 @@ def update_host_config(updater, host_id, session=None, **kwargs): @user_api.check_user_permission_in_session( permission.PERMISSION_ADD_HOST_CONFIG ) -def patch_host_config(updater, host_id, session=None, **kwargs): +def patch_host_config(host_id, user=None, session=None, **kwargs): host = utils.get_db_object( session, models.Host, id=host_id ) @@ -487,7 +487,7 @@ def patch_host_config(updater, host_id, session=None, **kwargs): ) def patch_config_internal(host, **in_kwargs): return _update_host_config( - session, updater, host, **in_kwargs + session, user, host, **in_kwargs ) return patch_config_internal( @@ -501,12 +501,12 @@ def patch_host_config(updater, host_id, session=None, **kwargs): permission.PERMISSION_DEL_HOST_CONFIG ) @utils.wrap_to_dict(RESP_CONFIG_FIELDS) -def del_host_config(deleter, host_id, session=None): +def del_host_config(host_id, user=None, session=None): """delete a host config.""" host = utils.get_db_object( session, models.Host, id=host_id ) - is_host_editable(session, host, deleter) + is_host_editable(session, host, user) return utils.update_db_object( session, host, os_config={}, config_validated=False ) @@ -520,7 +520,7 @@ def del_host_config(deleter, host_id, session=None): permission.PERMISSION_LIST_HOST_NETWORKS ) @utils.wrap_to_dict(RESP_NETWORK_FIELDS) -def list_host_networks(lister, host_id, session=None, **filters): +def list_host_networks(host_id, user=None, session=None, **filters): """Get host networks.""" return utils.list_db_objects( session, models.HostNetwork, @@ -536,7 +536,7 @@ def list_host_networks(lister, host_id, session=None, **filters): permission.PERMISSION_LIST_HOST_NETWORKS ) @utils.wrap_to_dict(RESP_NETWORK_FIELDS) -def list_hostnetworks(lister, session=None, **filters): +def list_hostnetworks(user=None, session=None, **filters): """Get host networks.""" return utils.list_db_objects( session, models.HostNetwork, **filters @@ -550,8 +550,8 @@ def list_hostnetworks(lister, session=None, **filters): ) @utils.wrap_to_dict(RESP_NETWORK_FIELDS) def get_host_network( - getter, host_id, - host_network_id, session=None, **kwargs + host_id, host_network_id, + user=None, session=None, **kwargs ): """Get host network.""" host_network = utils.get_db_object( @@ -573,7 +573,7 @@ def get_host_network( permission.PERMISSION_LIST_HOST_NETWORKS ) @utils.wrap_to_dict(RESP_NETWORK_FIELDS) -def get_hostnetwork(getter, host_network_id, session=None, **kwargs): +def get_hostnetwork(host_network_id, user=None, session=None, **kwargs): """Get host network.""" return utils.get_db_object( session, models.HostNetwork, @@ -591,7 +591,7 @@ def get_hostnetwork(getter, host_network_id, session=None, **kwargs): ) @utils.wrap_to_dict(RESP_NETWORK_FIELDS) def _add_host_network( - session, creator, host_id, exception_when_existing=True, + session, user, host_id, exception_when_existing=True, interface=None, ip=None, **kwargs ): host = utils.get_db_object( @@ -613,7 +613,7 @@ def _add_host_network( ip, host_network.id ) ) - is_host_editable(session, host, creator) + is_host_editable(session, host, user) return utils.add_db_object( session, models.HostNetwork, exception_when_existing, @@ -626,13 +626,12 @@ def _add_host_network( permission.PERMISSION_ADD_HOST_NETWORK ) def add_host_network( - creator, host_id, - exception_when_existing=True, - interface=None, session=None, **kwargs + host_id, exception_when_existing=True, + interface=None, user=None, session=None, **kwargs ): """Create a host network.""" return _add_host_network( - session, creator, host_id, exception_when_existing, + session, user, host_id, exception_when_existing, interface=interface, **kwargs ) @@ -642,9 +641,8 @@ def add_host_network( permission.PERMISSION_ADD_HOST_NETWORK ) def add_host_networks( - creator, exception_when_existing=False, - data=[], session=None + data=[], user=None, session=None ): """Create host networks.""" hosts = [] @@ -657,7 +655,7 @@ def add_host_networks( for network in networks: try: host_networks.append(_add_host_network( - session, creator, host_id, exception_when_existing, + session, user, host_id, exception_when_existing, **network )) except exception.DatabaseException as error: @@ -677,7 +675,7 @@ def add_host_networks( @utils.wrap_to_dict(RESP_NETWORK_FIELDS) def _update_host_network( - session, updater, host_network, **kwargs + session, user, host_network, **kwargs ): if 'interface' in kwargs: interface = kwargs['interface'] @@ -708,7 +706,7 @@ def _update_host_network( ip, host_network_by_ip.id ) ) - is_host_editable(session, host_network.host, updater) + is_host_editable(session, host_network.host, user) return utils.update_db_object(session, host_network, **kwargs) @@ -724,7 +722,7 @@ def _update_host_network( permission.PERMISSION_ADD_HOST_NETWORK ) def update_host_network( - updater, host_id, host_network_id, session=None, **kwargs + host_id, host_network_id, user=None, session=None, **kwargs ): """Update a host network.""" host_network = utils.get_db_object( @@ -738,7 +736,7 @@ def update_host_network( ) ) return _update_host_network( - session, updater, host_network, **kwargs + session, user, host_network, **kwargs ) @@ -753,13 +751,13 @@ def update_host_network( @user_api.check_user_permission_in_session( permission.PERMISSION_ADD_HOST_NETWORK ) -def update_hostnetwork(updater, host_network_id, session=None, **kwargs): +def update_hostnetwork(host_network_id, user=None, session=None, **kwargs): """Update a host network.""" host_network = utils.get_db_object( session, models.HostNetwork, id=host_network_id ) return _update_host_network( - session, updater, host_network, **kwargs + session, user, host_network, **kwargs ) @@ -770,7 +768,7 @@ def update_hostnetwork(updater, host_network_id, session=None, **kwargs): ) @utils.wrap_to_dict(RESP_NETWORK_FIELDS) def del_host_network( - deleter, host_id, host_network_id, + host_id, host_network_id, user=None, session=None, **kwargs ): """Delete a host network.""" @@ -784,7 +782,7 @@ def del_host_network( host_id, host_network_id ) ) - is_host_editable(session, host_network.host, deleter) + is_host_editable(session, host_network.host, user) return utils.del_db_object(session, host_network) @@ -794,12 +792,12 @@ def del_host_network( permission.PERMISSION_DEL_HOST_NETWORK ) @utils.wrap_to_dict(RESP_NETWORK_FIELDS) -def del_hostnetwork(deleter, host_network_id, session=None, **kwargs): +def del_hostnetwork(host_network_id, user=None, session=None, **kwargs): """Delete a host network.""" host_network = utils.get_db_object( session, models.HostNetwork, id=host_network_id ) - is_host_editable(session, host_network.host, deleter) + is_host_editable(session, host_network.host, user) return utils.del_db_object(session, host_network) @@ -809,7 +807,7 @@ def del_hostnetwork(deleter, host_network_id, session=None, **kwargs): permission.PERMISSION_GET_HOST_STATE ) @utils.wrap_to_dict(RESP_STATE_FIELDS) -def get_host_state(getter, host_id, session=None, **kwargs): +def get_host_state(host_id, user=None, session=None, **kwargs): """Get host state info.""" return utils.get_db_object( session, models.Host, id=host_id @@ -825,7 +823,7 @@ def get_host_state(getter, host_id, session=None, **kwargs): permission.PERMISSION_UPDATE_HOST_STATE ) @utils.wrap_to_dict(RESP_STATE_FIELDS) -def update_host_state(updater, host_id, session=None, **kwargs): +def update_host_state(host_id, user=None, session=None, **kwargs): """Update a host state.""" host = utils.get_db_object( session, models.Host, id=host_id @@ -837,7 +835,7 @@ def update_host_state(updater, host_id, session=None, **kwargs): @utils.supported_filters([]) @database.run_in_session() @utils.wrap_to_dict(RESP_LOG_FIELDS) -def get_host_log_histories(getter, host_id, session=None, **kwargs): +def get_host_log_histories(host_id, user=None, session=None, **kwargs): """Get host log history.""" return utils.list_db_objects( session, models.HostLogHistory, id=host_id @@ -847,7 +845,7 @@ def get_host_log_histories(getter, host_id, session=None, **kwargs): @utils.supported_filters([]) @database.run_in_session() @utils.wrap_to_dict(RESP_LOG_FIELDS) -def get_host_log_history(getter, host_id, filename, session=None, **kwargs): +def get_host_log_history(host_id, filename, user=None, session=None, **kwargs): """Get host log history.""" return utils.get_db_object( session, models.HostLogHistory, id=host_id, filename=filename @@ -861,7 +859,7 @@ def get_host_log_history(getter, host_id, filename, session=None, **kwargs): @database.run_in_session() @utils.wrap_to_dict(RESP_LOG_FIELDS) def update_host_log_history( - updater, host_id, filename, + host_id, filename, user=None, session=None, **kwargs ): """Update a host log history.""" @@ -879,8 +877,8 @@ def update_host_log_history( @database.run_in_session() @utils.wrap_to_dict(RESP_LOG_FIELDS) def add_host_log_history( - creator, host_id, exception_when_existing=False, - filename=None, session=None, **kwargs + host_id, exception_when_existing=False, + filename=None, user=None, session=None, **kwargs ): """add a host log history.""" return utils.add_db_object( @@ -899,7 +897,7 @@ def add_host_log_history( host=RESP_CONFIG_FIELDS ) def poweron_host( - deployer, host_id, poweron={}, session=None, **kwargs + host_id, poweron={}, user=None, session=None, **kwargs ): """power on host.""" from compass.tasks import client as celery_client @@ -927,7 +925,7 @@ def poweron_host( host=RESP_CONFIG_FIELDS ) def poweroff_host( - deployer, host_id, poweroff={}, session=None, **kwargs + host_id, poweroff={}, user=None, session=None, **kwargs ): """power off host.""" from compass.tasks import client as celery_client @@ -955,7 +953,7 @@ def poweroff_host( host=RESP_CONFIG_FIELDS ) def reset_host( - deployer, host_id, reset={}, session=None, **kwargs + host_id, reset={}, user=None, session=None, **kwargs ): """reset host.""" from compass.tasks import client as celery_client diff --git a/compass/db/api/machine.py b/compass/db/api/machine.py index 0920b410..6bf066a7 100644 --- a/compass/db/api/machine.py +++ b/compass/db/api/machine.py @@ -50,9 +50,8 @@ RESP_DEPLOY_FIELDS = [ ) @utils.wrap_to_dict(RESP_FIELDS) def get_machine( - getter, machine_id, - exception_when_missing=True, session=None, - **kwargs + machine_id, exception_when_missing=True, + user=None, session=None, **kwargs ): """get field dict of a machine.""" return utils.get_db_object( @@ -73,7 +72,7 @@ def get_machine( location=utils.general_filter_callback ) @utils.wrap_to_dict(RESP_FIELDS) -def list_machines(lister, session=None, **filters): +def list_machines(user=None, session=None, **filters): """List machines.""" return utils.list_db_objects( session, models.Machine, **filters @@ -81,7 +80,7 @@ def list_machines(lister, session=None, **filters): @utils.wrap_to_dict(RESP_FIELDS) -def _update_machine(session, updater, machine_id, **kwargs): +def _update_machine(session, machine_id, **kwargs): """Update a machine.""" machine = utils.get_db_object(session, models.Machine, id=machine_id) return utils.update_db_object(session, machine, **kwargs) @@ -96,9 +95,9 @@ def _update_machine(session, updater, machine_id, **kwargs): @user_api.check_user_permission_in_session( permission.PERMISSION_ADD_MACHINE ) -def update_machine(updater, machine_id, session=None, **kwargs): +def update_machine(machine_id, user=None, session=None, **kwargs): return _update_machine( - session, updater, machine_id, **kwargs + session, machine_id, **kwargs ) @@ -113,9 +112,12 @@ def update_machine(updater, machine_id, session=None, **kwargs): ) @database.run_in_session() @utils.output_validates(ipmi_credentials=utils.check_ipmi_credentials) -def patch_machine(updater, machine_id, session=None, **kwargs): +@user_api.check_user_permission_in_session( + permission.PERMISSION_ADD_MACHINE +) +def patch_machine(machine_id, user=None, session=None, **kwargs): return _update_machine( - session, updater, machine_id, **kwargs + session, machine_id, **kwargs ) @@ -125,7 +127,7 @@ def patch_machine(updater, machine_id, session=None, **kwargs): permission.PERMISSION_DEL_MACHINE ) @utils.wrap_to_dict(RESP_FIELDS) -def del_machine(deleter, machine_id, session=None, **kwargs): +def del_machine(machine_id, user=None, session=None, **kwargs): """Delete a machine.""" machine = utils.get_db_object(session, models.Machine, id=machine_id) if machine.host: @@ -148,7 +150,7 @@ def del_machine(deleter, machine_id, session=None, **kwargs): machine=RESP_FIELDS ) def poweron_machine( - deployer, machine_id, poweron={}, session=None, **kwargs + machine_id, poweron={}, user=None, session=None, **kwargs ): """power on machine.""" from compass.tasks import client as celery_client @@ -175,7 +177,7 @@ def poweron_machine( machine=RESP_FIELDS ) def poweroff_machine( - deployer, machine_id, poweroff={}, session=None, **kwargs + machine_id, poweroff={}, user=None, session=None, **kwargs ): """power off machine.""" from compass.tasks import client as celery_client @@ -202,7 +204,7 @@ def poweroff_machine( machine=RESP_FIELDS ) def reset_machine( - deployer, machine_id, reset={}, session=None, **kwargs + machine_id, reset={}, user=None, session=None, **kwargs ): """reset machine.""" from compass.tasks import client as celery_client diff --git a/compass/db/api/network.py b/compass/db/api/network.py index 35c51c90..ec1765b1 100644 --- a/compass/db/api/network.py +++ b/compass/db/api/network.py @@ -51,7 +51,7 @@ def _check_subnet(subnet): permission.PERMISSION_LIST_SUBNETS ) @utils.wrap_to_dict(RESP_FIELDS) -def list_subnets(lister, session=None, **filters): +def list_subnets(user=None, session=None, **filters): """List subnets.""" return utils.list_db_objects( session, models.Subnet, **filters @@ -65,8 +65,8 @@ def list_subnets(lister, session=None, **filters): ) @utils.wrap_to_dict(RESP_FIELDS) def get_subnet( - getter, subnet_id, - exception_when_missing=True, session=None, **kwargs + subnet_id, exception_when_missing=True, + user=None, session=None, **kwargs ): """Get subnet info.""" return utils.get_db_object( @@ -86,8 +86,8 @@ def get_subnet( ) @utils.wrap_to_dict(RESP_FIELDS) def add_subnet( - creator, exception_when_existing=True, - subnet=None, session=None, **kwargs + exception_when_existing=True, subnet=None, + user=None, session=None, **kwargs ): """Create a subnet.""" return utils.add_db_object( @@ -106,7 +106,7 @@ def add_subnet( permission.PERMISSION_ADD_SUBNET ) @utils.wrap_to_dict(RESP_FIELDS) -def update_subnet(updater, subnet_id, session=None, **kwargs): +def update_subnet(subnet_id, user=None, session=None, **kwargs): """Update a subnet.""" subnet = utils.get_db_object( session, models.Subnet, id=subnet_id @@ -120,7 +120,7 @@ def update_subnet(updater, subnet_id, session=None, **kwargs): permission.PERMISSION_DEL_SUBNET ) @utils.wrap_to_dict(RESP_FIELDS) -def del_subnet(deleter, subnet_id, session=None, **kwargs): +def del_subnet(subnet_id, user=None, session=None, **kwargs): """Delete a subnet.""" subnet = utils.get_db_object( session, models.Subnet, id=subnet_id diff --git a/compass/db/api/permission.py b/compass/db/api/permission.py index 380f3325..e3ddf69d 100644 --- a/compass/db/api/permission.py +++ b/compass/db/api/permission.py @@ -300,7 +300,7 @@ def list_permissions_internal(session, **filters): @database.run_in_session() @user_api.check_user_permission_in_session(PERMISSION_LIST_PERMISSIONS) @utils.wrap_to_dict(RESP_FIELDS) -def list_permissions(lister, session=None, **filters): +def list_permissions(user=None, session=None, **filters): """list permissions.""" return utils.list_db_objects( session, models.Permission, **filters @@ -312,8 +312,8 @@ def list_permissions(lister, session=None, **filters): @user_api.check_user_permission_in_session(PERMISSION_LIST_PERMISSIONS) @utils.wrap_to_dict(RESP_FIELDS) def get_permission( - getter, permission_id, - exception_when_missing=True, session=None, **kwargs + permission_id, exception_when_missing=True, + user=None, session=None, **kwargs ): """get permissions.""" return utils.get_db_object( diff --git a/compass/db/api/switch.py b/compass/db/api/switch.py index eb1ecc04..61a91503 100644 --- a/compass/db/api/switch.py +++ b/compass/db/api/switch.py @@ -140,8 +140,8 @@ def get_switch_internal( ) @utils.wrap_to_dict(RESP_FIELDS) def get_switch( - getter, switch_id, - exception_when_missing=True, session=None, **kwargs + switch_id, exception_when_missing=True, + user=None, session=None, **kwargs ): """get field dict of a switch.""" return utils.get_db_object( @@ -156,7 +156,7 @@ def get_switch( permission.PERMISSION_LIST_SWITCHES ) @utils.wrap_to_dict(RESP_FIELDS) -def list_switches(lister, session=None, **filters): +def list_switches(user=None, session=None, **filters): """List switches.""" switches = utils.list_db_objects( session, models.Switch, **filters @@ -176,7 +176,7 @@ def list_switches(lister, session=None, **filters): permission.PERMISSION_DEL_SWITCH ) @utils.wrap_to_dict(RESP_FIELDS) -def del_switch(deleter, switch_id, session=None, **kwargs): +def del_switch(switch_id, user=None, session=None, **kwargs): """Delete a switch.""" switch = utils.get_db_object(session, models.Switch, id=switch_id) default_switch_ip_int = long(netaddr.IPAddress(setting.DEFAULT_SWITCH_IP)) @@ -212,8 +212,8 @@ def del_switch(deleter, switch_id, session=None, **kwargs): ) @utils.wrap_to_dict(RESP_FIELDS) def add_switch( - creator, exception_when_existing=True, - ip=None, session=None, **kwargs + exception_when_existing=True, ip=None, + user=None, session=None, **kwargs ): """Create a switch.""" ip_int = long(netaddr.IPAddress(ip)) @@ -231,7 +231,7 @@ def update_switch_internal(session, switch, **kwargs): @utils.wrap_to_dict(RESP_FIELDS) -def _update_switch(session, updater, switch_id, **kwargs): +def _update_switch(session, switch_id, **kwargs): """Update a switch.""" switch = utils.get_db_object( session, models.Switch, id=switch_id @@ -254,9 +254,9 @@ def _update_switch(session, updater, switch_id, **kwargs): @user_api.check_user_permission_in_session( permission.PERMISSION_ADD_SWITCH ) -def update_switch(updater, switch_id, session=None, **kwargs): +def update_switch(switch_id, user=None, session=None, **kwargs): """Update fields of a switch.""" - return _update_switch(session, updater, switch_id, **kwargs) + return _update_switch(session, switch_id, **kwargs) @utils.replace_filters( @@ -274,9 +274,12 @@ def update_switch(updater, switch_id, session=None, **kwargs): @utils.output_validates( credentials=utils.check_switch_credentials ) -def patch_switch(updater, switch_id, session=None, **kwargs): +@user_api.check_user_permission_in_session( + permission.PERMISSION_ADD_SWITCH +) +def patch_switch(switch_id, user=None, session=None, **kwargs): """Patch fields of a switch.""" - return _update_switch(session, updater, switch_id, **kwargs) + return _update_switch(session, switch_id, **kwargs) @utils.supported_filters(optional_support_keys=SUPPORTED_FILTER_FIELDS) @@ -285,7 +288,7 @@ def patch_switch(updater, switch_id, session=None, **kwargs): permission.PERMISSION_LIST_SWITCH_FILTERS ) @utils.wrap_to_dict(RESP_FILTERS_FIELDS) -def list_switch_filters(lister, session=None, **filters): +def list_switch_filters(user=None, session=None, **filters): """List switch filters.""" return utils.list_db_objects( session, models.Switch, **filters @@ -299,7 +302,7 @@ def list_switch_filters(lister, session=None, **filters): ) @utils.wrap_to_dict(RESP_FILTERS_FIELDS) def get_switch_filters( - getter, switch_id, session=None, **kwargs + switch_id, user=None, session=None, **kwargs ): """get switch filter.""" return utils.get_db_object( @@ -320,7 +323,7 @@ def get_switch_filters( permission.PERMISSION_UPDATE_SWITCH_FILTERS ) @utils.wrap_to_dict(RESP_FILTERS_FIELDS) -def update_switch_filters(updater, switch_id, session=None, **kwargs): +def update_switch_filters(switch_id, user=None, session=None, **kwargs): """Update a switch filter.""" switch = utils.get_db_object(session, models.Switch, id=switch_id) return utils.update_db_object(session, switch, **kwargs) @@ -339,7 +342,7 @@ def update_switch_filters(updater, switch_id, session=None, **kwargs): permission.PERMISSION_UPDATE_SWITCH_FILTERS ) @utils.wrap_to_dict(RESP_FILTERS_FIELDS) -def patch_switch_filter(updater, switch_id, session=None, **kwargs): +def patch_switch_filter(switch_id, user=None, session=None, **kwargs): """Patch a switch filter.""" switch = utils.get_db_object(session, models.Switch, id=switch_id) return utils.update_db_object(session, switch, **kwargs) @@ -405,7 +408,7 @@ def _filter_vlans(vlan_filter, obj): location=utils.general_filter_callback ) @utils.wrap_to_dict(RESP_MACHINES_FIELDS) -def _filter_switch_machines(session, user, switch_machines): +def _filter_switch_machines(session, switch_machines): return [ switch_machine for switch_machine in switch_machines if not switch_machine.filtered @@ -424,7 +427,7 @@ def _filter_switch_machines(session, user, switch_machines): RESP_MACHINES_HOSTS_FIELDS, clusters=RESP_CLUSTER_FIELDS ) -def _filter_switch_machines_hosts(session, user, switch_machines): +def _filter_switch_machines_hosts(session, switch_machines): filtered_switch_machines = [ switch_machine for switch_machine in switch_machines if not switch_machine.filtered @@ -451,12 +454,12 @@ def _filter_switch_machines_hosts(session, user, switch_machines): @user_api.check_user_permission_in_session( permission.PERMISSION_LIST_SWITCH_MACHINES ) -def list_switch_machines(getter, switch_id, session=None, **filters): +def list_switch_machines(switch_id, user=None, session=None, **filters): """Get switch machines.""" switch_machines = get_switch_machines_internal( session, switch_id=switch_id, **filters ) - return _filter_switch_machines(session, getter, switch_machines) + return _filter_switch_machines(session, switch_machines) @utils.replace_filters( @@ -469,13 +472,13 @@ def list_switch_machines(getter, switch_id, session=None, **filters): @user_api.check_user_permission_in_session( permission.PERMISSION_LIST_SWITCH_MACHINES ) -def list_switchmachines(lister, session=None, **filters): +def list_switchmachines(user=None, session=None, **filters): """List switch machines.""" switch_machines = get_switch_machines_internal( session, **filters ) return _filter_switch_machines( - session, lister, switch_machines + session, switch_machines ) @@ -486,13 +489,13 @@ def list_switchmachines(lister, session=None, **filters): @user_api.check_user_permission_in_session( permission.PERMISSION_LIST_SWITCH_MACHINES ) -def list_switch_machines_hosts(getter, switch_id, session=None, **filters): +def list_switch_machines_hosts(switch_id, user=None, session=None, **filters): """Get switch machines hosts.""" switch_machines = get_switch_machines_internal( session, switch_id=switch_id, **filters ) return _filter_switch_machines_hosts( - session, getter, switch_machines + session, switch_machines ) @@ -506,7 +509,7 @@ def list_switch_machines_hosts(getter, switch_id, session=None, **filters): @user_api.check_user_permission_in_session( permission.PERMISSION_LIST_SWITCH_MACHINES ) -def list_switchmachines_hosts(lister, session=None, **filters): +def list_switchmachines_hosts(user=None, session=None, **filters): """List switch machines hosts.""" switch_machines = get_switch_machines_internal( session, **filters @@ -518,7 +521,7 @@ def list_switchmachines_hosts(lister, session=None, **filters): switch_machine for switch_machine in switch_machines ] return _filter_switch_machines_hosts( - session, lister, filtered_switch_machines + session, filtered_switch_machines ) @@ -534,9 +537,8 @@ def list_switchmachines_hosts(lister, session=None, **filters): ) @utils.wrap_to_dict(RESP_MACHINES_FIELDS) def add_switch_machine( - creator, switch_id, - exception_when_existing=True, - mac=None, session=None, **kwargs + switch_id, exception_when_existing=True, + mac=None, user=None, session=None, **kwargs ): """Add switch machine.""" switch = utils.get_db_object( @@ -566,13 +568,13 @@ def add_switch_machine( permission.PERMISSION_UPDATE_SWITCH_MACHINES ) @utils.wrap_to_dict(RESP_ACTION_FIELDS) -def poll_switch_machines(poller, switch_id, session=None, **kwargs): +def poll_switch_machines(switch_id, user=None, session=None, **kwargs): """poll switch machines.""" from compass.tasks import client as celery_client switch = utils.get_db_object(session, models.Switch, id=switch_id) celery_client.celery.send_task( 'compass.tasks.pollswitch', - (poller.email, switch.ip, switch.credentials) + (user.email, switch.ip, switch.credentials) ) return { 'status': 'action %s sent' % kwargs, @@ -588,8 +590,8 @@ def poll_switch_machines(poller, switch_id, session=None, **kwargs): ) @utils.wrap_to_dict(RESP_MACHINES_FIELDS) def get_switch_machine( - getter, switch_id, machine_id, - exception_when_missing=True, session=None, **kwargs + switch_id, machine_id, exception_when_missing=True, + user=None, session=None, **kwargs ): """get field dict of a switch machine.""" return utils.get_db_object( @@ -606,9 +608,8 @@ def get_switch_machine( ) @utils.wrap_to_dict(RESP_MACHINES_FIELDS) def get_switchmachine( - getter, switch_machine_id, - exception_when_missing=True, session=None, - **kwargs + switch_machine_id, exception_when_missing=True, + user=None, session=None, **kwargs ): """get field dict of a switch machine.""" return utils.get_db_object( @@ -648,7 +649,7 @@ def update_switch_machine_internal( ) @utils.wrap_to_dict(RESP_MACHINES_FIELDS) def update_switch_machine( - updater, switch_id, machine_id, + switch_id, machine_id, user=None, session=None, **kwargs ): """Update switch machine.""" @@ -672,7 +673,7 @@ def update_switch_machine( permission.PERMISSION_ADD_SWITCH_MACHINE ) @utils.wrap_to_dict(RESP_MACHINES_FIELDS) -def update_switchmachine(updater, switch_machine_id, session=None, **kwargs): +def update_switchmachine(switch_machine_id, user=None, session=None, **kwargs): """Update switch machine.""" switch_machine = utils.get_db_object( session, models.SwitchMachine, @@ -701,7 +702,7 @@ def update_switchmachine(updater, switch_machine_id, session=None, **kwargs): ) @utils.wrap_to_dict(RESP_MACHINES_FIELDS) def patch_switch_machine( - updater, switch_id, machine_id, + switch_id, machine_id, user=None, session=None, **kwargs ): """Patch switch machine.""" @@ -731,7 +732,7 @@ def patch_switch_machine( permission.PERMISSION_ADD_SWITCH_MACHINE ) @utils.wrap_to_dict(RESP_MACHINES_FIELDS) -def patch_switchmachine(updater, switch_machine_id, session=None, **kwargs): +def patch_switchmachine(switch_machine_id, user=None, session=None, **kwargs): """Patch switch machine.""" switch_machine = utils.get_db_object( session, models.SwitchMachine, @@ -749,7 +750,10 @@ def patch_switchmachine(updater, switch_machine_id, session=None, **kwargs): permission.PERMISSION_DEL_SWITCH_MACHINE ) @utils.wrap_to_dict(RESP_MACHINES_FIELDS) -def del_switch_machine(deleter, switch_id, machine_id, session=None, **kwargs): +def del_switch_machine( + switch_id, machine_id, user=None, + session=None, **kwargs +): """Delete switch machine by switch id and machine id.""" switch_machine = utils.get_db_object( session, models.SwitchMachine, @@ -777,7 +781,7 @@ def del_switch_machine(deleter, switch_id, machine_id, session=None, **kwargs): permission.PERMISSION_DEL_SWITCH_MACHINE ) @utils.wrap_to_dict(RESP_MACHINES_FIELDS) -def del_switchmachine(deleter, switch_machine_id, session=None, **kwargs): +def del_switchmachine(switch_machine_id, user=None, session=None, **kwargs): """Delete switch machine by switch_machine_id.""" switch_machine = utils.get_db_object( session, models.SwitchMachine, @@ -847,9 +851,8 @@ def _set_machines(session, switch, machines): ) @utils.wrap_to_dict(RESP_MACHINES_FIELDS) def update_switch_machines( - updater, switch_id, - add_machines=[], remove_machines=[], - set_machines=None, session=None, **kwargs + switch_id, add_machines=[], remove_machines=[], + set_machines=None, user=None, session=None, **kwargs ): """update switch machines.""" switch = utils.get_db_object( diff --git a/compass/db/api/user.py b/compass/db/api/user.py index 4600f9c0..2b42e744 100644 --- a/compass/db/api/user.py +++ b/compass/db/api/user.py @@ -101,13 +101,14 @@ def _check_user_permission(session, user, permission): def check_user_permission_in_session(permission): def decorator(func): @functools.wraps(func) - def wrapper(user, *args, **kwargs): - if 'session' in kwargs.keys(): + def wrapper(*args, **kwargs): + if 'user' in kwargs.keys() and 'session' in kwargs.keys(): session = kwargs['session'] + user = kwargs['user'] + _check_user_permission(session, user, permission) + return func(*args, **kwargs) else: - session = args[-1] - _check_user_permission(session, user, permission) - return func(user, *args, **kwargs) + return func(*args, **kwargs) return wrapper return decorator @@ -115,14 +116,18 @@ def check_user_permission_in_session(permission): def check_user_admin(): def decorator(func): @functools.wraps(func) - def wrapper(user, *args, **kwargs): - if not user.is_admin: - raise exception.Forbidden( - 'User %s is not admin.' % ( - user.email + def wrapper(*args, **kwargs): + if 'user' in kwargs.keys(): + user = kwargs['user'] + if not user.is_admin: + raise exception.Forbidden( + 'User %s is not admin.' % ( + user.email + ) ) - ) - return func(user, *args, **kwargs) + return func(*args, **kwargs) + else: + return func(*args, **kwargs) return wrapper return decorator @@ -130,14 +135,18 @@ def check_user_admin(): def check_user_admin_or_owner(): def decorator(func): @functools.wraps(func) - def wrapper(user, user_id, *args, **kwargs): - if not user.is_admin and user.id != user_id: - raise exception.Forbidden( - 'User %s is not admin or the owner of user id %s.' % ( - user.email, user_id + def wrapper(user_id, *args, **kwargs): + if 'user' in kwargs.keys(): + user = kwargs['user'] + if not user.is_admin and user.id != user_id: + raise exception.Forbidden( + 'User %s is not admin or the owner of user id %s.' % ( + user.email, user_id + ) ) - ) - return func(user, user_id, *args, **kwargs) + return func(user_id, *args, **kwargs) + else: + return func(user_id, *args, **kwargs) return wrapper return decorator @@ -266,7 +275,7 @@ def get_user_object_from_token(token, session=None): @database.run_in_session() @utils.wrap_to_dict(RESP_TOKEN_FIELDS) def record_user_token( - user, token, expire_timestamp, session=None + token, expire_timestamp, user=None, session=None ): """record user token in database.""" user_token = utils.get_db_object( @@ -289,7 +298,7 @@ def record_user_token( @utils.supported_filters() @database.run_in_session() @utils.wrap_to_dict(RESP_TOKEN_FIELDS) -def clean_user_token(user, token, session=None): +def clean_user_token(token, user=None, session=None): """clean user token in database.""" return utils.del_db_objects( session, models.UserToken, @@ -302,8 +311,8 @@ def clean_user_token(user, token, session=None): @database.run_in_session() @utils.wrap_to_dict(RESP_FIELDS) def get_user( - getter, user_id, - exception_when_missing=True, session=None, **kwargs + user_id, exception_when_missing=True, + user=None, session=None, **kwargs ): """get field dict of a user.""" return utils.get_db_object( @@ -315,12 +324,12 @@ def get_user( @database.run_in_session() @utils.wrap_to_dict(RESP_FIELDS) def get_current_user( - getter, - exception_when_missing=True, session=None, **kwargs + exception_when_missing=True, user=None, + session=None, **kwargs ): """get field dict of a user.""" return utils.get_db_object( - session, models.User, exception_when_missing, id=getter.id + session, models.User, exception_when_missing, id=user.id ) @@ -330,7 +339,7 @@ def get_current_user( @check_user_admin() @database.run_in_session() @utils.wrap_to_dict(RESP_FIELDS) -def list_users(lister, session=None, **filters): +def list_users(user=None, session=None, **filters): """List fields of all users by some fields.""" return utils.list_db_objects( session, models.User, **filters @@ -347,8 +356,7 @@ def list_users(lister, session=None, **filters): @database.run_in_session() @utils.wrap_to_dict(RESP_FIELDS) def add_user( - creator, - exception_when_existing=True, + exception_when_existing=True, user=None, session=None, **kwargs ): """Create a user and return created user object.""" @@ -361,7 +369,7 @@ def add_user( @check_user_admin() @database.run_in_session() @utils.wrap_to_dict(RESP_FIELDS) -def del_user(deleter, user_id, session=None, **kwargs): +def del_user(user_id, user=None, session=None, **kwargs): """delete a user and return the deleted user object.""" user = utils.get_db_object(session, models.User, id=user_id) return utils.del_db_object(session, user) @@ -374,22 +382,22 @@ def del_user(deleter, user_id, session=None, **kwargs): @utils.input_validates(email=_check_email) @database.run_in_session() @utils.wrap_to_dict(RESP_FIELDS) -def update_user(updater, user_id, session=None, **kwargs): +def update_user(user_id, user=None, session=None, **kwargs): """Update a user and return the updated user object.""" user = utils.get_db_object( session, models.User, id=user_id ) allowed_fields = set() - if updater.is_admin: + if user.is_admin: allowed_fields |= set(ADMIN_UPDATED_FIELDS) - if updater.id == user_id: + if user.id == user_id: allowed_fields |= set(SELF_UPDATED_FIELDS) unsupported_fields = set(kwargs) - allowed_fields if unsupported_fields: # The user is not allowed to update a user. raise exception.Forbidden( 'User %s has no permission to update user %s fields %s.' % ( - updater.email, user.email, unsupported_fields + user.email, user.email, unsupported_fields ) ) return utils.update_db_object(session, user, **kwargs) @@ -399,7 +407,7 @@ def update_user(updater, user_id, session=None, **kwargs): @check_user_admin_or_owner() @database.run_in_session() @utils.wrap_to_dict(PERMISSION_RESP_FIELDS) -def get_permissions(lister, user_id, session=None, **kwargs): +def get_permissions(user_id, user=None, session=None, **kwargs): """List permissions of a user.""" return utils.list_db_objects( session, models.UserPermission, user_id=user_id, **kwargs @@ -411,8 +419,8 @@ def get_permissions(lister, user_id, session=None, **kwargs): @database.run_in_session() @utils.wrap_to_dict(PERMISSION_RESP_FIELDS) def get_permission( - getter, user_id, permission_id, - exception_when_missing=True, session=None, **kwargs + user_id, permission_id, exception_when_missing=True, + user=None, session=None, **kwargs ): """Get a specific user permission.""" return utils.get_db_object( @@ -427,7 +435,7 @@ def get_permission( @check_user_admin_or_owner() @database.run_in_session() @utils.wrap_to_dict(PERMISSION_RESP_FIELDS) -def del_permission(deleter, user_id, permission_id, session=None, **kwargs): +def del_permission(user_id, permission_id, user=None, session=None, **kwargs): """Delete a specific user permission.""" user_permission = utils.get_db_object( session, models.UserPermission, @@ -445,8 +453,8 @@ def del_permission(deleter, user_id, permission_id, session=None, **kwargs): @database.run_in_session() @utils.wrap_to_dict(PERMISSION_RESP_FIELDS) def add_permission( - creator, user_id, - exception_when_missing=True, permission_id=None, session=None + user_id, exception_when_missing=True, + permission_id=None, user=None, session=None ): """Add an user permission.""" return utils.add_db_object( @@ -471,9 +479,8 @@ def _get_permission_filters(permission_ids): @database.run_in_session() @utils.wrap_to_dict(PERMISSION_RESP_FIELDS) def update_permissions( - updater, user_id, - add_permissions=[], remove_permissions=[], - set_permissions=None, session=None, **kwargs + user_id, add_permissions=[], remove_permissions=[], + set_permissions=None, user=None, session=None, **kwargs ): """update user permissions.""" user = utils.get_db_object(session, models.User, id=user_id) diff --git a/compass/db/api/user_log.py b/compass/db/api/user_log.py index 4f57e798..f58693b0 100644 --- a/compass/db/api/user_log.py +++ b/compass/db/api/user_log.py @@ -24,7 +24,7 @@ from compass.db import models SUPPORTED_FIELDS = ['user_email', 'timestamp'] USER_SUPPORTED_FIELDS = ['timestamp'] -RESP_FIELDS = ['user_id', 'logs', 'timestamp'] +RESP_FIELDS = ['user_id', 'action', 'timestamp'] @database.run_in_session() @@ -39,7 +39,7 @@ def log_user_action(user_id, action, session=None): @user_api.check_user_admin_or_owner() @database.run_in_session() @utils.wrap_to_dict(RESP_FIELDS) -def list_user_actions(lister, user_id, session=None, **filters): +def list_user_actions(user_id, user=None, session=None, **filters): """list user actions.""" return utils.list_db_objects( session, models.UserLog, order_by=['timestamp'], @@ -51,7 +51,7 @@ def list_user_actions(lister, user_id, session=None, **filters): @user_api.check_user_admin() @database.run_in_session() @utils.wrap_to_dict(RESP_FIELDS) -def list_actions(lister, session=None, **filters): +def list_actions(user=None, session=None, **filters): """list actions.""" return utils.list_db_objects( session, models.UserLog, order_by=['timestamp'], **filters @@ -62,7 +62,7 @@ def list_actions(lister, session=None, **filters): @user_api.check_user_admin_or_owner() @database.run_in_session() @utils.wrap_to_dict(RESP_FIELDS) -def del_user_actions(deleter, user_id, session=None, **filters): +def del_user_actions(user_id, user=None, session=None, **filters): """delete user actions.""" return utils.del_db_objects( session, models.UserLog, user_id=user_id, **filters @@ -73,7 +73,7 @@ def del_user_actions(deleter, user_id, session=None, **filters): @user_api.check_user_admin() @database.run_in_session() @utils.wrap_to_dict(RESP_FIELDS) -def del_actions(deleter, session=None, **filters): +def del_actions(user=None, session=None, **filters): """delete actions.""" return utils.del_db_objects( session, models.UserLog, **filters diff --git a/compass/tests/actions/update_progress/test_update_progress.py b/compass/tests/actions/update_progress/test_update_progress.py index e9a4bc9e..8ba0523d 100644 --- a/compass/tests/actions/update_progress/test_update_progress.py +++ b/compass/tests/actions/update_progress/test_update_progress.py @@ -85,7 +85,7 @@ class TestProgressCalculator(unittest2.TestCase): self.cluster_id = None # get adapter information - list_adapters = adapter.list_adapters(self.user_object) + list_adapters = adapter.list_adapters(user=self.user_object) for adptr in list_adapters: if ('package_installer' in adptr.keys() and adptr['flavors'] != [] and @@ -102,13 +102,13 @@ class TestProgressCalculator(unittest2.TestCase): #add cluster cluster.add_cluster( - self.user_object, adapter_id=self.adapter_id, os_id=self.os_id, flavor_id=self.flavor_id, - name='test_cluster' + name='test_cluster', + user=self.user_object, ) - list_clusters = cluster.list_clusters(self.user_object) + list_clusters = cluster.list_clusters(user=self.user_object) for list_cluster in list_clusters: if list_cluster['name'] == 'test_cluster': self.cluster_id = list_cluster['id'] @@ -118,51 +118,51 @@ class TestProgressCalculator(unittest2.TestCase): #add switch switch.add_switch( - self.user_object, - ip=SWITCH_IP + ip=SWITCH_IP, + user=self.user_object, ) - list_switches = switch.list_switches(self.user_object) + list_switches = switch.list_switches(user=self.user_object) for list_switch in list_switches: self.switch_id = list_switch['id'] switch.add_switch_machine( - self.user_object, self.switch_id, + user=self.user_object, mac=MACHINE_MAC, port='1' ) #get machine information - list_machines = machine.list_machines(self.user_object) + list_machines = machine.list_machines(user=self.user_object) for list_machine in list_machines: self.machine_id = list_machine['id'] #add cluster host cluster.add_cluster_host( - self.user_object, self.cluster_id, + user=self.user_object, machine_id=self.machine_id, name='test_clusterhost' ) - list_clusterhosts = cluster.list_clusterhosts(self.user_object) + list_clusterhosts = cluster.list_clusterhosts(user=self.user_object) for list_clusterhost in list_clusterhosts: self.host_id = list_clusterhost['host_id'] self.clusterhost_id = list_clusterhost['clusterhost_id'] #add subnet network.add_subnet( - self.user_object, - subnet=SUBNET + subnet=SUBNET, + user=self.user_object, ) list_subnets = network.list_subnets( - self.user_object + user=self.user_object ) for list_subnet in list_subnets: self.subnet_id = list_subnet['id'] #add host network host.add_host_network( - self.user_object, self.host_id, + user=self.user_object, interface='eth0', ip=HOST_IP, subnet_id=self.subnet_id, @@ -171,32 +171,32 @@ class TestProgressCalculator(unittest2.TestCase): #get clusterhost list_clusterhosts = cluster.list_clusterhosts( - self.user_object + user=self.user_object ) for list_clusterhost in list_clusterhosts: self.clusterhost_id = list_clusterhost['id'] #update host state - self.list_hosts = host.list_hosts(self.user_object) + self.list_hosts = host.list_hosts(user=self.user_object) for list_host in self.list_hosts: self.host_id = list_host['id'] self.host_state = host.update_host_state( - self.user_object, self.host_id, + user=self.user_object, state='INSTALLING' ) #update cluster state cluster.update_cluster_state( - self.user_object, self.cluster_id, + user=self.user_object, state='INSTALLING' ) #update clusterhost state cluster.update_clusterhost_state( - self.user_object, self.clusterhost_id, + user=self.user_object, state='INSTALLING' ) @@ -433,8 +433,8 @@ class TestProgressCalculator(unittest2.TestCase): self._file_generator('check_point_1') update_progress.update_progress() clusterhost_state = cluster.get_clusterhost_state( - self.user_object, - self.clusterhost_id + self.clusterhost_id, + user=self.user_object, ) self.assertAlmostEqual( clusterhost_state['percentage'], @@ -446,8 +446,8 @@ class TestProgressCalculator(unittest2.TestCase): self._file_generator('check_point_2') update_progress.update_progress() clusterhost_state = cluster.get_clusterhost_state( - self.user_object, - self.clusterhost_id + self.clusterhost_id, + user=self.user_object, ) self.assertAlmostEqual( clusterhost_state['percentage'], @@ -459,8 +459,8 @@ class TestProgressCalculator(unittest2.TestCase): self._file_generator('check_point_3') update_progress.update_progress() clusterhost_state = cluster.get_clusterhost_state( - self.user_object, - self.clusterhost_id + self.clusterhost_id, + user=self.user_object, ) self.assertAlmostEqual( clusterhost_state['percentage'], @@ -472,8 +472,8 @@ class TestProgressCalculator(unittest2.TestCase): self._file_generator('check_point_4') update_progress.update_progress() clusterhost_state = cluster.get_clusterhost_state( - self.user_object, - self.clusterhost_id + self.clusterhost_id, + user=self.user_object, ) self.assertAlmostEqual( clusterhost_state['percentage'], @@ -485,8 +485,8 @@ class TestProgressCalculator(unittest2.TestCase): self._file_generator('check_point_5') update_progress.update_progress() clusterhost_state = cluster.get_clusterhost_state( - self.user_object, - self.clusterhost_id + self.clusterhost_id, + user=self.user_object, ) self.assertEqual( clusterhost_state['percentage'], diff --git a/compass/tests/api/test_api.py b/compass/tests/api/test_api.py index 88fb4f4d..6f6fa313 100644 --- a/compass/tests/api/test_api.py +++ b/compass/tests/api/test_api.py @@ -375,9 +375,9 @@ class TestClusterAPI(ApiTestCase): ) ) cluster_api.update_cluster_state( - self.user_object, 1, - state='INSTALLING' + state='INSTALLING', + user=self.user_object, ) url = '/clusters/1' return_value = self.delete(url) @@ -577,7 +577,6 @@ class TestSwitchAPI(ApiTestCase): url = '/switches' return_value = self.get(url) resp = json.loads(return_value.get_data()) - print 'list switches: %s' % resp count = len(resp) self.assertEqual(count, 2) self.assertEqual(return_value.status_code, 200) diff --git a/compass/tests/api/test_health_check_api.py b/compass/tests/api/test_health_check_api.py index 146f7db6..e74419a4 100644 --- a/compass/tests/api/test_health_check_api.py +++ b/compass/tests/api/test_health_check_api.py @@ -154,7 +154,7 @@ class TestHealthCheckAPI(ApiTestCase): # Cluster has been deployed successfully. user = models.User.query.filter_by(email='admin@huawei.com').first() cluster_db.update_cluster_state( - user, self.cluster_id, state='SUCCESSFUL' + self.cluster_id, user=user, state='SUCCESSFUL' ) return_value = self.test_client.post(url, data=request_data) self.assertEqual(202, return_value.status_code) diff --git a/compass/tests/db/api/test_adapter_holder.py b/compass/tests/db/api/test_adapter_holder.py index 1be6273a..c9b7e722 100644 --- a/compass/tests/db/api/test_adapter_holder.py +++ b/compass/tests/db/api/test_adapter_holder.py @@ -73,7 +73,7 @@ class AdapterTestCase(unittest2.TestCase): with database.session() as session: adapter_api.add_adapters_internal(session) adapter.load_adapters() - self.adapter_object = adapter.list_adapters(self.user_object) + self.adapter_object = adapter.list_adapters(user=self.user_object) for adapter_obj in self.adapter_object: if adapter_obj['name'] == 'openstack_icehouse': self.adapter_id = adapter_obj['id'] @@ -97,7 +97,7 @@ class TestListAdapters(AdapterTestCase): def test_list_adapters(self): adapters = adapter.list_adapters( - self.user_object + user=self.user_object ) result = [] for item in adapters: @@ -124,8 +124,8 @@ class TestGetAdapter(AdapterTestCase): def test_get_adapter(self): get_adapter = adapter.get_adapter( - self.user_object, - self.adapter_id + self.adapter_id, + user=self.user_object, ) name = None for k, v in get_adapter.items(): diff --git a/compass/tests/db/api/test_cluster.py b/compass/tests/db/api/test_cluster.py index 061b9152..90f65437 100644 --- a/compass/tests/db/api/test_cluster.py +++ b/compass/tests/db/api/test_cluster.py @@ -72,7 +72,7 @@ class ClusterTestCase(unittest2.TestCase): self.cluster_id = None # get adapter information - list_adapters = adapter.list_adapters(self.user_object) + list_adapters = adapter.list_adapters(user=self.user_object) for list_adapter in list_adapters: for supported_os in list_adapter['supported_oses']: self.os_id = supported_os['os_id'] @@ -91,13 +91,13 @@ class ClusterTestCase(unittest2.TestCase): cluster_names = ['test_cluster1', 'test_cluster2'] for cluster_name in cluster_names: cluster.add_cluster( - self.user_object, + user=self.user_object, adapter_id=self.adapter_id, os_id=self.os_id, flavor_id=self.flavor_id, name=cluster_name ) - clusters = cluster.list_clusters(self.user_object) + clusters = cluster.list_clusters(user=self.user_object) self.roles = None for list_cluster in clusters: for item in list_cluster['flavor']['roles']: @@ -163,30 +163,30 @@ class ClusterTestCase(unittest2.TestCase): } # add cluster config cluster.update_cluster_config( - self.user_object, self.cluster_id, + user=self.user_object, os_config=self.os_configs, package_config=self.package_configs ) # add switch switch.add_switch( - self.user_object, + user=self.user_object, ip='172.29.8.40' ) - switches = switch.list_switches(self.user_object) + switches = switch.list_switches(user=self.user_object) self.switch_id = None for item in switches: self.switch_id = item['id'] macs = ['28:6e:d4:46:c4:25', '00:0c:29:bf:eb:1d'] for mac in macs: switch.add_switch_machine( - self.user_object, self.switch_id, + user=self.user_object, mac=mac, port='1' ) # get machine information - machines = machine.list_machines(self.user_object) + machines = machine.list_machines(user=self.user_object) self.machine_ids = [] for item in machines: self.machine_ids.append(item['id']) @@ -195,14 +195,14 @@ class ClusterTestCase(unittest2.TestCase): name = ['newname1', 'newname2'] for i in range(0, 2): cluster.add_cluster_host( - self.user_object, self.cluster_id, + user=self.user_object, machine_id=self.machine_ids[i], name=name[i] ) self.host_id = [] self.clusterhost_id = [] - clusterhosts = cluster.list_clusterhosts(self.user_object) + clusterhosts = cluster.list_clusterhosts(user=self.user_object) for clusterhost in clusterhosts: self.host_id.append(clusterhost['host_id']) self.clusterhost_id.append(clusterhost['clusterhost_id']) @@ -211,9 +211,9 @@ class ClusterTestCase(unittest2.TestCase): file_names = ['log_file1', 'log_file2'] for file_name in file_names: cluster.add_cluster_host_log_history( - self.user_object, self.cluster_id, self.host_id[0], + user=self.user_object, filename=file_name ) @@ -221,11 +221,11 @@ class ClusterTestCase(unittest2.TestCase): subnets = ['10.145.88.0/23', '192.168.100.0/23'] for subnet in subnets: network.add_subnet( - self.user_object, + user=self.user_object, subnet=subnet ) list_subnet = network.list_subnets( - self.user_object + user=self.user_object ) self.subnet_ids = [] for item in list_subnet: @@ -233,24 +233,24 @@ class ClusterTestCase(unittest2.TestCase): # add host network host.add_host_network( - self.user_object, self.host_id[0], + user=self.user_object, interface='eth0', ip='10.145.88.0', subnet_id=self.subnet_ids[0], is_mgmt=True ) host.add_host_network( - self.user_object, self.host_id[0], + user=self.user_object, interface='eth1', ip='10.145.88.10', subnet_id=self.subnet_ids[0], is_promiscuous=True ) host.list_host_networks( - self.user_object, - self.host_id[0] + self.host_id[0], + user=self.user_object, ) def tearDown(self): @@ -267,7 +267,7 @@ class TestListClusters(ClusterTestCase): super(TestListClusters, self).tearDown() def test_list_clusters(self): - clusters = cluster.list_clusters(self.user_object) + clusters = cluster.list_clusters(user=self.user_object) result = [] for list_cluster in clusters: result.append(list_cluster['name']) @@ -288,8 +288,8 @@ class TestGetCluster(ClusterTestCase): def test_get_cluster(self): get_cluster = cluster.get_cluster( - self.user_object, - self.cluster_id + self.cluster_id, + user=self.user_object, ) self.assertIsNotNone(get_cluster) self.assertEqual(get_cluster['name'], 'test_cluster1') @@ -298,8 +298,8 @@ class TestGetCluster(ClusterTestCase): self.assertRaises( exception.RecordNotExists, cluster.get_cluster, - self.user_object, - 99 + 99, + user=self.user_object, ) @@ -314,13 +314,13 @@ class TestAddCluster(ClusterTestCase): def test_add_cluster(self): cluster.add_cluster( - self.user_object, + user=self.user_object, adapter_id=self.adapter_id, os_id=self.os_id, flavor_id=self.flavor_id, name='test_add_cluster' ) - add_clusters = cluster.list_clusters(self.user_object) + add_clusters = cluster.list_clusters(user=self.user_object) result = [] for add_cluster in add_clusters: result.append(add_cluster['name']) @@ -328,14 +328,14 @@ class TestAddCluster(ClusterTestCase): def test_add_cluster_position_args(self): cluster.add_cluster( - self.user_object, True, 'test_add_cluster_position', + user=self.user_object, adapter_id=self.adapter_id, os_id=self.os_id, flavor_id=self.flavor_id, ) - add_clusters = cluster.list_clusters(self.user_object) + add_clusters = cluster.list_clusters(user=self.user_object) result = [] for add_cluster in add_clusters: result.append(add_cluster['name']) @@ -344,36 +344,19 @@ class TestAddCluster(ClusterTestCase): def test_add_cluster_session(self): with database.session() as session: cluster.add_cluster( - self.user_object, + user=self.user_object, adapter_id=self.adapter_id, os_id=self.os_id, flavor_id=self.flavor_id, name='test_add_cluster_session', session=session ) - add_clusters = cluster.list_clusters(self.user_object) + add_clusters = cluster.list_clusters(user=self.user_object) result = [] for add_cluster in add_clusters: result.append(add_cluster['name']) self.assertIn('test_add_cluster_session', result) - def test_add_cluster_position_args_session(self): - with database.session() as session: - cluster.add_cluster( - self.user_object, - True, - 'test_add_cluster_position_session', - session, - adapter_id=self.adapter_id, - os_id=self.os_id, - flavor_id=self.flavor_id, - ) - add_clusters = cluster.list_clusters(self.user_object) - result = [] - for add_cluster in add_clusters: - result.append(add_cluster['name']) - self.assertIn('test_add_cluster_position_session', result) - class TestUpdateCluster(ClusterTestCase): """Test update cluster.""" @@ -386,28 +369,28 @@ class TestUpdateCluster(ClusterTestCase): def test_update_cluster(self): cluster.update_cluster( - self.user_object, self.cluster_id, + user=self.user_object, name='test_update_cluster' ) update_cluster = cluster.get_cluster( - self.user_object, - self.cluster_id + self.cluster_id, + user=self.user_object, ) self.assertEqual(update_cluster['name'], 'test_update_cluster') def test_is_cluster_editable(self): # state is INSTALLING cluster.update_cluster_state( - self.user_object, self.cluster_id, + user=self.user_object, state='INSTALLING' ) self.assertRaises( exception.Forbidden, cluster.update_cluster, - self.user_object, self.cluster_id, + user=self.user_object, name='cluster_editable' ) @@ -415,8 +398,8 @@ class TestUpdateCluster(ClusterTestCase): self.assertRaises( exception.Forbidden, cluster.update_cluster, - self.user_object, self.cluster_id, + user=self.user_object, reinstall_distributed_system=True ) @@ -433,24 +416,28 @@ class TestDelCluster(ClusterTestCase): def test_del_cluster(self): from compass.tasks import client as celery_client celery_client.celery.send_task = mock.Mock() - del_cluster = cluster.del_cluster( - self.user_object, - self.cluster_id + cluster.del_cluster( + self.cluster_id, + user=self.user_object, ) - self.assertIsNotNone(del_cluster) + del_clusters = cluster.list_clusters( + user=self.user_object, + ) + for del_cluster in del_clusters: + self.assertNotEqual(1, del_cluster['id']) def test_is_cluster_editable(self): #state is INSTALLING cluster.update_cluster_state( - self.user_object, self.cluster_id, + user=self.user_object, state='INSTALLING' ) self.assertRaises( exception.Forbidden, cluster.del_cluster, - self.user_object, self.cluster_id, + user=self.user_object, ) @@ -460,8 +447,8 @@ class TestGetClusterConfig(ClusterTestCase): def setUp(self): super(TestGetClusterConfig, self).setUp() cluster.update_cluster_config( - self.user_object, self.cluster_id, + user=self.user_object, os_config=self.os_configs, package_config=self.package_configs ) @@ -471,8 +458,8 @@ class TestGetClusterConfig(ClusterTestCase): def test_get_cluster_config(self): cluster_config = cluster.get_cluster_config( - self.user_object, - self.cluster_id + self.cluster_id, + user=self.user_object, ) package_config = cluster_config['package_config'] os_config = cluster_config['os_config'] @@ -485,27 +472,27 @@ class TestGetClusterDeployedConfig(ClusterTestCase): def setUp(self): super(TestGetClusterDeployedConfig, self).setUp() cluster.update_cluster_config( - self.user_object, self.cluster_id, + user=self.user_object, os_config=self.os_configs, package_config=self.package_configs ) cluster.update_cluster_host( - self.user_object, self.cluster_id, self.host_id[0], + user=self.user_object, roles=['allinone-compute'] ) cluster.review_cluster( - self.user_object, self.cluster_id, + user=self.user_object, review={ 'hosts': [self.host_id[0]] } ) cluster.update_cluster_deployed_config( - self.user_object, self.cluster_id, + user=self.user_object, os_config=self.os_configs, package_config=self.package_configs ) @@ -515,8 +502,8 @@ class TestGetClusterDeployedConfig(ClusterTestCase): def test_get_cluster_deployed_config(self): configs = cluster.get_cluster_deployed_config( - self.user_object, - self.cluster_id + self.cluster_id, + user=self.user_object, ) os_config = configs['deployed_os_config'] package_config = configs['deployed_package_config'] @@ -535,8 +522,8 @@ class TestGetClusterMetadata(ClusterTestCase): def test_get_cluster_metadata(self): cluster_metadata = cluster.get_cluster_metadata( - self.user_object, - self.cluster_id + self.cluster_id, + user=self.user_object, ) results = [] for k, v in cluster_metadata.items(): @@ -558,14 +545,14 @@ class TestUpdateClusterConfig(ClusterTestCase): def test_update_cluster_config(self): cluster.update_cluster_config( - self.user_object, self.cluster_id, + user=self.user_object, put_os_config=self.os_configs, put_package_config=self.package_configs ) update_cluster_config = cluster.get_cluster_config( - self.user_object, - self.cluster_id + self.cluster_id, + user=self.user_object, ) package_config = update_cluster_config['package_config'] os_config = update_cluster_config['os_config'] @@ -584,8 +571,8 @@ class TestPatchClusterConfig(ClusterTestCase): def test_patch_cluster_config(self): patch_cluster_config = cluster.patch_cluster_config( - self.user_object, self.cluster_id, + user=self.user_object, package_config=self.package_configs, os_config=self.os_configs ) @@ -601,8 +588,8 @@ class TestDelClusterConfig(ClusterTestCase): def setUp(self): super(TestDelClusterConfig, self).setUp() cluster.update_cluster_config( - self.user_object, self.cluster_id, + user=self.user_object, os_config=self.os_configs, package_config=self.package_configs ) @@ -612,12 +599,12 @@ class TestDelClusterConfig(ClusterTestCase): def test_del_cluster_config(self): cluster.del_cluster_config( - self.user_object, - self.cluster_id + self.cluster_id, + user=self.user_object, ) del_cluster_config = cluster.get_cluster_config( - self.user_object, - self.cluster_id + self.cluster_id, + user=self.user_object, ) configs = [] for k, v in del_cluster_config.items(): @@ -628,15 +615,15 @@ class TestDelClusterConfig(ClusterTestCase): def test_cluster_editable(self): cluster.update_cluster_state( - self.user_object, self.cluster_id, + user=self.user_object, state='INSTALLING' ) self.assertRaises( exception.Forbidden, cluster.del_cluster_config, - self.user_object, - self.cluster_id + self.cluster_id, + user=self.user_object, ) @@ -651,8 +638,8 @@ class TestListClusterHosts(ClusterTestCase): def test_list_cluster_hosts(self): list_cluster_hosts = cluster.list_cluster_hosts( - self.user_object, - self.cluster_id + self.cluster_id, + user=self.user_object, ) results = [] expected = ['28:6e:d4:46:c4:25', '00:0c:29:bf:eb:1d'] @@ -672,7 +659,7 @@ class TestListClusterhosts(ClusterTestCase): super(TestListClusterhosts, self).tearDown() def test_list_clusterhosts(self): - list_clusterhosts = cluster.list_clusterhosts(self.user_object) + list_clusterhosts = cluster.list_clusterhosts(user=self.user_object) results = [] expected = ['28:6e:d4:46:c4:25', '00:0c:29:bf:eb:1d'] for item in list_clusterhosts: @@ -692,9 +679,9 @@ class TestGetClusterHost(ClusterTestCase): def test_get_cluster_host(self): get_cluster_host = cluster.get_cluster_host( - self.user_object, self.cluster_id, - self.host_id[1] + self.host_id[1], + user=self.user_object, ) self.assertEqual(get_cluster_host['mac'], '00:0c:29:bf:eb:1d') @@ -710,8 +697,8 @@ class TestGetClusterhost(ClusterTestCase): def test_get_clusterhost(self): get_clusterhost = cluster.get_clusterhost( - self.user_object, - self.clusterhost_id[1] + self.clusterhost_id[1], + user=self.user_object, ) self.assertEqual(get_clusterhost['mac'], '00:0c:29:bf:eb:1d') @@ -722,12 +709,12 @@ class TestAddClusterHost(ClusterTestCase): def setUp(self): super(TestAddClusterHost, self).setUp() switch.add_switch_machine( - self.user_object, self.switch_id, + user=self.user_object, mac='00:0c:29:5b:ee:eb', port='1' ) - machines = machine.list_machines(self.user_object) + machines = machine.list_machines(user=self.user_object) self.add_machine_id = None for item in machines: if item['mac'] == '00:0c:29:5b:ee:eb': @@ -739,29 +726,39 @@ class TestAddClusterHost(ClusterTestCase): def test_add_cluster_host(self): # add a cluster_host cluster.add_cluster_host( - self.user_object, self.cluster_id, + user=self.user_object, machine_id=self.add_machine_id, name='test_add_cluster_host' ) - add_cluster_hosts = cluster.list_clusterhosts(self.user_object) - result = [] - for item in add_cluster_hosts: - result.append(item['mac']) - self.assertIn('00:0c:29:5b:ee:eb', result) + add_cluster_hosts = cluster.list_clusterhosts(user=self.user_object) + expected = { + 'clusterhost_id': 3, + 'cluster_id': 1, + 'id': 3, + 'switch_ip': '172.29.8.40', + 'hostname': 'test_add_cluster_host', + 'owner': 'admin@huawei.com', + 'mac': '00:0c:29:5b:ee:eb', + 'host_id': 3, + 'name': 'test_add_cluster_host.test_cluster1', + } + self.assertTrue( + all(item in add_cluster_hosts[2].items() + for item in expected.items())) def test_is_cluster_editable(self): # installing cluster.update_cluster_state( - self.user_object, self.cluster_id, + user=self.user_object, state='INSTALLING' ) self.assertRaises( exception.Forbidden, cluster.add_cluster_host, - self.user_object, self.cluster_id, + user=self.user_object, machine_id=self.add_machine_id ) @@ -777,14 +774,14 @@ class TestUpdateClusterHost(ClusterTestCase): def test_update_cluster_host(self): cluster.update_cluster_host( - self.user_object, self.cluster_id, self.host_id[0], + user=self.user_object, roles=['allinone-compute'] ) update_cluster_hosts = cluster.list_cluster_hosts( - self.user_object, - self.cluster_id + self.cluster_id, + user=self.user_object, ) result = None for item in update_cluster_hosts: @@ -796,25 +793,25 @@ class TestUpdateClusterHost(ClusterTestCase): self.assertRaises( exception.InvalidParameter, cluster.update_cluster_host, - self.user_object, self.cluster_id, self.host_id[0], + user=self.user_object, roles=['invalid_role'] ) def test_is_cluster_editable(self): # state is INSTALLING cluster.update_cluster_state( - self.user_object, self.cluster_id, + user=self.user_object, state='INSTALLING' ) self.assertRaises( exception.Forbidden, cluster.update_cluster_host, - self.user_object, self.cluster_id, self.host_id[0], + user=self.user_object, ) @@ -829,12 +826,12 @@ class TestUpdateClusterhost(ClusterTestCase): def test_update_clusterhost(self): cluster.update_clusterhost( - self.user_object, self.clusterhost_id[0], + user=self.user_object, roles=['allinone-compute'] ) update_clusterhosts = cluster.list_clusterhosts( - self.user_object, + user=self.user_object, ) result = None for item in update_clusterhosts: @@ -846,23 +843,23 @@ class TestUpdateClusterhost(ClusterTestCase): self.assertRaises( exception.InvalidParameter, cluster.update_clusterhost, - self.user_object, self.clusterhost_id[0], + user=self.user_object, roles=['invalid_role'] ) def test_is_cluster_editable(self): # state is INSTALLING cluster.update_cluster_state( - self.user_object, self.cluster_id, + user=self.user_object, state='INSTALLING' ) self.assertRaises( exception.Forbidden, cluster.update_clusterhost, - self.user_object, - self.clusterhost_id[0] + self.clusterhost_id[0], + user=self.user_object, ) @@ -876,14 +873,14 @@ class TestPatchClusterHost(ClusterTestCase): def test_patch_cluster_host(self): cluster.patch_cluster_host( - self.user_object, self.cluster_id, self.host_id[0], + user=self.user_object, roles=['allinone-compute'] ) patch = cluster.list_cluster_hosts( - self.user_object, - self.cluster_id + self.cluster_id, + user=self.user_object, ) result = None for item in patch: @@ -893,16 +890,16 @@ class TestPatchClusterHost(ClusterTestCase): def test_is_cluster_editable(self): cluster.update_cluster_state( - self.user_object, self.cluster_id, + user=self.user_object, state='INSTALLING' ) self.assertRaises( exception.Forbidden, cluster.patch_cluster_host, - self.user_object, self.cluster_id, - self.host_id[0] + self.host_id[0], + user=self.user_object, ) @@ -915,13 +912,13 @@ class TestPatchClusterhost(ClusterTestCase): def test_patch_clusterhost(self): cluster.patch_clusterhost( - self.user_object, self.clusterhost_id[0], + user=self.user_object, roles=['allinone-compute'] ) patch = cluster.list_cluster_hosts( - self.user_object, - self.cluster_id + self.cluster_id, + user=self.user_object, ) result = None for item in patch: @@ -931,15 +928,15 @@ class TestPatchClusterhost(ClusterTestCase): def testi_is_cluster_editable(self): cluster.update_cluster_state( - self.user_object, self.cluster_id, + user=self.user_object, state='INSTALLING' ) self.assertRaises( exception.Forbidden, cluster.patch_clusterhost, - self.user_object, - self.clusterhost_id[0] + self.clusterhost_id[0], + user=self.user_object, ) @@ -955,25 +952,30 @@ class TestDelClusterHost(ClusterTestCase): def test_del_cluster_host(self): from compass.tasks import client as celery_client celery_client.celery.send_task = mock.Mock() - del_clusterhost = cluster.del_cluster_host( - self.user_object, + cluster.del_cluster_host( self.cluster_id, - self.host_id[0] + self.host_id[0], + user=self.user_object, ) - self.assertIsNotNone(del_clusterhost) + del_cluster_hosts = cluster.list_cluster_hosts( + self.cluster_id, + user=self.user_object, + ) + for del_cluster_host in del_cluster_hosts: + self.assertNotEqual(del_cluster_host['id'], 1) def test_is_cluster_editable(self): cluster.update_cluster_state( - self.user_object, self.cluster_id, + user=self.user_object, state='INSTALLING' ) self.assertRaises( exception.Forbidden, cluster.del_cluster_host, - self.user_object, self.cluster_id, - self.host_id[0] + self.host_id[0], + user=self.user_object, ) @@ -989,23 +991,27 @@ class TestDelClusterhost(ClusterTestCase): def test_del_clusterhost(self): from compass.tasks import client as celery_client celery_client.celery.send_task = mock.Mock() - del_clusterhost = cluster.del_clusterhost( - self.user_object, - self.clusterhost_id[0] + cluster.del_clusterhost( + self.clusterhost_id[0], + user=self.user_object, ) - self.assertIsNotNone(del_clusterhost) + del_clusterhosts = cluster.list_clusterhosts( + user=self.user_object, + ) + for del_clusterhost in del_clusterhosts: + self.assertNotEqual(del_clusterhost['id'], 1) def test_is_cluster_editable(self): cluster.update_cluster_state( - self.user_object, self.cluster_id, + user=self.user_object, state='INSTALLING' ) self.assertRaises( exception.Forbidden, cluster.del_clusterhost, - self.user_object, - self.clusterhost_id[0] + self.clusterhost_id[0], + user=self.user_object, ) @@ -1015,9 +1021,9 @@ class TestGetClusterHostConfig(ClusterTestCase): def setUp(self): super(TestGetClusterHostConfig, self).setUp() cluster.update_cluster_host_config( - self.user_object, self.cluster_id, self.host_id[0], + user=self.user_object, os_config=self.os_configs, package_config=self.package_configs ) @@ -1027,9 +1033,9 @@ class TestGetClusterHostConfig(ClusterTestCase): def test_get_cluster_host_config(self): configs = cluster.get_cluster_host_config( - self.user_object, self.cluster_id, - self.host_id[0] + self.host_id[0], + user=self.user_object, ) package_config = configs['package_config'] os_config = configs['os_config'] @@ -1043,8 +1049,8 @@ class TestGetClusterhostConfig(ClusterTestCase): def setUp(self): super(TestGetClusterhostConfig, self).setUp() cluster.update_clusterhost_config( - self.user_object, self.clusterhost_id[0], + user=self.user_object, os_config=self.os_configs, package_config=self.package_configs ) @@ -1054,8 +1060,8 @@ class TestGetClusterhostConfig(ClusterTestCase): def test_get_clusterhost_config(self): configs = cluster.get_clusterhost_config( - self.user_object, - self.clusterhost_id[0] + self.clusterhost_id[0], + user=self.user_object, ) package_config = configs['package_config'] os_config = configs['os_config'] @@ -1068,29 +1074,29 @@ class TestGetClusterHostDeployedConfig(ClusterTestCase): def setUp(self): super(TestGetClusterHostDeployedConfig, self).setUp() cluster.update_cluster_host_config( - self.user_object, self.cluster_id, self.host_id[0], + user=self.user_object, os_config=self.os_configs, package_config=self.package_configs ) cluster.update_cluster_host( - self.user_object, self.cluster_id, self.host_id[0], + user=self.user_object, roles=['allinone-compute'] ) cluster.review_cluster( - self.user_object, self.cluster_id, + user=self.user_object, review={ 'hosts': [self.host_id[0]] } ) cluster.update_cluster_host_deployed_config( - self.user_object, self.cluster_id, self.host_id[0], + user=self.user_object, os_config=self.os_configs, package_config=self.package_configs ) @@ -1100,9 +1106,9 @@ class TestGetClusterHostDeployedConfig(ClusterTestCase): def test_get_cluster_host_deployed_config(self): configs = cluster.get_cluster_host_deployed_config( - self.user_object, self.cluster_id, - self.host_id[0] + self.host_id[0], + user=self.user_object, ) package_config = configs['deployed_package_config'] os_config = configs['deployed_os_config'] @@ -1116,26 +1122,26 @@ class TestGetClusterhostDeployedConfig(ClusterTestCase): def setUp(self): super(TestGetClusterhostDeployedConfig, self).setUp() cluster.update_clusterhost_config( - self.user_object, self.clusterhost_id[0], + user=self.user_object, os_config=self.os_configs, package_config=self.package_configs ) cluster.update_clusterhost( - self.user_object, self.clusterhost_id[0], + user=self.user_object, roles=['allinone-compute'] ) cluster.review_cluster( - self.user_object, self.cluster_id, + user=self.user_object, review={ 'hosts': [self.host_id[0]] } ) cluster.update_clusterhost_deployed_config( - self.user_object, self.clusterhost_id[0], + user=self.user_object, os_config=self.os_configs, package_config=self.package_configs ) @@ -1145,8 +1151,8 @@ class TestGetClusterhostDeployedConfig(ClusterTestCase): def test_get_clusterhost_deployed_config(self): configs = cluster.get_clusterhost_deployed_config( - self.user_object, - self.clusterhost_id[0] + self.clusterhost_id[0], + user=self.user_object, ) package_config = configs['deployed_package_config'] os_config = configs['deployed_os_config'] @@ -1165,16 +1171,16 @@ class TestUpdateClusterHostConfig(ClusterTestCase): def test_update_cluster_host_config(self): cluster.update_cluster_host_config( - self.user_object, self.cluster_id, self.host_id[0], + user=self.user_object, os_config=self.os_configs, package_config=self.package_configs ) config = cluster.get_cluster_host_config( - self.user_object, self.cluster_id, - self.host_id[0] + self.host_id[0], + user=self.user_object, ) package_configs = config['package_config'] os_configs = config['os_config'] @@ -1183,16 +1189,16 @@ class TestUpdateClusterHostConfig(ClusterTestCase): def test_is_cluster_editable(self): cluster.update_cluster_state( - self.user_object, self.cluster_id, + user=self.user_object, state='INSTALLING' ) self.assertRaises( exception.Forbidden, cluster.update_cluster_host_config, - self.user_object, self.cluster_id, self.host_id[0], + user=self.user_object, os_config=self.os_configs, package_config=self.package_configs ) @@ -1204,20 +1210,20 @@ class TestUpdateClusterHostDeployedConfig(ClusterTestCase): def setUp(self): super(TestUpdateClusterHostDeployedConfig, self).setUp() cluster.update_cluster_host_config( - self.user_object, self.cluster_id, self.host_id[0], + user=self.user_object, os_config=self.os_configs, package_config=self.package_configs ) cluster.update_clusterhost( - self.user_object, self.clusterhost_id[0], + user=self.user_object, roles=['allinone-compute'] ) cluster.review_cluster( - self.user_object, self.cluster_id, + user=self.user_object, review={ 'clusterhosts': [self.clusterhost_id[0]] } @@ -1228,16 +1234,16 @@ class TestUpdateClusterHostDeployedConfig(ClusterTestCase): def test_udpate_cluster_host_deployed_config(self): cluster.update_cluster_host_deployed_config( - self.user_object, self.cluster_id, self.host_id[0], + user=self.user_object, os_config=self.os_configs, package_config=self.package_configs ) configs = cluster.get_cluster_host_deployed_config( - self.user_object, self.cluster_id, - self.host_id[0] + self.host_id[0], + user=self.user_object, ) package_config = configs['deployed_package_config'] os_config = configs['deployed_os_config'] @@ -1256,14 +1262,14 @@ class TestUpdateClusterhostConfig(ClusterTestCase): def test_update_clusterhost_config(self): cluster.update_clusterhost_config( - self.user_object, self.clusterhost_id[0], + user=self.user_object, os_config=self.os_configs, package_config=self.package_configs ) configs = cluster.get_clusterhost_config( - self.user_object, - self.clusterhost_id[0] + self.clusterhost_id[0], + user=self.user_object, ) package_config = configs['package_config'] os_config = configs['os_config'] @@ -1272,15 +1278,15 @@ class TestUpdateClusterhostConfig(ClusterTestCase): def test_id_cluster_editable(self): cluster.update_cluster_state( - self.user_object, self.cluster_id, + user=self.user_object, state='INSTALLING' ) self.assertRaises( exception.Forbidden, cluster.update_clusterhost_config, - self.user_object, self.clusterhost_id[0], + user=self.user_object, os_config=self.os_configs, package_config=self.package_configs ) @@ -1292,20 +1298,20 @@ class TestUpdateClusterhostDeployedConfig(ClusterTestCase): def setUp(self): super(TestUpdateClusterhostDeployedConfig, self).setUp() cluster.update_clusterhost_config( - self.user_object, self.clusterhost_id[0], + user=self.user_object, os_config=self.os_configs, package_config=self.package_configs ) cluster.update_cluster_host( - self.user_object, self.cluster_id, self.host_id[0], + user=self.user_object, roles=['allinone-compute'] ) cluster.review_cluster( - self.user_object, self.cluster_id, + user=self.user_object, review={ 'clusterhosts': [self.clusterhost_id[0]] } @@ -1316,14 +1322,14 @@ class TestUpdateClusterhostDeployedConfig(ClusterTestCase): def test_update_clusterhost_config(self): cluster.update_clusterhost_deployed_config( - self.user_object, self.clusterhost_id[0], + user=self.user_object, os_config=self.os_configs, package_config=self.package_configs ) configs = cluster.get_clusterhost_deployed_config( - self.user_object, - self.clusterhost_id[0] + self.clusterhost_id[0], + user=self.user_object, ) package_config = configs['deployed_package_config'] os_config = configs['deployed_os_config'] @@ -1342,16 +1348,16 @@ class TestPatchClusterHostConfig(ClusterTestCase): def test_patch_cluster_host_config(self): cluster.patch_cluster_host_config( - self.user_object, self.cluster_id, self.host_id[0], + user=self.user_object, os_config=self.os_configs, package_config=self.package_configs ) configs = cluster.get_cluster_host_config( - self.user_object, self.cluster_id, - self.host_id[0] + self.host_id[0], + user=self.user_object, ) package_config = configs['package_config'] os_config = configs['os_config'] @@ -1360,16 +1366,16 @@ class TestPatchClusterHostConfig(ClusterTestCase): def test_is_cluster_editable(self): cluster.update_cluster_state( - self.user_object, self.cluster_id, + user=self.user_object, state='INSTALLING' ) self.assertRaises( exception.Forbidden, cluster.patch_cluster_host_config, - self.user_object, self.cluster_id, self.host_id[0], + user=self.user_object, os_config=self.os_configs, package_config=self.package_configs ) @@ -1386,14 +1392,14 @@ class TestPatchClusterhostConfig(ClusterTestCase): def test_patch_clusterhost_config(self): cluster.patch_clusterhost_config( - self.user_object, self.clusterhost_id[0], + user=self.user_object, os_config=self.os_configs, package_config=self.package_configs ) config = cluster.get_clusterhost_config( - self.user_object, - self.clusterhost_id[0] + self.clusterhost_id[0], + user=self.user_object, ) package_config = config['package_config'] os_config = config['os_config'] @@ -1402,15 +1408,15 @@ class TestPatchClusterhostConfig(ClusterTestCase): def test_is_cluster_editable(self): cluster.update_cluster_state( - self.user_object, self.cluster_id, + user=self.user_object, state='INSTALLING' ) self.assertRaises( exception.Forbidden, cluster.patch_clusterhost_config, - self.user_object, self.clusterhost_id[0], + user=self.user_object, os_config=self.os_configs, package_config=self.package_configs ) @@ -1422,9 +1428,9 @@ class TestDeleteClusterHostConfig(ClusterTestCase): def setUp(self): super(TestDeleteClusterHostConfig, self).setUp() cluster.update_cluster_host_config( - self.user_object, self.cluster_id, self.host_id[0], + user=self.user_object, os_config=self.os_configs, package_config=self.package_configs ) @@ -1434,14 +1440,14 @@ class TestDeleteClusterHostConfig(ClusterTestCase): def test_delete_cluster_host_config(self): cluster.delete_cluster_host_config( - self.user_object, self.cluster_id, self.host_id[0], + user=self.user_object, ) del_cluster_host_config = cluster.get_cluster_host_config( - self.user_object, self.cluster_id, - self.host_id[0] + self.host_id[0], + user=self.user_object, ) configs = [] for k, v in del_cluster_host_config.items(): @@ -1452,16 +1458,16 @@ class TestDeleteClusterHostConfig(ClusterTestCase): def test_is_cluster_editable(self): cluster.update_cluster_state( - self.user_object, self.cluster_id, + user=self.user_object, state='INSTALLING' ) self.assertRaises( exception.Forbidden, cluster.delete_cluster_host_config, - self.user_object, self.cluster_id, - self.host_id[0] + self.host_id[0], + user=self.user_object, ) @@ -1471,8 +1477,8 @@ class TestDeleteClusterhostConfig(ClusterTestCase): def setUp(self): super(TestDeleteClusterhostConfig, self).setUp() cluster.update_clusterhost_config( - self.user_object, self.clusterhost_id[0], + user=self.user_object, os_config=self.os_configs, package_config=self.package_configs ) @@ -1482,12 +1488,12 @@ class TestDeleteClusterhostConfig(ClusterTestCase): def test_delete_clusterhost_config(self): cluster.delete_clusterhost_config( - self.user_object, - self.clusterhost_id[0] + self.clusterhost_id[0], + user=self.user_object, ) del_clusterhost_config = cluster.get_clusterhost_config( - self.user_object, - self.clusterhost_id[0] + self.clusterhost_id[0], + user=self.user_object, ) configs = [] for k, v in del_clusterhost_config.items(): @@ -1498,15 +1504,15 @@ class TestDeleteClusterhostConfig(ClusterTestCase): def test_is_cluster_editable(self): cluster.update_cluster_state( - self.user_object, self.cluster_id, + user=self.user_object, state='INSTALLING' ) self.assertRaises( exception.Forbidden, cluster.delete_clusterhost_config, - self.user_object, - self.clusterhost_id[0] + self.clusterhost_id[0], + user=self.user_object, ) @@ -1516,8 +1522,8 @@ class TestUpdateClusterHosts(ClusterTestCase): def setUp(self): super(TestUpdateClusterHosts, self).setUp() switch.add_switch_machine( - self.user_object, self.switch_id, + user=self.user_object, mac='00:0c:29:5b:ee:eb', port='1' ) @@ -1533,13 +1539,13 @@ class TestUpdateClusterHosts(ClusterTestCase): def test_update_cluster_hosts(self): # remove host cluster.update_cluster_hosts( - self.user_object, self.cluster_id, + user=self.user_object, remove_hosts={'hosts': self.host_id[0]} ) remove_hosts = cluster.list_cluster_hosts( - self.user_object, - self.cluster_id + self.cluster_id, + user=self.user_object, ) result = None for item in remove_hosts: @@ -1548,13 +1554,13 @@ class TestUpdateClusterHosts(ClusterTestCase): #add host cluster.update_cluster_hosts( - self.user_object, self.cluster_id, + user=self.user_object, add_hosts={'machines': [{'machine_id': self.add_machine_id}]} ) add_hosts = cluster.list_cluster_hosts( - self.user_object, - self.cluster_id + self.cluster_id, + user=self.user_object, ) result = None for item in add_hosts: @@ -1569,15 +1575,15 @@ class TestReviewCluster(ClusterTestCase): def setUp(self): super(TestReviewCluster, self).setUp() cluster.update_clusterhost_config( - self.user_object, self.clusterhost_id[0], + user=self.user_object, os_config=self.os_configs, package_config=self.package_configs ) cluster.update_cluster_host( - self.user_object, self.cluster_id, self.host_id[0], + user=self.user_object, roles=['allinone-compute'] ) @@ -1586,8 +1592,8 @@ class TestReviewCluster(ClusterTestCase): def test_review_cluster(self): review_cluster = cluster.review_cluster( - self.user_object, self.cluster_id, + user=self.user_object, review={ 'hosts': [self.host_id[0]] } @@ -1619,20 +1625,20 @@ class TestDeployedCluster(ClusterTestCase): def setUp(self): super(TestDeployedCluster, self).setUp() cluster.update_clusterhost_config( - self.user_object, self.clusterhost_id[0], + user=self.user_object, os_config=self.os_configs, package_config=self.package_configs ) cluster.update_cluster_host( - self.user_object, self.cluster_id, self.host_id[0], + user=self.user_object, roles=['allinone-compute'] ) cluster.review_cluster( - self.user_object, self.cluster_id, + user=self.user_object, review={ 'hosts': [self.host_id[0]], 'clusterhosts': [self.clusterhost_id[0]] @@ -1646,8 +1652,8 @@ class TestDeployedCluster(ClusterTestCase): from compass.tasks import client as celery_client celery_client.celery.send_task = mock.Mock() deploy_cluster = cluster.deploy_cluster( - self.user_object, self.cluster_id, + user=self.user_object, deploy={ 'hosts': [self.host_id[0]], } @@ -1661,17 +1667,17 @@ class TestDeployedCluster(ClusterTestCase): cluster_os_config = v self.assertItemsEqual(cluster_package_config, self.package_configs) self.assertItemsEqual(cluster_os_config, self.os_configs) - expecteds = { + expected = { + 'ip': '10.145.88.0', 'clusterhost_id': self.clusterhost_id[0], 'cluster_id': self.cluster_id, 'hostname': 'newname1', 'mac': '28:6e:d4:46:c4:25', 'clustername': 'test_cluster1' } - result = None - for item in deploy_cluster['hosts']: - result = item - self.assertDictContainsSubset(expecteds, result) + self.assertTrue( + all(item in deploy_cluster['hosts'][0].items() + for item in expected.items())) class TestGetClusterState(ClusterTestCase): @@ -1685,8 +1691,8 @@ class TestGetClusterState(ClusterTestCase): def test_get_cluster_state(self): cluster_state = cluster.get_cluster_state( - self.user_object, - self.cluster_id + self.cluster_id, + user=self.user_object, ) self.assertEqual(cluster_state['state'], 'UNINITIALIZED') @@ -1702,9 +1708,9 @@ class TestGetClusterHostState(ClusterTestCase): def test_get_cluster_host_state(self): cluster_host_state = cluster.get_cluster_host_state( - self.user_object, self.cluster_id, - self.host_id[0] + self.host_id[0], + user=self.user_object, ) self.assertEqual(cluster_host_state['state'], 'UNINITIALIZED') @@ -1720,9 +1726,9 @@ class TestGetClusterHostSelfState(ClusterTestCase): def test_get_cluster_host_self_state(self): cluster_host_self_state = cluster.get_cluster_host_self_state( - self.user_object, self.cluster_id, - self.host_id + self.host_id, + user=self.user_object, ) self.assertEqual(cluster_host_self_state['state'], 'UNINITIALIZED') @@ -1738,8 +1744,8 @@ class TestGetClusterhostState(ClusterTestCase): def test_get_clusterhost_state(self): clusterhost_state = cluster.get_clusterhost_state( - self.user_object, self.clusterhost_id[0], + user=self.user_object, ) self.assertEqual(clusterhost_state['state'], 'UNINITIALIZED') @@ -1755,8 +1761,8 @@ class TestGetClusterhostSelfState(ClusterTestCase): def test_get_clusterhost_state(self): clusterhost_state = cluster.get_clusterhost_self_state( - self.user_object, - self.clusterhost_id[0] + self.clusterhost_id[0], + user=self.user_object, ) self.assertEqual(clusterhost_state['state'], 'UNINITIALIZED') @@ -1772,15 +1778,15 @@ class TestUpdateClusterHostState(ClusterTestCase): def test_update_cluster_host_state(self): cluster.update_cluster_host_state( - self.user_object, self.cluster_id, self.host_id, + user=self.user_object, state='INSTALLING' ) update_state = cluster.get_cluster_host_state( - self.user_object, self.cluster_id, - self.host_id + self.host_id, + user=self.user_object, ) self.assertEqual(update_state['state'], 'INSTALLING') @@ -1796,13 +1802,13 @@ class TestUpdateClusterhostState(ClusterTestCase): def test_update_clusterhost_state(self): cluster.update_clusterhost_state( - self.user_object, self.clusterhost_id[0], + user=self.user_object, state='INSTALLING' ) clusterhost_state = cluster.get_clusterhost_state( - self.user_object, - self.clusterhost_id[0] + self.clusterhost_id[0], + user=self.user_object, ) self.assertEqual(clusterhost_state['state'], 'INSTALLING') @@ -1818,13 +1824,13 @@ class TestUpdateClusterState(ClusterTestCase): def test_update_cluster_state(self): cluster.update_cluster_state( - self.user_object, self.cluster_id, + user=self.user_object, state='INSTALLING' ) cluster_state = cluster.get_cluster_state( - self.user_object, - self.cluster_id + self.cluster_id, + user=self.user_object, ) self.assertEqual(cluster_state['state'], 'INSTALLING') @@ -1840,9 +1846,9 @@ class TestGetClusterHostLogHistories(ClusterTestCase): def test_get_cluster_host_log_histories(self): logs = cluster.get_cluster_host_log_histories( - self.user_object, self.cluster_id, - self.host_id[0] + self.host_id[0], + user=self.user_object, ) result = [] for log in logs: @@ -1862,8 +1868,8 @@ class TestGetClusterhostLogHistories(ClusterTestCase): def test_get_clusterhost_log_histories(self): logs = cluster.get_clusterhost_log_histories( - self.user_object, self.clusterhost_id[0], + user=self.user_object, ) result = [] for log in logs: @@ -1883,10 +1889,10 @@ class TestGetClusterHostLogHistory(ClusterTestCase): def test_get_cluster_host_log_history(self): log = cluster.get_cluster_host_log_history( - self.user_object, self.cluster_id, self.host_id[0], - 'log_file1' + 'log_file1', + user=self.user_object, ) self.assertEqual(log['filename'], 'log_file1') @@ -1902,9 +1908,9 @@ class TestGetClusterhostLogHistory(ClusterTestCase): def test_get_clusterhost_log_history(self): log = cluster.get_clusterhost_log_history( - self.user_object, self.clusterhost_id[0], - 'log_file1' + 'log_file1', + user=self.user_object, ) self.assertEqual(log['filename'], 'log_file1') @@ -1920,20 +1926,30 @@ class TestUpdateClusterHostLogHistory(ClusterTestCase): def test_update_cluster_host_log_history(self): cluster.update_cluster_host_log_history( - self.user_object, self.cluster_id, self.host_id[0], 'log_file1', + user=self.user_object, severity='WARNING', message='test update cluster host log history.' ) update_log = cluster.get_cluster_host_log_history( - self.user_object, self.cluster_id, self.host_id[0], - 'log_file1' + 'log_file1', + user=self.user_object, ) - self.assertEqual(update_log['severity'], 'WARNING') + expected = { + 'severity': 'WARNING', + 'clusterhost_id': 1, + 'filename': 'log_file1', + 'cluster_id': 1, + 'host_id': 1, + 'message': 'test update cluster host log history.' + } + self.assertTrue( + all(item in update_log.items() + for item in expected.items())) class TestUpdateClusterhostLogHistory(ClusterTestCase): @@ -1947,18 +1963,28 @@ class TestUpdateClusterhostLogHistory(ClusterTestCase): def test_update_clusterhost_log_history(self): cluster.update_clusterhost_log_history( - self.user_object, self.clusterhost_id[0], 'log_file1', + user=self.user_object, severity='WARNING', message='test update clusterhost log history.' ) update_log = cluster.get_clusterhost_log_history( - self.user_object, self.clusterhost_id[0], - 'log_file1' + 'log_file1', + user=self.user_object, + ) + expected = { + 'severity': 'WARNING', + 'clusterhost_id': 1, + 'filename': 'log_file1', + 'cluster_id': 1, + 'host_id': 1, + 'message': 'test update clusterhost log history.' + } + self.assertTrue( + all(item in update_log.items() for item in expected.items()) ) - self.assertEqual(update_log['severity'], 'WARNING') class TestAddClusterhostLogHistory(ClusterTestCase): @@ -1972,13 +1998,13 @@ class TestAddClusterhostLogHistory(ClusterTestCase): def test_add_clusterhost_log_history(self): cluster.add_clusterhost_log_history( - self.user_object, self.clusterhost_id[0], + user=self.user_object, filename='add_log_file' ) logs = cluster.get_clusterhost_log_histories( - self.user_object, - self.clusterhost_id[0] + self.clusterhost_id[0], + user=self.user_object, ) result = [] for log in logs: @@ -1987,14 +2013,14 @@ class TestAddClusterhostLogHistory(ClusterTestCase): def test_add_clusterhost_log_history_position_args(self): cluster.add_clusterhost_log_history( - self.user_object, self.clusterhost_id[0], False, - 'add_log_file_position' + 'add_log_file_position', + user=self.user_object, ) logs = cluster.get_clusterhost_log_histories( - self.user_object, - self.clusterhost_id[0] + self.clusterhost_id[0], + user=self.user_object, ) result = [] for log in logs: @@ -2004,38 +2030,20 @@ class TestAddClusterhostLogHistory(ClusterTestCase): def test_add_clusterhost_log_history_session(self): with database.session() as session: cluster.add_clusterhost_log_history( - self.user_object, self.clusterhost_id[0], + user=self.user_object, filename='add_log_file_session', session=session ) logs = cluster.get_clusterhost_log_histories( - self.user_object, - self.clusterhost_id[0] + self.clusterhost_id[0], + user=self.user_object, ) result = [] for log in logs: result.append(log['filename']) self.assertIn('add_log_file_session', result) - def test_add_clusterhost_log_history_position_args_session(self): - with database.session() as session: - cluster.add_clusterhost_log_history( - self.user_object, - self.clusterhost_id[0], - False, - 'add_log_file_position_session', - session - ) - logs = cluster.get_clusterhost_log_histories( - self.user_object, - self.clusterhost_id[0] - ) - result = [] - for log in logs: - result.append(log['filename']) - self.assertIn('add_log_file_position_session', result) - class TestAddClusterHostLogHistory(ClusterTestCase): """Test add cluster host log history.""" @@ -2048,15 +2056,15 @@ class TestAddClusterHostLogHistory(ClusterTestCase): def test_add_cluster_host_log_history(self): cluster.add_cluster_host_log_history( - self.user_object, self.cluster_id, self.host_id[0], + user=self.user_object, filename='add_log_file' ) logs = cluster.get_cluster_host_log_histories( - self.user_object, self.cluster_id, - self.host_id[0] + self.host_id[0], + user=self.user_object, ) result = [] for log in logs: @@ -2065,16 +2073,16 @@ class TestAddClusterHostLogHistory(ClusterTestCase): def test_add_cluster_host_log_history_position(self): cluster.add_cluster_host_log_history( - self.user_object, self.cluster_id, self.host_id[0], False, - 'add_log_file_position' + 'add_log_file_position', + user=self.user_object, ) logs = cluster.get_cluster_host_log_histories( - self.user_object, self.cluster_id, - self.host_id[0] + self.host_id[0], + user=self.user_object, ) result = [] for log in logs: @@ -2084,42 +2092,22 @@ class TestAddClusterHostLogHistory(ClusterTestCase): def test_add_cluster_host_log_history_session(self): with database.session() as session: cluster.add_cluster_host_log_history( - self.user_object, self.cluster_id, self.host_id[0], + user=self.user_object, filename='add_log_file_session', session=session ) logs = cluster.get_cluster_host_log_histories( - self.user_object, self.cluster_id, - self.host_id[0] + self.host_id[0], + user=self.user_object, ) result = [] for log in logs: result.append(log['filename']) self.assertIn('add_log_file_session', result) - def test_add_cluster_host_log_history_position_session(self): - with database.session() as session: - cluster.add_cluster_host_log_history( - self.user_object, - self.cluster_id, - self.host_id[0], - False, - 'add_log_file_position_session', - session - ) - logs = cluster.get_cluster_host_log_histories( - self.user_object, - self.cluster_id, - self.host_id[0] - ) - result = [] - for log in logs: - result.append(log['filename']) - self.assertIn('add_log_file_position_session', result) - if __name__ == '__main__': flags.init() diff --git a/compass/tests/db/api/test_host.py b/compass/tests/db/api/test_host.py index c45c06bd..42bd283a 100644 --- a/compass/tests/db/api/test_host.py +++ b/compass/tests/db/api/test_host.py @@ -67,7 +67,7 @@ class HostTestCase(unittest2.TestCase): ) ) # get adapter information - list_adapters = adapter.list_adapters(self.user_object) + list_adapters = adapter.list_adapters(user=self.user_object) for list_adapter in list_adapters: for supported_os in list_adapter['supported_oses']: self.os_id = supported_os['os_id'] @@ -86,13 +86,13 @@ class HostTestCase(unittest2.TestCase): cluster_names = ['test_cluster1', 'test_cluster2'] for cluster_name in cluster_names: cluster.add_cluster( - self.user_object, + user=self.user_object, adapter_id=self.adapter_id, os_id=self.os_id, flavor_id=self.flavor_id, name=cluster_name ) - clusters = cluster.list_clusters(self.user_object) + clusters = cluster.list_clusters(user=self.user_object) self.roles = None for list_cluster in clusters: for item in list_cluster['flavor']['roles']: @@ -102,23 +102,23 @@ class HostTestCase(unittest2.TestCase): break # add switch switch.add_switch( - self.user_object, + user=self.user_object, ip='172.29.8.40' ) - switches = switch.list_switches(self.user_object) + switches = switch.list_switches(user=self.user_object) self.switch_id = None for item in switches: self.switch_id = item['id'] macs = ['28:6e:d4:46:c4:25', '00:0c:29:bf:eb:1d'] for mac in macs: switch.add_switch_machine( - self.user_object, self.switch_id, + user=self.user_object, mac=mac, port='1' ) # get machine information - machines = machine.list_machines(self.user_object) + machines = machine.list_machines(user=self.user_object) self.machine_ids = [] for item in machines: self.machine_ids.append(item['id']) @@ -126,40 +126,40 @@ class HostTestCase(unittest2.TestCase): name = ['newname1', 'newname2'] for i in range(0, 2): cluster.add_cluster_host( - self.user_object, self.cluster_id, + user=self.user_object, machine_id=self.machine_ids[i], name=name[i] ) self.host_ids = [] - clusterhosts = cluster.list_clusterhosts(self.user_object) + clusterhosts = cluster.list_clusterhosts(user=self.user_object) for clusterhost in clusterhosts: self.host_ids.append(clusterhost['host_id']) # add subnet subnets = ['10.145.88.0/23', '192.168.100.0/23'] for subnet in subnets: network.add_subnet( - self.user_object, + user=self.user_object, subnet=subnet ) list_subnet = network.list_subnets( - self.user_object + user=self.user_object ) self.subnet_ids = [] for item in list_subnet: self.subnet_ids.append(item['id']) # add host network host.add_host_network( - self.user_object, self.host_ids[0], + user=self.user_object, interface='eth0', ip='10.145.88.0', subnet_id=self.subnet_ids[0], is_mgmt=True ) host.add_host_network( - self.user_object, self.host_ids[1], + user=self.user_object, interface='eth1', ip='192.168.100.0', subnet_id=self.subnet_ids[1], @@ -169,8 +169,8 @@ class HostTestCase(unittest2.TestCase): filenames = ['log1', 'log2'] for filename in filenames: host.add_host_log_history( - self.user_object, self.host_ids[0], + user=self.user_object, filename=filename ) @@ -238,7 +238,7 @@ class TestListHosts(HostTestCase): def test_list_hosts(self): list_hosts = host.list_hosts( - self.user_object + user=self.user_object ) result = [] for list_host in list_hosts: @@ -258,7 +258,7 @@ class TestListMachinesOrHosts(HostTestCase): def test_list__hosts(self): list_hosts = host.list_machines_or_hosts( - self.user_object + user=self.user_object ) result = [] for list_host in list_hosts: @@ -268,15 +268,15 @@ class TestListMachinesOrHosts(HostTestCase): def test_list_machines(self): host.del_host( - self.user_object, - self.host_ids[0] + self.host_ids[0], + user=self.user_object, ) host.del_host( - self.user_object, - self.host_ids[1] + self.host_ids[1], + user=self.user_object, ) list_hosts = host.list_machines_or_hosts( - self.user_object + user=self.user_object ) macs = [] names = [] @@ -302,8 +302,8 @@ class TestGetHost(HostTestCase): def test_get_host(self): get_host = host.get_host( - self.user_object, - self.host_ids[0] + self.host_ids[0], + user=self.user_object, ) self.assertIsNotNone(get_host) self.assertEqual(get_host['mac'], '28:6e:d4:46:c4:25') @@ -320,20 +320,20 @@ class TestGetMachineOrHost(HostTestCase): def test_get_host(self): get_host = host.get_machine_or_host( - self.user_object, - self.host_ids[0] + self.host_ids[0], + user=self.user_object, ) self.assertIsNotNone(get_host) self.assertEqual(get_host['mac'], '28:6e:d4:46:c4:25') def test_get_machine(self): host.del_host( - self.user_object, - self.host_ids[0] + self.host_ids[0], + user=self.user_object, ) get_machine = host.get_machine_or_host( - self.user_object, - self.host_ids[0] + self.host_ids[0], + user=self.user_object, ) name = [] for k, v in get_machine.items(): @@ -354,8 +354,8 @@ class TestGetHostClusters(HostTestCase): def test_get_host_clusters(self): host_clusters = host.get_host_clusters( - self.user_object, - self.host_ids[0] + self.host_ids[0], + user=self.user_object, ) name = None for item in host_clusters: @@ -374,27 +374,27 @@ class TestUpdateHost(HostTestCase): def test_update_host(self): host.update_host( - self.user_object, self.host_ids[0], + user=self.user_object, name='update_test_name' ) update_host = host.get_host( - self.user_object, - self.host_ids[0] + self.host_ids[0], + user=self.user_object, ) self.assertEqual(update_host['name'], 'update_test_name') def test_is_host_etitable(self): host.update_host_state( - self.user_object, self.host_ids[0], + user=self.user_object, state='INSTALLING' ) self.assertRaises( exception.Forbidden, host.update_host, - self.user_object, self.host_ids[0], + user=self.user_object, name='invalid' ) @@ -402,8 +402,8 @@ class TestUpdateHost(HostTestCase): self.assertRaises( exception.InvalidParameter, host.update_host, - self.user_object, self.host_ids[1], + user=self.user_object, name='newname1' ) @@ -419,7 +419,7 @@ class TestUpdateHosts(HostTestCase): def test_update_hosts(self): update_hosts = host.update_hosts( - self.user_object, + user=self.user_object, data=[ { 'host_id': self.host_ids[0], @@ -451,22 +451,22 @@ class TestDelHost(HostTestCase): from compass.tasks import client as celery_client celery_client.celery.send_task = mock.Mock() del_host = host.del_host( - self.user_object, - self.host_ids[0] + self.host_ids[0], + user=self.user_object, ) self.assertIsNotNone(del_host) def test_is_host_editable(self): host.update_host_state( - self.user_object, self.host_ids[0], + user=self.user_object, state='INSTALLING' ) self.assertRaises( exception.Forbidden, host.del_host, - self.user_object, - self.host_ids[0] + self.host_ids[0], + user=self.user_object, ) @@ -476,8 +476,8 @@ class TestGetHostConfig(HostTestCase): def setUp(self): super(TestGetHostConfig, self).setUp() host.update_host_config( - self.user_object, self.host_ids[0], + user=self.user_object, os_config=self.os_configs ) @@ -486,8 +486,8 @@ class TestGetHostConfig(HostTestCase): def test_get_host_config(self): os_configs = host.get_host_config( - self.user_object, - self.host_ids[0] + self.host_ids[0], + user=self.user_object, ) self.assertItemsEqual(self.os_configs, os_configs['os_config']) @@ -498,32 +498,32 @@ class TestGetHostDeployedConfig(HostTestCase): def setUp(self): super(TestGetHostDeployedConfig, self).setUp() host.update_host_config( - self.user_object, self.host_ids[0], + user=self.user_object, os_config=self.os_configs ) cluster.update_cluster_config( - self.user_object, self.cluster_id, + user=self.user_object, os_config=self.os_configs, package_config=self.package_configs ) cluster.update_cluster_host( - self.user_object, self.cluster_id, self.host_ids[0], + user=self.user_object, roles=['allinone-compute'] ) cluster.review_cluster( - self.user_object, self.cluster_id, + user=self.user_object, review={ 'hosts': [self.host_ids[0]] } ) host.update_host_deployed_config( - self.user_object, self.host_ids[0], + user=self.user_object, os_config=self.os_configs ) @@ -532,8 +532,8 @@ class TestGetHostDeployedConfig(HostTestCase): def test_get_host_deployed_config(self): os_configs = host.get_host_deployed_config( - self.user_object, - self.host_ids[0] + self.host_ids[0], + user=self.user_object, ) self.assertItemsEqual( os_configs['deployed_os_config'], @@ -547,25 +547,25 @@ class TestUpdateHostDeployedConfig(HostTestCase): def setUp(self): super(TestUpdateHostDeployedConfig, self).setUp() host.update_host_config( - self.user_object, self.host_ids[0], + user=self.user_object, os_config=self.os_configs ) cluster.update_cluster_config( - self.user_object, self.cluster_id, + user=self.user_object, os_config=self.os_configs, package_config=self.package_configs ) cluster.update_cluster_host( - self.user_object, self.cluster_id, self.host_ids[0], + user=self.user_object, roles=['allinone-compute'] ) cluster.review_cluster( - self.user_object, self.cluster_id, + user=self.user_object, review={ 'hosts': [self.host_ids[0]] } @@ -576,13 +576,13 @@ class TestUpdateHostDeployedConfig(HostTestCase): def test_update_host_deployed_config(self): host.update_host_deployed_config( - self.user_object, self.host_ids[0], + user=self.user_object, os_config=self.os_configs ) os_configs = host.get_host_deployed_config( - self.user_object, - self.host_ids[0] + self.host_ids[0], + user=self.user_object, ) self.assertItemsEqual( os_configs['deployed_os_config'], @@ -591,15 +591,15 @@ class TestUpdateHostDeployedConfig(HostTestCase): def test_is_host_editable(self): host.update_host_state( - self.user_object, self.host_ids[0], + user=self.user_object, state='INSTALLING' ) self.assertRaises( exception.Forbidden, host.update_host_deployed_config, - self.user_object, self.host_ids[0], + user=self.user_object, os_config=self.os_configs ) @@ -615,27 +615,27 @@ class TestUpdateHostConfig(HostTestCase): def test_update_host_config(self): host.update_host_config( - self.user_object, self.host_ids[0], + user=self.user_object, os_config=self.os_configs ) os_configs = host.get_host_config( - self.user_object, - self.host_ids[0] + self.host_ids[0], + user=self.user_object, ) self.assertItemsEqual(self.os_configs, os_configs['os_config']) def test_is_host_editable(self): host.update_host_state( - self.user_object, self.host_ids[0], + user=self.user_object, state='INSTALLING' ) self.assertRaises( exception.Forbidden, host.update_host_config, - self.user_object, self.host_ids[0], + user=self.user_object, os_config=self.os_configs ) @@ -651,27 +651,27 @@ class TestPatchHostConfig(HostTestCase): def test_patch_host_config(self): host.patch_host_config( - self.user_object, self.host_ids[0], + user=self.user_object, os_config=self.os_configs ) os_configs = host.get_host_config( - self.user_object, - self.host_ids[0] + self.host_ids[0], + user=self.user_object, ) self.assertItemsEqual(self.os_configs, os_configs['os_config']) def test_is_host_editable(self): host.update_host_state( - self.user_object, self.host_ids[0], + user=self.user_object, state='INSTALLING' ) self.assertRaises( exception.Forbidden, host.patch_host_config, - self.user_object, self.host_ids[0], + user=self.user_object, os_config=self.os_configs ) @@ -682,8 +682,8 @@ class TestDelHostConfig(HostTestCase): def setUp(self): super(TestDelHostConfig, self).setUp() host.update_host_config( - self.user_object, self.host_ids[0], + user=self.user_object, os_config=self.os_configs ) @@ -692,26 +692,26 @@ class TestDelHostConfig(HostTestCase): def test_del_host_config(self): host.del_host_config( - self.user_object, - self.host_ids[0] + self.host_ids[0], + user=self.user_object, ) os_configs = host.get_host_config( - self.user_object, - self.host_ids[0] + self.host_ids[0], + user=self.user_object, ) self.assertEqual(os_configs['os_config'], {}) def test_is_host_editable(self): host.update_host_state( - self.user_object, self.host_ids[0], + user=self.user_object, state='INSTALLING' ) self.assertRaises( exception.Forbidden, host.del_host_config, - self.user_object, - self.host_ids[0] + self.host_ids[0], + user=self.user_object, ) @@ -721,10 +721,10 @@ class TestListHostNetworks(HostTestCase): def setUp(self): super(TestListHostNetworks, self).setUp() host.add_host_network( - self.user_object, self.host_ids[0], interface='eth1', ip='10.145.88.10', + user=self.user_object, subnet_id=self.subnet_ids[0], is_promiscuous=True ) @@ -734,8 +734,8 @@ class TestListHostNetworks(HostTestCase): def test_list_host_networs(self): host_networks = host.list_host_networks( - self.user_object, - self.host_ids[0] + self.host_ids[0], + user=self.user_object, ) results = [] for host_network in host_networks: @@ -755,7 +755,7 @@ class TestListHostnetworks(HostTestCase): def test_list_hostnetworks(self): host_networks = host.list_hostnetworks( - self.user_object, + user=self.user_object, ) results = [] for host_network in host_networks: @@ -769,25 +769,34 @@ class TestGetHostNetwork(HostTestCase): def setUp(self): super(TestGetHostNetwork, self).setUp() + result = host.add_host_network( + self.host_ids[0], + interface='eth1', + ip='10.145.88.10', + user=self.user_object, + subnet_id=self.subnet_ids[0], + is_promiscuous=True + ) + self.host_network_id = result['id'] def tearDown(self): super(TestGetHostNetwork, self).tearDown() def test_get_host_network(self): host_network = host.get_host_network( - self.user_object, self.host_ids[0], - self.host_ids[0] + self.host_network_id, + user=self.user_object, ) - self.assertEqual(host_network['ip'], '10.145.88.0') + self.assertEqual(host_network['ip'], '10.145.88.10') def test_record_not_exists(self): self.assertRaises( exception.RecordNotExists, host.get_host_network, - self.user_object, 2, - self.host_ids[0] + self.host_network_id, + user=self.user_object, ) @@ -796,16 +805,25 @@ class TestGetHostnetwork(HostTestCase): def setUp(self): super(TestGetHostnetwork, self).setUp() + result = host.add_host_network( + self.host_ids[0], + interface='eth1', + ip='10.145.88.10', + user=self.user_object, + subnet_id=self.subnet_ids[0], + is_promiscuous=True + ) + self.host_network_id = result['id'] def tearDown(self): super(TestGetHostnetwork, self).tearDown() def test_get_hostnetwork(self): host_network = host.get_hostnetwork( - self.user_object, - self.host_ids[0] + self.host_network_id, + user=self.user_object, ) - self.assertEqual(host_network['ip'], '10.145.88.0') + self.assertEqual(host_network['ip'], '10.145.88.10') class TestAddHostNetwork(HostTestCase): @@ -819,16 +837,16 @@ class TestAddHostNetwork(HostTestCase): def test_add_host_network(self): host.add_host_network( - self.user_object, self.host_ids[0], + user=self.user_object, interface='eth1', ip='10.145.88.20', subnet_id=self.subnet_ids[0], is_mgmt=True ) host_network = host.list_host_networks( - self.user_object, - self.host_ids[0] + self.host_ids[0], + user=self.user_object, ) result = [] for item in host_network: @@ -837,17 +855,17 @@ class TestAddHostNetwork(HostTestCase): def test_add_host_network_position(self): host.add_host_network( - self.user_object, self.host_ids[0], True, 'eth1', + user=self.user_object, ip='10.145.88.30', subnet_id=self.subnet_ids[0], is_mgmt=True ) host_network = host.list_host_networks( - self.user_object, - self.host_ids[0] + self.host_ids[0], + user=self.user_object, ) result = [] for item in host_network: @@ -857,8 +875,8 @@ class TestAddHostNetwork(HostTestCase): def test_add_host_network_session(self): with database.session() as session: host.add_host_network( - self.user_object, self.host_ids[0], + user=self.user_object, interface='eth1', ip='10.145.88.40', subnet_id=self.subnet_ids[0], @@ -866,41 +884,20 @@ class TestAddHostNetwork(HostTestCase): session=session ) host_network = host.list_host_networks( - self.user_object, - self.host_ids[0] + self.host_ids[0], + user=self.user_object, ) result = [] for item in host_network: result.append(item['ip']) self.assertIn('10.145.88.40', result) - def test_add_host_network_position_session(self): - with database.session() as session: - host.add_host_network( - self.user_object, - self.host_ids[0], - True, - 'eth1', - session, - ip='10.145.88.50', - subnet_id=self.subnet_ids[0], - is_mgmt=True - ) - host_network = host.list_host_networks( - self.user_object, - self.host_ids[0] - ) - result = [] - for item in host_network: - result.append(item['ip']) - self.assertIn('10.145.88.50', result) - def test_invalid_parameter(self): self.assertRaises( exception.InvalidParameter, host.add_host_network, - self.user_object, self.host_ids[0], + user=self.user_object, interface='eth3', ip='10.145.88.0', subnet_id=self.subnet_ids[0] @@ -918,7 +915,7 @@ class TestAddHostNetworks(HostTestCase): def test_addhost_networks(self): host_networks = host.add_host_networks( - self.user_object, + user=self.user_object, data=[ { 'host_id': self.host_ids[0], @@ -962,15 +959,15 @@ class TestUpdateHostNetwork(HostTestCase): def test_update_host_network(self): host.update_host_network( - self.user_object, self.host_ids[0], self.host_ids[0], + user=self.user_object, interface='eth10', ip='10.145.88.100' ) host_networks = host.list_host_networks( - self.user_object, - self.host_ids[0] + self.host_ids[0], + user=self.user_object, ) interface = None ip = None @@ -984,9 +981,9 @@ class TestUpdateHostNetwork(HostTestCase): self.assertRaises( exception.RecordNotExists, host.update_host_network, - self.user_object, self.host_ids[0], - 2 + 2, + user=self.user_object, ) @@ -1001,14 +998,14 @@ class TestUpdateHostnetwork(HostTestCase): def test_update_hostnetwork(self): host.update_hostnetwork( - self.user_object, self.host_ids[0], + user=self.user_object, interface='eth10', ip='10.145.88.100' ) host_networks = host.list_host_networks( - self.user_object, - self.host_ids[0] + self.host_ids[0], + user=self.user_object, ) interface = None ip = None @@ -1020,8 +1017,8 @@ class TestUpdateHostnetwork(HostTestCase): def test_invalid_parameter(self): host.add_host_network( - self.user_object, self.host_ids[0], + user=self.user_object, interface='eth11', ip='10.145.88.101', subnet_id=self.subnet_ids[0], @@ -1030,15 +1027,15 @@ class TestUpdateHostnetwork(HostTestCase): self.assertRaises( exception.InvalidParameter, host.update_hostnetwork, - self.user_object, self.host_ids[0], + user=self.user_object, interface='eth11' ) self.assertRaises( exception.InvalidParameter, host.update_hostnetwork, - self.user_object, self.host_ids[0], + user=self.user_object, ip='10.145.88.101' ) @@ -1054,13 +1051,13 @@ class TestDelHostNetwork(HostTestCase): def test_del_host_network(self): host.del_host_network( - self.user_object, self.host_ids[0], - self.host_ids[0] + self.host_ids[0], + user=self.user_object, ) host_network = host.list_host_networks( - self.user_object, - self.host_ids[0] + self.host_ids[0], + user=self.user_object, ) self.assertEqual(host_network, []) @@ -1068,9 +1065,9 @@ class TestDelHostNetwork(HostTestCase): self.assertRaises( exception.RecordNotExists, host.del_host_network, - self.user_object, 100, - self.host_ids[0] + self.host_ids[0], + user=self.user_object, ) @@ -1085,12 +1082,12 @@ class TestDelHostnetwork(HostTestCase): def test_del_hostnetwork(self): host.del_hostnetwork( - self.user_object, - self.host_ids[0] + self.host_ids[0], + user=self.user_object, ) host_network = host.list_host_networks( - self.user_object, - self.host_ids[0] + self.host_ids[0], + user=self.user_object, ) self.assertEqual(host_network, []) @@ -1106,8 +1103,8 @@ class TestGetHostState(HostTestCase): def test_get_host_state(self): host_states = host.get_host_state( - self.user_object, - self.host_ids[0] + self.host_ids[0], + user=self.user_object, ) self.assertEqual(host_states['state'], 'UNINITIALIZED') @@ -1123,13 +1120,13 @@ class TestUpdateHostState(HostTestCase): def test_update_host_state(self): host.update_host_state( - self.user_object, self.host_ids[0], + user=self.user_object, state='INSTALLING' ) host_states = host.get_host_state( - self.user_object, - self.host_ids[0] + self.host_ids[0], + user=self.user_object, ) self.assertEqual(host_states['state'], 'INSTALLING') @@ -1145,8 +1142,8 @@ class TestGetHostLogHistories(HostTestCase): def test_get_host_log_histories(self): logs = host.get_host_log_histories( - self.user_object, - self.host_ids[0] + self.host_ids[0], + user=self.user_object, ) filenames = [] for log in logs: @@ -1166,9 +1163,9 @@ class TestGetHostLogHistory(HostTestCase): def test_get_host_log_history(self): log = host.get_host_log_history( - self.user_object, self.host_ids[0], - 'log1' + 'log1', + user=self.user_object, ) self.assertEqual(log['filename'], 'log1') @@ -1184,23 +1181,26 @@ class TestUpdateHostLogHistory(HostTestCase): def test_update_host_log_history(self): host.update_host_log_history( - self.user_object, self.host_ids[0], 'log1', + user=self.user_object, severity='WARNING', message='update log' ) logs = host.get_host_log_histories( - self.user_object, - self.host_ids[0] + self.host_ids[0], + user=self.user_object, + ) + expected = { + 'line_matcher_name': 'start', + 'severity': 'WARNING', + 'id': 1, + 'filename': 'log1', + 'message': 'update log' + } + self.assertTrue( + all(item in logs[0].items() for item in expected.items()) ) - result = [] - for log in logs: - result.append(log['severity']) - result.append(log['message']) - expects = ['WARNING', 'update log'] - for expect in expects: - self.assertIn(expect, result) class TestAddHostLogHistory(HostTestCase): @@ -1214,13 +1214,13 @@ class TestAddHostLogHistory(HostTestCase): def test_add_host_log_history(self): host.add_host_log_history( - self.user_object, self.host_ids[0], + user=self.user_object, filename='add_log' ) logs = host.get_host_log_histories( - self.user_object, - self.host_ids[0] + self.host_ids[0], + user=self.user_object, ) result = [] for log in logs: @@ -1229,14 +1229,14 @@ class TestAddHostLogHistory(HostTestCase): def test_add_host_log_history_position(self): host.add_host_log_history( - self.user_object, self.host_ids[0], False, - 'add_log_position' + 'add_log_position', + user=self.user_object, ) logs = host.get_host_log_histories( - self.user_object, - self.host_ids[0] + self.host_ids[0], + user=self.user_object, ) result = [] for log in logs: @@ -1246,38 +1246,20 @@ class TestAddHostLogHistory(HostTestCase): def test_add_host_log_history_session(self): with database.session() as session: host.add_host_log_history( - self.user_object, self.host_ids[0], + user=self.user_object, filename='add_log_session', session=session ) logs = host.get_host_log_histories( - self.user_object, - self.host_ids[0] + self.host_ids[0], + user=self.user_object, ) result = [] for log in logs: result.append(log['filename']) self.assertIn('add_log_session', result) - def test_add_host_log_history_position_session(self): - with database.session() as session: - host.add_host_log_history( - self.user_object, - self.host_ids[0], - False, - 'add_log_position_session', - session - ) - logs = host.get_host_log_histories( - self.user_object, - self.host_ids[0] - ) - result = [] - for log in logs: - result.append(log['filename']) - self.assertIn('add_log_position_session', result) - class TestPoweronHost(HostTestCase): """Test poweron host.""" @@ -1285,25 +1267,25 @@ class TestPoweronHost(HostTestCase): def setUp(self): super(TestPoweronHost, self).setUp() host.update_host_config( - self.user_object, self.host_ids[0], + user=self.user_object, os_config=self.os_configs ) cluster.update_cluster_config( - self.user_object, self.cluster_id, + user=self.user_object, os_config=self.os_configs, package_config=self.package_configs ) cluster.update_cluster_host( - self.user_object, self.cluster_id, self.host_ids[0], + user=self.user_object, roles=['allinone-compute'] ) cluster.review_cluster( - self.user_object, self.cluster_id, + user=self.user_object, review={ 'hosts': [self.host_ids[0]] } @@ -1316,8 +1298,8 @@ class TestPoweronHost(HostTestCase): from compass.tasks import client as celery_client celery_client.celery.send_task = mock.Mock() poweron_host = host.poweron_host( - self.user_object, self.host_ids[0], + user=self.user_object, poweron={'poweron': True} ) self.assertEqual( @@ -1332,25 +1314,25 @@ class TestPoweroffHost(HostTestCase): def setUp(self): super(TestPoweroffHost, self).setUp() host.update_host_config( - self.user_object, self.host_ids[0], + user=self.user_object, os_config=self.os_configs ) cluster.update_cluster_config( - self.user_object, self.cluster_id, + user=self.user_object, os_config=self.os_configs, package_config=self.package_configs ) cluster.update_cluster_host( - self.user_object, self.cluster_id, self.host_ids[0], + user=self.user_object, roles=['allinone-compute'] ) cluster.review_cluster( - self.user_object, self.cluster_id, + user=self.user_object, review={ 'hosts': [self.host_ids[0]] } @@ -1363,8 +1345,8 @@ class TestPoweroffHost(HostTestCase): from compass.tasks import client as celery_client celery_client.celery.send_task = mock.Mock() poweroff_host = host.poweroff_host( - self.user_object, self.host_ids[0], + user=self.user_object, poweroff={'poweroff': True} ) self.assertEqual( @@ -1379,25 +1361,25 @@ class TestResetHost(HostTestCase): def setUp(self): super(TestResetHost, self).setUp() host.update_host_config( - self.user_object, self.host_ids[0], + user=self.user_object, os_config=self.os_configs ) cluster.update_cluster_config( - self.user_object, self.cluster_id, + user=self.user_object, os_config=self.os_configs, package_config=self.package_configs ) cluster.update_cluster_host( - self.user_object, self.cluster_id, self.host_ids[0], + user=self.user_object, roles=['allinone-compute'] ) cluster.review_cluster( - self.user_object, self.cluster_id, + user=self.user_object, review={ 'hosts': [self.host_ids[0]] } @@ -1410,8 +1392,8 @@ class TestResetHost(HostTestCase): from compass.tasks import client as celery_client celery_client.celery.send_task = mock.Mock() reset_host = host.reset_host( - self.user_object, self.host_ids[0], + user=self.user_object, reset={'reset': True} ) self.assertEqual( diff --git a/compass/tests/db/api/test_machine.py b/compass/tests/db/api/test_machine.py index b2f6c95e..a94e1bab 100644 --- a/compass/tests/db/api/test_machine.py +++ b/compass/tests/db/api/test_machine.py @@ -45,16 +45,17 @@ class TestGetMachine(BaseTest): def test_get_machine(self): switch.add_switch_machine( - self.user_object, 1, mac='28:6e:d4:46:c4:25', - port='1' + port='1', + user=self.user_object, ) get_machine = machine.get_machine( - self.user_object, - 1 + 1, + user=self.user_object, ) self.assertIsNotNone(get_machine) + self.assertEqual(get_machine['mac'], '28:6e:d4:46:c4:25') class TestListMachines(BaseTest): @@ -68,13 +69,14 @@ class TestListMachines(BaseTest): def test_list_machines(self): switch.add_switch_machine( - self.user_object, 1, mac='28:6e:d4:46:c4:25', - port='1' + port='1', + user=self.user_object, ) list_machine = machine.list_machines(self.user_object) self.assertIsNotNone(list_machine) + self.assertEqual(list_machine[0]['mac'], '28:6e:d4:46:c4:25') class TestUpdateMachine(BaseTest): @@ -88,20 +90,26 @@ class TestUpdateMachine(BaseTest): def test_update_machine(self): switch.add_switch_machine( - self.user_object, 1, mac='28:6e:d4:46:c4:25', - port='1' + port='1', + user=self.user_object, ) machine.update_machine( - self.user_object, 1, - tag='test' + tag='test', + user=self.user_object, ) update_machine = machine.list_machines(self.user_object) - expected = {'tag': 'test'} + expected = { + 'id': 1, + 'mac': '28:6e:d4:46:c4:25', + 'tag': 'test', + 'switch_ip': '0.0.0.0', + 'port': '1' + } self.assertTrue( - item in update_machine[0].items() for item in expected.items() + all(item in update_machine[0].items() for item in expected.items()) ) @@ -116,20 +124,20 @@ class TestPatchMachine(BaseTest): def test_patch_machine(self): switch.add_switch_machine( - self.user_object, 1, mac='28:6e:d4:46:c4:25', - port='1' + port='1', + user=self.user_object, ) machine.patch_machine( - self.user_object, 1, + user=self.user_object, tag={'patched_tag': 'test'} ) patch_machine = machine.list_machines(self.user_object) - expected = {'patched_tag': 'test'} + expected = {'tag': {'patched_tag': 'test'}} self.assertTrue( - item in patch_machine[0].items() for item in expected.items() + all(item in patch_machine[0].items() for item in expected.items()) ) @@ -144,14 +152,14 @@ class TestDelMachine(BaseTest): def test_del_machine(self): switch.add_switch_machine( - self.user_object, 1, mac='28:6e:d4:46:c4:25', - port='1' + port='1', + user=self.user_object, ) machine.del_machine( - self.user_object, - 1 + 1, + user=self.user_object, ) del_machine = machine.list_machines(self.user_object) self.assertEqual([], del_machine) diff --git a/compass/tests/db/api/test_network.py b/compass/tests/db/api/test_network.py index 9909a9a6..404ca931 100644 --- a/compass/tests/db/api/test_network.py +++ b/compass/tests/db/api/test_network.py @@ -46,15 +46,19 @@ class TestListSubnets(BaseTest): def test_list_subnets(self): network.add_subnet( - self.user_object, - subnet='10.145.89.0/24' + subnet='10.145.89.0/24', + user=self.user_object, ) list_subnet = network.list_subnets( - self.user_object + user=self.user_object ) - expected = '10.145.89.0/24' + expected = { + 'subnet': '10.145.89.0/24', + 'id': 1, + 'name': '10.145.89.0/24' + } self.assertTrue( - item in list_subnet[0].items() for item in expected + all(item in list_subnet[0].items() for item in expected.items()) ) @@ -69,12 +73,12 @@ class TestGetSubnet(BaseTest): def test_get_subnet(self): network.add_subnet( - self.user_object, - subnet='10.145.89.0/24' + subnet='10.145.89.0/24', + user=self.user_object, ) get_subnet = network.get_subnet( - self.user_object, - 1 + 1, + user=self.user_object, ) self.assertEqual( '10.145.89.0/24', @@ -83,8 +87,8 @@ class TestGetSubnet(BaseTest): def tset_get_subnet_no_exist(self): get_subnet_no_exist = network.get_subnet( - self.user_object, - 2 + 2, + user=self.user_object, ) self.assertEqual([], get_subnet_no_exist) @@ -100,11 +104,11 @@ class TestAddSubnet(BaseTest): def test_add_subnet(self): network.add_subnet( - self.user_object, - subnet='10.145.89.0/24' + subnet='10.145.89.0/24', + user=self.user_object, ) add_subnets = network.list_subnets( - self.user_object + user=self.user_object ) expected = '10.145.89.0/24' for add_subnet in add_subnets: @@ -112,12 +116,12 @@ class TestAddSubnet(BaseTest): def test_add_subnet_position(self): network.add_subnet( - self.user_object, True, - '10.145.89.0/23' + '10.145.89.0/23', + user=self.user_object, ) add_subnets = network.list_subnets( - self.user_object + user=self.user_object ) expected = '10.145.89.0/23' for add_subnet in add_subnets: @@ -128,30 +132,16 @@ class TestAddSubnet(BaseTest): network.add_subnet( self.user_object, subnet='10.145.89.0/22', + user=self.user_object, session=session ) add_subnets = network.list_subnets( - self.user_object + user=self.user_object ) expected = '10.145.89.0/22' for add_subnet in add_subnets: self.assertEqual(expected, add_subnet['subnet']) - def test_add_subnet_position_session(self): - with database.session() as session: - network.add_subnet( - self.user_object, - True, - '10.145.89.0/21', - session - ) - add_subnets = network.list_subnets( - self.user_object - ) - expected = '10.145.89.0/21' - for add_subnet in add_subnets: - self.assertEqual(expected, add_subnet['subnet']) - class TestUpdateSubnet(BaseTest): """Test update subnet.""" @@ -164,28 +154,32 @@ class TestUpdateSubnet(BaseTest): def test_update_subnet(self): network.add_subnet( - self.user_object, - subnet='10.145.89.0/24' + subnet='10.145.89.0/24', + user=self.user_object, ) network.update_subnet( - self.user_object, 1, + user=self.user_object, subnet='192.168.100.0/24' ) update_subnet = network.list_subnets( - self.user_object + user=self.user_object ) - expected = '192.168.100.0/24' + expected = { + 'subnet': '192.168.100.0/24', + 'id': 1, + 'name': '192.168.100.0/24' + } self.assertTrue( - item in update_subnet[0].items() for item in expected + all(item in update_subnet[0].items() for item in expected.items()) ) def test_update_subnet_no_exist(self): self.assertRaises( exception.DatabaseException, network.update_subnet, - self.user_object, - 2 + 2, + user=self.user_object, ) @@ -200,15 +194,15 @@ class TestDelSubnet(BaseTest): def test_del_subnet(self): network.add_subnet( - self.user_object, + user=self.user_object, subnet='10.145.89.0/24' ) network.del_subnet( - self.user_object, - 1 + 1, + user=self.user_object, ) del_subnet = network.list_subnets( - self.user_object + user=self.user_object ) self.assertEqual([], del_subnet) @@ -216,8 +210,8 @@ class TestDelSubnet(BaseTest): self.assertRaises( exception.RecordNotExists, network.del_subnet, - self.user_object, - 2 + 2, + user=self.user_object, ) diff --git a/compass/tests/db/api/test_permission.py b/compass/tests/db/api/test_permission.py index f3ddccc0..ebb7eccb 100644 --- a/compass/tests/db/api/test_permission.py +++ b/compass/tests/db/api/test_permission.py @@ -44,7 +44,7 @@ class TestListPermissions(BaseTest): super(TestListPermissions, self).tearDown() def test_list_permissions(self): - permissions = permission.list_permissions(self.user_object) + permissions = permission.list_permissions(user=self.user_object) self.assertIsNotNone(permissions) self.assertEqual(54, len(permissions)) @@ -59,7 +59,9 @@ class TestGetPermission(BaseTest): super(TestGetPermission, self).tearDown() def test_get_permission(self): - get_permission = permission.get_permission(self.user_object, 1) + get_permission = permission.get_permission( + 1, + user=self.user_object) self.assertIsNotNone(get_permission) expected = { 'alias': 'list permissions', diff --git a/compass/tests/db/api/test_switch.py b/compass/tests/db/api/test_switch.py index 72906475..75a39dba 100644 --- a/compass/tests/db/api/test_switch.py +++ b/compass/tests/db/api/test_switch.py @@ -46,10 +46,11 @@ class TestGetSwitch(BaseTest): def test_get_switch(self): get_switch = switch.get_switch( - self.user_object, - 1 + 1, + user=self.user_object, ) self.assertIsNotNone(get_switch) + self.assertEqual(get_switch['ip'], '0.0.0.0') class TestAddSwitch(BaseTest): @@ -63,43 +64,31 @@ class TestAddSwitch(BaseTest): def test_add_switch(self): add_switch = switch.add_switch( - self.user_object, ip='2887583784', + user=self.user_object, ) expected = '172.29.8.40' self.assertEqual(expected, add_switch['ip']) def test_add_switch_position_args(self): add_switch = switch.add_switch( - self.user_object, True, '2887583784', + user=self.user_object, ) - print add_switch expected = '172.29.8.40' self.assertEqual(expected, add_switch['ip']) def test_add_switch_session(self): with database.session() as session: add_switch = switch.add_switch( - self.user_object, ip='2887583784', + user=self.user_object, session=session ) expected = '172.29.8.40' self.assertEqual(expected, add_switch['ip']) - def test_add_switch_position_args_session(self): - with database.session() as session: - add_switch = switch.add_switch( - self.user_object, - True, - '2887583784', - session - ) - expected = '172.29.8.40' - self.assertEqual(expected, add_switch['ip']) - class TestListSwitches(BaseTest): """Test list switch.""" @@ -112,38 +101,39 @@ class TestListSwitches(BaseTest): def test_list_switches_ip_int_invalid(self): switch.add_switch( - self.user_object, - ip='2887583784' + ip='2887583784', + user=self.user_object, ) list_switches = switch.list_switches( - self.user_object, - ip_int='test' + ip_int='test', + user=self.user_object, ) self.assertEqual(list_switches, []) def test_list_switches_with_ip_int(self): switch.add_switch( - self.user_object, - ip='2887583784' + ip='2887583784', + user=self.user_object, ) list_switches = switch.list_switches( - self.user_object, - ip_int='2887583784' - ) - expected = '2887583784' - self.assertTrue( - item in expected.items() for item in list_switches[0].items() + ip_int='2887583784', + user=self.user_object, ) + expected = '172.29.8.40' + self.assertIsNotNone(list_switches) + self.assertEqual(expected, list_switches[0]['ip']) def test_list_switches(self): switch.add_switch( - self.user_object, - ip='2887583784' + ip='2887583784', + user=self.user_object, ) list_switches = switch.list_switches( - self.user_object + user=self.user_object ) + expected = '172.29.8.40' self.assertIsNotNone(list_switches) + self.assertEqual(expected, list_switches[0]['ip']) class TestDelSwitch(BaseTest): @@ -157,11 +147,11 @@ class TestDelSwitch(BaseTest): def test_del_switch(self): switch.del_switch( - self.user_object, - 1 + 1, + user=self.user_object, ) del_switch = switch.list_switches( - self.user_object + user=self.user_object ) self.assertEqual([], del_switch) @@ -177,13 +167,13 @@ class TestUpdateSwitch(BaseTest): def test_update_switch(self): switch.update_switch( - self.user_object, 1, + user=self.user_object, vendor='test_update' ) update_switch = switch.get_switch( - self.user_object, - 1 + 1, + user=self.user_object, ) expected = 'test_update' self.assertEqual(expected, update_switch['vendor']) @@ -200,23 +190,24 @@ class TestPatchSwitch(BaseTest): def test_patch_switch(self): switch.patch_switch( - self.user_object, 1, + user=self.user_object, patched_credentials={ 'version': '2c', 'community': 'public' } ) patch_switch = switch.get_switch( - self.user_object, - 1 + 1, + user=self.user_object, ) expected = { + 'credentials': { 'version': '2c', - 'community': 'public' + 'community': 'public'} } self.assertTrue( - item in expected.items() for item in patch_switch.items() + all(item in patch_switch.items() for item in expected.items()) ) @@ -231,9 +222,17 @@ class TestListSwitchFilters(BaseTest): def test_list_switch_filters(self): list_switch_filters = switch.list_switch_filters( - self.user_object + user=self.user_object ) + expected = { + 'ip': '0.0.0.0', + 'id': 1, + 'filters': 'allow ports all', + } self.assertIsNotNone(list_switch_filters) + self.assertTrue( + all(item in list_switch_filters[0].items() + for item in expected.items())) class TestGetSwitchFilters(BaseTest): @@ -247,10 +246,18 @@ class TestGetSwitchFilters(BaseTest): def test_get_swtich_filters(self): get_switch_filter = switch.get_switch_filters( - self.user_object, - 1 + 1, + user=self.user_object, ) + expected = { + 'ip': '0.0.0.0', + 'id': 1, + 'filters': 'allow ports all', + } self.assertIsNotNone(get_switch_filter) + self.assertTrue( + all(item in get_switch_filter.items() + for item in expected.items())) class TestUpdateSwitchFilters(BaseTest): @@ -264,8 +271,8 @@ class TestUpdateSwitchFilters(BaseTest): def test_update_switch_filters(self): switch.update_switch_filters( - self.user_object, 1, + user=self.user_object, filters=[ { 'filter_type': 'allow' @@ -273,15 +280,15 @@ class TestUpdateSwitchFilters(BaseTest): ] ) update_switch_filters = switch.get_switch_filters( - self.user_object, - 1 + 1, + user=self.user_object, ) expected = { - 'filter_type': 'allow' + 'filters': 'allow' } self.assertTrue( - item in update_switch_filters[0].items() - for item in expected.items() + all(item in update_switch_filters.items() + for item in expected.items()) ) @@ -295,9 +302,13 @@ class TestPatchSwitchFilter(BaseTest): super(TestPatchSwitchFilter, self).tearDown() def test_patch_switch_filter(self): + switch.add_switch( + ip='2887583784', + user=self.user_object, + ) switch.patch_switch_filter( - self.user_object, - 1, + 2, + user=self.user_object, patched_filters=[ { 'filter_type': 'allow' @@ -305,14 +316,15 @@ class TestPatchSwitchFilter(BaseTest): ] ) patch_switch_filter = switch.get_switch_filters( - self.user_object, - 1 + 2, + user=self.user_object, ) expected = { - 'filter_type': 'allow' + 'filters': 'allow' } self.assertTrue( - item in patch_switch_filter[0].items() for item in expected.items() + all(item in patch_switch_filter.items() + for item in expected.items()) ) @@ -327,21 +339,21 @@ class TestAddSwitchMachine(BaseTest): def test_add_switch_machine(self): add_switch_machine = switch.add_switch_machine( - self.user_object, 1, mac='28:6e:d4:46:c4:25', - port='1' + port='1', + user=self.user_object, ) expected = '28:6e:d4:46:c4:25' self.assertEqual(expected, add_switch_machine['mac']) def test_add_switch_machine_position_args(self): add_switch_machine = switch.add_switch_machine( - self.user_object, 1, True, '28:6e:d4:46:c4:25', - port='1' + port='1', + user=self.user_object, ) expected = '28:6e:d4:46:c4:25' self.assertEqual(expected, add_switch_machine['mac']) @@ -349,28 +361,15 @@ class TestAddSwitchMachine(BaseTest): def test_add_switch_machine_session(self): with database.session() as session: add_switch_machine = switch.add_switch_machine( - self.user_object, 1, mac='28:6e:d4:46:c4:25', + user=self.user_object, session=session, port='1' ) expected = '28:6e:d4:46:c4:25' self.assertEqual(expected, add_switch_machine['mac']) - def test_add_switch_machine_position_args_session(self): - with database.session() as session: - add_switch_machine = switch.add_switch_machine( - self.user_object, - 1, - True, - '28:6e:d4:46:c4:25', - session, - port='1' - ) - expected = '28:6e:d4:46:c4:25' - self.assertEqual(expected, add_switch_machine['mac']) - class TestListSwitchMachines(BaseTest): """Test get switch machines.""" @@ -383,20 +382,32 @@ class TestListSwitchMachines(BaseTest): def test_list_switch_machines(self): switch.add_switch( - self.user_object, - ip='2887583784' + ip='2887583784', + user=self.user_object, ) switch.add_switch_machine( - self.user_object, 2, mac='28:6e:d4:46:c4:25', - port='1' + port='1', + user=self.user_object, ) list_switch_machines = switch.list_switch_machines( - self.user_object, - 2 + 2, + user=self.user_object, ) + expected = { + 'switch_id': 2, + 'id': 1, + 'mac': '28:6e:d4:46:c4:25', + 'switch_ip': '172.29.8.40', + 'machine_id': 1, + 'port': '1', + 'switch_machine_id': 1 + } self.assertIsNotNone(list_switch_machines) + self.assertTrue( + all(item in list_switch_machines[0].items() + for item in expected.items())) class TestListSwitchmachines(BaseTest): @@ -410,54 +421,59 @@ class TestListSwitchmachines(BaseTest): def test_list_switch_machines_with_ip_int(self): switch.add_switch( - self.user_object, - ip='2887583784' + ip='2887583784', + user=self.user_object, ) switch.add_switch_machine( - self.user_object, 2, mac='28:6e:d4:46:c4:25', - port='1' + port='1', + user=self.user_object, ) list_switch_machines = switch.list_switchmachines( - self.user_object, - switch_ip_int='2887583784' + switch_ip_int='2887583784', + user=self.user_object, ) - expected = '172.29.8.40' - self.assertTrue(expected for item in list_switch_machines[0].items()) + expected = {'switch_ip': '172.29.8.40'} + self.assertTrue( + all(item in list_switch_machines[0].items() + for item in expected.items())) def test_list_switch_machines_ip_invalid(self): switch.add_switch( - self.user_object, - ip='2887583784' + ip='2887583784', + user=self.user_object, ) switch.add_switch_machine( - self.user_object, 2, mac='28:6e:d4:46:c4:25', - port='1' + port='1', + user=self.user_object, ) list_switch_machines = switch.list_switchmachines( - self.user_object, - switch_ip_int='test' + switch_ip_int='test', + user=self.user_object, ) self.assertEqual(list_switch_machines, []) def test_list_switch_machines_without_ip(self): switch.add_switch( - self.user_object, - ip='2887583784' + ip='2887583784', + user=self.user_object, ) switch.add_switch_machine( - self.user_object, 2, mac='28:6e:d4:46:c4:25', - port='1' + port='1', + user=self.user_object, ) list_switch_machines = switch.list_switchmachines( - self.user_object + user=self.user_object ) - self.assertIsNotNone(list_switch_machines) + expected = {'switch_ip': '172.29.8.40'} + self.assertTrue( + all(item in list_switch_machines[0].items() + for item in expected.items())) class TestListSwitchMachinesHosts(BaseTest): @@ -471,20 +487,31 @@ class TestListSwitchMachinesHosts(BaseTest): def test_list_hosts(self): switch.add_switch( - self.user_object, - ip='2887583784' + ip='2887583784', + user=self.user_object, ) switch.add_switch_machine( - self.user_object, 2, mac='28:6e:d4:46:c4:25', - port='1' + port='1', + user=self.user_object, ) list_hosts = switch.list_switch_machines_hosts( - self.user_object, - 2 + 2, + user=self.user_object, ) - self.assertIsNotNone(list_hosts) + expected = { + 'switch_id': 2, + 'id': 1, + 'mac': '28:6e:d4:46:c4:25', + 'switch_ip': '172.29.8.40', + 'machine_id': 1, + 'port': '1', + 'switch_machine_id': 1 + } + self.assertTrue( + all(item in list_hosts[0].items() + for item in expected.items())) class TestListSwitchmachinesHosts(BaseTest): @@ -498,53 +525,59 @@ class TestListSwitchmachinesHosts(BaseTest): def test_list_hosts_with_ip_int(self): switch.add_switch( - self.user_object, - ip='2887583784' + ip='2887583784', + user=self.user_object, ) switch.add_switch_machine( - self.user_object, 2, mac='28:6e:d4:46:c4:25', - port='1' + port='1', + user=self.user_object, ) list_hosts = switch.list_switchmachines_hosts( - self.user_object, - switch_ip_int='2887583784' + switch_ip_int='2887583784', + user=self.user_object, ) - expected = '172.29.8.40' - self.assertTrue(expected for item in list_hosts[0].items()) + expected = {'switch_ip': '172.29.8.40'} + self.assertTrue( + all(item in list_hosts[0].items() + for item in expected.items())) def test_list_hosts_ip_invalid(self): switch.add_switch( - self.user_object, - ip='2887583784' + ip='2887583784', + user=self.user_object, ) switch.add_switch_machine( - self.user_object, 2, mac='28:6e:d4:46:c4:25', - port='1' + port='1', + user=self.user_object, ) list_hosts = switch.list_switchmachines_hosts( - self.user_object, - switch_ip_int='test' + switch_ip_int='test', + user=self.user_object, ) self.assertEqual(list_hosts, []) def test_list_hosts_without_ip(self): switch.add_switch( - self.user_object, - ip='2887583784' + ip='2887583784', + user=self.user_object, ) switch.add_switch_machine( - self.user_object, 2, mac='28:6e:d4:46:c4:25', - port='1' + port='1', + user=self.user_object, ) list_hosts = switch.list_switchmachines_hosts( - self.user_object + user=self.user_object ) + expected = {'switch_ip': '172.29.8.40'} + self.assertTrue( + all(item in list_hosts[0].items() + for item in expected.items())) self.assertIsNotNone(list_hosts) @@ -559,21 +592,22 @@ class TestGetSwitchMachine(BaseTest): def test_get_switch_machine(self): switch.add_switch( - self.user_object, - ip='2887583784' + ip='2887583784', + user=self.user_object, ) switch.add_switch_machine( - self.user_object, 2, mac='28:6e:d4:46:c4:25', - port='1' + port='1', + user=self.user_object, ) get_switch_machine = switch.get_switch_machine( - self.user_object, 2, - 1 + 1, + user=self.user_object, ) self.assertIsNotNone(get_switch_machine) + self.assertEqual(get_switch_machine['mac'], '28:6e:d4:46:c4:25') class TestGetSwitchmachine(BaseTest): @@ -587,16 +621,17 @@ class TestGetSwitchmachine(BaseTest): def test_get_switchmachine(self): switch.add_switch_machine( - self.user_object, 1, mac='28:6e:d4:46:c4:25', - port='1' + port='1', + user=self.user_object, ) get_switchmachine = switch.get_switchmachine( - self.user_object, - 1 + 1, + user=self.user_object, ) self.assertIsNotNone(get_switchmachine) + self.assertEqual(get_switchmachine['mac'], '28:6e:d4:46:c4:25') class TestUpdateSwitchMachine(BaseTest): @@ -610,24 +645,34 @@ class TestUpdateSwitchMachine(BaseTest): def test_update_switch_machine(self): switch.add_switch_machine( - self.user_object, 1, mac='28:6e:d4:46:c4:25', - port='1' + port='1', + user=self.user_object, ) switch.update_switch_machine( - self.user_object, 1, 1, - tag='test_tag' + tag='test_tag', + user=self.user_object, ) update_switch_machine = switch.list_switch_machines( - self.user_object, - 1 + 1, + user=self.user_object, ) - expected = {'tag': 'test_tag'} + expected = { + 'switch_id': 1, + 'id': 1, + 'mac': '28:6e:d4:46:c4:25', + 'tag': 'test_tag', + 'switch_ip': '0.0.0.0', + 'machine_id': 1, + 'port': '1', + 'switch_machine_id': 1 + } self.assertTrue( - item in update_switch_machine[0].items for item in expected.items() + all(item in update_switch_machine[0].items() + for item in expected.items()) ) @@ -642,23 +687,32 @@ class TestUpdateSwitchmachine(BaseTest): def test_update_switchmachine(self): switch.add_switch_machine( - self.user_object, 1, mac='28:6e:d4:46:c4:25', - port='1' + port='1', + user=self.user_object, ) switch.update_switchmachine( - self.user_object, 1, - location='test_location' + location='test_location', + user=self.user_object, ) update_switchmachine = switch.list_switchmachines( - self.user_object, + user=self.user_object, ) - expected = {'location': 'test_location'} + expected = { + 'switch_id': 1, + 'id': 1, + 'mac': '28:6e:d4:46:c4:25', + 'location': 'test_location', + 'switch_ip': '0.0.0.0', + 'machine_id': 1, + 'port': '1', + 'switch_machine_id': 1 + } self.assertTrue( - item in update_switchmachine[0].items() - for item in expected.items() + all(item in update_switchmachine[0].items() + for item in expected.items()) ) @@ -673,27 +727,29 @@ class TestPatchSwitchMachine(BaseTest): def test_pathc_switch_machine(self): switch.add_switch_machine( - self.user_object, 1, mac='28:6e:d4:46:c4:25', - port='1' + port='1', + user=self.user_object, ) switch.patch_switch_machine( - self.user_object, 1, 1, + user=self.user_object, patched_tag={ 'patched_tag': 'test_patched_tag' } ) switch_patch_switch_machine = switch.list_switch_machines( - self.user_object, - 1 + 1, + user=self.user_object, ) - expected = {'patched_tag': 'test_patched_tag'} + expected = {'tag': { + 'patched_tag': 'test_patched_tag'} + } self.assertTrue( - item in switch_patch_switch_machine[0].items() - for item in expected.items() + all(item in switch_patch_switch_machine[0].items() + for item in expected.items()) ) @@ -708,24 +764,27 @@ class TestPatchSwitchmachine(BaseTest): def test_patch_switchmachine(self): switch.add_switch_machine( - self.user_object, 1, mac='28:6e:d4:46:c4:25', - port='1' + port='1', + user=self.user_object, ) switch.patch_switchmachine( - self.user_object, 1, + user=self.user_object, patched_location={ 'patched_location': 'test_location' } ) patch_switchmachine = switch.list_switchmachines( - self.user_object + user=self.user_object ) - expected = {'patched_location': 'test_location'} + expected = {'location': { + 'patched_location': 'test_location'} + } self.assertTrue( - item in patch_switchmachine[0].items() for item in expected.items() + all(item in patch_switchmachine[0].items() + for item in expected.items()) ) @@ -740,19 +799,19 @@ class TestDelSwitchMachine(BaseTest): def test_del_switch_machine(self): switch.add_switch_machine( - self.user_object, 1, mac='28:6e:d4:46:c4:25', - port='1' + port='1', + user=self.user_object, ) switch.del_switch_machine( - self.user_object, 1, - 1 + 1, + user=self.user_object, ) del_switch_machine = switch.list_switch_machines( - self.user_object, - 1 + 1, + user=self.user_object, ) self.assertEqual([], del_switch_machine) @@ -768,17 +827,17 @@ class TestDelSwitchmachine(BaseTest): def test_switchmachine(self): switch.add_switch_machine( - self.user_object, 1, mac='28:6e:d4:46:c4:25', - port='1' + port='1', + user=self.user_object, ) switch.del_switchmachine( - self.user_object, - 1 + 1, + user=self.user_object, ) del_switchmachine = switch.list_switchmachines( - self.user_object + user=self.user_object ) self.assertEqual([], del_switchmachine) @@ -794,23 +853,23 @@ class TestUpdateSwitchMachines(BaseTest): def test_update_switch_machines_remove(self): switch.add_switch( - self.user_object, - ip='2887583784' + ip='2887583784', + user=self.user_object, ) switch.add_switch_machine( - self.user_object, 2, mac='28:6e:d4:46:c4:25', - port='1' + port='1', + user=self.user_object, ) switch.update_switch_machines( - self.user_object, 2, - remove_machines=1 + remove_machines=1, + user=self.user_object, ) update_remove = switch.list_switch_machines( - self.user_object, - 2 + 2, + user=self.user_object, ) self.assertEqual([], update_remove) diff --git a/compass/tests/db/api/test_user.py b/compass/tests/db/api/test_user.py index 98ea29a5..d13475be 100644 --- a/compass/tests/db/api/test_user.py +++ b/compass/tests/db/api/test_user.py @@ -75,22 +75,25 @@ class TestGetRecordCleanToken(BaseTest): def test_record_user_token(self): token = user_api.record_user_token( - self.user_object, 'test_token', - datetime.datetime.now() + datetime.timedelta(seconds=10000) + datetime.datetime.now() + datetime.timedelta(seconds=10000), + user=self.user_object, ) self.assertIsNotNone(token) self.assertEqual(token['token'], 'test_token') def test_clean_user_token(self): - token = user_api.clean_user_token(self.user_object, 'test_token') + token = user_api.clean_user_token( + 'test_token', + user=self.user_object, + ) self.assertEqual([], token) def test_get_user_object_from_token(self): token = user_api.record_user_token( - self.user_object, 'test_token', - datetime.datetime.now() + datetime.timedelta(seconds=10000) + datetime.datetime.now() + datetime.timedelta(seconds=10000), + user=self.user_object, ) self.assertIsNotNone(token) @@ -112,9 +115,12 @@ class TestGetUser(BaseTest): super(TestGetUser, self).tearDown() def test_get_user(self): - user = user_api.get_user(self.user_object, self.user_object.id) - self.assertIsNotNone(user) - self.assertEqual(user['email'], setting.COMPASS_ADMIN_EMAIL) + get_user = user_api.get_user( + self.user_object.id, + user=self.user_object + ) + self.assertIsNotNone(get_user) + self.assertEqual(get_user['email'], setting.COMPASS_ADMIN_EMAIL) class TestGetCurrentUser(BaseTest): @@ -128,7 +134,7 @@ class TestGetCurrentUser(BaseTest): def test_get_current_user(self): current_user = user_api.get_current_user( - self.user_object + user=self.user_object ) self.assertIsNotNone(current_user) self.assertEqual(current_user['email'], setting.COMPASS_ADMIN_EMAIL) @@ -140,7 +146,7 @@ class TestListUsers(BaseTest): def setUp(self): super(TestListUsers, self).setUp() user_api.add_user( - self.user_object, + user=self.user_object, email='test@huawei.com', password='test' ) @@ -149,11 +155,13 @@ class TestListUsers(BaseTest): super(TestListUsers, self).tearDown() def test_list_users(self): - user = user_api.list_users(self.user_object) - self.assertIsNotNone(user) + list_users = user_api.list_users( + user=self.user_object + ) + self.assertIsNotNone(list_users) result = [] - for item in user: - result.append(item['email']) + for list_user in list_users: + result.append(list_user['email']) expects = ['test@huawei.com', setting.COMPASS_ADMIN_EMAIL] for expect in expects: self.assertIn(expect, result) @@ -170,18 +178,18 @@ class TestAddUser(BaseTest): def test_add_user(self): user_objs = user_api.add_user( - self.user_object, email='test@abc.com', - password='password' + password='password', + user=self.user_object, ) self.assertEqual('test@abc.com', user_objs['email']) def test_add_user_session(self): with database.session() as session: user_objs = user_api.add_user( - self.user_object, email='test@abc.com', password='password', + user=self.user_object, session=session ) self.assertEqual('test@abc.com', user_objs['email']) @@ -197,8 +205,11 @@ class TestDelUser(BaseTest): super(TestDelUser, self).tearDown() def test_del_user(self): - user_api.del_user(self.user_object, self.user_object.id) - del_user = user_api.list_users(self.user_object) + user_api.del_user( + self.user_object.id, + user=self.user_object, + ) + del_user = user_api.list_users(user=self.user_object) self.assertEqual([], del_user) @@ -213,8 +224,8 @@ class TestUpdateUser(BaseTest): def test_update_admin(self): user_objs = user_api.update_user( - self.user_object, self.user_object.id, + user=self.user_object, email=setting.COMPASS_ADMIN_EMAIL, firstname='a', lastname='b', @@ -228,7 +239,7 @@ class TestUpdateUser(BaseTest): def test_not_admin(self): user_api.add_user( - self.user_object, + user=self.user_object, email='dummy@abc.com', password='dummy', is_admin=False @@ -237,8 +248,8 @@ class TestUpdateUser(BaseTest): self.assertRaises( exception.Forbidden, user_api.update_user, - user_object, 2, + user=user_object, is_admin=False ) @@ -254,8 +265,8 @@ class TestGetPermissions(BaseTest): def test_get_permissions(self): user_permissions = user_api.get_permissions( - self.user_object, - self.user_object.id + self.user_object.id, + user=self.user_object, ) self.assertIsNotNone(user_permissions) result = [] @@ -275,9 +286,9 @@ class TestGetPermission(BaseTest): def test_get_permission(self): user_permission = user_api.get_permission( - self.user_object, self.user_object.id, 1, + user=self.user_object, ) self.assertEqual(user_permission['name'], 'list_permissions') @@ -294,13 +305,13 @@ class TestAddDelUserPermission(BaseTest): def test_add_permission(self): user_api.add_permission( - self.user_object, self.user_object.id, + user=self.user_object, permission_id=2 ) permissions = user_api.get_permissions( - self.user_object, - self.user_object.id + self.user_object.id, + user=self.user_object, ) result = None for permission in permissions: @@ -310,14 +321,14 @@ class TestAddDelUserPermission(BaseTest): def test_add_permission_position(self): user_api.add_permission( - self.user_object, self.user_object.id, True, - 2 + 2, + user=self.user_object, ) permissions = user_api.get_permissions( - self.user_object, - self.user_object.id + self.user_object.id, + user=self.user_object, ) result = None for permission in permissions: @@ -328,33 +339,14 @@ class TestAddDelUserPermission(BaseTest): def test_add_permission_session(self): with database.session() as session: user_api.add_permission( - self.user_object, self.user_object.id, + user=self.user_object, permission_id=2, session=session ) permissions = user_api.get_permissions( - self.user_object, - self.user_object.id - ) - result = None - for permission in permissions: - if permission['id'] == 2: - result = permission['name'] - self.assertEqual(result, 'list_switches') - - def test_add_permission_position_session(self): - with database.session() as session: - user_api.add_permission( - self.user_object, - self.user_object.id, - True, - 2, - session - ) - permissions = user_api.get_permissions( - self.user_object, - self.user_object.id + self.user_object.id, + user=self.user_object, ) result = None for permission in permissions: @@ -364,13 +356,13 @@ class TestAddDelUserPermission(BaseTest): def test_del_permission(self): user_api.del_permission( - self.user_object, self.user_object.id, - 1 + 1, + user=self.user_object, ) del_user = user_api.get_permissions( - self.user_object, - self.user_object.id + self.user_object.id, + user=self.user_object, ) self.assertEqual([], del_user) @@ -386,25 +378,25 @@ class TestUpdatePermissions(BaseTest): def test_remove_permissions(self): user_api.update_permissions( - self.user_object, self.user_object.id, + user=self.user_object, remove_permissions=1 ) del_user_permission = user_api.get_permissions( - self.user_object, - self.user_object.id + self.user_object.id, + user=self.user_object, ) self.assertEqual([], del_user_permission) def test_add_permissions(self): user_api.update_permissions( - self.user_object, self.user_object.id, + user=self.user_object, add_permissions=2 ) permissions = user_api.get_permissions( - self.user_object, - self.user_object.id + self.user_object.id, + user=self.user_object, ) result = None for permission in permissions: diff --git a/compass/tests/db/api/test_user_log.py b/compass/tests/db/api/test_user_log.py index 12535fa9..ed62626f 100644 --- a/compass/tests/db/api/test_user_log.py +++ b/compass/tests/db/api/test_user_log.py @@ -46,16 +46,19 @@ class TestListUserActions(BaseTest): def test_list_user_actions(self): user_log.log_user_action( self.user_object.id, - action='/testaction' + action='/users/login' ) user_action = user_log.list_user_actions( - self.user_object, - self.user_object.id - ) - self.assertEqual( - 1, - user_action[0]['user_id'] + self.user_object.id, + user=self.user_object ) + expected = { + 'action': '/users/login', + 'user_id': 1 + } + self.assertTrue( + all(item in user_action[0].items() + for item in expected.items())) def test_list_none_user_actions(self): user_log.log_user_action( @@ -63,8 +66,8 @@ class TestListUserActions(BaseTest): action='/testaction' ) user_action = user_log.list_user_actions( - self.user_object, - 2 + 2, + user=self.user_object ) self.assertEqual([], user_action) @@ -83,8 +86,16 @@ class TestListActions(BaseTest): self.user_object.id, action='/testaction' ) - action = user_log.list_actions(self.user_object) + action = user_log.list_actions(user=self.user_object) self.assertIsNotNone(action) + expected = { + 'action': '/testaction', + 'user_id': 1 + } + print action + self.assertTrue( + all(item in action[0].items() + for item in expected.items())) class TestDelUserActions(BaseTest): @@ -102,12 +113,12 @@ class TestDelUserActions(BaseTest): action='/testaction' ) user_log.del_user_actions( - self.user_object, - self.user_object.id + self.user_object.id, + user=self.user_object ) del_user_action = user_log.list_user_actions( - self.user_object, - self.user_object.id + self.user_object.id, + user=self.user_object ) self.assertEqual([], del_user_action) @@ -127,10 +138,10 @@ class TestDelActions(BaseTest): action='/testaction' ) user_log.del_actions( - self.user_object + user=self.user_object ) del_action = user_log.list_actions( - self.user_object + user=self.user_object ) self.assertEqual([], del_action)