Skip to content
Snippets Groups Projects
Commit dda36d95 authored by jurgenhaas's avatar jurgenhaas
Browse files

New option "group" which can be used instead of "target" to move to any host within the same group

parent 2637c4e9
No related branches found
No related tags found
No related merge requests found
import json import json
import requests
from ansible.errors import AnsibleError as ae from ansible.errors import AnsibleError as ae
from ansible.plugins.action import ActionBase from ansible.plugins.action import ActionBase
import requests
try: try:
from __main__ import display from __main__ import display
except ImportError: except ImportError:
...@@ -131,6 +132,12 @@ class ActionModule(ActionBase): ...@@ -131,6 +132,12 @@ class ActionModule(ActionBase):
return self.devices[block] return self.devices[block]
return False return False
def find_host_in_group(self, group, excluded):
for name in group:
if name != excluded:
return self.find_host(name)
return False
def find_ip(self, address): def find_ip(self, address):
self.load_ips() self.load_ips()
for block in self.ips: for block in self.ips:
...@@ -194,12 +201,15 @@ class ActionModule(ActionBase): ...@@ -194,12 +201,15 @@ class ActionModule(ActionBase):
def move_ip(self): def move_ip(self):
display.vv("Moving IP ...") display.vv("Moving IP ...")
target = self._task.args.get('target', False) target = self._task.args.get('target', False)
if not target: group = self._task.args.get('group', False)
if not target and not group:
raise ae("'target' is a required argument.") raise ae("'target' is a required argument.")
targethost = self.find_host(target) if target:
if not targethost: targethost = self.find_host(target)
raise ae("Target host %s unknown in this JiffyBox account." % target) if not targethost:
display.vv("- target host: %s" % target) raise ae("Target host %s unknown in this JiffyBox account." % target)
else:
targethost = False
ips = self.find_floating_ip() ips = self.find_floating_ip()
for ip in ips: for ip in ips:
...@@ -213,8 +223,14 @@ class ActionModule(ActionBase): ...@@ -213,8 +223,14 @@ class ActionModule(ActionBase):
else: else:
display.vv("- source host: %s" % sourcehost.get('name')) display.vv("- source host: %s" % sourcehost.get('name'))
if sourcehost.get('id') == targethost.get('id'): if not target:
targethost = self.find_host_in_group(group, sourcehost.get('name'))
if not targethost:
pass
elif sourcehost.get('id') == targethost.get('id'):
display.vv('- cancelled as source and target are the same') display.vv('- cancelled as source and target are the same')
else: else:
display.vv("- target host: %s" % targethost.get('name'))
path = 'ips/' + str(sourcehost.get('id')) + '/' + str(ip.get('id')) + '/move' path = 'ips/' + str(sourcehost.get('id')) + '/' + str(ip.get('id')) + '/move'
self._request(path, {'targetid': targethost.get('id')}) self._request(path, {'targetid': targethost.get('id')})
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment