util.py 文件源码

python
阅读 25 收藏 0 点赞 0 评论 0

项目:oneview-redfish-toolkit 作者: HewlettPackard 项目源码 文件源码
def get_oneview_client(session_id=None, is_service_root=False):
    """Establishes a OneView connection to be used in the module

        Establishes a OV connection if one does not exists.
        If one exists, do a single OV access to check if its sill
        valid. If not tries to establish a new connection.
        Sets the connection on the ov_conn global var

        Args:
            session_id: The ID of a valid authenticated session, if the
            authentication_mode is session. Defaults to None.

            is_service_root: Informs if who is calling this function is the
            ServiceRoot blueprint. If true, even if authentication_mode is
            set to session it will use the information on the conf file to
            return a connection.  This is a workaround to allow ServiceRoot
            to retrieve the appliance UUID before user logs in.

        Returns:
            OneViewClient object

        Exceptions:
            HPOneViewException if can't connect or reconnect to OV
    """

    config = globals()['config']

    auth_mode = config["redfish"]["authentication_mode"]

    if auth_mode == "conf" or is_service_root:
        # Doing conf based authentication
        ov_client = globals()['ov_client']
        ov_config = globals()['ov_config']

        # Check if connection is ok yet
        try:
            ov_client.connection.get('/rest/logindomains')
            return ov_client
        # If expired try to make a new connection
        except Exception:
            try:
                logging.exception('Re-authenticated')
                ov_client.connection.login(ov_config['credentials'])
                return ov_client
            # if faild abort
            except Exception:
                raise
    else:
        # Auth mode is session
        oneview_config = dict(config.items('oneview_config'))
        oneview_config['credentials'] = {"sessionID": session_id}
        oneview_config['api_version'] = int(oneview_config['api_version'])
        try:
            oneview_client = OneViewClient(oneview_config)
            oneview_client.connection.get('/rest/logindomains')
            return oneview_client
        except Exception:
            logging.exception("Failed to recover session based connection")
            raise
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号