Beispiel #1
0
    def __init__(self, customer_name, credentials, data_source, report_name=None, **kwargs):
        """
        Constructor.

        Args:
            customer_name    (String) Name of the customer
            credentials   (Dict) credentials credential for S3 bucket (RoleARN)
            report_name      (String) Name of the Cost Usage Report to download (optional)
            bucket           (String) Name of the S3 bucket containing the CUR

        """
        super().__init__(**kwargs)

        arn = credentials.get("role_arn")
        bucket = data_source.get("bucket")
        if customer_name[4:] in settings.DEMO_ACCOUNTS:
            demo_account = settings.DEMO_ACCOUNTS.get(customer_name[4:])
            LOG.info(f"Info found for demo account {customer_name[4:]} = {demo_account}.")
            if arn in demo_account:
                demo_info = demo_account.get(arn)
                self.customer_name = customer_name.replace(" ", "_")
                self._provider_uuid = kwargs.get("provider_uuid")
                self.report_name = demo_info.get("report_name")
                self.report = {"S3Bucket": bucket, "S3Prefix": demo_info.get("report_prefix"), "Compression": "GZIP"}
                self.bucket = bucket
                session = utils.get_assume_role_session(utils.AwsArn(arn), "MasuDownloaderSession")
                self.s3_client = session.client("s3")
                return

        self.customer_name = customer_name.replace(" ", "_")
        self._provider_uuid = kwargs.get("provider_uuid")

        LOG.debug("Connecting to AWS...")
        session = utils.get_assume_role_session(utils.AwsArn(arn), "MasuDownloaderSession")
        self.cur = session.client("cur")

        # fetch details about the report from the cloud provider
        defs = self.cur.describe_report_definitions()
        if not report_name:
            report_names = []
            for report in defs.get("ReportDefinitions", []):
                if bucket == report.get("S3Bucket"):
                    report_names.append(report["ReportName"])

            # FIXME: Get the first report in the bucket until Koku can specify
            # which report the user wants
            if report_names:
                report_name = report_names[0]
        self.report_name = report_name
        self.bucket = bucket
        report_defs = defs.get("ReportDefinitions", [])
        report = [rep for rep in report_defs if rep["ReportName"] == self.report_name]

        if not report:
            raise MasuProviderError("Cost and Usage Report definition not found.")

        self.report = report.pop()
        self.s3_client = session.client("s3")
    def __init__(self,
                 task,
                 customer_name,
                 auth_credential,
                 bucket,
                 report_name=None,
                 **kwargs):
        """
        Constructor.

        Args:
            task             (Object) bound celery object
            customer_name    (String) Name of the customer
            auth_credential  (String) Authentication credential for S3 bucket (RoleARN)
            report_name      (String) Name of the Cost Usage Report to download (optional)
            bucket           (String) Name of the S3 bucket containing the CUR

        """
        super().__init__(task, **kwargs)

        self.customer_name = customer_name.replace(' ', '_')
        self._provider_uuid = kwargs.get('provider_uuid')

        LOG.debug('Connecting to AWS...')
        session = utils.get_assume_role_session(utils.AwsArn(auth_credential),
                                                'MasuDownloaderSession')
        self.cur = session.client('cur')

        # fetch details about the report from the cloud provider
        defs = self.cur.describe_report_definitions()
        if not report_name:
            report_names = []
            for report in defs.get('ReportDefinitions', []):
                if bucket == report.get('S3Bucket'):
                    report_names.append(report['ReportName'])

            # FIXME: Get the first report in the bucket until Koku can specify
            # which report the user wants
            if report_names:
                report_name = report_names[0]
        self.report_name = report_name
        self.bucket = bucket
        report_defs = defs.get('ReportDefinitions', [])
        report = [
            rep for rep in report_defs if rep['ReportName'] == self.report_name
        ]

        if not report:
            raise MasuProviderError(
                'Cost and Usage Report definition not found.')

        self.report = report.pop()
        self.s3_client = session.client('s3')
    def __init__(self, provider_uuid, schema='public'):
        """
        Establish ProviderStatus database connection.

        Args:
            provider_uuid  (String) the uuid of the provider
            schema         (String) database schema (i.e. public or customer tenant value)

        """
        super().__init__(schema)
        self._provider_uuid = provider_uuid
        self._table = self.get_base().classes.api_providerstatus

        with ProviderDBAccessor(self._provider_uuid) as provider_accessor:
            provider = provider_accessor.get_provider()
            if not provider:
                raise MasuProviderError(
                    f'Unknown provider: {self._provider_uuid}')
            self.provider_id = provider.id

        self._obj = self._get_db_obj_query().first()

        # default to READY if there is no previous status.
        if self._obj is None:
            message = f'No status found for provider {provider_uuid} in ' + \
                f'schema "{schema}". Setting status to READY.'
            LOG.debug(message)
            ready_status = {
                'provider_id': self.provider_id,
                'status': ProviderStatusCode.READY,
                'last_message': 'none',
                'timestamp': DateAccessor().today(),
                'retries': 0
            }
            self.add(**ready_status)
            self.commit()
            self._obj = self._get_db_obj_query().first()
    def __init__(self, provider_uuid, schema="public"):
        """
        Establish ProviderStatus database connection.

        Args:
            provider_uuid  (String) the uuid of the provider
            schema         (String) database schema (i.e. public or customer tenant value)

        """
        super().__init__(schema)
        self._provider_uuid = provider_uuid
        self._table = ProviderStatus

        with ProviderDBAccessor(self._provider_uuid) as provider_accessor:
            provider = provider_accessor.get_provider()
            if not provider:
                raise MasuProviderError(
                    f"Unknown provider: {self._provider_uuid}")
            self.provider_uuid = provider.uuid

        self._obj = self._get_db_obj_query().order_by("-id").first()

        # default to READY if there is no previous status.
        if self._obj is None:
            message = (f"No status found for provider {provider_uuid} in " +
                       f'schema "{schema}". Setting status to READY.')
            LOG.debug(message)
            ready_status = {
                "provider_id": self.provider_uuid,
                "status": ProviderStatusCode.READY,
                "last_message": "none",
                "timestamp": DateAccessor().today(),
                "retries": 0,
            }
            self.add(**ready_status)
            self._obj = self._get_db_obj_query().first()