Skip to content

Commit

Permalink
Compare archive length with free trial
Browse files Browse the repository at this point in the history
If archive exists raise error as free trial should be disabled.
Error is added to csp config to propagate adapter error to product.
  • Loading branch information
smarlowucf committed Apr 16, 2024
1 parent 4c5f1be commit 20c302e
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion csp_billing_adapter/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,21 @@ def event_loop_handler(
add_usage_record(usage, cache, config.billing_interval)
csp_config['base_product'] = usage.get('base_product', '')

try:
archive = retry_on_exception(
functools.partial(
hook.get_metering_archive,
config=config,
),
logger=log,
func_name='hook.get_metering_archive'
)
except Exception:
archive = None

if archive is None:
archive = []

log.debug(
"Now: %s, Next Reporting Time: %s, Next Bill Time: %s",
date_to_string(now),
Expand All @@ -262,7 +277,15 @@ def event_loop_handler(
trial_remaining = cache.get('trial_remaining', 0)

if now >= string_to_date(cache['next_bill_time']):
if trial_remaining > 0:
if trial_remaining > 0 and len(archive) > 0:
msg = (
'Free trial is active but archive contains metering history. '
'Usage will be billed for the previous cycle.'
)
log.warning(msg)
csp_config['errors'].append(msg)

if trial_remaining > 0 and len(archive) == 0:
log.info('Attempt a free trial billing cycle update')
process_metering(
hook,
Expand Down

0 comments on commit 20c302e

Please sign in to comment.