Skip to content

Commit

Permalink
Changed the cost reports strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
athiruma committed Dec 4, 2023
1 parent f0e5996 commit 5723285
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 11 deletions.
1 change: 1 addition & 0 deletions .github/workflows/Build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ jobs:
AZURE_CLIENT_ID: ${{ secrets.AZURE_CLIENT_ID }}
AZURE_TENANT_ID: ${{ secrets.AZURE_TENANT_ID }}
AZURE_CLIENT_SECRET: ${{ secrets.AZURE_CLIENT_SECRET }}
AZURE_ACCOUNT_ID: ${{ secrets.AZURE_ACCOUNT_ID }}
GCP_DATABASE_NAME: ${{ secrets.GCP_DATABASE_NAME }}
GCP_DATABASE_TABLE_NAME: ${{ secrets.GCP_DATABASE_TABLE_NAME }}
run: |
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/PR.yml
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ jobs:
AZURE_CLIENT_ID: ${{ secrets.AZURE_CLIENT_ID }}
AZURE_TENANT_ID: ${{ secrets.AZURE_TENANT_ID }}
AZURE_CLIENT_SECRET: ${{ secrets.AZURE_CLIENT_SECRET }}
AZURE_ACCOUNT_ID: ${{ secrets.AZURE_ACCOUNT_ID }}
GCP_DATABASE_NAME: ${{ secrets.GCP_DATABASE_NAME }}
GCP_DATABASE_TABLE_NAME: ${{ secrets.GCP_DATABASE_TABLE_NAME }}
run: |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,25 @@ def get_billing_profiles(self):
response = self.billing_client.billing_profiles.list_by_billing_account(self.__account_id)
return response

def get_billing_profiles_list(self, subscription_id: str = ''):
"""
This method returns list of billing profiles
:param subscription_id:
:type subscription_id:
:return:
:rtype:
"""
billing_profiles = []
responses = self.billing_client.billing_profiles.list_by_billing_account(self.__account_id)
for response in responses:
if subscription_id:
if subscription_id == response.subscriptionId:
return response.id
else:
billing_profiles.append(response.id)
return billing_profiles





17 changes: 11 additions & 6 deletions cloud_governance/policy/azure/cost_billing_reports.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from cloud_governance.common.elasticsearch.elastic_upload import ElasticUpload
from cloud_governance.common.google_drive.google_drive_operations import GoogleDriveOperations
from cloud_governance.common.google_drive.upload_to_gsheet import UploadToGsheet
from cloud_governance.common.logger.init_logger import logger
from cloud_governance.common.logger.logger_time_stamp import logger_time_stamp
from cloud_governance.main.environment_variables import environment_variables

Expand Down Expand Up @@ -161,12 +162,16 @@ def collect_and_upload_cost_data(self):
"""
cost_billing_data = {}
if not self.__total_account:
usage_data = self.cost_mgmt_operations.get_usage(scope=self.azure_operations.scope)
forecast_data = self.cost_mgmt_operations.get_forecast(scope=self.azure_operations.scope)
if usage_data:
self.get_data_from_costs(cost_data_rows=usage_data.get('rows'), cost_data_columns=usage_data.get('columns'), cost_type='Actual', cost_billing_data=cost_billing_data)
if forecast_data:
self.get_data_from_costs(cost_data_rows=forecast_data.get('rows'), cost_data_columns=forecast_data.get('columns'), cost_type='Forecast', cost_billing_data=cost_billing_data, subscription_id=self.azure_operations.subscription_id)
scope = self.azure_operations.get_billing_profiles_list(subscription_id=self.azure_operations.subscription_id)
if scope:
usage_data = self.cost_mgmt_operations.get_usage(scope=scope)
forecast_data = self.cost_mgmt_operations.get_forecast(scope=self.azure_operations.scope)
if usage_data:
self.get_data_from_costs(cost_data_rows=usage_data.get('rows'), cost_data_columns=usage_data.get('columns'), cost_type='Actual', cost_billing_data=cost_billing_data)
if forecast_data:
self.get_data_from_costs(cost_data_rows=forecast_data.get('rows'), cost_data_columns=forecast_data.get('columns'), cost_type='Forecast', cost_billing_data=cost_billing_data, subscription_id=self.azure_operations.subscription_id)
else:
logger.warning(f"No billing scopes found in the subscription: {self.azure_operations.subscription_id}")
else:
cost_billing_data = self.get_total_billing_accounts()
if cost_billing_data:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,14 @@ def test_get_usage():
end_date = datetime.datetime.utcnow() - datetime.timedelta(days=2)
start_date = end_date - datetime.timedelta(days=1)
granularity = 'Daily'
cost_usage_data = cost_management_operations.get_usage(scope=cost_management_operations.azure_operations.scope,
start_date=start_date, end_date=end_date,
granularity=granularity
)
assert cost_usage_data
scope = cost_management_operations.azure_operations.get_billing_profiles_list()[0]
if scope:
cost_usage_data = cost_management_operations.get_usage(scope=scope,
start_date=start_date, end_date=end_date,
granularity=granularity
)
assert cost_usage_data
assert False


def test_get_forecast():
Expand Down

0 comments on commit 5723285

Please sign in to comment.