Sfoglia il codice sorgente

'加入变更配置接口'

JamesIter 8 anni fa
parent
commit
1060e282ac
4 ha cambiato i file con 77 aggiunte e 2 eliminazioni
  1. 60 2
      api/guest.py
  2. 5 0
      api_route_table.py
  3. 8 0
      models/event_processor.py
  4. 4 0
      state_code.py

+ 60 - 2
api/guest.py

@@ -1131,6 +1131,9 @@ def r_allocate_bandwidth(uuids, bandwidth, bandwidth_unit):
     try:
         ji.Check.previewing(args_rules, {'uuids': uuids, 'bandwidth': bandwidth, 'bandwidth_unit': bandwidth_unit})
 
+        ret = dict()
+        ret['state'] = ji.Common.exchange_state(20000)
+
         bandwidth = int(bandwidth)
 
         if bandwidth_unit == 'k':
@@ -1143,9 +1146,8 @@ def r_allocate_bandwidth(uuids, bandwidth, bandwidth_unit):
             bandwidth = bandwidth * 1000 ** 3
 
         else:
-            ret = dict()
             ret['state'] = ji.Common.exchange_state(41203)
-            raise ji.PreviewingError(json.dumps(ret, ensure_ascii=False))
+            return ret
 
         # http://man7.org/linux/man-pages/man8/tc.8.html
         # 如果带宽大于 tc 所控最大速率,则置其为无限带宽
@@ -1176,8 +1178,64 @@ def r_allocate_bandwidth(uuids, bandwidth, bandwidth_unit):
 
             Utils.emit_instruction(message=json.dumps(message, ensure_ascii=False))
 
+        return ret
+
+    except ji.PreviewingError, e:
+        return json.loads(e.message)
+
+
+@Utils.dumps2response
+def r_adjust_ability(uuids, cpu, memory):
+    args_rules = [
+        Rules.UUIDS.value,
+        Rules.CPU.value,
+        Rules.MEMORY.value,
+    ]
+
+    try:
         ret = dict()
         ret['state'] = ji.Common.exchange_state(20000)
+
+        cpu = int(cpu)
+        memory = int(memory)
+
+        ji.Check.previewing(args_rules, {'uuids': uuids, 'cpu': cpu, 'memory': memory})
+
+        not_ready_yet_of_guests = list()
+
+        guest = Guest()
+
+        # 检测所指定的 UUDIs 实例都存在。且状态都为可以操作状态(即关闭状态)。
+        for uuid in uuids.split(','):
+            guest.uuid = uuid
+            guest.get_by('uuid')
+
+            if guest.status != status.GuestState.shutoff.value:
+                not_ready_yet_of_guests.append(guest.__dict__)
+
+        if not_ready_yet_of_guests.__len__() > 0:
+            ret['state'] = ji.Common.exchange_state(41261)
+            ret['data'] = not_ready_yet_of_guests
+            return ret
+
+        for uuid in uuids.split(','):
+            guest.uuid = uuid
+            guest.get_by('uuid')
+            guest.cpu = cpu
+            guest.memory = memory
+
+            message = {
+                '_object': 'guest',
+                'action': 'adjust_ability',
+                'uuid': guest.uuid,
+                'node_id': guest.node_id,
+                'cpu': guest.cpu,
+                'memory': guest.memory,
+                'passback_parameters': {'cpu': guest.cpu, 'memory': guest.memory}
+            }
+
+            Utils.emit_instruction(message=json.dumps(message, ensure_ascii=False))
+
         return ret
 
     except ji.PreviewingError, e:

+ 5 - 0
api_route_table.py

@@ -105,8 +105,13 @@ add_rule_api(guest.blueprint, '/<uuid>', api_func='guest.r_update', methods=['PA
 add_rule_api(guest.blueprints, '/<uuids>', api_func='guest.r_delete', methods=['DELETE'])
 add_rule_api(guest.blueprints, '/_reset_password/<uuids>/<password>', api_func='guest.r_reset_password',
              methods=['PUT'])
+
 add_rule_api(guest.blueprints, '/_allocate_bandwidth/<uuids>/<bandwidth>/<bandwidth_unit>',
              api_func='guest.r_allocate_bandwidth', methods=['PUT'])
+
+add_rule_api(guest.blueprints, '/_adjust_ability/<uuids>/<cpu>/<memory>', api_func='guest.r_adjust_ability',
+             methods=['PUT'])
+
 add_rule_api(guest.blueprints, '/_distribute_count', api_func='guest.r_distribute_count', methods=['GET'])
 
 # Disk操作

+ 8 - 0
models/event_processor.py

@@ -241,6 +241,14 @@ class EventProcessor(object):
                     cls.guest.bandwidth = cls.message['message']['passback_parameters']['bandwidth']
                     cls.guest.update()
 
+            elif action == 'adjust_ability':
+                if state == ResponseState.success.value:
+                    cls.guest.uuid = uuid
+                    cls.guest.get_by('uuid')
+                    cls.guest.cpu = cls.message['message']['passback_parameters']['cpu']
+                    cls.guest.memory = cls.message['message']['passback_parameters']['memory']
+                    cls.guest.update()
+
         elif _object == 'disk':
             if action == 'create':
                 cls.disk.uuid = uuid

+ 4 - 0
state_code.py

@@ -61,6 +61,10 @@ own_state_branch = {
         'code': '41260',
         'zh-cn': u'Guest 与 磁盘不在同一宿主机上'
     },
+    '41261': {
+        'code': '41261',
+        'zh-cn': u'Guest 状态不符合预期,期望 Guest 为关闭状态'
+    },
     '50050': {
         'code': '50050',
         'zh-cn': u'MySQL 链接或执行出错'