Skip to content

Commit

Permalink
Merge branch 'feature/billing-breakdown' into 'develop'
Browse files Browse the repository at this point in the history
Add support for billing breakdown

See merge request core/sevenbridges-python!70
  • Loading branch information
Aleksandar Pavlovic committed Feb 11, 2021
2 parents 8d14963 + dff7d38 commit 290486f
Show file tree
Hide file tree
Showing 10 changed files with 261 additions and 1 deletion.
2 changes: 2 additions & 0 deletions sevenbridges/http/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,8 @@ def _rate_limit(self):
@check_for_error
def _request(self, verb, url, headers=None, params=None, data=None,
append_base=False, stream=False):
if not url:
raise SbgError(message='Request url must be provided')
if append_base:
url = self.url + url
if not headers:
Expand Down
2 changes: 1 addition & 1 deletion sevenbridges/meta/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def fetch(self, item=None):
href = self.data.get('href', None)
headers = dict(self.api.headers)

if href is not None:
if href:
self.data = self.api.get(
href,
headers=headers,
Expand Down
55 changes: 55 additions & 0 deletions sevenbridges/models/billing_analysis_breakdown.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
from sevenbridges.meta.resource import Resource
from sevenbridges.meta.fields import (
UuidField, CompoundField, StringField,
DateTimeField, FloatField, BooleanField
)
from sevenbridges.models.compound.analysis_cost import AnalysisCost


class BillingGroupAnalysisBreakdown(Resource):
_URL = {
'query': '/billing/groups/{id}/breakdown/analysis'
}

project_name = StringField(read_only=True)
analysis_app_name = StringField(read_only=True)
analysis_name = StringField(read_only=True)
analysis_type = StringField(read_only=True)
analysis_id = UuidField(read_only=True)
ran_by = StringField(read_only=True)
analysis_status = StringField(read_only=True)
analysis_cost = CompoundField(AnalysisCost, read_only=True)
refunded_amount = FloatField(read_only=True)
time_started = DateTimeField(read_only=True)
time_finished = DateTimeField(read_only=True)
project_locked = BooleanField(read_only=True)

@classmethod
def query(cls, bg_id, api=None, date_from=None, date_to=None,
invoice_id=None, fields=None, offset=0, limit=50):
"""
Query (List) billing group analysis breakdown. Date parameters must be
string in format MM-DD-YYYY
:param fields:
:param invoice_id:
:param date_to: include all analysis transactions charged before and
including date_to
:param date_from: include all analysis transactions charged after and
including date_from
:param bg_id: Billing Group ID
:param offset: Pagination offset.
:param limit: Pagination limit.
:param api: Api instance.
:return: Collection object.
"""
api = api or cls._API

return super(BillingGroupAnalysisBreakdown, cls)._query(
url=cls._URL['query'].format(id=bg_id), offset=offset, limit=limit,
date_from=date_from, date_to=date_to, invoice_id=invoice_id,
fields=fields, api=api
)

def __str__(self):
return '<BillingGroupAnalysisBreakdown>'
48 changes: 48 additions & 0 deletions sevenbridges/models/billing_egress_breakdown.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
from sevenbridges.meta.resource import Resource
from sevenbridges.meta.fields import (
StringField, CompoundField, BooleanField
)
from sevenbridges.models.compound.measurement import Measurement
from sevenbridges.models.compound.egress_cost import EgressCost


class BillingGroupEgressBreakdown(Resource):
_URL = {
'query': '/billing/groups/{id}/breakdown/egress'
}

project_name = StringField(read_only=True)
downloaded_by = StringField(read_only=True)
downloaded = CompoundField(Measurement, read_only=True)
egress_cost = CompoundField(EgressCost, read_only=True)
project_locked = BooleanField(read_only=True)

@classmethod
def query(cls, bg_id, api=None, date_from=None, date_to=None,
invoice_id=None, fields=None, offset=0, limit=50):
"""
Query (List) billing group egress breakdown. Date parameters must be
string in format MM-DD-YYYY
:param fields:
:param invoice_id:
:param date_to: include all egress transactions charged before and
including date_to
:param date_from: include all egress transactions charged after and
including date_from
:param bg_id: Billing Group ID
:param offset: Pagination offset.
:param limit: Pagination limit.
:param api: Api instance.
:return: Collection object.
"""
api = api or cls._API

return super(BillingGroupEgressBreakdown, cls)._query(
url=cls._URL['query'].format(id=bg_id), offset=offset, limit=limit,
date_from=date_from, date_to=date_to, invoice_id=invoice_id,
fields=fields, api=api
)

def __str__(self):
return '<BillingGroupEgressBreakdown>'
39 changes: 39 additions & 0 deletions sevenbridges/models/billing_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,16 @@
HrefField, UuidField, StringField, BooleanField, CompoundField
)
from sevenbridges.meta.resource import Resource
from sevenbridges.models.billing_analysis_breakdown import (
BillingGroupAnalysisBreakdown
)
from sevenbridges.models.billing_breakdown import BillingGroupBreakdown
from sevenbridges.models.billing_storage_breakdown import (
BillingGroupStorageBreakdown
)
from sevenbridges.models.billing_egress_breakdown import (
BillingGroupEgressBreakdown
)
from sevenbridges.models.compound.price import Price


Expand Down Expand Up @@ -51,3 +60,33 @@ def breakdown(self):
Get Billing group breakdown for the current billing group.
"""
return BillingGroupBreakdown.get(self.id, self._api)

def analysis_breakdown(self, date_from=None, date_to=None, invoice_id=None,
fields=None, offset=0, limit=50):
"""
Get Billing group analysis breakdown for the current billing group.
"""
return BillingGroupAnalysisBreakdown.query(
self.id, self._api, date_from, date_to, invoice_id, fields,
offset, limit
)

def storage_breakdown(self, date_from=None, date_to=None, invoice_id=None,
fields=None, offset=0, limit=50):
"""
Get Billing group storage breakdown for the current billing group.
"""
return BillingGroupStorageBreakdown.query(
self.id, self._api, date_from, date_to, invoice_id, fields,
offset, limit
)

def egress_breakdown(self, date_from=None, date_to=None, invoice_id=None,
fields=None, offset=0, limit=50):
"""
Get Billing group egress breakdown for the current billing group.
"""
return BillingGroupEgressBreakdown.query(
self.id, self._api, date_from, date_to, invoice_id, fields,
offset, limit
)
48 changes: 48 additions & 0 deletions sevenbridges/models/billing_storage_breakdown.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
from sevenbridges.meta.resource import Resource
from sevenbridges.meta.fields import (
CompoundField, StringField, BooleanField
)
from sevenbridges.models.compound.measurement import Measurement


class BillingGroupStorageBreakdown(Resource):
_URL = {
'query': '/billing/groups/{id}/breakdown/storage'
}

project_name = StringField(read_only=True)
project_created_by = StringField(read_only=True)
location = StringField(read_only=True)
active = CompoundField(Measurement, read_only=True)
archived = CompoundField(Measurement, read_only=True)
project_locked = BooleanField(read_only=True)

@classmethod
def query(cls, bg_id, api=None, date_from=None, date_to=None,
invoice_id=None, fields=None, offset=0, limit=50):
"""
Query (List) billing group storage breakdown. Date parameters must be
string in format MM-DD-YYYY
:param fields:
:param invoice_id:
:param date_to: include all storage transactions charged before and
including date_to
:param date_from: include all storage transactions charged after and
including date_from
:param bg_id: Billing Group ID
:param offset: Pagination offset.
:param limit: Pagination limit.
:param api: Api instance.
:return: Collection object.
"""
api = api or cls._API

return super(BillingGroupStorageBreakdown, cls)._query(
url=cls._URL['query'].format(id=bg_id), offset=offset, limit=limit,
date_from=date_from, date_to=date_to, invoice_id=invoice_id,
fields=fields, api=api
)

def __str__(self):
return '<BillingGroupStorageBreakdown>'
21 changes: 21 additions & 0 deletions sevenbridges/models/compound/analysis_cost.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from sevenbridges.meta.resource import Resource
from sevenbridges.meta.fields import StringField, CompoundField
from sevenbridges.models.compound.analysis_cost_breakdown import (
AnalysisCostBreakdown
)


class AnalysisCost(Resource):
"""
AnalysisCost resource contains an information regarding the currency, the
monet value and breakdown of a analysis cost.
"""
currency = StringField(read_only=True)
amount = StringField(read_only=True)
breakdown = CompoundField(AnalysisCostBreakdown, read_only=True)

def __str__(self):
return (
f'<AnalysisCost: currency={self.currency}, '
f'amount={self.amount}>'
)
19 changes: 19 additions & 0 deletions sevenbridges/models/compound/analysis_cost_breakdown.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from sevenbridges.meta.resource import Resource
from sevenbridges.meta.fields import FloatField


class AnalysisCostBreakdown(Resource):
"""
AnalysisCostBreakdown resource contains price breakdown by storage and
computation.
"""
storage = FloatField(read_only=True)
computation = FloatField(read_only=True)
data_transfer_in = FloatField(read_only=True)

def __str__(self):
return (
f'<AnalysisCostBreakdown: storage={self.storage}, '
f'computation={self.computation}, '
f'data_transfer_in={self.data_transfer_in}'
)
14 changes: 14 additions & 0 deletions sevenbridges/models/compound/egress_cost.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from sevenbridges.meta.resource import Resource
from sevenbridges.meta.fields import StringField


class EgressCost(Resource):
"""
EgressCost resource contains an information regarding the currency and the
monet value of a egress cost.
"""
currency = StringField(read_only=True)
amount = StringField(read_only=True)

def __str__(self):
return f'<EgressCost: currency={self.currency}, amount={self.amount}>'
14 changes: 14 additions & 0 deletions sevenbridges/models/compound/measurement.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from sevenbridges.meta.resource import Resource
from sevenbridges.meta.fields import StringField, FloatField


class Measurement(Resource):
"""
Measurement resource contains an information regarding the size and the
unit of a certain resource.
"""
size = FloatField(read_only=True)
unit = StringField(read_only=True)

def __str__(self):
return f'<Measurement size={self.size}, unit={self.unit}>'

0 comments on commit 290486f

Please sign in to comment.