Merge "Use zoneinfo instead of pytz if available"

This commit is contained in:
Zuul 2024-06-05 14:10:51 +00:00 committed by Gerrit Code Review
commit b8347142f8
4 changed files with 47 additions and 15 deletions

View File

@ -30,14 +30,20 @@ from oslo_utils import timeutils
from oslo_utils import uuidutils from oslo_utils import uuidutils
import pecan import pecan
from pecan import rest from pecan import rest
import pytz
import pytz.exceptions
from stevedore import extension from stevedore import extension
from urllib import parse as urlparse from urllib import parse as urlparse
import wsme import wsme
from wsme import types as wtypes from wsme import types as wtypes
import wsmeext.pecan as wsme_pecan import wsmeext.pecan as wsme_pecan
try:
import zoneinfo
except ImportError:
# zoneinfo is available in Python >= 3.9
import pytz
import pytz.exceptions
zoneinfo = None
import aodh import aodh
from aodh.api.controllers.v2 import base from aodh.api.controllers.v2 import base
from aodh.api.controllers.v2 import utils as v2_utils from aodh.api.controllers.v2 import utils as v2_utils
@ -176,9 +182,12 @@ class AlarmTimeConstraint(base.Base):
@staticmethod @staticmethod
def validate(tc): def validate(tc):
if tc.timezone: if tc.timezone:
checker = zoneinfo.ZoneInfo if zoneinfo else pytz.timezone
exc = (zoneinfo.ZoneInfoNotFoundError if zoneinfo else
pytz.exceptions.UnknownTimeZoneError)
try: try:
pytz.timezone(tc.timezone) checker(tc.timezone)
except pytz.exceptions.UnknownTimeZoneError: except exc:
raise base.ClientSideError(_("Timezone %s is not valid") raise base.ClientSideError(_("Timezone %s is not valid")
% tc.timezone) % tc.timezone)
return tc return tc

View File

@ -27,9 +27,15 @@ from oslo_config import cfg
from oslo_log import log from oslo_log import log
from oslo_utils import timeutils from oslo_utils import timeutils
from oslo_utils import uuidutils from oslo_utils import uuidutils
import pytz
from stevedore import extension from stevedore import extension
try:
import zoneinfo
except ImportError:
# zoneinfo is available in Python >= 3.9
import pytz
zoneinfo = None
import aodh import aodh
from aodh import coordination from aodh import coordination
from aodh import keystone_client from aodh import keystone_client
@ -148,9 +154,13 @@ class Evaluator(object, metaclass=abc.ABCMeta):
if not alarm.time_constraints: if not alarm.time_constraints:
return True return True
now_utc = timeutils.utcnow().replace(tzinfo=pytz.utc) now_utc = timeutils.utcnow().replace(tzinfo=datetime.timezone.utc)
for tc in alarm.time_constraints: for tc in alarm.time_constraints:
tz = pytz.timezone(tc['timezone']) if tc['timezone'] else None if zoneinfo:
tz = (zoneinfo.ZoneInfo(tc['timezone'])
if tc['timezone'] else None)
else:
tz = pytz.timezone(tc['timezone']) if tc['timezone'] else None
now_tz = now_utc.astimezone(tz) if tz else now_utc now_tz = now_utc.astimezone(tz) if tz else now_utc
start_cron = croniter.croniter(tc['start'], now_tz) start_cron = croniter.croniter(tc['start'], now_tz)
if cls._is_exact_match(start_cron, now_tz): if cls._is_exact_match(start_cron, now_tz):

View File

@ -22,7 +22,13 @@ from unittest import mock
from gnocchiclient import exceptions from gnocchiclient import exceptions
from oslo_utils import timeutils from oslo_utils import timeutils
from oslo_utils import uuidutils from oslo_utils import uuidutils
import pytz
try:
import zoneinfo
except ImportError:
# zoneinfo is available in Python >= 3.9
import pytz
zoneinfo = None
from aodh.evaluator import gnocchi from aodh.evaluator import gnocchi
from aodh import messaging from aodh import messaging
@ -351,9 +357,12 @@ class TestGnocchiResourceThresholdEvaluate(TestGnocchiEvaluatorBase):
'duration': 10800, # 3 hours 'duration': 10800, # 3 hours
'timezone': 'Europe/Ljubljana'} 'timezone': 'Europe/Ljubljana'}
] ]
dt = datetime.datetime(2014, 1, 1, 15, 0, 0, if zoneinfo:
tzinfo=pytz.timezone('Europe/Ljubljana')) tzinfo = zoneinfo.ZoneInfo('Europe/Ljubljana')
mock_utcnow.return_value = dt.astimezone(pytz.UTC) else:
tzinfo = pytz.timezone('Europe/Ljubljana')
dt = datetime.datetime(2014, 1, 1, 15, 0, 0, tzinfo=tzinfo)
mock_utcnow.return_value = dt.astimezone(datetime.timezone.utc)
self.client.metric.get_measures.return_value = [] self.client.metric.get_measures.return_value = []
self._evaluate_all_alarms() self._evaluate_all_alarms()
self._assert_all_alarms('ok') self._assert_all_alarms('ok')
@ -373,9 +382,12 @@ class TestGnocchiResourceThresholdEvaluate(TestGnocchiEvaluatorBase):
'duration': 10800, # 3 hours 'duration': 10800, # 3 hours
'timezone': 'Europe/Ljubljana'} 'timezone': 'Europe/Ljubljana'}
] ]
dt = datetime.datetime(2014, 1, 1, 12, 0, 0, if zoneinfo:
tzinfo=pytz.timezone('Europe/Ljubljana')) tzinfo = zoneinfo.ZoneInfo('Europe/Ljubljana')
mock_utcnow.return_value = dt.astimezone(pytz.UTC) else:
tzinfo = pytz.timezone('Europe/Ljubljana')
dt = datetime.datetime(2014, 1, 1, 12, 0, 0, tzinfo=tzinfo)
mock_utcnow.return_value = dt.astimezone(datetime.timezone.utc)
self.client.metric.get_measures.return_value = [] self.client.metric.get_measures.return_value = []
self._evaluate_all_alarms() self._evaluate_all_alarms()
self._assert_all_alarms('insufficient data') self._assert_all_alarms('insufficient data')

View File

@ -19,7 +19,7 @@ oslo.messaging>=5.2.0 # Apache-2.0
oslo.middleware>=3.22.0 # Apache-2.0 oslo.middleware>=3.22.0 # Apache-2.0
oslo.utils>=4.7.0 # Apache-2.0 oslo.utils>=4.7.0 # Apache-2.0
python-keystoneclient>=1.6.0 python-keystoneclient>=1.6.0
pytz>=2013.6 pytz>=2013.6;python_version<"3.9" # MIT
requests>=2.5.2 requests>=2.5.2
stevedore>=1.5.0 # Apache-2.0 stevedore>=1.5.0 # Apache-2.0
SQLAlchemy>=1.4.1 SQLAlchemy>=1.4.1
@ -34,3 +34,4 @@ python-observabilityclient>=0.0.4
python-octaviaclient>=1.8.0 python-octaviaclient>=1.8.0
python-dateutil>=2.8.2 # BSD python-dateutil>=2.8.2 # BSD
python-heatclient>=1.17.0 python-heatclient>=1.17.0
tzdata>=2022.4;python_version>="3.9" # MIT