Small refactor for billing

parent cc5d5b73
......@@ -5,7 +5,7 @@ from time import gmtime, strftime
from akinaka_update.commands import update as update_commands
from akinaka_cleanup.commands import cleanup as cleanup_commands
from akinaka_rds.commands import copy as copy_commands
from akinaka_reporting.commands import billing as billing_commands
from akinaka_reporting.commands import reporting as reporting_commands
@click.group()
def cli():
......@@ -14,6 +14,6 @@ def cli():
cli.add_command(update_commands)
cli.add_command(cleanup_commands)
cli.add_command(copy_commands)
cli.add_command(billing_commands)
cli.add_command(reporting_commands)
cli()
......@@ -11,6 +11,7 @@ class CostExplorer():
def get_bill_estimates(self, from_days_ago):
days_ago = 0 or int(from_days_ago)
if days_ago > 0:
end = datetime.now().strftime("%Y-%m-%d")
datetime_days_ago = datetime.now() - timedelta(days=days_ago + 1)
......
......@@ -7,17 +7,17 @@ import sys
import tabulate
class BillingQueries():
def __init__(self, region, assume_role_arn, days_ago):
def __init__(self, region, assume_role_arn):
self.costexplorer = akinaka_libs.costexplorer.CostExplorer(region, assume_role_arn)
self.days_ago = days_ago
def print_x_days_estimates(self):
def days_estimates(self, from_days_ago):
try:
response = self.costexplorer.get_bill_estimates(from_days_ago=self.days_ago)
response = self.costexplorer.get_bill_estimates(from_days_ago)
data = response['ResultsByTime']
except Exception as e:
print("Billing estimates is not available: {}".format(e))
return e
results = []
if len(data) == 1:
amount = float(data[0]['Total']['UnblendedCost']['Amount'])
......@@ -31,7 +31,9 @@ class BillingQueries():
amount = float(d['Total']['UnblendedCost']['Amount'])
results.append([d['TimePeriod']['End'], "{} {:.2f}".format(unit, amount)])
message = "\nEstimated bill for the past {} days\n".format(str(len(data)))
message += tabulate.tabulate(results, headers=["Date", "Total"], tablefmt='psql')
message += "\n"
print(message)
return message
\ No newline at end of file
......@@ -5,18 +5,19 @@ import click
@click.option("--region", required=True, help="Region your resources are located in. N.B. Currently, only us-east-1 supports estimates")
@click.option("--role-arn", required=True, help="Role ARN which contains necessary assume permissions")
@click.pass_context
def billing(ctx, region, role_arn):
def reporting(ctx, region, role_arn):
ctx.obj = {'region': region, 'role_arn': role_arn}
pass
@billing.command()
@reporting.command(name="bill-estimates")
@click.pass_context
@click.option("--days-ago", "days_ago", default=0, help="Number of days ago.")
def estimate(ctx, days_ago):
@click.option("--from-days-ago", "from_days_ago", default=0, help="Number of days ago.")
def bill_estimates(ctx, from_days_ago):
region = ctx.obj.get('region')
role_arn = ctx.obj.get('role_arn')
from .billing_summary import billing_queries
billing_queries.BillingQueries(region, role_arn, days_ago).print_x_days_estimates()
billing_queries.BillingQueries(region, role_arn).days_estimates(from_days_ago)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment