Return untouhced servers with exact_count

This commit is contained in:
Matt Martz 2014-03-31 19:38:25 -05:00
parent 3d8997d219
commit 9178ed1dbd

View file

@ -200,7 +200,7 @@ def rax_slugify(value):
return 'rax_%s' % (re.sub('[^\w-]', '_', value).lower().lstrip('_')) return 'rax_%s' % (re.sub('[^\w-]', '_', value).lower().lstrip('_'))
def pyrax_object_to_dict(obj): def server_to_dict(obj):
instance = {} instance = {}
for key in dir(obj): for key in dir(obj):
value = getattr(obj, key) value = getattr(obj, key)
@ -216,7 +216,7 @@ def pyrax_object_to_dict(obj):
def create(module, names, flavor, image, meta, key_name, files, def create(module, names, flavor, image, meta, key_name, files,
wait, wait_timeout, disk_config, group, nics, wait, wait_timeout, disk_config, group, nics,
extra_create_args): extra_create_args, existing=[]):
cs = pyrax.cloudservers cs = pyrax.cloudservers
changed = False changed = False
@ -266,7 +266,7 @@ def create(module, names, flavor, image, meta, key_name, files,
server.get() server.get()
except: except:
server.status == 'ERROR' server.status == 'ERROR'
instance = pyrax_object_to_dict(server) instance = server_to_dict(server)
if server.status == 'ACTIVE' or not wait: if server.status == 'ACTIVE' or not wait:
success.append(instance) success.append(instance)
elif server.status == 'ERROR': elif server.status == 'ERROR':
@ -274,15 +274,18 @@ def create(module, names, flavor, image, meta, key_name, files,
elif wait: elif wait:
timeout.append(instance) timeout.append(instance)
untouched = [server_to_dict(s) for s in existing]
instances = success + untouched
results = { results = {
'changed': changed, 'changed': changed,
'action': 'create', 'action': 'create',
'instances': success + error + timeout, 'instances': instances,
'success': success, 'success': success,
'error': error, 'error': error,
'timeout': timeout, 'timeout': timeout,
'instance_ids': { 'instance_ids': {
'instances': [i['id'] for i in success + error + timeout], 'instances': [i['id'] for i in instances],
'success': [i['id'] for i in success], 'success': [i['id'] for i in success],
'error': [i['id'] for i in error], 'error': [i['id'] for i in error],
'timeout': [i['id'] for i in timeout] 'timeout': [i['id'] for i in timeout]
@ -300,7 +303,7 @@ def create(module, names, flavor, image, meta, key_name, files,
module.exit_json(**results) module.exit_json(**results)
def delete(module, instance_ids, wait, wait_timeout): def delete(module, instance_ids, wait, wait_timeout, kept=[]):
cs = pyrax.cloudservers cs = pyrax.cloudservers
changed = False changed = False
@ -318,7 +321,7 @@ def delete(module, instance_ids, wait, wait_timeout):
else: else:
changed = True changed = True
instance = pyrax_object_to_dict(server) instance = server_to_dict(server)
instances[instance['id']] = instance instances[instance['id']] = instance
# If requested, wait for server deletion # If requested, wait for server deletion
@ -332,6 +335,7 @@ def delete(module, instance_ids, wait, wait_timeout):
server.get() server.get()
except: except:
instances[instance_id]['status'] = 'DELETED' instances[instance_id]['status'] = 'DELETED'
instances[instance_id]['rax_status'] = 'DELETED'
if not filter(lambda s: s['status'] not in ('', 'DELETED', if not filter(lambda s: s['status'] not in ('', 'DELETED',
'ERROR'), 'ERROR'),
@ -347,15 +351,17 @@ def delete(module, instance_ids, wait, wait_timeout):
success = filter(lambda s: s['status'] in ('', 'DELETED'), success = filter(lambda s: s['status'] in ('', 'DELETED'),
instances.values()) instances.values())
instances = [server_to_dict(s) for s in kept]
results = { results = {
'changed': changed, 'changed': changed,
'action': 'delete', 'action': 'delete',
'instances': success + error + timeout, 'instances': instances,
'success': success, 'success': success,
'error': error, 'error': error,
'timeout': timeout, 'timeout': timeout,
'instance_ids': { 'instance_ids': {
'instances': [i['id'] for i in success + error + timeout], 'instances': [i['id'] for i in instances],
'success': [i['id'] for i in success], 'success': [i['id'] for i in success],
'error': [i['id'] for i in error], 'error': [i['id'] for i in error],
'timeout': [i['id'] for i in timeout] 'timeout': [i['id'] for i in timeout]
@ -501,11 +507,13 @@ def cloudservers(module, state, name, flavor, image, meta, key_name, files,
if len(servers) > count: if len(servers) > count:
state = 'absent' state = 'absent'
kept = servers[:count]
del servers[:count] del servers[:count]
instance_ids = [] instance_ids = []
for server in servers: for server in servers:
instance_ids.append(server.id) instance_ids.append(server.id)
delete(module, instance_ids, wait, wait_timeout) delete(module, instance_ids, wait, wait_timeout,
kept=kept)
elif len(servers) < count: elif len(servers) < count:
if auto_increment: if auto_increment:
names = [] names = []
@ -516,9 +524,15 @@ def cloudservers(module, state, name, flavor, image, meta, key_name, files,
else: else:
names = [name] * (count - len(servers)) names = [name] * (count - len(servers))
else: else:
module.exit_json(changed=False, action=None, instances=[], instances = []
instance_ids = []
for server in servers:
instances.append(server_to_dict(server))
instance_ids.append(server.id)
module.exit_json(changed=False, action=None,
instances=instances,
success=[], error=[], timeout=[], success=[], error=[], timeout=[],
instance_ids={'instances': [], instance_ids={'instances': instance_ids,
'success': [], 'error': [], 'success': [], 'error': [],
'timeout': []}) 'timeout': []})
else: else:
@ -568,7 +582,7 @@ def cloudservers(module, state, name, flavor, image, meta, key_name, files,
if len(servers) >= count: if len(servers) >= count:
instances = [] instances = []
for server in servers: for server in servers:
instances.append(pyrax_object_to_dict(server)) instances.append(server_to_dict(server))
instance_ids = [i['id'] for i in instances] instance_ids = [i['id'] for i in instances]
module.exit_json(changed=False, action=None, module.exit_json(changed=False, action=None,
@ -581,7 +595,8 @@ def cloudservers(module, state, name, flavor, image, meta, key_name, files,
names = [name] * (count - len(servers)) names = [name] * (count - len(servers))
create(module, names, flavor, image, meta, key_name, files, create(module, names, flavor, image, meta, key_name, files,
wait, wait_timeout, disk_config, group, nics, extra_create_args) wait, wait_timeout, disk_config, group, nics, extra_create_args,
existing=servers)
elif state == 'absent': elif state == 'absent':
if instance_ids is None: if instance_ids is None: