def test_create_subnet_with_subnetpool_ipv4_with_prefixlen(self): resource = 'subnet' cmd = subnet.CreateSubnet(test_cli20.MyApp(sys.stdout), None) name = 'myname' myid = 'myid' netid = 'netid' args = ['--tenant_id', 'tenantid', '--ip-version', '4', '--ipv6-address-mode', 'slaac', '--subnetpool', 'subnetpool_id', '--prefixlen', '31', netid] position_names = ['ip_version', 'ipv6_address_mode', 'network_id', 'subnetpool_id'] position_values = [4, None, netid, 'subnetpool_id'] self.mox.StubOutWithMock(neutronV20, 'find_resource_by_name_or_id') neutronV20.find_resource_by_name_or_id( self.client, 'subnetpool', 'subnetpool_id').AndReturn({'id': 'subnetpool_id', 'ip_version': 4}) self._test_create_resource( resource, cmd, name, myid, args, position_names, position_values, tenant_id='tenantid', no_api_call=True, expected_exception=exceptions.CommandError)
def test_create_subnet_with_subnetpool_ipv6_and_ip_ver_ignored(self): resource = 'subnet' cmd = subnet.CreateSubnet(test_cli20.MyApp(sys.stdout), None) name = 'myname' myid = 'myid' netid = 'netid' args = [ '--tenant_id', 'tenantid', '--ip-version', '4', '--subnetpool', 'subnetpool_id', netid ] position_names = ['ip_version', 'network_id', 'subnetpool_id'] position_values = [6, netid, 'subnetpool_id'] self.mox.StubOutWithMock(neutronV20, 'find_resource_by_name_or_id') neutronV20.find_resource_by_name_or_id(self.client, 'subnetpool', 'subnetpool_id').AndReturn({ 'id': 'subnetpool_id', 'ip_version': 6 }) self._test_create_resource(resource, cmd, name, myid, args, position_names, position_values, tenant_id='tenantid')
def test_create_subnet_with_subnetpool_ipv4_with_prefixlen(self): resource = 'subnet' cmd = subnet.CreateSubnet(test_cli20.MyApp(sys.stdout), None) name = 'myname' myid = 'myid' netid = 'netid' args = [ '--tenant_id', 'tenantid', '--ip-version', '4', '--ipv6-address-mode', 'slaac', '--subnetpool', 'subnetpool_id', '--prefixlen', '31', netid ] position_names = [ 'ip_version', 'ipv6_address_mode', 'network_id', 'subnetpool_id' ] position_values = [4, None, netid, 'subnetpool_id'] self.mox.StubOutWithMock(neutronV20, 'find_resource_by_name_or_id') neutronV20.find_resource_by_name_or_id(self.client, 'subnetpool', 'subnetpool_id').AndReturn({ 'id': 'subnetpool_id', 'ip_version': 4 }) self._test_create_resource(resource, cmd, name, myid, args, position_names, position_values, tenant_id='tenantid', no_api_call=True, expected_exception=exceptions.CommandError)
def test_create_subnet_with_subnetpool_ipv6_and_ip_ver_ignored(self): resource = "subnet" cmd = subnet.CreateSubnet(test_cli20.MyApp(sys.stdout), None) name = "myname" myid = "myid" netid = "netid" args = ["--tenant_id", "tenantid", "--ip-version", "4", "--subnetpool", "subnetpool_id", netid] position_names = ["ip_version", "network_id", "subnetpool_id"] position_values = [6, netid, "subnetpool_id"] self.mox.StubOutWithMock(neutronV20, "find_resource_by_name_or_id") neutronV20.find_resource_by_name_or_id(self.client, "subnetpool", "subnetpool_id").AndReturn( {"id": "subnetpool_id", "ip_version": 6} ) self._test_create_resource( resource, cmd, name, myid, args, position_names, position_values, tenant_id="tenantid" )
def GetNetID(self, network_name): try: detail = neutronv2.find_resource_by_name_or_id( self.neutron, 'network', network_name) except neutronexceptions.NotFound as e: log.error(e) return e return detail['id']
def test_create_subnet_with_subnetpool_ipv6_and_ip_ver_ignored(self): resource = 'subnet' cmd = subnet.CreateSubnet(test_cli20.MyApp(sys.stdout), None) name = 'myname' myid = 'myid' netid = 'netid' args = ['--tenant_id', 'tenantid', '--ip-version', '4', '--subnetpool', 'subnetpool_id', netid] position_names = ['ip_version', 'network_id', 'subnetpool_id'] position_values = [6, netid, 'subnetpool_id'] self.mox.StubOutWithMock(neutronV20, 'find_resource_by_name_or_id') neutronV20.find_resource_by_name_or_id( self.client, 'subnetpool', 'subnetpool_id').AndReturn({'id': 'subnetpool_id', 'ip_version': 6}) self._test_create_resource( resource, cmd, name, myid, args, position_names, position_values, tenant_id='tenantid')
def test_create_subnet_with_subnetpool_ipv4_with_prefixlen(self): resource = "subnet" cmd = subnet.CreateSubnet(test_cli20.MyApp(sys.stdout), None) name = "myname" myid = "myid" netid = "netid" args = [ "--tenant_id", "tenantid", "--ip-version", "4", "--ipv6-address-mode", "slaac", "--subnetpool", "subnetpool_id", "--prefixlen", "31", netid, ] position_names = ["ip_version", "ipv6_address_mode", "network_id", "subnetpool_id"] position_values = [4, None, netid, "subnetpool_id"] self.mox.StubOutWithMock(neutronV20, "find_resource_by_name_or_id") neutronV20.find_resource_by_name_or_id(self.client, "subnetpool", "subnetpool_id").AndReturn( {"id": "subnetpool_id", "ip_version": 4} ) self._test_create_resource( resource, cmd, name, myid, args, position_names, position_values, tenant_id="tenantid", no_api_call=True, expected_exception=exceptions.CommandError, )
def args2body(self, parsed_args): _network_id = neutronV20.find_resourceid_by_name_or_id( self.get_client(), 'network', parsed_args.network_id) body = {'network_id': _network_id} if parsed_args.prefixlen: body['prefixlen'] = parsed_args.prefixlen ip_version = parsed_args.ip_version if parsed_args.use_default_subnetpool: body['use_default_subnetpool'] = True if parsed_args.segment: body['segment_id'] = neutronV20.find_resourceid_by_name_or_id( self.get_client(), 'segment', parsed_args.segment) if parsed_args.subnetpool: if parsed_args.subnetpool == 'None': _subnetpool_id = None else: _subnetpool = neutronV20.find_resource_by_name_or_id( self.get_client(), 'subnetpool', parsed_args.subnetpool) _subnetpool_id = _subnetpool['id'] # Now that we have the pool_id - let's just have a check on the # ip version used in the pool ip_version = _subnetpool['ip_version'] body['subnetpool_id'] = _subnetpool_id # IP version needs to be set as IP version can be # determined by subnetpool. body['ip_version'] = ip_version if parsed_args.cidr: # With subnetpool, cidr is now optional for creating subnet. cidr = parsed_args.cidr body['cidr'] = cidr unusable_cidr = '/32' if ip_version == 4 else '/128' if cidr.endswith(unusable_cidr): self.log.warning( _("An IPv%(ip)d subnet with a %(cidr)s CIDR " "will have only one usable IP address so " "the device attached to it will not have " "any IP connectivity."), { "ip": ip_version, "cidr": unusable_cidr }) updatable_args2body(parsed_args, body, ip_version=ip_version) if parsed_args.tenant_id: body['tenant_id'] = parsed_args.tenant_id return {'subnet': body}
def args2body(self, parsed_args): _network_id = neutronV20.find_resourceid_by_name_or_id( self.get_client(), 'network', parsed_args.network_id) body = {'network_id': _network_id} if parsed_args.prefixlen: body['prefixlen'] = parsed_args.prefixlen ip_version = parsed_args.ip_version if parsed_args.use_default_subnetpool: body['use_default_subnetpool'] = True if parsed_args.segment: body['segment_id'] = neutronV20.find_resourceid_by_name_or_id( self.get_client(), 'segment', parsed_args.segment) if parsed_args.subnetpool: if parsed_args.subnetpool == 'None': _subnetpool_id = None else: _subnetpool = neutronV20.find_resource_by_name_or_id( self.get_client(), 'subnetpool', parsed_args.subnetpool) _subnetpool_id = _subnetpool['id'] # Now that we have the pool_id - let's just have a check on the # ip version used in the pool ip_version = _subnetpool['ip_version'] body['subnetpool_id'] = _subnetpool_id # IP version needs to be set as IP version can be # determined by subnetpool. body['ip_version'] = ip_version if parsed_args.cidr: # With subnetpool, cidr is now optional for creating subnet. cidr = parsed_args.cidr body['cidr'] = cidr unusable_cidr = '/32' if ip_version == 4 else '/128' if cidr.endswith(unusable_cidr): self.log.warning(_("An IPv%(ip)d subnet with a %(cidr)s CIDR " "will have only one usable IP address so " "the device attached to it will not have " "any IP connectivity."), {"ip": ip_version, "cidr": unusable_cidr}) updatable_args2body(parsed_args, body, ip_version=ip_version) if parsed_args.tenant_id: body['tenant_id'] = parsed_args.tenant_id return {'subnet': body}
def args2body(self, parsed_args): _network_id = neutronV20.find_resourceid_by_name_or_id(self.get_client(), "network", parsed_args.network_id) body = {"subnet": {"network_id": _network_id}} if parsed_args.prefixlen: body["subnet"].update({"prefixlen": parsed_args.prefixlen}) ip_version = parsed_args.ip_version if parsed_args.subnetpool: if parsed_args.subnetpool == "None": _subnetpool_id = None else: _subnetpool = neutronV20.find_resource_by_name_or_id( self.get_client(), "subnetpool", parsed_args.subnetpool ) _subnetpool_id = _subnetpool["id"] # Now that we have the pool_id - let's just have a check on the # ip version used in the pool ip_version = _subnetpool["ip_version"] body["subnet"].update({"subnetpool_id": _subnetpool_id}) # IP version needs to be set as IP version can be # determined by subnetpool. body["subnet"]["ip_version"] = ip_version if parsed_args.cidr: # With subnetpool, cidr is now optional for creating subnet. cidr = parsed_args.cidr body["subnet"].update({"cidr": cidr}) unusable_cidr = "/32" if ip_version == 4 else "/128" if cidr.endswith(unusable_cidr): self.log.warning( _( "An IPv%(ip)d subnet with a %(cidr)s CIDR " "will have only one usable IP address so " "the device attached to it will not have " "any IP connectivity." ) % {"ip": ip_version, "cidr": unusable_cidr} ) updatable_args2body(parsed_args, body, ip_version=ip_version) if parsed_args.tenant_id: body["subnet"].update({"tenant_id": parsed_args.tenant_id}) return body
def get_fixed_network_id(self, osc, cluster_template): try: subnet = neutronV20.find_resource_by_name_or_id( osc.neutron(), 'subnet', cluster_template.fixed_subnet) except n_exception.NeutronException as e: # NOTE(yuanying): NeutronCLIError doesn't have status_code # if subnet name is duplicated, NeutronClientNoUniqueMatch # (which is kind of NeutronCLIError) will be raised. if getattr(e, 'status_code', 400) < 500: raise exception.InvalidSubnet(message=("%s" % e)) else: raise e if subnet['ip_version'] != 4: raise exception.InvalidSubnet( message="Subnet IP version should be 4") return subnet['network_id']
def get_fixed_network_id(self, osc, cluster_template): try: subnet = neutronV20.find_resource_by_name_or_id( osc.neutron(), 'subnet', cluster_template.fixed_subnet ) except n_exception.NeutronException as e: # NOTE(yuanying): NeutronCLIError doesn't have status_code # if subnet name is duplicated, NeutronClientNoUniqueMatch # (which is kind of NeutronCLIError) will be raised. if getattr(e, 'status_code', 400) < 500: raise exception.InvalidSubnet(message=("%s" % e)) else: raise e if subnet['ip_version'] != 4: raise exception.InvalidSubnet( message="Subnet IP version should be 4" ) return subnet['network_id']
def _get_listener(client, listener_id_or_name): return neutronV20.find_resource_by_name_or_id( client, 'listener', listener_id_or_name)
def find_resource_by_name_or_id(self, resource_name, name_or_id): return neutronV20.find_resource_by_name_or_id(self.client, resource_name, name_or_id)
def find_resource_by_name_or_id(self, resource_name, name_or_id): return neutronV20.find_resource_by_name_or_id(self.client, resource_name, name_or_id)
def _get_pool(client, pool_id_or_name): return neutronV20.find_resource_by_name_or_id(client, 'pool', pool_id_or_name, cmd_resource='lbaas_pool')
def _get_listener(client, listener_id_or_name): return neutronV20.find_resource_by_name_or_id( client, 'listener', listener_id_or_name)
def _get_pool(client, pool_id_or_name): return neutronV20.find_resource_by_name_or_id( client, 'pool', pool_id_or_name, cmd_resource='lbaas_pool')