"Fossies" - the Fresh Open Source Software Archive

Member "monasca-api-3.1.0/monasca_api/conf/types.py" (27 Sep 2019, 1978 Bytes) of package /linux/misc/openstack/monasca-api-3.1.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "types.py" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 3.1.0_vs_4.0.0.

    1 # Copyright 2017 FUJITSU LIMITED
    2 #
    3 # Licensed under the Apache License, Version 2.0 (the "License"); you may
    4 # not use this file except in compliance with the License. You may obtain
    5 # a copy of the License at
    6 #
    7 #      http://www.apache.org/licenses/LICENSE-2.0
    8 #
    9 # Unless required by applicable law or agreed to in writing, software
   10 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
   11 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
   12 # License for the specific language governing permissions and limitations
   13 # under the License.
   14 
   15 from oslo_config import cfg
   16 from oslo_config import types
   17 
   18 
   19 class HostAddressPortOpt(cfg.Opt):
   20     """Option for HostAddressPortType.
   21 
   22     Accept hostname or ip address with TCP/IP port number.
   23     """
   24     def __init__(self, name, **kwargs):
   25         ip_port_type = HostAddressPortType()
   26         super(HostAddressPortOpt, self).__init__(name,
   27                                                  type=ip_port_type,
   28                                                  **kwargs)
   29 
   30 
   31 class HostAddressPortType(types.HostAddress):
   32     """HostAddress with additional port."""
   33 
   34     def __init__(self, version=None):
   35         type_name = 'ip and port value'
   36         super(HostAddressPortType, self).__init__(version, type_name=type_name)
   37 
   38     def __call__(self, value):
   39         addr, port = value.split(':')
   40         addr = self.validate_addr(addr)
   41         port = self._validate_port(port)
   42         if not addr and not port:
   43             raise ValueError('%s is not valid ip with optional port')
   44         return '%s:%d' % (addr, port)
   45 
   46     @staticmethod
   47     def _validate_port(port):
   48         return types.Port()(port)
   49 
   50     def validate_addr(self, addr):
   51         try:
   52             addr = self.ip_address(addr)
   53         except ValueError:
   54             try:
   55                 addr = self.hostname(addr)
   56             except ValueError:
   57                 raise ValueError("%s is not a valid host address", addr)
   58         return addr