# Path: opsmgr/inventory/resource_mgr.py
# I_MANAGER_DEVICE_PLUGIN = "opsmgr.inventory.interfaces.IManagerDevicePlugin"
# I_MANAGER_DEVICE_HOOK = "opsmgr.inventory.interfaces.IManagerDeviceHook"
# I_MANAGER_RACK_HOOK = "opsmgr.inventory.interfaces.IManagerRackHook"
# def add_resource(label, device_type, address, userid, password, rackid='', rack_location='',
#                  ssh_key=None, offline=False):
# def change_resource_password(label=None, deviceid=None, old_password=None, new_password=None):
# def change_resource_properties(label=None, deviceid=None, new_label=None,
#                                userid=None, password=None, address=None,
#                                rackid=None, rack_location=None, ssh_key=None):
# def list_resources(labels=None, isbriefly=False, device_types=None, deviceids=None,
#                    list_device_id=False, is_detail=False, racks=None):
# def remove_resource(labels=None, all_devices=False, deviceids=None):
# def list_resource_types():
# def get_labels_message(items):
# def get_resource_id_by_label(resource_label):
# def add_resource_roles(resource_label=None, resource_id=None, roles=None, additional_data=None):
# def validate(address, userid, password, device_type, ssh_key=None):
# def _validate(address, userid, password, device_type, ssh_key=None):
# def _get_devicetag_text_id(tag_name):
# def _change_device_key(device, address, userid, ssh_key_string, password):
# def _change_device_userpass(device, address, userid, password):
# def _load_device_plugins():
# def _load_inventory_device_plugins():
# def _check_device_exist(session, address):
# def _check_device_exist_by_props(session, device_type, mtm, serialnum):
# def validate_address(address):
# def validate_label(label):
# def _check_address(address):
# 
# Path: opsmgr/common/utils.py
# def entry_exit(exclude_index=None, exclude_name=None, log_name=None, level=logging.INFO):
#     """
#     it's a decorator that to add entry and exit log for a function
# 
#     input:
#     excludeIndex -- the index of params that you don't want to be record
#     excludeName -- the name of dictionary params that you don't wnat to be record
#     log_name -- name of the log to write the logging to.
#     level -- the logging level to log the entry/exit with.  default is INFO level
#     """
# 
#     def f(func):
#         @functools.wraps(func)
#         def wrapper(*args, **kwargs):
# 
#             args_for_print = []
#             tmp_index = 0
#             arg_len = len(args)
#             while tmp_index < arg_len:
#                 if tmp_index not in exclude_index:
#                     args_for_print.append(args[tmp_index])
#                 tmp_index += 1
# 
#             kwargs_for_print = {}
#             for a in kwargs:
#                 if a in exclude_name:
#                     continue
#                 else:
#                     kwargs_for_print[a] = kwargs[a]
# 
#             logging.getLogger(log_name).log(level,
#                                             "%s::Entry params %s %s", func.__name__, "{}".format(
#                                                 args_for_print), "{}".format(kwargs_for_print))
#             result = func(*args, **kwargs)
#             logging.getLogger(log_name).log(level,
#                                             "%s::Exit %s ", func.__name__, "{}".format(result))
#             return result
#         return wrapper
#     return f
# 
# def push_message(message, new_message):
#     if new_message is not None and len(new_message) > 0:
#         if message:
#             message = message + '\n' + new_message
#         else:
#             message = new_message
#     return message
# 
# def load_plugin_by_namespace(namespace):
#     plugins = {}
#     extensions = extension.ExtensionManager(namespace=namespace)
# 
#     for ext in extensions:
#         plugins[ext.name] = ext.plugin()
#     return plugins
# 
# Path: opsmgr/inventory/data_model.py
# class Rack(Base):
#     __tablename__ = "rack"
#     rack_id = Column(Integer, primary_key=True)
# 
#     # user defined label for rack
#     label = Column(String(255), nullable=False, unique=True)
# 
#     data_center = Column(String(255))
#     room = Column(String(255))
#     row = Column(String(255))
#     notes = Column(Text())
# 
#     resources = relationship("Resource", back_populates="rack")
# 
#     def to_dict_obj(self):
#         result = {}
#         result["rackid"] = self.rack_id
#         result["label"] = self.label
#         result["room"] = self.room
#         result["row"] = self.row
#         result["data-center"] = self.data_center
#         result["notes"] = self.notes
#         return result
# 
#     def __repr__(self, *args, **kwargs):
#         _dict = self.to_dict_obj()
#         return str(_dict)


import gettext
import logging
from opsmgr.inventory import persistent_mgr, resource_mgr
from opsmgr.common.utils import entry_exit, push_message, load_plugin_by_namespace
from opsmgr.inventory.data_model import Rack
                "%s::rack (%s) has devices. only empty racks can be removed.", _method_, label)
            continue

        try:
            for hook_name, hook_plugin in hooks.items():
                hook_plugin.remove_rack_pre_save(rack)
        except Exception as e:
            logging.exception(e)
            not_remove_racks.append(rack)
            not_remove_racks_msgs.append(
                _("Error in plugin (%s). Unable to remove resource: Reason: %s") % (hook_name, e))
            continue

        # ok to remove rack.
        remove_racks.append(rack)

    result_message = ""
    ret = 0
    if len(remove_racks) > 0:
        persistent_mgr.delete_racks(session, remove_racks)
        labels_message = resource_mgr.get_labels_message(remove_racks)
        message = push_message(message, _("racks removed: %s") % labels_message)

        # Call hook for remove_rack_post_save
        for rack in remove_racks:
            try:
                for hook_name, hook_plugin in hooks.items():
                    hook_plugin.remove_rack_post_save(rack)
            except Exception as e:
                logging.exception(e)

# Path: opsmgr/inventory/resource_mgr.py
# I_MANAGER_DEVICE_PLUGIN = "opsmgr.inventory.interfaces.IManagerDevicePlugin"
# I_MANAGER_DEVICE_HOOK = "opsmgr.inventory.interfaces.IManagerDeviceHook"
# I_MANAGER_RACK_HOOK = "opsmgr.inventory.interfaces.IManagerRackHook"
# def add_resource(label, device_type, address, userid, password, rackid='', rack_location='',
#                  ssh_key=None, offline=False):
# def change_resource_password(label=None, deviceid=None, old_password=None, new_password=None):
# def change_resource_properties(label=None, deviceid=None, new_label=None,
#                                userid=None, password=None, address=None,
#                                rackid=None, rack_location=None, ssh_key=None):
# def list_resources(labels=None, isbriefly=False, device_types=None, deviceids=None,
#                    list_device_id=False, is_detail=False, racks=None):
# def remove_resource(labels=None, all_devices=False, deviceids=None):
# def list_resource_types():
# def get_labels_message(items):
# def get_resource_id_by_label(resource_label):
# def add_resource_roles(resource_label=None, resource_id=None, roles=None, additional_data=None):
# def validate(address, userid, password, device_type, ssh_key=None):
# def _validate(address, userid, password, device_type, ssh_key=None):
# def _get_devicetag_text_id(tag_name):
# def _change_device_key(device, address, userid, ssh_key_string, password):
# def _change_device_userpass(device, address, userid, password):
# def _load_device_plugins():
# def _load_inventory_device_plugins():
# def _check_device_exist(session, address):
# def _check_device_exist_by_props(session, device_type, mtm, serialnum):
# def validate_address(address):
# def validate_label(label):
# def _check_address(address):
# 
# Path: opsmgr/common/utils.py
# def entry_exit(exclude_index=None, exclude_name=None, log_name=None, level=logging.INFO):
#     """
#     it's a decorator that to add entry and exit log for a function
# 
#     input:
#     excludeIndex -- the index of params that you don't want to be record
#     excludeName -- the name of dictionary params that you don't wnat to be record
#     log_name -- name of the log to write the logging to.
#     level -- the logging level to log the entry/exit with.  default is INFO level
#     """
# 
#     def f(func):
#         @functools.wraps(func)
#         def wrapper(*args, **kwargs):
# 
#             args_for_print = []
#             tmp_index = 0
#             arg_len = len(args)
#             while tmp_index < arg_len:
#                 if tmp_index not in exclude_index:
#                     args_for_print.append(args[tmp_index])
#                 tmp_index += 1
# 
#             kwargs_for_print = {}
#             for a in kwargs:
#                 if a in exclude_name:
#                     continue
#                 else:
#                     kwargs_for_print[a] = kwargs[a]
# 
#             logging.getLogger(log_name).log(level,
#                                             "%s::Entry params %s %s", func.__name__, "{}".format(
#                                                 args_for_print), "{}".format(kwargs_for_print))
#             result = func(*args, **kwargs)
#             logging.getLogger(log_name).log(level,
#                                             "%s::Exit %s ", func.__name__, "{}".format(result))
#             return result
#         return wrapper
#     return f
# 
# def push_message(message, new_message):
#     if new_message is not None and len(new_message) > 0:
#         if message:
#             message = message + '\n' + new_message
#         else:
#             message = new_message
#     return message
# 
# def load_plugin_by_namespace(namespace):
#     plugins = {}
#     extensions = extension.ExtensionManager(namespace=namespace)
# 
#     for ext in extensions:
#         plugins[ext.name] = ext.plugin()
#     return plugins
# 
# Path: opsmgr/inventory/data_model.py
# class Rack(Base):
#     __tablename__ = "rack"
#     rack_id = Column(Integer, primary_key=True)
# 
#     # user defined label for rack
#     label = Column(String(255), nullable=False, unique=True)
# 
#     data_center = Column(String(255))
#     room = Column(String(255))
#     row = Column(String(255))
#     notes = Column(Text())
# 
#     resources = relationship("Resource", back_populates="rack")
# 
#     def to_dict_obj(self):
#         result = {}
#         result["rackid"] = self.rack_id
#         result["label"] = self.label
#         result["room"] = self.room
#         result["row"] = self.row
#         result["data-center"] = self.data_center
#         result["notes"] = self.notes
#         return result
# 
#     def __repr__(self, *args, **kwargs):
#         _dict = self.to_dict_obj()
#         return str(_dict)


# Path: opsmgr/inventory/rack_mgr.py
import gettext
import logging
from opsmgr.inventory import persistent_mgr, resource_mgr
from opsmgr.common.utils import entry_exit, push_message, load_plugin_by_namespace
from opsmgr.inventory.data_model import Rack

                "%s::rack (%s) has devices. only empty racks can be removed.", _method_, label)
            continue

        try:
            for hook_name, hook_plugin in hooks.items():
                hook_plugin.remove_rack_pre_save(rack)
        except Exception as e:
            logging.exception(e)
            not_remove_racks.append(rack)
            not_remove_racks_msgs.append(
                _("Error in plugin (%s). Unable to remove resource: Reason: %s") % (hook_name, e))
            continue

        # ok to remove rack.
        remove_racks.append(rack)

    result_message = ""
    ret = 0
    if len(remove_racks) > 0:
        persistent_mgr.delete_racks(session, remove_racks)
        labels_message = resource_mgr.get_labels_message(remove_racks)
        message = push_message(message, _("racks removed: %s") % labels_message)

        # Call hook for remove_rack_post_save
        for rack in remove_racks:
            try:
                for hook_name, hook_plugin in hooks.items():
                    hook_plugin.remove_rack_post_save(rack)
            except Exception as e:
                logging.exception(e)

                message = push_message(message, _("After rack (%s) was removed. "