Minor refactoring to avoid code duplication
This commit is contained in:
parent
b94401247f
commit
4b1b0f8514
@ -141,37 +141,33 @@ class InstanceBackend(object):
|
|||||||
|
|
||||||
def start_instances(self, instance_ids):
|
def start_instances(self, instance_ids):
|
||||||
started_instances = []
|
started_instances = []
|
||||||
for instance in self.all_instances():
|
for instance in self.get_multi_instances_by_id(instance_ids):
|
||||||
if instance.id in instance_ids:
|
instance.start()
|
||||||
instance.start()
|
started_instances.append(instance)
|
||||||
started_instances.append(instance)
|
|
||||||
|
|
||||||
return started_instances
|
return started_instances
|
||||||
|
|
||||||
def stop_instances(self, instance_ids):
|
def stop_instances(self, instance_ids):
|
||||||
stopped_instances = []
|
stopped_instances = []
|
||||||
for instance in self.all_instances():
|
for instance in self.get_multi_instances_by_id(instance_ids):
|
||||||
if instance.id in instance_ids:
|
instance.stop()
|
||||||
instance.stop()
|
stopped_instances.append(instance)
|
||||||
stopped_instances.append(instance)
|
|
||||||
|
|
||||||
return stopped_instances
|
return stopped_instances
|
||||||
|
|
||||||
def terminate_instances(self, instance_ids):
|
def terminate_instances(self, instance_ids):
|
||||||
terminated_instances = []
|
terminated_instances = []
|
||||||
for instance in self.all_instances():
|
for instance in self.get_multi_instances_by_id(instance_ids):
|
||||||
if instance.id in instance_ids:
|
instance.terminate()
|
||||||
instance.terminate()
|
terminated_instances.append(instance)
|
||||||
terminated_instances.append(instance)
|
|
||||||
|
|
||||||
return terminated_instances
|
return terminated_instances
|
||||||
|
|
||||||
def reboot_instances(self, instance_ids):
|
def reboot_instances(self, instance_ids):
|
||||||
rebooted_instances = []
|
rebooted_instances = []
|
||||||
for instance in self.all_instances():
|
for instance in self.get_multi_instances_by_id(instance_ids):
|
||||||
if instance.id in instance_ids:
|
instance.reboot()
|
||||||
instance.reboot()
|
rebooted_instances.append(instance)
|
||||||
rebooted_instances.append(instance)
|
|
||||||
|
|
||||||
return rebooted_instances
|
return rebooted_instances
|
||||||
|
|
||||||
@ -192,6 +188,20 @@ class InstanceBackend(object):
|
|||||||
instances.append(instance)
|
instances.append(instance)
|
||||||
return instances
|
return instances
|
||||||
|
|
||||||
|
def get_multi_instances_by_id(self, instance_ids):
|
||||||
|
"""
|
||||||
|
:param instance_ids: A string list with instance ids
|
||||||
|
:return: A list with instance objects
|
||||||
|
"""
|
||||||
|
result = []
|
||||||
|
|
||||||
|
for reservation in self.all_reservations():
|
||||||
|
for instance in reservation.instances:
|
||||||
|
if instance.id in instance_ids:
|
||||||
|
result.append(instance)
|
||||||
|
|
||||||
|
return result
|
||||||
|
|
||||||
def get_instance_by_id(self, instance_id):
|
def get_instance_by_id(self, instance_id):
|
||||||
for reservation in self.all_reservations():
|
for reservation in self.all_reservations():
|
||||||
for instance in reservation.instances:
|
for instance in reservation.instances:
|
||||||
|
Loading…
Reference in New Issue
Block a user