Coverage for nova/api/openstack/compute/views/addresses.py: 100%

19 statements  

« prev     ^ index     » next       coverage.py v7.6.12, created at 2025-04-17 15:08 +0000

1# Copyright 2010-2011 OpenStack Foundation 

2# All Rights Reserved. 

3# 

4# Licensed under the Apache License, Version 2.0 (the "License"); you may 

5# not use this file except in compliance with the License. You may obtain 

6# a copy of the License at 

7# 

8# http://www.apache.org/licenses/LICENSE-2.0 

9# 

10# Unless required by applicable law or agreed to in writing, software 

11# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 

12# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 

13# License for the specific language governing permissions and limitations 

14# under the License. 

15 

16import collections 

17import itertools 

18 

19from nova.api.openstack import common 

20 

21 

22class ViewBuilder(common.ViewBuilder): 

23 """Models server addresses as a dictionary.""" 

24 

25 _collection_name = "addresses" 

26 

27 def basic(self, request, ip, extend_address=False): 

28 """Return a dictionary describing an IP address.""" 

29 address = { 

30 "version": ip["version"], 

31 "addr": ip["address"], 

32 } 

33 if extend_address: 

34 address.update({ 

35 "OS-EXT-IPS:type": ip["type"], 

36 "OS-EXT-IPS-MAC:mac_addr": ip['mac_address'], 

37 }) 

38 return address 

39 

40 def show(self, request, network, label, extend_address=False): 

41 """Returns a dictionary describing a network.""" 

42 all_ips = itertools.chain(network["ips"], network["floating_ips"]) 

43 return { 

44 label: [self.basic(request, ip, extend_address) for ip in all_ips], 

45 } 

46 

47 def index(self, request, networks, extend_address=False): 

48 """Return a dictionary describing a list of networks.""" 

49 addresses = collections.OrderedDict() 

50 for label, network in networks.items(): 

51 network_dict = self.show(request, network, label, extend_address) 

52 addresses[label] = network_dict[label] 

53 return {'addresses': addresses}