def test_get_endpoint_ids_connection_error(self):
     """Test to get endpoint id with connection error."""
     client = SourcesHTTPClient(auth_header=Config.SOURCES_FAKE_HEADER,
                                source_id=self.source_id)
     with requests_mock.mock() as m:
         m.get(
             f"http://www.sources.com/api/v1.0/endpoints?filter[source_id]={self.source_id}",
             exc=RequestException)
         with self.assertRaises(SourcesHTTPClientError):
             client.get_endpoint_id()
Example #2
0
    def test_get_endpoint_id_no_data(self):
        """Test to get endpoint_id from Source_id with no data in response."""
        source_id = 3

        client = SourcesHTTPClient(auth_header=Config.SOURCES_FAKE_HEADER,
                                   source_id=source_id)
        with requests_mock.mock() as m:
            m.get(
                f'http://www.sources.com/api/v1.0/endpoints?filter[source_id]={source_id}',
                status_code=200,
                json={'data': []})
            with self.assertRaises(SourcesHTTPClientError):
                client.get_endpoint_id()
    def test_get_endpoint_id_misconfigured(self):
        """Test to get endpoint_id from Source_id with route not found."""
        resource_id = 2
        source_id = 3

        client = SourcesHTTPClient(auth_header=Config.SOURCES_FAKE_HEADER, source_id=source_id)
        with requests_mock.mock() as m:
            m.get(
                f"http://www.sources.com/api/v1.0/endpoints?filter[source_id]={source_id}",
                status_code=404,
                json={"data": [{"id": resource_id}]},
            )
            with self.assertRaises(SourcesHTTPClientError):
                client.get_endpoint_id()
Example #4
0
 def __init__(self, auth_header, source_id):
     """Constructor."""
     sources_network = SourcesHTTPClient(auth_header, source_id)
     details = sources_network.get_source_details()
     self.name = details.get("name")
     self.source_type_id = int(details.get("source_type_id"))
     self.source_uuid = details.get("uid")
     self.source_type_name = sources_network.get_source_type_name(self.source_type_id)
     self.endpoint_id = sources_network.get_endpoint_id()
     self.source_type = SOURCE_PROVIDER_MAP.get(self.source_type_name)
    def test_get_endpoint_id(self):
        """Test to get endpoint_id from Source_id."""
        resource_id = 2
        source_id = 3

        client = SourcesHTTPClient(auth_header=Config.SOURCES_FAKE_HEADER, source_id=source_id)
        with requests_mock.mock() as m:
            m.get(
                f"http://www.sources.com/api/v1.0/endpoints?filter[source_id]={source_id}",
                status_code=200,
                json={"data": [{"id": resource_id}]},
            )
            response = client.get_endpoint_id()
            self.assertEqual(response, resource_id)
Example #6
0
def sources_network_info(source_id, auth_header):
    """
    Get additional sources context from Sources REST API.

    Additional details retrieved from the network includes:
        - Source Name
        - Source ID Type -> AWS, Azure, or OCP
        - Authentication: OCP -> Source uid; AWS -> Network call to Sources Authentication Store

    Details are stored in the Sources database table.

    Args:
        source_id (Integer): Source identifier
        auth_header (String): Authentication Header.

    Returns:
        None

    """
    sources_network = SourcesHTTPClient(auth_header, source_id)
    try:
        source_details = sources_network.get_source_details()
    except SourcesHTTPClientError as conn_err:
        err_msg = f'Unable to get for Source {source_id} information. Reason: {str(conn_err)}'
        LOG.error(err_msg)
        return
    source_name = source_details.get('name')
    source_type_id = int(source_details.get('source_type_id'))
    source_uuid = source_details.get('uid')
    source_type_name = sources_network.get_source_type_name(source_type_id)
    endpoint_id = sources_network.get_endpoint_id()

    if not endpoint_id and not source_type_name == SOURCES_OCP_SOURCE_NAME:
        LOG.error(f'Unable to find endpoint for Source ID: {source_id}')
        return

    if source_type_name == SOURCES_OCP_SOURCE_NAME:
        source_type = 'OCP'
    elif source_type_name == SOURCES_AWS_SOURCE_NAME:
        source_type = 'AWS'
    elif source_type_name == SOURCES_AZURE_SOURCE_NAME:
        source_type = 'AZURE'
    else:
        LOG.error(f'Unexpected source type ID: {source_type_id}')
        return

    storage.add_provider_sources_network_info(source_id, source_uuid,
                                              source_name, source_type,
                                              endpoint_id)
    save_auth_info(auth_header, source_id)
Example #7
0
def sources_network_info(source_id, auth_header):
    """
    Get additional sources context from Sources REST API.

    Additional details retrieved from the network includes:
        - Source Name
        - Source ID Type -> AWS, Azure, or OCP
        - Authentication: OCP -> Source uid; AWS -> Network call to Sources Authentication Store

    Details are stored in the Sources database table.

    Args:
        source_id (Integer): Source identifier
        auth_header (String): Authentication Header.

    Returns:
        None

    """
    sources_network = SourcesHTTPClient(auth_header, source_id)
    source_details = sources_network.get_source_details()
    source_name = source_details.get("name")
    source_type_id = int(source_details.get("source_type_id"))
    source_uuid = source_details.get("uid")
    source_type_name = sources_network.get_source_type_name(source_type_id)
    endpoint_id = sources_network.get_endpoint_id()

    if not endpoint_id and not source_type_name == SOURCES_OCP_SOURCE_NAME:
        LOG.warning(f"Unable to find endpoint for Source ID: {source_id}")
        return

    source_type = SOURCE_PROVIDER_MAP.get(source_type_name)
    if not source_type:
        LOG.warning(f"Unexpected source type ID: {source_type_id}")
        return

    storage.add_provider_sources_network_info(source_id, source_uuid,
                                              source_name, source_type,
                                              endpoint_id)
    save_auth_info(auth_header, source_id)