Refactor network info with nics format

Change-Id: I8db4ea95833c36546417e4021fc3cec0adcbf11e
Depends-On: Ie38e50287a2a602e030d19c8b45196ed8337d677
This commit is contained in:
liusheng 2017-04-22 11:06:21 +08:00
parent eebca27f08
commit 0badcece6c
4 changed files with 49 additions and 44 deletions

View File

@ -284,11 +284,11 @@ class ListServer(command.Lister):
return parser return parser
@staticmethod @staticmethod
def _networks_formatter(network_info): def _nics_formatter(nics):
return_info = [] return_info = []
for port_uuid in network_info: for nics in nics:
port_ips = [] port_ips = []
for fixed_ip in network_info[port_uuid]['fixed_ips']: for fixed_ip in nics['fixed_ips']:
port_ips.append(fixed_ip['ip_address']) port_ips.append(fixed_ip['ip_address'])
return_info.append(', '.join(port_ips)) return_info.append(', '.join(port_ips))
return '; '.join(return_info) return '; '.join(return_info)
@ -314,7 +314,7 @@ class ListServer(command.Lister):
"name", "name",
"status", "status",
"power_state", "power_state",
"network_info", "nics",
"image_uuid", "image_uuid",
"flavor_uuid", "flavor_uuid",
"availability_zone", "availability_zone",
@ -332,13 +332,13 @@ class ListServer(command.Lister):
"uuid", "uuid",
"name", "name",
"status", "status",
"network_info", "nics",
"image_uuid", "image_uuid",
) )
data = bc_client.server.list(detailed=True, data = bc_client.server.list(detailed=True,
all_projects=parsed_args.all_projects) all_projects=parsed_args.all_projects)
formatters = {'network_info': self._networks_formatter, formatters = {'nics': self._nics_formatter,
'extra': utils.format_dict} 'extra': utils.format_dict}
return (column_headers, return (column_headers,
(utils.get_item_properties( (utils.get_item_properties(
@ -530,16 +530,10 @@ class ShowServerNetworkInfo(command.Lister):
bc_client.server, bc_client.server,
parsed_args.server, parsed_args.server,
) )
data = bc_client.server.get_network_info(server.uuid) data = bc_client.server.get_server_nics(server.uuid)
info = data._info columns = ('network_id', 'port_id', 'mac_address', 'fixed_ips',
nics = []
for port_id in list(info):
nic = {'port_id': port_id}
nic.update(info[port_id])
nics.append(nic)
columns = ('network', 'port_id', 'mac_address', 'fixed_ips',
'floatingip', 'port_type') 'floatingip', 'port_type')
formatters = {'fixed_ips': lambda s: json.dumps(s, indent=4)} formatters = {'fixed_ips': lambda s: json.dumps(s, indent=4)}
return (columns, return (columns,
(utils.get_dict_properties( (utils.get_item_properties(
s, columns, formatters=formatters) for s in nics)) nic, columns, formatters=formatters) for nic in data))

View File

@ -238,10 +238,11 @@ class FakeServer(object):
attrs = attrs or {} attrs = attrs or {}
attrs_data = copy.deepcopy(attrs) attrs_data = copy.deepcopy(attrs)
networks = attrs_data.pop('networks', []) networks = attrs_data.pop('networks', [])
network_info = {} nics = []
for network in networks: for network in networks:
network_info[network.get('uuid')] = {} nics.append({'netowrk_id': network.get('uuid'),
attrs_data["network_info"] = network_info 'port_id': uuidutils.generate_uuid()})
attrs_data["nics"] = nics
# Set default attribute # Set default attribute
server_info = { server_info = {
@ -253,8 +254,20 @@ class FakeServer(object):
"links": [], "links": [],
"name": "server-name-" + uuidutils.generate_uuid( "name": "server-name-" + uuidutils.generate_uuid(
dashed=False), dashed=False),
"network_info": {"net-id-" + uuidutils.generate_uuid( "nics": [{
dashed=False): {}}, "network_id": uuidutils.generate_uuid(),
"port_id": uuidutils.generate_uuid(),
"floating_ip": '',
"port_type": '',
"mac_address": "52:54:00:cc:ed:87",
"fixed_ips": [{
"subnet_id": uuidutils.generate_uuid(),
"ip_address": "11.0.0.6"
}, {
"subnet_id": uuidutils.generate_uuid(),
"ip_address": "fdaa:67c7:e09e:0:5054:ff:fecc:ed87"
}]
}],
"updated_at": None, "updated_at": None,
"uuid": "server-id-" + uuidutils.generate_uuid( "uuid": "server-id-" + uuidutils.generate_uuid(
dashed=False), dashed=False),

View File

@ -40,7 +40,7 @@ class TestServer(test_base.TestBaremetalComputeV1):
'max_count', 'max_count',
'min_count', 'min_count',
'name', 'name',
'network_info', 'nics',
'updated_at', 'updated_at',
'uuid') 'uuid')
@ -54,7 +54,7 @@ class TestServer(test_base.TestBaremetalComputeV1):
1, 1,
1, 1,
fake_server.name, fake_server.name,
fake_server.network_info, fake_server.nics,
fake_server.updated_at, fake_server.updated_at,
fake_server.uuid) fake_server.uuid)
@ -143,7 +143,7 @@ class TestServerCreate(TestServer):
1, 1,
1, 1,
fk_server.name, fk_server.name,
fk_server.network_info, fk_server.nics,
fk_server.updated_at, fk_server.updated_at,
fk_server.uuid) fk_server.uuid)
self.assertEqual(expected_data, data) self.assertEqual(expected_data, data)
@ -293,26 +293,24 @@ class TestServerList(test_base.TestBaremetalComputeV1):
def setUp(self): def setUp(self):
super(TestServerList, self).setUp() super(TestServerList, self).setUp()
self.cmd = server.ListServer(self.app, None) self.cmd = server.ListServer(self.app, None)
fake_return_net = { fake_return_net = [{
"12cffc4a-b845-409e-b589-7c84be4b10d9": { "network_id": "f31af5a2-f14d-4007-b2e5-abeb82429b87",
"fixed_ips": [ "port_id": "99845c22-6268-46c1-b068-1dbcb8adaf68",
{ "floating_ip": '',
"ip_address": "172.24.4.4", "port_type": '',
"subnet_id": "a9d47430-f90b-4513-af5f-6315af54de7d" "mac_address": "52:54:00:cc:ed:87",
}, "fixed_ips": [{
{ "subnet_id": "5a324b29-9aca-43d8-a6c3-31986dda95b5",
"ip_address": "2001:db8::a", "ip_address": "172.24.4.4"
"subnet_id": "5e7b3e2d-f36f-4e30-874c-16c2d126fe53" }, {
} "subnet_id": "9baceab1-40ec-4c53-ad83-530a625bddb1",
], "ip_address": "2001:db8::a"
"mac_address": "52:54:00:6c:c4:17", }]
"network": "ade2b658-929b-439f-9528-c47057960942" }]
}
}
self.fake_servers = fakes.FakeServer.create_servers( self.fake_servers = fakes.FakeServer.create_servers(
attrs={'status': 'active', 'power_state': 'power on'}, count=3) attrs={'status': 'active', 'power_state': 'power on'}, count=3)
for s in self.fake_servers: for s in self.fake_servers:
setattr(s, 'network_info', fake_return_net) setattr(s, 'nics', fake_return_net)
self.list_columns = ( self.list_columns = (
"UUID", "UUID",
"Name", "Name",
@ -661,7 +659,7 @@ class TestServerUnLock(TestServerLockActionBase):
mock_update_all, mock_find) mock_update_all, mock_find)
@mock.patch.object(server_mgr.ServerManager, 'get_network_info') @mock.patch.object(server_mgr.ServerManager, 'get_server_nics')
@mock.patch.object(server_mgr.ServerManager, 'get') @mock.patch.object(server_mgr.ServerManager, 'get')
class TestServerShowNetInfo(TestServer): class TestServerShowNetInfo(TestServer):
def setUp(self): def setUp(self):

View File

@ -123,6 +123,6 @@ class ServerManager(base.ManagerWithFind):
url = '/servers/%s/states/lock' % base.getid(server_id) url = '/servers/%s/states/lock' % base.getid(server_id)
return self._update_all(url, data={'target': lock_state}) return self._update_all(url, data={'target': lock_state})
def get_network_info(self, server_id): def get_server_nics(self, server_id):
url = '/servers/%s/networks' % base.getid(server_id) url = '/servers/%s/networks' % base.getid(server_id)
return self._get(url, response_key='ports') return self._list(url, response_key='nics')