Esempio n. 1
0
    def test_get_oneview_client(self, mock_get_map_clients, mock_ov_client,
                                mock_config):
        ip_oneview = "1.1.1.1"
        token = "abc123"
        mock_config.auth_mode_is_session.return_value = True
        mock_ov_client.return_value = 'ov_client'
        result = client_session.get_oneview_client(ip_oneview, token)
        self.assertEqual(result, mock_ov_client.return_value)

        mock_config.auth_mode_is_session.return_value = False
        mock_config.auth_mode_is_conf.return_value = True
        mock_get_map_clients.return_value = 'ov_client'
        result = client_session.get_oneview_client(ip_oneview)
        self.assertEqual(result, mock_ov_client.return_value)
    def get_scmb_certs(self):
        # Get CA
        ov_client = client_session.get_oneview_client(self.ov_ip, self.token)

        cert = self._get_ov_ca_cert_base64data(ov_client)

        if cert is None:
            raise OneViewRedfishException(
                "Failed to fetch OneView CA Certificate")

        # Create the base scmb dir to save the scmb files
        os.makedirs(name=_scmb_base_dir(), exist_ok=True)

        # Create the dir to save the scmb files for respective ov
        os.makedirs(name=_scmb_oneview_dir(self.ov_ip), exist_ok=True)

        with open(_oneview_ca_path(self.ov_ip), 'w+') as f:
            f.write(cert)

        certs = self._get_scmb_certificate_from_ov(ov_client)

        # Save cert
        with open(_scmb_cert_path(self.ov_ip), 'w+') as f:
            f.write(certs['base64SSLCertData'])

        # Save key
        with open(_scmb_key_path(self.ov_ip), 'w+') as f:
            f.write(certs['base64SSLKeyData'])
Esempio n. 3
0
def query_ov_client_by_resource(resource_id, resource, function, *args,
                                **kwargs):
    """Query resource on OneViews.

        Query specific resource ID on multiple OneViews.
        Look resource ID on cached map ResourceID->OneViewIP for query
        on specific cached OneView IP.
        If the resource ID is not cached yet it searches on all OneViews.

        Returns:
            dict: OneView resource
    """
    # Get OneView's IP in the single OneView context
    single_oneview_ip = single.is_single_oneview_context() and \
        single.get_single_oneview_ip()
    # Get OneView's IP for cached resource ID
    cached_oneview_ip = get_ov_ip_by_resource(resource_id)

    # Get OneView's IP in the single OneView context or cached by resource ID
    ip_oneview = single_oneview_ip or cached_oneview_ip

    # If resource is not cached yet search in all OneViews
    if not ip_oneview:
        return search_resource_multiple_ov(resource, function, resource_id,
                                           None, *args, **kwargs)

    # If it's Single Oneview context and no IP is saved on context yet
    if single.is_single_oneview_context() and not single_oneview_ip:
        single.set_single_oneview_ip(ip_oneview)

    ov_client = client_session.get_oneview_client(ip_oneview)

    try:
        resp = execute_query_ov_client(ov_client, resource, function, *args,
                                       **kwargs)
    except HPOneViewException as e:
        if e.oneview_response["errorCode"] not in NOT_FOUND_ONEVIEW_ERRORS:
            raise

        cleanup_map_resources_entry(resource_id)
        ov_ips = config.get_oneview_multiple_ips()
        ov_ips.remove(ip_oneview)  # just search in the other ips

        if not ov_ips:
            raise

        return search_resource_multiple_ov(resource, function, resource_id,
                                           ov_ips, *args, **kwargs)

    # If it's on Single OneView Context and the resource is not
    # mapped to an OneView IP, then we update cache in advance for
    # future requests for this resource
    if single_oneview_ip and not cached_oneview_ip:
        set_map_resources_entry(resource_id, single_oneview_ip)

    return resp
Esempio n. 4
0
def get_managers(uuid):
    """Get the Redfish Managers.

        Get method to return Managers JSON when
        /redfish/v1/Managers/id is requested.

        Returns:
            JSON: JSON with Managers info.
    """
    try:
        state_url = "/controller-state.json"
        ov_health_status_url = "/rest/appliance/health-status"

        ov_ip = get_oneview_ip_by_manager_uuid(uuid)
        if not ov_ip:
            abort(status.HTTP_404_NOT_FOUND,
                  "Manager with id {} was not found".format(uuid))

        ov_client = client_session.get_oneview_client(ov_ip)

        ov_appliance_info = multiple_oneview.execute_query_ov_client(
            ov_client, "appliance_node_information", "get_version"
        )
        ov_appliance_state = multiple_oneview.execute_query_ov_client(
            ov_client, "connection", "get", state_url
        )
        ov_appliance_health_status = multiple_oneview.execute_query_ov_client(
            ov_client, "connection", "get", ov_health_status_url
        )

        manager = Manager(ov_appliance_info,
                          ov_appliance_state,
                          ov_appliance_health_status
                          )

        json_str = manager.serialize()

        response = Response(
            response=json_str,
            status=status.HTTP_200_OK,
            mimetype="application/json")
        return response
    except HPOneViewException as e:
        # In case of error log exception and abort
        logging.exception(e)
        abort(status.HTTP_404_NOT_FOUND, "Manager not found")
Esempio n. 5
0
def search_resource_multiple_ov(resource, function, resource_id, ov_ips, *args,
                                **kwargs):
    """Search resource on multiple OneViews

        Query resource on all OneViews.
        If it's looking for a specific resource:
            -Once resource is found it will cache the resource ID for the
                OneView's IP that was found;
            -If it is not found return NotFound exception.
        If it's looking for all resources(get_all):
            -Always query on all OneViews and return a list appended the
                results for all OneViews

        Args:
            resource: resource type (server_hardware)
            function: resource function name (get_all)
            resource_id: set only if it should look for a specific resource ID
            ov_ips: List of Oneview IPs to search for the resource.
                If None is passed, it will search in all IPs based on the
                toolkit configuration.
            *args: original arguments for the OneView client query
            **kwargs: original keyword arguments for the OneView client query

        Returns:
            OneView resource(s)

        Exceptions:
            HPOneViewException: When occur an error on any OneViews which is
            not an not found error.
    """
    result = []
    error_not_found = []
    single_oneview_ip = single.is_single_oneview_context() and \
        single.get_single_oneview_ip()

    # If it's on Single Oneview Context and there is already an OneView IP
    # on the context, then uses it. If not search on All OneViews
    if not ov_ips and single_oneview_ip:
        list_ov_ips = [single_oneview_ip]
    else:
        list_ov_ips = ov_ips or config.get_oneview_multiple_ips()

    # Loop in all OneView's IP
    for ov_ip in list_ov_ips:

        ov_client = client_session.get_oneview_client(ov_ip)

        try:
            # Query resource on OneView
            expected_resource = \
                execute_query_ov_client(ov_client, resource, function,
                                        *args, **kwargs)

            if expected_resource:
                # If it's looking for a especific resource and was found
                if resource_id:
                    set_map_resources_entry(resource_id, ov_ip)

                    # If it's SingleOneviewContext and there is no OneView IP
                    # on the context, then set OneView's IP on the context
                    if single.is_single_oneview_context() and \
                            not single_oneview_ip:
                        single.set_single_oneview_ip(ov_ip)

                    return expected_resource
                else:
                    # If it's looking for a resource list (get_all)
                    if isinstance(expected_resource, list):
                        result.extend(expected_resource)
                    else:
                        result.append(expected_resource)
        except HPOneViewException as exception:
            # If get any error that is not a notFoundError
            if exception.oneview_response["errorCode"] not in \
                    NOT_FOUND_ONEVIEW_ERRORS:
                logging.exception("Error while searching on multiple "
                                  "OneViews for Oneview {}: {}".format(
                                      ov_ip, exception))
                raise exception

            error_not_found.append(exception)

    # If it's looking for a specific resource returns a NotFound exception
    if resource_id and error_not_found:
        raise error_not_found.pop()

    return result