Source code for heat.engine.constraint.common_constraints

#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

import croniter
import netaddr
import pytz
import six

from oslo_utils import netutils
from oslo_utils import timeutils

from heat.common.i18n import _
from heat.engine import constraints


[docs]class IPConstraint(constraints.BaseCustomConstraint):
[docs] def validate(self, value, context): self._error_message = 'Invalid IP address' return netutils.is_valid_ip(value)
[docs]class MACConstraint(constraints.BaseCustomConstraint):
[docs] def validate(self, value, context): self._error_message = 'Invalid MAC address.' return netaddr.valid_mac(value)
[docs]class CIDRConstraint(constraints.BaseCustomConstraint): def _validate_whitespace(self, data): self._error_message = ("Invalid net cidr '%s' contains " "whitespace" % data) if len(data.split()) > 1: return False return True
[docs] def validate(self, value, context): try: netaddr.IPNetwork(value) return self._validate_whitespace(value) except Exception as ex: self._error_message = 'Invalid net cidr %s ' % six.text_type(ex) return False
[docs]class ISO8601Constraint(object):
[docs] def validate(self, value, context): try: timeutils.parse_isotime(value) except Exception: return False else: return True
[docs]class CRONExpressionConstraint(constraints.BaseCustomConstraint):
[docs] def validate(self, value, context): if not value: return True try: croniter.croniter(value) return True except Exception as ex: self._error_message = _( 'Invalid CRON expression: %s') % six.text_type(ex) return False
[docs]class TimezoneConstraint(constraints.BaseCustomConstraint):
[docs] def validate(self, value, context): if not value: return True try: pytz.timezone(value) return True except Exception as ex: self._error_message = _( 'Invalid timezone: %s') % six.text_type(ex) return False

Project Source