Skip to content

Commit 6acadca

Browse files
committed
Fix utcnow deprecation warnings
1 parent cf91b93 commit 6acadca

42 files changed

Lines changed: 145 additions & 145 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

cloud_governance/cloud_resource_orchestration/clouds/aws/ec2/aws_monitor_tickets.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import json
22
import tempfile
3-
from datetime import datetime
3+
from datetime import datetime, timezone
44

55
import typeguard
66

@@ -232,7 +232,7 @@ def update_cluster_cost(self):
232232
self.__es_operations.update_elasticsearch_index(index=self.es_cro_index, id=ticket_id,
233233
metadata={
234234
'cluster_cost': cluster_cost,
235-
'timestamp': datetime.utcnow()
235+
'timestamp': datetime.now(tz=timezone.utc)
236236
})
237237

238238
def __prepare_athena_query_for_cluster_cost(self, names: list):

cloud_governance/cloud_resource_orchestration/clouds/aws/ec2/collect_cro_reports.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import logging
2-
from datetime import datetime, timedelta
2+
from datetime import datetime, timedelta, timezone
33

44
import typeguard
55

@@ -45,7 +45,7 @@ def get_account_budget_from_payer_ce_report(self):
4545
"must": [
4646
{"term": {"CloudName.keyword": self.__public_cloud_name}},
4747
{"term": {"AccountId.keyword": self.__account_id}},
48-
{"term": {"Month": str(datetime.utcnow().year)}},
48+
{"term": {"Month": str(datetime.now(tz=timezone.utc).year)}},
4949
]
5050
}
5151
},
@@ -61,7 +61,7 @@ def get_total_account_usage_cost(self):
6161
This method returns the total account budget till date for this year
6262
:return:
6363
"""
64-
current_date = datetime.utcnow().date()
64+
current_date = datetime.now(tz=timezone.utc).date()
6565
start_date = datetime(current_date.year, 1, 1).date()
6666
cost_explorer_operations = self.__cost_over_usage.get_cost_explorer_operations()
6767
response = cost_explorer_operations.get_cost_and_usage_from_aws(start_date=str(start_date), end_date=str(current_date+timedelta(days=1)), granularity='MONTHLY')
@@ -92,7 +92,7 @@ def get_user_cost_data(self, group_by_tag_name: str, group_by_tag_value: str, re
9292
return_key = 'Forecast'
9393
else:
9494
response = self.__cost_over_usage.get_monthly_user_es_cost_data(start_date=start_date,
95-
end_date=datetime.utcnow().replace(microsecond=self.ZERO) + timedelta(days=1),
95+
end_date=datetime.now(tz=timezone.utc).replace(microsecond=self.ZERO) + timedelta(days=1),
9696
extra_matches=extra_filter_matches, extra_operation=self.AND, tag_name=group_by_tag_name)
9797
return_key = 'Cost'
9898
if response:
@@ -173,7 +173,7 @@ def __prepare_update_es_data(self, source: dict, instance_data: list, user_cost:
173173
source['user_cro'] = instance_data[self.ZERO].get('user_cro')
174174
if instance_data[self.ZERO].get('user') and source.get('user') != instance_data[self.ZERO].get('user'):
175175
source['user'] = instance_data[self.ZERO].get('user')
176-
source['timestamp'] = datetime.utcnow()
176+
source['timestamp'] = datetime.now(tz=timezone.utc)
177177
if source.get('ticket_id_state') != 'in-progress':
178178
source['ticket_id_state'] = 'in-progress'
179179
source['approved_manager'] = instance_data[self.ZERO].get('approved_manager')
@@ -213,7 +213,7 @@ def __upload_cro_report_to_es(self, monitor_data: dict):
213213
user_cost = self.get_user_cost_data(group_by_tag_name=group_by_tag_name, group_by_tag_value=ticket_id,
214214
requested_date=ticket_opened_date)
215215
duration = int(instance_data[self.ZERO].get('duration', 0))
216-
user_forecast = self.get_user_cost_data(group_by_tag_name=group_by_tag_name, group_by_tag_value=ticket_id, requested_date=datetime.utcnow(), extra_filter_key_values={'Project': user_project}, forecast=True, duration=duration)
216+
user_forecast = self.get_user_cost_data(group_by_tag_name=group_by_tag_name, group_by_tag_value=ticket_id, requested_date=datetime.now(tz=timezone.utc), extra_filter_key_values={'Project': user_project}, forecast=True, duration=duration)
217217
cost_estimation = float(instance_data[self.ZERO].get('estimated_cost', self.ZERO))
218218
if self.__cost_over_usage.es_operations.verify_elastic_index_doc_id(index=self.__cost_over_usage.es_index_cro, doc_id=ticket_id):
219219
es_data = self.__cost_over_usage.es_operations.get_es_data_by_id(id=ticket_id,index=self.__cost_over_usage.es_index_cro)
@@ -270,10 +270,10 @@ def update_in_progress_ticket_cost(self):
270270
user_name=user_name)
271271
user_daily_cost.update(ce_user_daily_report)
272272
user_forecast = self.get_user_cost_data(group_by_tag_name=group_by_tag_name,
273-
group_by_tag_value=ticket_id, requested_date=datetime.utcnow(),
273+
group_by_tag_value=ticket_id, requested_date=datetime.now(tz=timezone.utc),
274274
forecast=True, duration=duration)
275-
update_data = {'actual_cost': user_cost, 'forecast': user_forecast, 'timestamp': datetime.utcnow(),
276-
f'TotalCurrentUsage-{datetime.utcnow().year}': total_account_cost,
275+
update_data = {'actual_cost': user_cost, 'forecast': user_forecast, 'timestamp': datetime.now(tz=timezone.utc),
276+
f'TotalCurrentUsage-{datetime.now(tz=timezone.utc).year}': total_account_cost,
277277
'user_daily_cost': str(user_daily_cost)}
278278
if not source_data.get(self.ALLOCATED_BUDGET):
279279
update_data[self.ALLOCATED_BUDGET] = self.get_account_budget_from_payer_ce_report()
@@ -302,7 +302,7 @@ def __get_user_usage_by_granularity(self, result_back_data: dict, tag_name: str,
302302
:param tag_value:
303303
:return:
304304
"""
305-
end_date = datetime.utcnow().date()
305+
end_date = datetime.now(tz=timezone.utc).date()
306306
start_date = end_date - timedelta(days=days)
307307
cost_explorer_object = self.__cost_over_usage.get_cost_explorer_operations()
308308
ce_daily_usage = cost_explorer_object.get_cost_by_tags(tag=tag_name,

cloud_governance/cloud_resource_orchestration/clouds/aws/ec2/cost_over_usage.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import json
22
import logging
33
from ast import literal_eval
4-
from datetime import datetime, timedelta
4+
from datetime import datetime, timedelta, timezone
55

66
import typeguard
77

@@ -45,7 +45,7 @@ def __init__(self):
4545
self.__cro_admins = self.__environment_variables_dict.get('CRO_DEFAULT_ADMINS', [])
4646
self.es_index_cro = self.__environment_variables_dict.get('CRO_ES_INDEX', '')
4747
self.__cro_duration_days = self.__environment_variables_dict.get('CRO_DURATION_DAYS')
48-
self.current_end_date = datetime.utcnow()
48+
self.current_end_date = datetime.now(tz=timezone.utc)
4949
self.current_start_date = self.current_end_date - timedelta(days=self.__cro_duration_days)
5050
self.__public_cloud_name = self.__environment_variables_dict.get('PUBLIC_CLOUD_NAME')
5151
self.__ce_operations = CostExplorerOperations()
@@ -293,7 +293,7 @@ def get_last_mail_alert_status(self, user: str):
293293
last_alert = response[0]
294294
last_send_date = last_alert.get('_source').get('timestamp')
295295
alert_number = last_alert.get('_source').get('Alert', 0)
296-
current_date = datetime.utcnow().date()
296+
current_date = datetime.now(tz=timezone.utc).date()
297297
last_send_date = datetime.strptime(last_send_date, self.TIMESTAMP_DATE_FORMAT).date()
298298
days = (current_date - last_send_date).days
299299
if days % self.SEND_ALERT_DAY == 0 and last_send_date != current_date:

cloud_governance/cloud_resource_orchestration/clouds/aws/ec2/run_cro.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from datetime import datetime
1+
from datetime import datetime, timezone
22

33
import boto3
44

@@ -14,7 +14,7 @@
1414

1515
class RunCRO:
1616

17-
PERSISTENT_RUN_DOC_ID = f'cro_run_persistence-{datetime.utcnow().date()}'
17+
PERSISTENT_RUN_DOC_ID = f'cro_run_persistence-{datetime.now(tz=timezone.utc).date()}'
1818
PERSISTENT_RUN_INDEX = 'cloud_resource_orchestration_persistence_run'
1919

2020
def __init__(self):
@@ -39,7 +39,7 @@ def send_cro_alerts(self):
3939
last_run_time = source.get(f'last_run_{self.account.lower()}')
4040
if last_run_time:
4141
last_updated_time = datetime.strptime(last_run_time, "%Y-%m-%dT%H:%M:%S.%f").date()
42-
if last_updated_time == datetime.utcnow().date():
42+
if last_updated_time == datetime.now(tz=timezone.utc).date():
4343
first_run = False
4444
self.__environment_variables_dict.update({'CRO_FIRST_RUN': first_run})
4545
if first_run:
@@ -57,9 +57,9 @@ def save_current_timestamp(self):
5757
:return:
5858
"""
5959
if not self.cro_cost_over_usage.es_operations.verify_elastic_index_doc_id(index=self.PERSISTENT_RUN_INDEX, doc_id=self.PERSISTENT_RUN_DOC_ID):
60-
self.cro_cost_over_usage.es_operations.upload_to_elasticsearch(index=self.PERSISTENT_RUN_INDEX, data={f'last_run_{self.account}': datetime.utcnow()}, id=self.PERSISTENT_RUN_DOC_ID)
60+
self.cro_cost_over_usage.es_operations.upload_to_elasticsearch(index=self.PERSISTENT_RUN_INDEX, data={f'last_run_{self.account}': datetime.now(tz=timezone.utc)}, id=self.PERSISTENT_RUN_DOC_ID)
6161
else:
62-
self.cro_cost_over_usage.es_operations.update_elasticsearch_index(index=self.PERSISTENT_RUN_INDEX, metadata={f'last_run_{self.account}': datetime.utcnow()}, id=self.PERSISTENT_RUN_DOC_ID)
62+
self.cro_cost_over_usage.es_operations.update_elasticsearch_index(index=self.PERSISTENT_RUN_INDEX, metadata={f'last_run_{self.account}': datetime.now(tz=timezone.utc)}, id=self.PERSISTENT_RUN_DOC_ID)
6363

6464
@logger_time_stamp
6565
def run_cloud_resources(self):

cloud_governance/cloud_resource_orchestration/clouds/azure/resource_groups/collect_cro_reports.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import logging
2-
from datetime import datetime, timedelta
2+
from datetime import datetime, timedelta, timezone
33

44
import typeguard
55

@@ -33,7 +33,7 @@ def _get_account_budget_from_payer_ce_report(self):
3333
"must": [
3434
{"term": {"CloudName.keyword": self._public_cloud_name}},
3535
{"term": {"AccountId.keyword": self._account_id}},
36-
{"term": {"Month": str(datetime.utcnow().year)}},
36+
{"term": {"Month": str(datetime.now(tz=timezone.utc).year)}},
3737
]
3838
}
3939
},
@@ -69,7 +69,7 @@ def get_user_cost_data(self, group_by_tag_name: str, group_by_tag_value: str, re
6969
resource_type = 'Forecast'
7070
pass
7171
else:
72-
end_date = datetime.utcnow().replace(microsecond=self.ZERO) + timedelta(days=1)
72+
end_date = datetime.now(tz=timezone.utc).replace(microsecond=self.ZERO) + timedelta(days=1)
7373
response = self.__cost_over_usage.get_monthly_user_es_cost_data(start_date=start_date, end_date=end_date,
7474
extra_matches=extra_filter_matches,
7575
extra_operation=self.AND,
@@ -124,7 +124,7 @@ def _get_total_account_usage_cost(self):
124124
This method returns the total account budget till date for this year
125125
:return:
126126
"""
127-
current_date = datetime.utcnow()
127+
current_date = datetime.now(tz=timezone.utc)
128128
start_date = datetime(current_date.year, 1, 1, 0, 0, 0)
129129
end_date = current_date + timedelta(days=1)
130130
cost_explorer_operations = self.__cost_over_usage.get_cost_management_object()
@@ -163,8 +163,8 @@ def update_in_progress_ticket_cost(self):
163163
group_by_tag_name=group_by_tag_name,
164164
user_name=user_name)
165165
user_daily_cost.update(ce_user_daily_report)
166-
update_data = {'actual_cost': user_cost, 'timestamp': datetime.utcnow(),
167-
f'TotalCurrentUsage-{datetime.utcnow().year}': total_account_cost,
166+
update_data = {'actual_cost': user_cost, 'timestamp': datetime.now(tz=timezone.utc),
167+
f'TotalCurrentUsage-{datetime.now(tz=timezone.utc).year}': total_account_cost,
168168
'user_daily_cost': str(user_daily_cost)}
169169
if not source_data.get(self.ALLOCATED_BUDGET):
170170
update_data[self.ALLOCATED_BUDGET] = self._get_account_budget_from_payer_ce_report()
@@ -192,7 +192,7 @@ def _get_user_usage_by_granularity(self, result_back_data: dict, tag_name: str,
192192
:param tag_value:
193193
:return:
194194
"""
195-
end_date = datetime.utcnow()
195+
end_date = datetime.now(tz=timezone.utc)
196196
start_date = end_date - timedelta(days=days)
197197
cost_explorer_object = self.__cost_over_usage.get_cost_management_object()
198198
ce_daily_usage = cost_explorer_object.get_usage(scope=self.__scope, grouping=[tag_name], granularity='Daily',

cloud_governance/cloud_resource_orchestration/clouds/common/abstract_collect_cro_reports.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import logging
22
from abc import ABC
3-
from datetime import datetime, timedelta
3+
from datetime import datetime, timedelta, timezone
44

55
import typeguard
66

@@ -116,7 +116,7 @@ def _prepare_update_es_data(self, source: dict, instance_data: list, user_cost:
116116
source['user_cro'] = instance_data[self.ZERO].get('user_cro')
117117
if instance_data[self.ZERO].get('user') and source.get('user') != instance_data[self.ZERO].get('user'):
118118
source['user'] = instance_data[self.ZERO].get('user')
119-
source['timestamp'] = datetime.utcnow()
119+
source['timestamp'] = datetime.now(tz=timezone.utc)
120120
if source.get('ticket_id_state') != 'in-progress':
121121
source['ticket_id_state'] = 'in-progress'
122122
source['approved_manager'] = instance_data[self.ZERO].get('approved_manager')

cloud_governance/cloud_resource_orchestration/clouds/common/abstract_cost_over_usage.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import logging
22
from abc import ABC, abstractmethod
33
from ast import literal_eval
4-
from datetime import datetime, timedelta
4+
from datetime import datetime, timedelta, timezone
55

66
import typeguard
77

@@ -35,7 +35,7 @@ def __init__(self):
3535
self.es_index_cro = self._environment_variables_dict.get('CRO_ES_INDEX', '')
3636
self._cro_duration_days = self._environment_variables_dict.get('CRO_DURATION_DAYS')
3737
self._over_usage_threshold = OVER_USAGE_THRESHOLD * self._over_usage_amount
38-
self.current_end_date = datetime.utcnow()
38+
self.current_end_date = datetime.now(tz=timezone.utc)
3939
self.current_start_date = self.current_end_date - timedelta(days=self._cro_duration_days)
4040
self.es_operations = ElasticSearchOperations(es_host=self._es_host, es_port=self._es_port)
4141
self._elastic_search_queries = ElasticSearchQueries(cro_duration_days=self._cro_duration_days)

cloud_governance/cloud_resource_orchestration/common/run_cro.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def save_current_timestamp(self):
3434
if not self.__es_operations.verify_elastic_index_doc_id(index=self.PERSISTENT_RUN_INDEX,
3535
doc_id=self.PERSISTENT_RUN_DOC_ID):
3636
self.__es_operations.upload_to_elasticsearch(index=self.PERSISTENT_RUN_INDEX, data={
37-
f'last_run_{self.__account.lower()}': datetime.utcnow()}, id=self.PERSISTENT_RUN_DOC_ID)
37+
f'last_run_{self.__account.lower()}': datetime.now(tz=timezone.utc)}, id=self.PERSISTENT_RUN_DOC_ID)
3838
else:
3939
self.__es_operations.update_elasticsearch_index(index=self.PERSISTENT_RUN_INDEX,
4040
metadata={
@@ -56,7 +56,7 @@ def __send_cro_alerts(self):
5656
last_run_time = source.get(f'last_run_{self.__account.lower()}')
5757
if last_run_time:
5858
last_updated_time = datetime.strptime(last_run_time, "%Y-%m-%dT%H:%M:%S.%f").date()
59-
if last_updated_time == datetime.utcnow().date():
59+
if last_updated_time == datetime.now(tz=timezone.utc).date():
6060
first_run = False
6161
self.__environment_variables_dict.update({'CRO_FIRST_RUN': first_run})
6262
if first_run:

cloud_governance/cloud_resource_orchestration/utils/elastic_search_queries.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
from datetime import datetime, timedelta
2+
from datetime import datetime, timedelta, timezone
33

44

55
class ElasticSearchQueries:
@@ -8,7 +8,7 @@ class ElasticSearchQueries:
88
"""
99

1010
def __init__(self, cro_duration_days: int = 30):
11-
self.current_end_date = datetime.utcnow()
11+
self.current_end_date = datetime.now(tz=timezone.utc)
1212
self.current_start_date = self.current_end_date - timedelta(days=cro_duration_days)
1313

1414
def get_all_in_progress_tickets(self, match_conditions: list = None, fields: list = None, **kwargs):

cloud_governance/common/clouds/aws/athena/abstract_athena_operations.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
from abc import ABC, abstractmethod
2-
from datetime import datetime
2+
from datetime import datetime, timezone
33

44
from cloud_governance.main.environment_variables import environment_variables
55

66

77
class AbstractAthenaOperations(ABC):
88

9-
CURRENT_DATE = str(datetime.utcnow().date()).replace("-", "")
9+
CURRENT_DATE = str(datetime.now(tz=timezone.utc).date()).replace("-", "")
1010

1111
def __init__(self):
1212
self.__environment_variables_dict = environment_variables.environment_variables_dict

0 commit comments

Comments
 (0)