2013-06-19 22:01:47 +00:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""
|
|
|
|
Ansible module to add boundary meters.
|
|
|
|
|
|
|
|
(c) 2013, curtis <curtis@serverascode.com>
|
|
|
|
|
|
|
|
This file is part of Ansible
|
|
|
|
|
|
|
|
Ansible is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Ansible is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
"""
|
|
|
|
|
|
|
|
import json
|
|
|
|
import datetime
|
|
|
|
import urllib2
|
|
|
|
import base64
|
2013-06-24 22:20:36 +00:00
|
|
|
import os
|
2013-06-19 22:01:47 +00:00
|
|
|
|
|
|
|
DOCUMENTATION = '''
|
|
|
|
|
|
|
|
module: boundary_meter
|
|
|
|
short_description: Manage boundary meters
|
|
|
|
description:
|
|
|
|
- This module manages boundary meters
|
|
|
|
version_added: "1.3"
|
|
|
|
author: curtis@serverascode.com
|
|
|
|
requirements:
|
|
|
|
- Boundary API access
|
2013-06-24 22:20:36 +00:00
|
|
|
- bprobe is required to send data, but not to register a meter
|
|
|
|
- Python urllib2
|
2013-06-19 22:01:47 +00:00
|
|
|
options:
|
|
|
|
name:
|
|
|
|
description:
|
|
|
|
- meter name
|
|
|
|
required: true
|
|
|
|
state:
|
|
|
|
description:
|
|
|
|
- Whether to create or remove the client from boundary
|
|
|
|
required: false
|
|
|
|
default: true
|
2013-06-24 22:20:36 +00:00
|
|
|
choices: ["present", "absent"]
|
2013-06-19 22:01:47 +00:00
|
|
|
apiid:
|
|
|
|
description:
|
|
|
|
- Organizations boundary API ID
|
|
|
|
required: true
|
|
|
|
apikey:
|
|
|
|
description:
|
|
|
|
- Organizations boundary API KEY
|
|
|
|
required: true
|
|
|
|
|
|
|
|
notes:
|
2013-07-23 17:16:30 +00:00
|
|
|
- This module does not yet support boundary tags.
|
2013-06-19 22:01:47 +00:00
|
|
|
|
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES='''
|
|
|
|
- name: Create meter
|
2013-07-23 17:16:30 +00:00
|
|
|
boundary_meter: apiid=AAAAAA api_key=BBBBBB state=present name={{ inventory_hostname }}"
|
2013-06-19 22:01:47 +00:00
|
|
|
|
|
|
|
- name: Delete meter
|
2013-07-23 17:16:30 +00:00
|
|
|
boundary_meter: apiid=AAAAAA api_key=BBBBBB state=absent name={{ inventory_hostname }}"
|
2013-06-19 22:01:47 +00:00
|
|
|
|
|
|
|
'''
|
|
|
|
|
|
|
|
try:
|
|
|
|
import urllib2
|
|
|
|
HAS_URLLIB2 = True
|
|
|
|
except ImportError:
|
|
|
|
HAS_URLLIB2 = False
|
|
|
|
|
2013-06-24 22:20:36 +00:00
|
|
|
api_host = "api.boundary.com"
|
|
|
|
config_directory = "/etc/bprobe"
|
2013-06-19 22:01:47 +00:00
|
|
|
|
2013-06-24 22:20:36 +00:00
|
|
|
# "resource" like thing or apikey?
|
|
|
|
def auth_encode(apikey):
|
|
|
|
auth = base64.standard_b64encode(apikey)
|
2013-06-19 22:01:47 +00:00
|
|
|
auth.replace("\n", "")
|
|
|
|
return auth
|
|
|
|
|
2013-06-24 22:20:36 +00:00
|
|
|
def build_url(name, apiid, action, meter_id=None, cert_type=None):
|
2013-06-19 22:01:47 +00:00
|
|
|
if action == "create":
|
2013-07-02 15:41:11 +00:00
|
|
|
return 'https://%s/%s/meters' % (api_host, apiid)
|
2013-06-19 22:01:47 +00:00
|
|
|
elif action == "search":
|
2013-07-02 15:41:11 +00:00
|
|
|
return "https://%s/%s/meters?name=%s" % (api_host, apiid, name)
|
2013-06-19 22:01:47 +00:00
|
|
|
elif action == "certificates":
|
2013-07-02 15:41:11 +00:00
|
|
|
return "https://%s/%s/meters/%s/%s.pem" % (api_host, apiid, meter_id, cert_type)
|
2013-06-19 22:01:47 +00:00
|
|
|
elif action == "tags":
|
2013-07-02 15:41:11 +00:00
|
|
|
return "https://%s/%s/meters/%s/tags" % (api_host, apiid, meter_id)
|
2013-06-19 22:01:47 +00:00
|
|
|
elif action == "delete":
|
2013-07-02 15:41:11 +00:00
|
|
|
return "https://%s/%s/meters/%s" % (api_host, apiid, meter_id)
|
2013-06-19 22:01:47 +00:00
|
|
|
|
2013-06-24 22:20:36 +00:00
|
|
|
def http_request(name, apiid, apikey, action, meter_id=None, cert_type=None):
|
2013-06-19 22:01:47 +00:00
|
|
|
|
2013-06-24 22:20:36 +00:00
|
|
|
if meter_id is None:
|
|
|
|
url = build_url(name, apiid, action)
|
2013-06-19 22:01:47 +00:00
|
|
|
else:
|
2013-06-24 22:20:36 +00:00
|
|
|
if cert_type is None:
|
|
|
|
url = build_url(name, apiid, action, meter_id)
|
2013-06-19 22:01:47 +00:00
|
|
|
else:
|
2013-06-24 22:20:36 +00:00
|
|
|
url = build_url(name, apiid, action, meter_id, cert_type)
|
2013-06-19 22:01:47 +00:00
|
|
|
|
2013-06-24 22:20:36 +00:00
|
|
|
auth = auth_encode(apikey)
|
2013-06-19 22:01:47 +00:00
|
|
|
request = urllib2.Request(url)
|
2013-07-02 15:41:11 +00:00
|
|
|
request.add_header("Authorization", "Basic %s" % (auth))
|
2013-06-19 22:01:47 +00:00
|
|
|
request.add_header("Content-Type", "application/json")
|
|
|
|
return request
|
|
|
|
|
2013-06-24 22:20:36 +00:00
|
|
|
def create_meter(module, name, apiid, apikey):
|
2013-06-19 22:01:47 +00:00
|
|
|
|
2013-06-24 22:20:36 +00:00
|
|
|
meters = search_meter(module, name, apiid, apikey)
|
2013-06-19 22:01:47 +00:00
|
|
|
|
|
|
|
if len(meters) > 0:
|
|
|
|
# If the meter already exists, do nothing
|
2013-06-24 22:20:36 +00:00
|
|
|
module.exit_json(status="Meter " + name + " already exists",changed=False)
|
2013-06-19 22:01:47 +00:00
|
|
|
else:
|
|
|
|
# If it doesn't exist, create it
|
2013-06-24 22:20:36 +00:00
|
|
|
request = http_request(name, apiid, apikey, action="create")
|
2013-06-19 22:01:47 +00:00
|
|
|
# A create request seems to need a json body with the name of the meter in it
|
2013-06-24 22:20:36 +00:00
|
|
|
body = '{"name":"' + name + '"}'
|
2013-06-19 22:01:47 +00:00
|
|
|
request.add_data(body)
|
|
|
|
|
|
|
|
try:
|
|
|
|
result = urllib2.urlopen(request)
|
|
|
|
except urllib2.URLError, e:
|
|
|
|
module.fail_json(msg="Failed to connect to api host to create meter")
|
|
|
|
|
2013-06-24 22:20:36 +00:00
|
|
|
# If the config directory doesn't exist, create it
|
|
|
|
if not os.path.exists(config_directory):
|
|
|
|
try:
|
|
|
|
os.makedirs(config_directory)
|
|
|
|
except:
|
|
|
|
module.fail_json("Could not create " + config_directory)
|
|
|
|
|
2013-06-19 22:01:47 +00:00
|
|
|
|
|
|
|
# Download both cert files from the api host
|
|
|
|
types = ['key', 'cert']
|
|
|
|
for cert_type in types:
|
|
|
|
try:
|
|
|
|
# If we can't open the file it's not there, so we should download it
|
2013-07-02 15:41:11 +00:00
|
|
|
cert_file = open('%s/%s.pem' % (config_directory,cert_type))
|
2013-06-19 22:01:47 +00:00
|
|
|
except IOError:
|
|
|
|
# Now download the file...
|
2013-06-24 22:20:36 +00:00
|
|
|
rc = download_request(module, name, apiid, apikey, cert_type)
|
2013-06-19 22:01:47 +00:00
|
|
|
if rc == False:
|
|
|
|
module.fail_json("Download request for " + cert_type + ".pem failed")
|
|
|
|
|
2013-06-24 22:20:36 +00:00
|
|
|
return 0, "Meter " + name + " created"
|
2013-06-19 22:01:47 +00:00
|
|
|
|
2013-06-24 22:20:36 +00:00
|
|
|
def search_meter(module, name, apiid, apikey):
|
2013-06-19 22:01:47 +00:00
|
|
|
|
2013-06-24 22:20:36 +00:00
|
|
|
request = http_request(name, apiid, apikey, action="search")
|
2013-06-19 22:01:47 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
result = urllib2.urlopen(request)
|
|
|
|
except urllib2.URLError, e:
|
2013-06-24 22:20:36 +00:00
|
|
|
module.fail_json("Failed to connect to api host to search for meter")
|
2013-06-19 22:01:47 +00:00
|
|
|
|
|
|
|
# Return meters
|
|
|
|
return json.loads(result.read())
|
|
|
|
|
2013-06-24 22:20:36 +00:00
|
|
|
def get_meter_id(module, name, apiid, apikey):
|
2013-06-19 22:01:47 +00:00
|
|
|
# In order to delete the meter we need its id
|
2013-06-24 22:20:36 +00:00
|
|
|
meters = search_meter(module, name, apiid, apikey)
|
2013-06-19 22:01:47 +00:00
|
|
|
|
|
|
|
if len(meters) > 0:
|
|
|
|
return meters[0]['id']
|
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
2013-06-24 22:20:36 +00:00
|
|
|
def delete_meter(module, name, apiid, apikey):
|
2013-06-19 22:01:47 +00:00
|
|
|
|
2013-06-24 22:20:36 +00:00
|
|
|
meter_id = get_meter_id(module, name, apiid, apikey)
|
2013-06-19 22:01:47 +00:00
|
|
|
|
|
|
|
if meter_id is None:
|
|
|
|
return 1, "Meter does not exist, so can't delete it"
|
|
|
|
else:
|
|
|
|
action = "delete"
|
2013-06-24 22:20:36 +00:00
|
|
|
request = http_request(name, apiid, apikey, action, meter_id)
|
2013-06-19 22:01:47 +00:00
|
|
|
# See http://stackoverflow.com/questions/4511598/how-to-make-http-delete-method-using-urllib2
|
|
|
|
# urllib2 only does GET or POST I believe, but here we need delete
|
|
|
|
request.get_method = lambda: 'DELETE'
|
|
|
|
|
|
|
|
try:
|
|
|
|
result = urllib2.urlopen(request)
|
|
|
|
except urllib2.URLError, e:
|
2013-06-24 22:20:36 +00:00
|
|
|
module.fail_json("Failed to connect to api host to delete meter")
|
|
|
|
|
|
|
|
# Each new meter gets a new key.pem and ca.pem file, so they should be deleted
|
|
|
|
types = ['cert', 'key']
|
|
|
|
for cert_type in types:
|
|
|
|
try:
|
2013-07-02 15:41:11 +00:00
|
|
|
cert_file = '%s/%s.pem' % (config_directory,cert_type)
|
2013-06-24 22:20:36 +00:00
|
|
|
os.remove(cert_file)
|
2013-08-03 18:55:01 +00:00
|
|
|
except OSError, e:
|
2013-06-24 22:20:36 +00:00
|
|
|
module.fail_json("Failed to remove " + cert_type + ".pem file")
|
2013-06-19 22:01:47 +00:00
|
|
|
|
2013-06-24 22:20:36 +00:00
|
|
|
return 0, "Meter " + name + " deleted"
|
2013-06-19 22:01:47 +00:00
|
|
|
|
2013-06-24 22:20:36 +00:00
|
|
|
def download_request(module, name, apiid, apikey, cert_type):
|
2013-06-19 22:01:47 +00:00
|
|
|
|
2013-06-24 22:20:36 +00:00
|
|
|
meter_id = get_meter_id(module, name, apiid, apikey)
|
2013-06-19 22:01:47 +00:00
|
|
|
|
|
|
|
if meter_id is not None:
|
|
|
|
action = "certificates"
|
2013-06-24 22:20:36 +00:00
|
|
|
request = http_request(name, apiid, apikey, action, meter_id, cert_type)
|
2013-06-19 22:01:47 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
result = urllib2.urlopen(request)
|
|
|
|
except urllib2.URLError, e:
|
2013-06-24 22:20:36 +00:00
|
|
|
module.fail_json("Failed to connect to api host to download certificate")
|
2013-06-19 22:01:47 +00:00
|
|
|
|
|
|
|
if result:
|
|
|
|
try:
|
2013-07-02 15:41:11 +00:00
|
|
|
cert_file_path = '%s/%s.pem' % (config_directory,cert_type)
|
2013-06-19 22:01:47 +00:00
|
|
|
body = result.read()
|
|
|
|
cert_file = open(cert_file_path, 'w')
|
|
|
|
cert_file.write(body)
|
|
|
|
cert_file.close
|
|
|
|
os.chmod(cert_file_path, 0o600)
|
|
|
|
except:
|
|
|
|
module.fail_json("Could not write to certificate file")
|
|
|
|
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
module.fail_json("Could not get meter id")
|
|
|
|
|
|
|
|
def main():
|
|
|
|
|
|
|
|
if not HAS_URLLIB2:
|
|
|
|
module.fail_json(msg="urllib2 is not installed")
|
|
|
|
|
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec=dict(
|
2013-06-24 22:20:36 +00:00
|
|
|
state=dict(required=True, choices=['present', 'absent']),
|
2013-06-19 22:01:47 +00:00
|
|
|
name=dict(required=False),
|
|
|
|
apikey=dict(required=True),
|
|
|
|
apiid=dict(required=True),
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
state = module.params['state']
|
2013-06-24 22:20:36 +00:00
|
|
|
name= module.params['name']
|
2013-07-23 17:16:30 +00:00
|
|
|
apikey = module.params['api_key']
|
|
|
|
apiid = module.params['api_id']
|
2013-06-19 22:01:47 +00:00
|
|
|
|
|
|
|
if state == "present":
|
2013-06-24 22:20:36 +00:00
|
|
|
(rc, result) = create_meter(module, name, apiid, apikey)
|
2013-06-19 22:01:47 +00:00
|
|
|
|
2013-06-24 22:20:36 +00:00
|
|
|
if state == "absent":
|
|
|
|
(rc, result) = delete_meter(module, name, apiid, apikey)
|
2013-06-19 22:01:47 +00:00
|
|
|
|
|
|
|
if rc != 0:
|
|
|
|
module.fail_json(msg=result)
|
|
|
|
|
|
|
|
module.exit_json(status=result,changed=True)
|
|
|
|
|
2013-12-02 20:11:23 +00:00
|
|
|
# import module snippets
|
|
|
|
from ansible.module_utils.basic import *
|
2013-08-03 18:55:01 +00:00
|
|
|
main()
|
2013-08-03 18:55:22 +00:00
|
|
|
|