Skip to content

Commit

Permalink
Update default value handling for archive
Browse files Browse the repository at this point in the history
Drop defaults as these are not functional. The way the config is
written a value must be provided in config.

- Treat a 0 or 1 in bytes limit as "disable archive"
- Treat < 1 in retention period as "disable archive"
- Treat < 0 in bytes limit as no limit
  • Loading branch information
smarlowucf committed Jan 10, 2024
1 parent 0a5f1ad commit 91b0e70
Show file tree
Hide file tree
Showing 12 changed files with 91 additions and 14 deletions.
14 changes: 9 additions & 5 deletions csp_billing_adapter/archive.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@

log = logging.getLogger('CSPBillingAdapter')

DEFAULT_RETENTION_PERIOD = 6 # in months
DEFAULT_BYTES_LIMIT = 0


def append_metering_records(
archive: list,
Expand Down Expand Up @@ -95,6 +92,13 @@ def archive_record(
The dictionary containing the most recent
metering and usage records to be archived.
"""
retention_period = config.archive_retention_period
bytes_limit = config.archive_bytes_limit

if retention_period < 1 or bytes_limit in (0, 1):
# Archive feature has been disabled, do nothing
return

archive = retry_on_exception(
functools.partial(
hook.get_metering_archive,
Expand All @@ -110,8 +114,8 @@ def archive_record(
archive = append_metering_records(
archive,
billing_record,
config.archive_retention_period or DEFAULT_RETENTION_PERIOD,
config.archive_bytes_limit or DEFAULT_BYTES_LIMIT
retention_period,
bytes_limit
)

retry_on_exception(
Expand Down
2 changes: 1 addition & 1 deletion examples/csp-billing-adapter-example-SUMA.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ reporting_api_is_cumulative: CSP_DEPENDENT_SETTING
reporting_interval: CSP_DEPENDENT_SETTING
support_info_collection_cmd: CSP_DEPENDENT_SETTING
archive_retention_period: 6
archive_bytes_limit: 0
archive_bytes_limit: -1
usage_metrics:
ha_systems:
consumption_reporting: volume
Expand Down
2 changes: 1 addition & 1 deletion tests/data/config_bad.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ query_interval: 3600
reporting_api_is_cumulative: true
reporting_interval: 3600
archive_retention_period: 6
archive_bytes_limit: 0
archive_bytes_limit: -1
usage_metrics:
managed_node_count:
consumption_reporting: volume
Expand Down
2 changes: 1 addition & 1 deletion tests/data/config_dimensions_gap.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ query_interval: 3600
reporting_api_is_cumulative: true
reporting_interval: 3600
archive_retention_period: 6
archive_bytes_limit: 0
archive_bytes_limit: -1
usage_metrics:
managed_node_count:
consumption_reporting: volume
Expand Down
2 changes: 1 addition & 1 deletion tests/data/config_dimensions_no_tail.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ query_interval: 3600
reporting_api_is_cumulative: true
reporting_interval: 3600
archive_retention_period: 6
archive_bytes_limit: 0
archive_bytes_limit: -1
usage_metrics:
managed_node_count:
consumption_reporting: volume
Expand Down
2 changes: 1 addition & 1 deletion tests/data/config_good_average.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ query_interval: 3600
reporting_api_is_cumulative: true
reporting_interval: 3600
archive_retention_period: 6
archive_bytes_limit: 0
archive_bytes_limit: -1
usage_metrics:
managed_node_count:
consumption_reporting: volume
Expand Down
31 changes: 31 additions & 0 deletions tests/data/config_good_average_no_archive.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
billing_interval: monthly
product_code: foo
query_interval: 3600
reporting_api_is_cumulative: true
reporting_interval: 3600
archive_retention_period: 0
archive_bytes_limit: -1
usage_metrics:
managed_node_count:
consumption_reporting: volume
dimensions:
- dimension: tier_1
max: 15
min: 0
- dimension: tier_2
max: 50
min: 16
- dimension: tier_3
max: 100
min: 51
- dimension: tier_4
max: 250
min: 101
- dimension: tier_5
max: 1000
min: 251
- dimension: tier_6
min: 1001
min_consumption: 5
usage_aggregation: average
version: 1.1.1
2 changes: 1 addition & 1 deletion tests/data/config_good_maximum.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ query_interval: 3600
reporting_api_is_cumulative: true
reporting_interval: 3600
archive_retention_period: 6
archive_bytes_limit: 0
archive_bytes_limit: -1
usage_metrics:
managed_node_count:
consumption_reporting: volume
Expand Down
2 changes: 1 addition & 1 deletion tests/data/config_invalid_consumption.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ query_interval: 3600
reporting_api_is_cumulative: true
reporting_interval: 3600
archive_retention_period: 6
archive_bytes_limit: 0
archive_bytes_limit: -1
usage_metrics:
managed_node_count:
consumption_reporting: invalid
Expand Down
2 changes: 1 addition & 1 deletion tests/data/config_no_usage_metrics.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ query_interval: 3600
reporting_api_is_cumulative: true
reporting_interval: 3600
archive_retention_period: 6
archive_bytes_limit: 0
archive_bytes_limit: -1
version: 1.1.1
2 changes: 1 addition & 1 deletion tests/data/config_testing_mixed.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ query_interval: 3600
reporting_api_is_cumulative: true
reporting_interval: 3600
archive_retention_period: 6
archive_bytes_limit: 0
archive_bytes_limit: -1
usage_metrics:
jobs:
# test metric with average aggregation, volume reporting
Expand Down
42 changes: 42 additions & 0 deletions tests/unit/test_archive.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
for the archive util functions.
"""

from pytest import mark

from csp_billing_adapter.archive import (
append_metering_records,
archive_record
Expand Down Expand Up @@ -104,3 +106,43 @@ def test_archive_record(cba_pm, cba_config):
archive = cba_pm.hook.get_metering_archive(config=cba_config)
assert len(archive) == 1
assert archive[0] == record


@mark.config('config_good_average_no_archive.yaml')
def test_archive_disabled(cba_pm, cba_config):
record = {
'billing_time': '2024-02-09T18:11:59.527064+00:00',
'billing_status': {
'tier_1': {
'record_id': 'd92c6e6556b14770994f5b64ebe3d339',
'status': 'succeeded'
}
},
'billed_usage': {
'tier_1': 10
},
'usage_records': [
{
'managed_node_count': 9,
'reporting_time': '2024-01-09T18:11:59.527673+00:00',
'base_product': 'cpe:/o:suse:product:v1.2.3'
},
{
'managed_node_count': 9,
'reporting_time': '2024-01-09T18:11:59.529096+00:00',
'base_product': 'cpe:/o:suse:product:v1.2.3'
},
{
'managed_node_count': 10,
'reporting_time': '2024-01-09T18:11:59.531424+00:00',
'base_product': 'cpe:/o:suse:product:v1.2.3'
}
]
}
archive_record(
cba_pm.hook,
cba_config,
record
)
archive = cba_pm.hook.get_metering_archive(config=cba_config)
assert len(archive) == 0

0 comments on commit 91b0e70

Please sign in to comment.