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 requests
from ansible.errors import AnsibleError as ae
from ansible.plugins.action import ActionBase
import requests
try:
from __main__ import display
except ImportError:
......@@ -131,6 +132,12 @@ class ActionModule(ActionBase):
return self.devices[block]
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):
self.load_ips()
for block in self.ips:
......@@ -194,12 +201,15 @@ class ActionModule(ActionBase):
def move_ip(self):
display.vv("Moving IP ...")
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.")
targethost = self.find_host(target)
if not targethost:
raise ae("Target host %s unknown in this JiffyBox account." % target)
display.vv("- target host: %s" % target)
if target:
targethost = self.find_host(target)
if not targethost:
raise ae("Target host %s unknown in this JiffyBox account." % target)
else:
targethost = False
ips = self.find_floating_ip()
for ip in ips:
......@@ -213,8 +223,14 @@ class ActionModule(ActionBase):
else:
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')
else:
display.vv("- target host: %s" % targethost.get('name'))
path = 'ips/' + str(sourcehost.get('id')) + '/' + str(ip.get('id')) + '/move'
self._request(path, {'targetid': targethost.get('id')})
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment