Debian Codegraph @

neutron_lib/api/validators/availability_zone.py

raw
1 # Licensed under the Apache License, Version 2.0 (the "License");
2 # you may not use this file except in compliance with the License.
3 # You may obtain a copy of the License at
4 #
5 # http://www.apache.org/licenses/LICENSE-2.0
6 #
7 # Unless required by applicable law or agreed to in writing, software
8 # distributed under the License is distributed on an "AS IS" BASIS,
9 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
10 # implied.
11 # See the License for the specific language governing permissions and
12 # limitations under the License.
13
14 from oslo_serialization import jsonutils
15
16 from neutron_lib._i18n import _
17 from neutron_lib.api import validators
18 from neutron_lib.db import constants as db_const
19 from neutron_lib import exceptions
20
21
22 def convert_az_list_to_string(az_list):
23 """Convert a list of availability zones into a string.
24
25 :param az_list: A list of AZs.
26 :returns: The az_list in string format.
27 """
28 return jsonutils.dumps(az_list)
29
30
31 def convert_az_string_to_list(az_string):
32 """Convert an AZ list in string format into a python list.
33
34 :param az_string: The AZ list in string format.
35 :returns: The python list of AZs build from az_string.
36 """
37 return jsonutils.loads(az_string) if az_string else []
38
39
40 def _validate_availability_zone_hints(data, valid_value=None):
41 msg = validators.validate_list_of_unique_strings(data)
42 if msg:
43 return msg
44 az_string = convert_az_list_to_string(data)
45 if len(az_string) > db_const.AZ_HINTS_DB_LEN:
46 msg = _("Too many availability_zone_hints specified")
47 raise exceptions.InvalidInput(error_message=msg)