Example #1
0
 def test_azure_route(self, mock_update):
     """Test that Azure charge updating works as expected."""
     updater = CostModelCostUpdater(self.schema,
                                    self.azure_test_provider_uuid)
     self.assertIsInstance(updater._updater, AzureCostModelCostUpdater)
     updater.update_cost_model_costs()
     mock_update.assert_called()
Example #2
0
def update_cost_model_costs(schema_name,
                            provider_uuid,
                            start_date=None,
                            end_date=None):
    """Update usage charge information.

    Args:
        schema_name (str) The DB schema name.
        provider_uuid (str) The provider uuid.
        start_date (str, Optional) - Start date of range to update derived cost.
        end_date (str, Optional) - End date of range to update derived cost.

    Returns
        None

    """
    worker_stats.COST_MODEL_COST_UPDATE_ATTEMPTS_COUNTER.inc()

    stmt = (f"update_cost_model_costs called with args:\n"
            f" schema_name: {schema_name},\n"
            f" provider_uuid: {provider_uuid}")
    LOG.info(stmt)

    updater = CostModelCostUpdater(schema_name, provider_uuid)
    if updater:
        updater.update_cost_model_costs(start_date, end_date)
Example #3
0
def update_cost_model_costs(schema_name,
                            provider_uuid,
                            start_date=None,
                            end_date=None,
                            queue_name=None,
                            synchronous=False):
    """Update usage charge information.

    Args:
        schema_name (str) The DB schema name.
        provider_uuid (str) The provider uuid.
        start_date (str, Optional) - Start date of range to update derived cost.
        end_date (str, Optional) - End date of range to update derived cost.

    Returns
        None

    """
    task_name = "masu.processor.tasks.update_cost_model_costs"
    cache_args = [schema_name, provider_uuid, start_date, end_date]
    if not synchronous:
        worker_cache = WorkerCache()
        if worker_cache.single_task_is_running(task_name, cache_args):
            msg = f"Task {task_name} already running for {cache_args}. Requeuing."
            LOG.info(msg)
            update_cost_model_costs.s(
                schema_name,
                provider_uuid,
                start_date=start_date,
                end_date=end_date,
                queue_name=queue_name,
                synchronous=synchronous,
            ).apply_async(queue=queue_name or UPDATE_COST_MODEL_COSTS_QUEUE)
            return
        worker_cache.lock_single_task(task_name, cache_args, timeout=600)

    worker_stats.COST_MODEL_COST_UPDATE_ATTEMPTS_COUNTER.inc()

    stmt = (f"update_cost_model_costs called with args:\n"
            f" schema_name: {schema_name},\n"
            f" provider_uuid: {provider_uuid}")
    LOG.info(stmt)

    try:
        updater = CostModelCostUpdater(schema_name, provider_uuid)
        if updater:
            updater.update_cost_model_costs(start_date, end_date)
    except Exception as ex:
        if not synchronous:
            worker_cache.release_single_task(task_name, cache_args)
        raise ex

    if not synchronous:
        worker_cache.release_single_task(task_name, cache_args)
Example #4
0
def update_cost_model_costs(schema_name,
                            provider_uuid,
                            start_date=None,
                            end_date=None,
                            provider_type=None,
                            synchronous=False):
    """Update usage charge information.

    Args:
        schema_name (str) The DB schema name.
        provider_uuid (str) The provider uuid.
        start_date (str, Optional) - Start date of range to update derived cost.
        end_date (str, Optional) - End date of range to update derived cost.

    Returns
        None

    """
    task_name = "masu.processor.tasks.update_cost_model_costs"
    cache_args = [schema_name, provider_uuid, start_date, end_date]
    if not synchronous:
        worker_cache = WorkerCache()
        while worker_cache.single_task_is_running(task_name, cache_args):
            time.sleep(5)
        worker_cache.lock_single_task(task_name, cache_args, timeout=300)

    worker_stats.COST_MODEL_COST_UPDATE_ATTEMPTS_COUNTER.inc()

    stmt = (f"update_cost_model_costs called with args:\n"
            f" schema_name: {schema_name},\n"
            f" provider_uuid: {provider_uuid}")
    LOG.info(stmt)

    updater = CostModelCostUpdater(schema_name, provider_uuid)
    if updater:
        updater.update_cost_model_costs(start_date, end_date)

    if not synchronous:
        worker_cache.release_single_task(task_name, cache_args)
Example #5
0
def update_cost_model_costs(schema_name,
                            provider_uuid,
                            start_date=None,
                            end_date=None):
    """Update usage charge information.

    Args:
        schema_name (str) The DB schema name.
        provider_uuid (str) The provider uuid.
        start_date (str, Optional) - Start date of range to update derived cost.
        end_date (str, Optional) - End date of range to update derived cost.

    Returns
        None

    """
    with CostModelDBAccessor(schema_name,
                             provider_uuid) as cost_model_accessor:
        cost_model = cost_model_accessor.cost_model
    if cost_model is not None:
        worker_stats.COST_MODEL_COST_UPDATE_ATTEMPTS_COUNTER.inc()

        stmt = (f"update_cost_model_costs called with args:\n"
                f" schema_name: {schema_name},\n"
                f" provider_uuid: {provider_uuid}")
        LOG.info(stmt)

        updater = CostModelCostUpdater(schema_name, provider_uuid)
        if updater:
            updater.update_cost_model_costs(start_date, end_date)
    else:
        stmt = (
            f"\n update_cost_model_costs skipped. No cost model available for \n"
            f" schema_name: {schema_name},\n"
            f" provider_uuid: {provider_uuid}")
        LOG.info(stmt)
Example #6
0
 def test_unknown_provider(self, mock_accessor):
     """Test no exception when initializing unknown provider."""
     try:
         CostModelCostUpdater(self.schema, self.unkown_test_provider_uuid)
     except Exception as err:
         self.fail(f"Failed with exception: {err}")
Example #7
0
    def test_init_fail(self, mock_updater):
        """Test that an unimplemented provider throws an error."""
        mock_updater.side_effect = Exception("general error")

        with self.assertRaises(CostModelCostUpdaterError):
            CostModelCostUpdater(self.schema, self.ocp_test_provider_uuid)