def get_host(cls, cli, _id, force_create=False, tenant=None): if isinstance(_id, six.string_types) and ('.' in _id or ':' in _id): # it looks like an ip address, find or create the host address = converter.url_to_host(_id) netmask = converter.url_to_mask(_id) ports = UnityHostIpPortList(cli=cli, address=address) # since tenant is not supported by all kinds of system. So we # should avoid send the tenant request if tenant is None tenant = None if tenant is None else UnityTenant.get(cli, tenant) ports = [port for port in ports if port.host.tenant == tenant] if len(ports) == 1: ret = ports[0].host elif force_create: log.info('cannot find an existing host with ip {}. ' 'create a new host "{}" to attach it.'.format( address, address)) host_type = (HostTypeEnum.SUBNET if netmask else HostTypeEnum.HOST_MANUAL) host_name = ('{}_{}'.format(address, netmask) if netmask else address) host = cls.create(cli, host_name, host_type=host_type, tenant=tenant) host.add_ip_port(address, netmask=netmask) ret = host else: ret = None else: ret = cls.get(cli=cli, _id=_id) return ret
def get_host(cls, cli, _id, force_create=False): if isinstance(_id, six.string_types) and ('.' in _id or ':' in _id): # it looks like an ip address, find or create the host address = converter.url_to_host(_id) netmask = converter.url_to_mask(_id) ports = UnityHostIpPortList(cli=cli, address=address) if len(ports) == 1: ret = ports[0].host elif force_create: log.info('cannot find an existing host with ip {}. ' 'create a new host "{}" to attach it.'.format( address, address)) host_type = (HostTypeEnum.SUBNET if netmask else HostTypeEnum.HOST_MANUAL) host_name = ('{}_{}'.format(address, netmask) if netmask else address) host = cls.create(cli, host_name, host_type=host_type) host.add_ip_port(address, netmask=netmask) ret = host else: ret = None else: ret = cls.get(cli=cli, _id=_id) return ret
def get_host(cls, cli, _id, force_create=False): if isinstance(_id, six.string_types) and ('.' in _id or ':' in _id): # it looks like an ip address, find or create the host address = converter.url_to_host(_id) netmask = converter.url_to_mask(_id) ports = UnityHostIpPortList(cli=cli, address=address) if len(ports) == 1: ret = ports[0].host elif force_create: log.info('cannot find an existing host with ip {}. ' 'create a new host "{}" to attach it.' .format(address, address)) host_type = (HostTypeEnum.SUBNET if netmask else HostTypeEnum.HOST_MANUAL) host_name = ('{}_{}'.format(address, netmask) if netmask else address) host = cls.create(cli, host_name, host_type=host_type) host.add_ip_port(address, netmask=netmask) ret = host else: ret = None else: ret = cls.get(cli=cli, _id=_id) return ret
def test_url_to_host_suffix(self): url = '10.0.0.1/32' ret = converter.url_to_host(url) assert_that(ret, equal_to('10.0.0.1'))
def test_url_to_host_port(self): url = '10.0.0.1:90' ret = converter.url_to_host(url) assert_that(ret, equal_to('10.0.0.1'))
def test_url_to_host_default_protocol(self): url = '10.0.0.1/my.txt' ret = converter.url_to_host(url) assert_that(ret, equal_to('10.0.0.1'))
def test_url_to_host_default_port(self): url = 'https://2001:db8:a0b:12f0::1-eth0/my.txt' ret = converter.url_to_host(url) assert_that(ret, equal_to('2001:db8:a0b:12f0::1-eth0'))
def test_url_to_host_ipv6(self): url = 'https://[2001:db8:a0b:12f0::1%eth0]:21/my.txt' ret = converter.url_to_host(url) assert_that(ret, equal_to('[2001:db8:a0b:12f0::1%eth0]'))
def test_url_to_host_ssl(self): url = 'https://10.0.0.1:4443/page/my.html?filter=name' ret = converter.url_to_host(url) assert_that(ret, equal_to('10.0.0.1'))
def test_url_to_host(self): url = 'http://www.abc.com:5050/page.xml' ret = converter.url_to_host(url) assert_that(ret, equal_to('www.abc.com'))
def expect_error(): converter.url_to_host(url)