|
|
|
@ -27,23 +27,17 @@
|
|
|
|
|
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
|
|
|
|
|
# USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
import json
|
|
|
|
|
except ImportError:
|
|
|
|
|
try:
|
|
|
|
|
import simplejson as json
|
|
|
|
|
except ImportError:
|
|
|
|
|
# Let snippet from module_utils/basic.py return a proper error in this case
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
import os
|
|
|
|
|
|
|
|
|
|
# import module snippets
|
|
|
|
|
from ansible.module_utils.pycompat24 import get_exception
|
|
|
|
|
from ansible.module_utils.six.moves import configparser
|
|
|
|
|
from ansible.module_utils.six import integer_types, string_types
|
|
|
|
|
from ansible.module_utils._text import to_text
|
|
|
|
|
from ansible.module_utils.urls import fetch_url
|
|
|
|
|
|
|
|
|
|
EXO_DNS_BASEURL="https://api.exoscale.ch/dns/v1"
|
|
|
|
|
EXO_DNS_BASEURL = "https://api.exoscale.ch/dns/v1"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def exo_dns_argument_spec():
|
|
|
|
|
return dict(
|
|
|
|
@ -54,6 +48,7 @@ def exo_dns_argument_spec():
|
|
|
|
|
validate_certs=dict(default='yes', type='bool'),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def exo_dns_required_together():
|
|
|
|
|
return [['api_key', 'api_secret']]
|
|
|
|
|
|
|
|
|
@ -73,7 +68,7 @@ class ExoDns(object):
|
|
|
|
|
self.api_secret = config['secret']
|
|
|
|
|
except Exception:
|
|
|
|
|
e = get_exception()
|
|
|
|
|
self.module.fail_json(msg=str(e))
|
|
|
|
|
self.module.fail_json(msg="Error while processing config: %s" % e)
|
|
|
|
|
|
|
|
|
|
self.headers = {
|
|
|
|
|
'X-DNS-Token': "%s:%s" % (self.api_key, self.api_secret),
|
|
|
|
@ -95,10 +90,10 @@ class ExoDns(object):
|
|
|
|
|
keys = ['key', 'secret']
|
|
|
|
|
env_conf = {}
|
|
|
|
|
for key in keys:
|
|
|
|
|
if 'CLOUDSTACK_{0}'.format(key.upper()) not in os.environ:
|
|
|
|
|
if 'CLOUDSTACK_%s' % key.upper() not in os.environ:
|
|
|
|
|
break
|
|
|
|
|
else:
|
|
|
|
|
env_conf[key] = os.environ['CLOUDSTACK_{0}'.format(key.upper())]
|
|
|
|
|
env_conf[key] = os.environ['CLOUDSTACK_%s' % key.upper()]
|
|
|
|
|
else:
|
|
|
|
|
return env_conf
|
|
|
|
|
|
|
|
|
@ -112,8 +107,7 @@ class ExoDns(object):
|
|
|
|
|
if 'CLOUDSTACK_CONFIG' in os.environ:
|
|
|
|
|
paths += (os.path.expanduser(os.environ['CLOUDSTACK_CONFIG']),)
|
|
|
|
|
if not any([os.path.exists(c) for c in paths]):
|
|
|
|
|
raise SystemExit("Config file not found. Tried {0}".format(
|
|
|
|
|
", ".join(paths)))
|
|
|
|
|
self.module.fail_json(msg="Config file not found. Tried : %s" % ", ".join(paths))
|
|
|
|
|
|
|
|
|
|
conf = configparser.ConfigParser()
|
|
|
|
|
conf.read(paths)
|
|
|
|
@ -122,24 +116,26 @@ class ExoDns(object):
|
|
|
|
|
def api_query(self, resource="/domains", method="GET", data=None):
|
|
|
|
|
url = EXO_DNS_BASEURL + resource
|
|
|
|
|
if data:
|
|
|
|
|
data = json.dumps(data)
|
|
|
|
|
data = self.module.jsonify(data)
|
|
|
|
|
|
|
|
|
|
response, info = fetch_url(
|
|
|
|
|
module = self.module,
|
|
|
|
|
url = url,
|
|
|
|
|
data = data,
|
|
|
|
|
method = method,
|
|
|
|
|
headers = self.headers,
|
|
|
|
|
timeout = self.module.params.get('api_timeout'),
|
|
|
|
|
module=self.module,
|
|
|
|
|
url=url,
|
|
|
|
|
data=data,
|
|
|
|
|
method=method,
|
|
|
|
|
headers=self.headers,
|
|
|
|
|
timeout=self.module.params.get('api_timeout'),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if info['status'] not in (200, 201, 204):
|
|
|
|
|
self.module.fail_json(msg="%s returned %s, with body: %s" % (url, info['status'], info['msg']))
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
return json.load(response)
|
|
|
|
|
return self.module.from_json(to_text(response.read()))
|
|
|
|
|
|
|
|
|
|
except Exception:
|
|
|
|
|
return {}
|
|
|
|
|
e = get_exception()
|
|
|
|
|
self.module.fail_json(msg="Could not process response into json: %s" % e)
|
|
|
|
|
|
|
|
|
|
def has_changed(self, want_dict, current_dict, only_keys=None):
|
|
|
|
|
changed = False
|
|
|
|
@ -151,17 +147,19 @@ class ExoDns(object):
|
|
|
|
|
if value is None:
|
|
|
|
|
continue
|
|
|
|
|
if key in current_dict:
|
|
|
|
|
if isinstance(current_dict[key], (int, long, float, complex)):
|
|
|
|
|
if isinstance(current_dict[key], integer_types):
|
|
|
|
|
if value != current_dict[key]:
|
|
|
|
|
self.result['diff']['before'][key] = current_dict[key]
|
|
|
|
|
self.result['diff']['after'][key] = value
|
|
|
|
|
changed = True
|
|
|
|
|
elif value.lower() != current_dict[key].encode('utf-8').lower():
|
|
|
|
|
self.result['diff']['before'][key] = current_dict[key]
|
|
|
|
|
self.result['diff']['after'][key] = value
|
|
|
|
|
changed = True
|
|
|
|
|
elif isinstance(current_dict[key], string_types):
|
|
|
|
|
if value.lower() != current_dict[key].lower():
|
|
|
|
|
self.result['diff']['before'][key] = current_dict[key]
|
|
|
|
|
self.result['diff']['after'][key] = value
|
|
|
|
|
changed = True
|
|
|
|
|
else:
|
|
|
|
|
self.module.fail_json(msg="Unable to determine comparison for key %s" % key)
|
|
|
|
|
else:
|
|
|
|
|
self.result['diff']['after'][key] = value
|
|
|
|
|
changed = True
|
|
|
|
|
return changed
|
|
|
|
|
|
|
|
|
|