Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Azure: Changed the cost reports strategy #698

Merged
merged 1 commit into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -43,7 +43,10 @@ def __get_query_dataset(self, grouping: list, tags: dict, granularity: str):
if grouping:
filter_grouping = []
for group in grouping:
filter_grouping.append({"name": group.lower(), "type": "TagKey"})
if isinstance(group, dict):
filter_grouping.append({"name": group['name'], "type": group['type']})
else:
filter_grouping.append({"name": group.lower(), "type": "TagKey"})
query_dataset['grouping'] = filter_grouping
return query_dataset

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





24 changes: 14 additions & 10 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 @@ -131,8 +132,8 @@ def get_total_billing_accounts(self):
cost_center = int(profile.get('display_name').split('(CC ')[-1].split(')')[0])
self.__common_data['CostCenter'] = cost_center
filters = {
'grouping': [QueryGrouping(type='Dimension', name='SubscriptionName'),
QueryGrouping(type='Dimension', name='SubscriptionId')]
'grouping': [{'type':'Dimension', 'name': 'SubscriptionName'},
{'type': 'Dimension', 'name': 'SubscriptionId'}]
}
usage_data = self.cost_mgmt_operations.get_usage(scope=scope, **filters)
subscription_ids = []
Expand All @@ -142,8 +143,7 @@ def get_total_billing_accounts(self):
cost_billing_data=cost_billing_data)
for subscription in subscription_ids:
query_filter = {
'filter': QueryFilter(dimensions=QueryComparisonExpression(name='SubscriptionId', operator='in',
values=[subscription[0]]))}
'filter': {'name': 'SubscriptionId', 'values': [subscription[0]]}}
forecast_data = self.cost_mgmt_operations.get_forecast(scope=scope, **query_filter)
if forecast_data and forecast_data.get('rows'):
self.get_data_from_costs(cost_data_rows=forecast_data.get('rows'),
Expand All @@ -161,12 +161,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
Loading