Coverage for watcher/applier/actions/resize.py: 79%

37 statements  

« prev     ^ index     » next       coverage.py v7.8.2, created at 2025-06-17 12:22 +0000

1# -*- encoding: utf-8 -*- 

2# Copyright (c) 2017 Servionica 

3# 

4# Authors: Alexander Chadin <a.chadin@servionica.ru> 

5# 

6# Licensed under the Apache License, Version 2.0 (the "License"); 

7# you may not use this file except in compliance with the License. 

8# You may obtain a copy of the License at 

9# 

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

11# 

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

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

14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 

15# implied. 

16# See the License for the specific language governing permissions and 

17# limitations under the License. 

18# 

19 

20from oslo_log import log 

21 

22from watcher.applier.actions import base 

23from watcher.common import nova_helper 

24 

25LOG = log.getLogger(__name__) 

26 

27 

28class Resize(base.BaseAction): 

29 """Resizes a server with specified flavor. 

30 

31 This action will allow you to resize a server to another flavor. 

32 

33 The action schema is:: 

34 

35 schema = Schema({ 

36 'resource_id': str, # should be a UUID 

37 'flavor': str, # should be either ID or Name of Flavor 

38 }) 

39 

40 The `resource_id` is the UUID of the server to resize. 

41 The `flavor` is the ID or Name of Flavor (Nova accepts either ID or Name 

42 of Flavor to resize() function). 

43 """ 

44 

45 # input parameters constants 

46 FLAVOR = 'flavor' 

47 

48 @property 

49 def schema(self): 

50 return { 

51 'type': 'object', 

52 'properties': { 

53 'resource_id': { 

54 'type': 'string', 

55 'minlength': 1, 

56 'pattern': ('^([a-fA-F0-9]){8}-([a-fA-F0-9]){4}-' 

57 '([a-fA-F0-9]){4}-([a-fA-F0-9]){4}-' 

58 '([a-fA-F0-9]){12}$') 

59 }, 

60 'flavor': { 

61 'type': 'string', 

62 'minlength': 1, 

63 }, 

64 }, 

65 'required': ['resource_id', 'flavor'], 

66 'additionalProperties': False, 

67 } 

68 

69 @property 

70 def instance_uuid(self): 

71 return self.resource_id 

72 

73 @property 

74 def flavor(self): 

75 return self.input_parameters.get(self.FLAVOR) 

76 

77 def resize(self): 

78 nova = nova_helper.NovaHelper(osc=self.osc) 

79 LOG.debug("Resize instance %s to %s flavor", self.instance_uuid, 

80 self.flavor) 

81 instance = nova.find_instance(self.instance_uuid) 

82 result = None 

83 if instance: 83 ↛ 92line 83 didn't jump to line 92 because the condition on line 83 was always true

84 try: 

85 result = nova.resize_instance( 

86 instance_id=self.instance_uuid, flavor=self.flavor) 

87 except Exception as exc: 

88 LOG.exception(exc) 

89 LOG.critical( 

90 "Unexpected error occurred. Resizing failed for " 

91 "instance %s.", self.instance_uuid) 

92 return result 

93 

94 def execute(self): 

95 return self.resize() 

96 

97 def revert(self): 

98 LOG.warning("revert not supported") 

99 

100 def pre_condition(self): 

101 # TODO(jed): check if the instance exists / check if the instance is on 

102 # the source_node 

103 pass 

104 

105 def post_condition(self): 

106 # TODO(jed): check extra parameters (network response, etc.) 

107 pass 

108 

109 def get_description(self): 

110 """Description of the action""" 

111 return "Resize a server with specified flavor."