Dedupe PTR record related code in rax_dns_record

This commit is contained in:
Matt Martz 2014-08-06 16:35:06 -05:00
parent 28fcdec2db
commit 7d0eba19ab
4 changed files with 33 additions and 31 deletions

View file

@ -33,7 +33,6 @@ REPLACER_COMPLEX = "\"<<INCLUDE_ANSIBLE_MODULE_COMPLEX_ARGS>>\""
REPLACER_WINDOWS = "# POWERSHELL_COMMON"
REPLACER_VERSION = "\"<<ANSIBLE_VERSION>>\""
class ModuleReplacer(object):
"""

View file

@ -170,6 +170,29 @@ def rax_find_server(module, rax_module, server):
return server
def rax_find_loadbalancer(module, rax_module, loadbalancer):
clb = rax_module.cloud_loadbalancers
try:
UUID(loadbalancer)
found = clb.get(loadbalancer)
except:
for lb in clb.list():
if loadbalancer == lb.name:
found.append(lb)
if not found:
module.fail_json(msg='No loadbalancer was matched')
if len(found) > 1:
module.fail_json(msg='Multiple loadbalancers matched')
# We made it this far, grab the first and hopefully only item
# in the list
found = found[0]
return found
def rax_argument_spec():
return dict(
api_key=dict(type='str', aliases=['password'], no_log=True),

View file

@ -219,7 +219,7 @@ def create(module, names=[], flavor=None, image=None, meta={}, key_name=None,
except Exception, e:
module.fail_json(msg='Failed to load %s' % user_data)
# Handle the file contents
# Handle the file contents
for rpath in files.keys():
lpath = os.path.expanduser(files[rpath])
try:
@ -707,5 +707,5 @@ def main():
from ansible.module_utils.basic import *
from ansible.module_utils.rax import *
### invoke the module
# invoke the module
main()

View file

@ -120,43 +120,23 @@ except ImportError:
HAS_PYRAX = False
def rax_dns_record(module, data=None, comment=None, loadbalancer=None,
name=None, server=None, state='present', ttl=7200):
def rax_dns_record_ptr(module, data=None, comment=None, loadbalancer=None,
name=None, server=None, state='present', ttl=7200):
changed = False
results = []
cs = pyrax.cloudservers
clb = pyrax.cloud_loadbalancers
dns = pyrax.cloud_dns
if not cs or not clb or not dns:
if not dns:
module.fail_json(msg='Failed to instantiate client. This '
'typically indicates an invalid region or an '
'incorrectly capitalized region name.')
if loadbalancer:
try:
UUID(loadbalancer)
found = [clb.get(loadbalancer)]
except:
for lb in clb.list():
if loadbalancer == lb.name:
found.append(lb)
if len(found) != 1:
module.fail_json(msg='Could not match a loadbalancer with %s' %
loadbalancer)
item = rax_find_loadbalancer(module, pyrax, loadbalancer)
elif server:
try:
UUID(server)
found = [cs.servers.get(server)]
except:
found = cs.servers.list(search_opts=dict(name='^%s$' % server))
if len(found) != 1:
module.fail_json(msg='Could not match a server with %s' %
server)
item = rax_find_server(module, pyrax, server)
item = found[0]
if state == 'present':
current = dns.list_ptr_records(item)
for record in current:
@ -169,10 +149,10 @@ def rax_dns_record(module, data=None, comment=None, loadbalancer=None,
module.fail_json(msg='%s' % e.message)
record.ttl = ttl
record.name = name
results.append(to_dict(record))
results.append(rax_to_dict(record))
break
else:
results.append(to_dict(record))
results.append(rax_to_dict(record))
break
if not results:
@ -190,7 +170,7 @@ def rax_dns_record(module, data=None, comment=None, loadbalancer=None,
current = dns.list_ptr_records(item)
for record in current:
if record.data == data:
results.append(to_dict(record))
results.append(rax_to_dict(record))
break
if results: