예제 #1
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

    """
    src_details = SourceDetails(auth_header, source_id)

    if not src_details.source_type:
        LOG.warning(f"Unexpected source type ID: {src_details.source_type_id}")
        return

    storage.add_provider_sources_network_info(src_details, source_id)
    save_auth_info(auth_header, source_id)
    app_settings = src_details.app_settings
    if app_settings:
        try:
            storage.update_application_settings(source_id, app_settings)
        except storage.SourcesStorageError as error:
            LOG.error(
                f"Unable to apply application settings. error: {str(error)}")
            return
예제 #2
0
    def test_update_application_settings(self):
        """Test to update application settings."""
        test_source_id = 3
        resource_group = "testrg"
        subscription_id = "testsubid"
        settings = {
            "billing_source": {
                "data_source": {
                    "resource_group": resource_group
                }
            },
            "authentication": {
                "subscription_id": subscription_id
            },
        }
        azure_obj = Sources(
            source_id=test_source_id,
            auth_header=self.test_header,
            offset=3,
            source_type=Provider.PROVIDER_AZURE,
            name="Test AZURE Source",
        )
        azure_obj.save()

        storage.update_application_settings(test_source_id, settings)
        db_obj = Sources.objects.get(source_id=test_source_id)

        self.assertEqual(db_obj.authentication.get("subscription_id"),
                         subscription_id)
        self.assertEqual(
            db_obj.billing_source.get("data_source").get("resource_group"),
            resource_group)
예제 #3
0
    def test_update_application_settings_billing_and_auth_starting_with_billing(
            self):
        """Test to update application settings with both billing and auth where billing already exists."""
        test_source_id = 3
        subscription_id = "testsubid"
        tenant_id = "testtenant"
        client_id = "myclientid"
        client_secret = "mysecret"
        resource_group = "myrg"
        storage_account = "mysa"
        settings = {
            "billing_source": {
                "data_source": {
                    "storage_account": storage_account
                }
            },
            "authentication": {
                "credentials": {
                    "subscription_id": subscription_id,
                    "client_id": client_id,
                    "tenant_id": tenant_id,
                    "client_secret": client_secret,
                }
            },
        }

        azure_obj = Sources(
            source_id=test_source_id,
            auth_header=self.test_header,
            offset=3,
            billing_source={"data_source": {
                "resource_group": resource_group
            }},
            source_type=Provider.PROVIDER_AZURE,
            name="Test AZURE Source",
        )
        azure_obj.save()
        storage.update_application_settings(test_source_id, settings)
        db_obj = Sources.objects.get(source_id=test_source_id)

        self.assertEqual(
            db_obj.authentication.get("credentials").get("subscription_id"),
            subscription_id)
        self.assertEqual(
            db_obj.authentication.get("credentials").get("client_secret"),
            client_secret)
        self.assertEqual(
            db_obj.authentication.get("credentials").get("client_id"),
            client_id)
        self.assertEqual(
            db_obj.authentication.get("credentials").get("tenant_id"),
            tenant_id)
        self.assertEqual(
            db_obj.billing_source.get("data_source").get("resource_group"),
            resource_group)
        self.assertEqual(
            db_obj.billing_source.get("data_source").get("storage_account"),
            storage_account)
예제 #4
0
    def test_update_application_settings_gcp_table_id_present(self):
        """Test to update application settings for GCP when table ID is known."""
        test_source_id = 3
        old_dataset = "olddataset"
        new_dataset = "newdataset"
        project_id = "myproject"
        table_id = "bigquerytableid"
        settings = {
            "billing_source": {
                "data_source": {
                    "dataset": new_dataset
                }
            }
        }

        gcp_obj = Sources(
            source_id=test_source_id,
            auth_header=self.test_header,
            offset=3,
            authentication={"credentials": {
                "project_id": project_id
            }},
            billing_source={
                "data_source": {
                    "dataset": old_dataset,
                    "table_id": table_id
                }
            },
            source_type=Provider.PROVIDER_GCP,
            name="Test GCP Source",
        )
        gcp_obj.save()

        storage.update_application_settings(test_source_id, settings)
        db_obj = Sources.objects.get(source_id=test_source_id)

        self.assertEqual(
            db_obj.authentication.get("credentials").get("project_id"),
            project_id)
        self.assertEqual(
            db_obj.billing_source.get("data_source").get("dataset"),
            new_dataset)
        self.assertEqual(
            db_obj.billing_source.get("data_source").get("table_id"), table_id)
예제 #5
0
    def test_update_application_settings_only_authentication(self):
        """Test to update application settings (only authentication)."""
        test_source_id = 3
        subscription_id = "testsubid"
        settings = {"authentication": {"subscription_id": subscription_id}}
        azure_obj = Sources(
            source_id=test_source_id,
            auth_header=self.test_header,
            offset=3,
            source_type=Provider.PROVIDER_AZURE,
            name="Test AZURE Source",
        )
        azure_obj.save()

        storage.update_application_settings(test_source_id, settings)
        db_obj = Sources.objects.get(source_id=test_source_id)

        self.assertEqual(db_obj.authentication.get("subscription_id"), subscription_id)
        self.assertIsNone(db_obj.billing_source.get("data_source"))