From f8c3a83cd5999a6a546f4775b2ffb0d1cb88225c Mon Sep 17 00:00:00 2001 From: Matt Martz Date: Fri, 8 Nov 2013 13:11:15 -0600 Subject: [PATCH] DRY up dict conversion and fix serialization error --- cloud/rax_clb | 54 +++++++++++++++++++++++++-------------------------- 1 file changed, 26 insertions(+), 28 deletions(-) diff --git a/cloud/rax_clb b/cloud/rax_clb index 76564db59cb..d826d49bdee 100644 --- a/cloud/rax_clb +++ b/cloud/rax_clb @@ -136,6 +136,30 @@ PROTOCOLS = ['DNS_TCP', 'DNS_UDP', 'FTP', 'HTTP', 'HTTPS', 'IMAPS', 'IMAPv4', 'TCP_CLIENT_FIRST', 'UDP', 'UDP_STREAM', 'SFTP'] +def to_dict(obj): + instance = {} + for key in dir(obj): + value = getattr(obj, key) + if key == 'virtual_ips': + virtual_ips = [] + instance[key] = [] + for vip in value: + vip_dict = {} + for vip_key, vip_value in vars(vip).iteritems(): + if isinstance(vip_value, NON_CALLABLES): + vip_dict[vip_key] = vip_value + instance[key].append(vip_dict) + elif key == 'nodes': + instance[key] = [] + for node in value: + instance[key].append(node.to_dict()) + elif (isinstance(value, NON_CALLABLES) and + not key.startswith('_')): + instance[key] = value + + return instance + + def cloud_load_balancer(module, state, name, meta, algorithm, port, protocol, vip_type, timeout, wait, wait_timeout): for arg in (state, name, port, protocol, vip_type): @@ -210,20 +234,7 @@ def cloud_load_balancer(module, state, name, meta, algorithm, port, protocol, pyrax.utils.wait_for_build(balancer, interval=5, attempts=attempts) balancer.get() - instance = {} - for key, value in vars(balancer).iteritems(): - if key == 'virtual_ips': - virtual_ips = [] - instance[key] = [] - for vip in value: - vip_dict = {} - for vip_key, vip_value in vars(vip).iteritems(): - if isinstance(vip_value, NON_CALLABLES): - vip_dict[vip_key] = vip_value - instance[key].append(vip_dict) - elif (isinstance(value, NON_CALLABLES) and - not key.startswith('_')): - instance[key] = value + instance = to_dict(balancer) result = dict(changed=changed, balancer=instance) @@ -246,20 +257,7 @@ def cloud_load_balancer(module, state, name, meta, algorithm, port, protocol, except Exception, e: module.fail_json(msg='%s' % e.message) - instance = {} - for key, value in vars(balancer).iteritems(): - if key == 'virtual_ips': - virtual_ips = [] - instance[key] = [] - for vip in value: - vip_dict = {} - for vip_key, vip_value in vars(vip).iteritems(): - if isinstance(vip_value, NON_CALLABLES): - vip_dict[vip_key] = vip_value - instance[key].append(vip_dict) - elif (isinstance(value, NON_CALLABLES) and - not key.startswith('_')): - instance[key] = value + instance = to_dict(balancer) if wait: attempts = wait_timeout / 5