generated from ansible-collections/collection_template
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Move generic open_url code for plugins to module_utils. * Move failover code from module_utils/robot to module_utils/failover. * Add boilerplate. * Add tests for plugin_open_url_json. * Make it work with all Python versions. * Use community.internal_test_tools from ansible-collections/community.internal_test_tools#24 * Fix error instantiation. * Add more complete test. * The branch has been merged, but the collection hasn't been released yet. * Forgot to remove path to my repo.
- Loading branch information
1 parent
f737104
commit 954f033
Showing
9 changed files
with
485 additions
and
256 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
# Copyright (c), Felix Fontein <felix@fontein.de>, 2019 | ||
# Simplified BSD License (see licenses/simplified_bsd.txt or https://opensource.org/licenses/BSD-2-Clause) | ||
|
||
from __future__ import absolute_import, division, print_function | ||
__metaclass__ = type | ||
|
||
|
||
import json | ||
|
||
from ansible.module_utils.six.moves.urllib.parse import urlencode | ||
|
||
from ansible_collections.community.hrobot.plugins.module_utils.robot import ( | ||
BASE_URL, | ||
fetch_url_json, | ||
) | ||
|
||
|
||
def get_failover_record(module, ip): | ||
''' | ||
Get information record of failover IP. | ||
See https://robot.your-server.de/doc/webservice/en.html#get-failover-failover-ip | ||
''' | ||
url = "{0}/failover/{1}".format(BASE_URL, ip) | ||
result, error = fetch_url_json(module, url) | ||
if 'failover' not in result: | ||
module.fail_json(msg='Cannot interpret result: {0}'.format(json.dumps(result, sort_keys=True))) | ||
return result['failover'] | ||
|
||
|
||
def get_failover(module, ip): | ||
''' | ||
Get current routing target of failover IP. | ||
The value ``None`` represents unrouted. | ||
See https://robot.your-server.de/doc/webservice/en.html#get-failover-failover-ip | ||
''' | ||
return get_failover_record(module, ip)['active_server_ip'] | ||
|
||
|
||
def set_failover(module, ip, value, timeout=180): | ||
''' | ||
Set current routing target of failover IP. | ||
Return a pair ``(value, changed)``. The value ``None`` for ``value`` represents unrouted. | ||
See https://robot.your-server.de/doc/webservice/en.html#post-failover-failover-ip | ||
and https://robot.your-server.de/doc/webservice/en.html#delete-failover-failover-ip | ||
''' | ||
url = "{0}/failover/{1}".format(BASE_URL, ip) | ||
if value is None: | ||
result, error = fetch_url_json( | ||
module, | ||
url, | ||
method='DELETE', | ||
timeout=timeout, | ||
accept_errors=['FAILOVER_ALREADY_ROUTED'] | ||
) | ||
else: | ||
headers = {"Content-type": "application/x-www-form-urlencoded"} | ||
data = dict( | ||
active_server_ip=value, | ||
) | ||
result, error = fetch_url_json( | ||
module, | ||
url, | ||
method='POST', | ||
timeout=timeout, | ||
data=urlencode(data), | ||
headers=headers, | ||
accept_errors=['FAILOVER_ALREADY_ROUTED'] | ||
) | ||
if error is not None: | ||
return value, False | ||
else: | ||
return result['failover']['active_server_ip'], True | ||
|
||
|
||
def get_failover_state(value): | ||
''' | ||
Create result dictionary for failover IP's value. | ||
The value ``None`` represents unrouted. | ||
''' | ||
return dict( | ||
value=value, | ||
state='routed' if value else 'unrouted' | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.