Coverage for nova/api/openstack/compute/views/keypairs.py: 95%

34 statements  

« prev     ^ index     » next       coverage.py v7.6.12, created at 2025-04-24 11:16 +0000

1# Copyright 2016 Mirantis Inc 

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 

16from nova.api.openstack import common 

17 

18 

19class ViewBuilder(common.ViewBuilder): 

20 

21 _collection_name = 'os-keypairs' 

22 # TODO(takashin): After v2 and v2.1 is no longer supported, 

23 # 'type' can always be included in the response. 

24 _index_params = ('name', 'public_key', 'fingerprint') 

25 _create_params = _index_params + ('user_id',) 

26 _show_params = _create_params + ('created_at', 'deleted', 'deleted_at', 

27 'id', 'updated_at') 

28 _index_params_v2_2 = _index_params + ('type',) 

29 _show_params_v2_2 = _show_params + ('type',) 

30 

31 def get_links(self, request, keypairs): 

32 return self._get_collection_links(request, keypairs, 

33 self._collection_name, 'name') 

34 

35 # TODO(oomichi): It is necessary to filter a response of keypair with 

36 # _build_keypair() when v2.1+microversions for implementing consistent 

37 # behaviors in this keypair resource. 

38 @staticmethod 

39 def _build_keypair(keypair, attrs): 

40 body = {} 

41 for attr in attrs: 

42 body[attr] = keypair[attr] 

43 return body 

44 

45 def create(self, keypair, private_key=False, key_type=False): 

46 params = [] 

47 if private_key: 

48 params.append('private_key') 

49 # TODO(takashin): After v2 and v2.1 is no longer supported, 

50 # 'type' can always be included in the response. 

51 if key_type: 

52 params.append('type') 

53 params.extend(self._create_params) 

54 

55 return {'keypair': self._build_keypair(keypair, params)} 

56 

57 def index(self, req, key_pairs, key_type=False, links=False): 

58 keypairs_list = [ 

59 {'keypair': self._build_keypair( 

60 key_pair, 

61 self._index_params_v2_2 if key_type else self._index_params)} 

62 for key_pair in key_pairs] 

63 keypairs_dict = {'keypairs': keypairs_list} 

64 

65 if links: 

66 keypairs_links = self.get_links(req, key_pairs) 

67 

68 if keypairs_links: 68 ↛ 69line 68 didn't jump to line 69 because the condition on line 68 was never true

69 keypairs_dict['keypairs_links'] = keypairs_links 

70 

71 return keypairs_dict 

72 

73 def show(self, keypair, key_type=False): 

74 return {'keypair': self._build_keypair( 

75 keypair, self._show_params_v2_2 if key_type 

76 else self._show_params)}