예제 #1
0
    def get_accounts_from_source(self):
        """
        Retrieve all accounts from the Koku database.

        This will return a list of dicts for the Orchestrator to use to access reports.

        Args:
            None

        Returns:
            ([{}]) : A list of dicts

        """
        collector = ProviderCollector()
        all_providers = collector.get_providers()
        collector.close_session()

        accounts = []
        for provider in all_providers:
            provider_accessor = ProviderDBAccessor(provider.uuid)
            accounts.append({
                'authentication':
                provider_accessor.get_authentication(),
                'billing_source':
                provider_accessor.get_billing_source(),
                'customer_name':
                provider_accessor.get_customer_name(),
                'provider_type':
                provider_accessor.get_type(),
                'schema_name':
                provider_accessor.get_schema()
            })
            provider_accessor.close_session()
        return accounts
예제 #2
0
 def test_get_uuids(self):
     """Test getting all uuids."""
     collector = ProviderCollector()
     providers = collector.get_providers()
     test_provider_found = False
     for provider in providers:
         if uuid.UUID(self.aws_provider_uuid) == provider.uuid:
             test_provider_found = True
     self.assertTrue(test_provider_found)
예제 #3
0
 def test_get_uuids(self):
     """Test getting all uuids."""
     collector = ProviderCollector()
     providers = collector.get_providers()
     test_provider_found = False
     for provider in providers:
         if '6e212746-484a-40cd-bba0-09a19d132d64' in provider.uuid:
             test_provider_found = True
     self.assertTrue(test_provider_found)
     collector.close_session()
예제 #4
0
    def get_accounts_from_source(self, provider_uuid=None):
        """
        Retrieve all accounts from the Koku database.

        This will return a list of dicts for the Orchestrator to use to access reports.

        Args:
            provider_uuid (String) - Optional, return specific account

        Returns:
            ([{}]) : A list of dicts

        """
        accounts = []
        with ProviderCollector() as collector:
            all_providers = collector.get_provider_uuid_map()
            provider = all_providers.get(str(provider_uuid))
            if provider_uuid and provider:
                if provider.active and not provider.paused:
                    return [self.get_account_information(provider)]
                LOG.info(
                    f"Provider {provider.uuid} is active={provider.active} "
                    f"or paused={provider.paused}. Processing suspended...")
                return []

            for _, provider in all_providers.items():
                if provider.active is False or provider.paused:
                    LOG.info(
                        f"Provider {provider.uuid} is active={provider.active} "
                        f"or paused={provider.paused}. Processing suspended..."
                    )
                    continue
                accounts.append(self.get_account_information(provider))
        return accounts
예제 #5
0
    def get_accounts_from_source(self, provider_uuid=None):
        """
        Retrieve all accounts from the Koku database.

        This will return a list of dicts for the Orchestrator to use to access reports.

        Args:
            provider_uuid (String) - Optional, return specific account

        Returns:
            ([{}]) : A list of dicts

        """
        accounts = []
        with ProviderCollector() as collector:
            all_providers = collector.get_providers()
            for provider in all_providers:
                if provider_uuid and str(provider.uuid) != provider_uuid:
                    continue
                account = {
                    'authentication': self.get_authentication(provider),
                    'customer_name': provider.customer.schema_name,
                    'billing_source': self.get_billing_source(provider),
                    'provider_type': provider.type,
                    'schema_name': provider.customer.schema_name,
                    'provider_uuid': provider.uuid
                }
                accounts.append(account)
        return accounts
예제 #6
0
    def get_accounts_from_source(self, provider_uuid=None):
        """
        Retrieve all accounts from the Koku database.

        This will return a list of dicts for the Orchestrator to use to access reports.

        Args:
            provider_uuid (String) - Optional, return specific account

        Returns:
            ([{}]) : A list of dicts

        """
        accounts = []
        with ProviderCollector() as collector:
            all_providers = collector.get_providers()
            for provider in all_providers:
                if provider.active is False:
                    LOG.info(f"Provider {provider.uuid} is not active. Processing suspended...")
                    continue
                if provider_uuid and str(provider.uuid) != provider_uuid:
                    continue
                account = {
                    "authentication": self.get_authentication(provider),
                    "customer_name": provider.customer.schema_name,
                    "billing_source": self.get_billing_source(provider),
                    "provider_type": provider.type,
                    "schema_name": provider.customer.schema_name,
                    "provider_uuid": provider.uuid,
                }
                accounts.append(account)
        return accounts
예제 #7
0
def crawl_account_hierarchy(request):
    """Return crawl account hierarchy async task ID."""
    # Require provider_uuid parameter for both GET & POST method
    params = request.query_params
    provider_uuid = params.get("provider_uuid")
    if provider_uuid is None:
        errmsg = "provider_uuid is a required parameter."
        return Response({"Error": errmsg}, status=status.HTTP_400_BAD_REQUEST)

    if request.method == "GET":
        # Note: That we need to check that the provider uuid exists here, because the
        # Orchestrator.get_accounts will return all accounts if the provider_uuid does
        # not exist.
        with ProviderCollector() as collector:
            all_providers = collector.get_provider_uuid_map()
            provider = all_providers.get(str(provider_uuid))
            if not provider:
                errmsg = f"The provider_uuid {provider_uuid} does not exist."
                return Response({"Error": errmsg},
                                status=status.HTTP_400_BAD_REQUEST)

        async_crawl_hierarchy = crawl_hierarchy.delay(
            provider_uuid=provider_uuid)
        return Response(
            {"Crawl Account Hierarchy Task ID": str(async_crawl_hierarchy)})

    if request.method == "POST":
        data = request.data
        schema_name = data.get("schema")
        if schema_name is None:
            errmsg = "schema is a required parameter."
            return Response({"Error": errmsg},
                            status=status.HTTP_400_BAD_REQUEST)
        days_list = data.get("account_structure", {}).get("days")
        if days_list is None:
            errmsg = "Unexpected json structure. Can not find days key."
            return Response({"Error": errmsg},
                            status=status.HTTP_400_BAD_REQUEST)
        if data.get("start_date"):
            insert_obj = InsertAwsOrgTree(schema=schema_name,
                                          provider_uuid=provider_uuid,
                                          start_date=data.get("start_date"))
        else:
            insert_obj = InsertAwsOrgTree(schema=schema_name,
                                          provider_uuid=provider_uuid)
        insert_obj.insert_tree(day_list=days_list)
        return Response(data)
예제 #8
0
def crawl_account_hierarchy(request):
    """Return crawl account hierarchy async task ID."""
    params = request.query_params
    provider_uuid = params.get("provider_uuid")
    if provider_uuid is None:
        errmsg = "provider_uuid is a required parameter."
        return Response({"Error": errmsg}, status=status.HTTP_400_BAD_REQUEST)

    # Note: That we need to check that the provider uuid exists here, because the
    # Orchestrator.get_accounts will return all accounts if the provider_uuid does
    # not exist.
    with ProviderCollector() as collector:
        all_providers = collector.get_provider_uuid_map()
        provider = all_providers.get(str(provider_uuid))
        if not provider:
            errmsg = f"The provider_uuid {provider_uuid} does not exist."
            return Response({"Error": errmsg},
                            status=status.HTTP_400_BAD_REQUEST)

    async_crawl_hierarchy = crawl_hierarchy.delay(provider_uuid=provider_uuid)
    return Response(
        {"Crawl Account Hierarchy Task ID": str(async_crawl_hierarchy)})
예제 #9
0
 def test_initializer(self):
     """Test Initializer"""
     collector = ProviderCollector()
     self.assertIsNotNone(collector._session)
     collector.close_session()