mirror of https://github.com/ansible/ansible.git
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
257 lines
8.0 KiB
Python
257 lines
8.0 KiB
Python
12 years ago
|
#!/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 base64
|
||
12 years ago
|
import os
|
||
12 years ago
|
|
||
|
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
|
||
12 years ago
|
- bprobe is required to send data, but not to register a meter
|
||
|
- Python urllib2
|
||
12 years ago
|
options:
|
||
|
name:
|
||
|
description:
|
||
|
- meter name
|
||
|
required: true
|
||
|
state:
|
||
|
description:
|
||
|
- Whether to create or remove the client from boundary
|
||
|
required: false
|
||
|
default: true
|
||
12 years ago
|
choices: ["present", "absent"]
|
||
12 years ago
|
apiid:
|
||
|
description:
|
||
|
- Organizations boundary API ID
|
||
|
required: true
|
||
|
apikey:
|
||
|
description:
|
||
|
- Organizations boundary API KEY
|
||
|
required: true
|
||
11 years ago
|
validate_certs:
|
||
|
description:
|
||
|
- If C(no), SSL certificates will not be validated. This should only be used
|
||
|
on personally controlled sites using self-signed certificates.
|
||
|
required: false
|
||
|
default: 'yes'
|
||
|
choices: ['yes', 'no']
|
||
|
version_added: 1.5.1
|
||
12 years ago
|
|
||
|
notes:
|
||
12 years ago
|
- This module does not yet support boundary tags.
|
||
12 years ago
|
|
||
|
'''
|
||
|
|
||
|
EXAMPLES='''
|
||
|
- name: Create meter
|
||
12 years ago
|
boundary_meter: apiid=AAAAAA api_key=BBBBBB state=present name={{ inventory_hostname }}"
|
||
12 years ago
|
|
||
|
- name: Delete meter
|
||
12 years ago
|
boundary_meter: apiid=AAAAAA api_key=BBBBBB state=absent name={{ inventory_hostname }}"
|
||
12 years ago
|
|
||
|
'''
|
||
|
|
||
12 years ago
|
api_host = "api.boundary.com"
|
||
|
config_directory = "/etc/bprobe"
|
||
12 years ago
|
|
||
12 years ago
|
# "resource" like thing or apikey?
|
||
|
def auth_encode(apikey):
|
||
|
auth = base64.standard_b64encode(apikey)
|
||
12 years ago
|
auth.replace("\n", "")
|
||
|
return auth
|
||
|
|
||
12 years ago
|
def build_url(name, apiid, action, meter_id=None, cert_type=None):
|
||
12 years ago
|
if action == "create":
|
||
12 years ago
|
return 'https://%s/%s/meters' % (api_host, apiid)
|
||
12 years ago
|
elif action == "search":
|
||
12 years ago
|
return "https://%s/%s/meters?name=%s" % (api_host, apiid, name)
|
||
12 years ago
|
elif action == "certificates":
|
||
12 years ago
|
return "https://%s/%s/meters/%s/%s.pem" % (api_host, apiid, meter_id, cert_type)
|
||
12 years ago
|
elif action == "tags":
|
||
12 years ago
|
return "https://%s/%s/meters/%s/tags" % (api_host, apiid, meter_id)
|
||
12 years ago
|
elif action == "delete":
|
||
12 years ago
|
return "https://%s/%s/meters/%s" % (api_host, apiid, meter_id)
|
||
12 years ago
|
|
||
11 years ago
|
def http_request(module, name, apiid, apikey, action, data=None, meter_id=None, cert_type=None):
|
||
12 years ago
|
|
||
12 years ago
|
if meter_id is None:
|
||
|
url = build_url(name, apiid, action)
|
||
12 years ago
|
else:
|
||
12 years ago
|
if cert_type is None:
|
||
|
url = build_url(name, apiid, action, meter_id)
|
||
12 years ago
|
else:
|
||
12 years ago
|
url = build_url(name, apiid, action, meter_id, cert_type)
|
||
12 years ago
|
|
||
11 years ago
|
headers = dict()
|
||
|
headers["Authorization"] = "Basic %s" % auth_encode(apikey)
|
||
|
headers["Content-Type"] = "application/json"
|
||
|
|
||
|
return fetch_url(module, url, data=data, headers=headers)
|
||
12 years ago
|
|
||
12 years ago
|
def create_meter(module, name, apiid, apikey):
|
||
12 years ago
|
|
||
12 years ago
|
meters = search_meter(module, name, apiid, apikey)
|
||
12 years ago
|
|
||
|
if len(meters) > 0:
|
||
|
# If the meter already exists, do nothing
|
||
12 years ago
|
module.exit_json(status="Meter " + name + " already exists",changed=False)
|
||
12 years ago
|
else:
|
||
|
# If it doesn't exist, create it
|
||
12 years ago
|
body = '{"name":"' + name + '"}'
|
||
11 years ago
|
response, info = http_request(module, name, apiid, apikey, data=body, action="create")
|
||
12 years ago
|
|
||
11 years ago
|
if info['status'] != 200:
|
||
12 years ago
|
module.fail_json(msg="Failed to connect to api host to create meter")
|
||
|
|
||
12 years ago
|
# 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)
|
||
|
|
||
12 years ago
|
|
||
|
# 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
|
||
12 years ago
|
cert_file = open('%s/%s.pem' % (config_directory,cert_type))
|
||
12 years ago
|
except IOError:
|
||
|
# Now download the file...
|
||
12 years ago
|
rc = download_request(module, name, apiid, apikey, cert_type)
|
||
12 years ago
|
if rc == False:
|
||
|
module.fail_json("Download request for " + cert_type + ".pem failed")
|
||
|
|
||
12 years ago
|
return 0, "Meter " + name + " created"
|
||
12 years ago
|
|
||
12 years ago
|
def search_meter(module, name, apiid, apikey):
|
||
12 years ago
|
|
||
11 years ago
|
response, info = http_request(module, name, apiid, apikey, action="search")
|
||
12 years ago
|
|
||
11 years ago
|
if info['status'] != 200:
|
||
12 years ago
|
module.fail_json("Failed to connect to api host to search for meter")
|
||
12 years ago
|
|
||
|
# Return meters
|
||
11 years ago
|
return json.loads(response.read())
|
||
12 years ago
|
|
||
12 years ago
|
def get_meter_id(module, name, apiid, apikey):
|
||
12 years ago
|
# In order to delete the meter we need its id
|
||
12 years ago
|
meters = search_meter(module, name, apiid, apikey)
|
||
12 years ago
|
|
||
|
if len(meters) > 0:
|
||
|
return meters[0]['id']
|
||
|
else:
|
||
|
return None
|
||
|
|
||
12 years ago
|
def delete_meter(module, name, apiid, apikey):
|
||
12 years ago
|
|
||
12 years ago
|
meter_id = get_meter_id(module, name, apiid, apikey)
|
||
12 years ago
|
|
||
|
if meter_id is None:
|
||
|
return 1, "Meter does not exist, so can't delete it"
|
||
|
else:
|
||
11 years ago
|
response, info = http_request(module, name, apiid, apikey, action, meter_id)
|
||
|
if info['status'] != 200:
|
||
|
module.fail_json("Failed to delete meter")
|
||
12 years ago
|
|
||
|
# 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:
|
||
12 years ago
|
cert_file = '%s/%s.pem' % (config_directory,cert_type)
|
||
12 years ago
|
os.remove(cert_file)
|
||
11 years ago
|
except OSError, e:
|
||
12 years ago
|
module.fail_json("Failed to remove " + cert_type + ".pem file")
|
||
12 years ago
|
|
||
12 years ago
|
return 0, "Meter " + name + " deleted"
|
||
12 years ago
|
|
||
12 years ago
|
def download_request(module, name, apiid, apikey, cert_type):
|
||
12 years ago
|
|
||
12 years ago
|
meter_id = get_meter_id(module, name, apiid, apikey)
|
||
12 years ago
|
|
||
|
if meter_id is not None:
|
||
|
action = "certificates"
|
||
11 years ago
|
response, info = http_request(module, name, apiid, apikey, action, meter_id, cert_type)
|
||
|
if info['status'] != 200:
|
||
12 years ago
|
module.fail_json("Failed to connect to api host to download certificate")
|
||
12 years ago
|
|
||
|
if result:
|
||
|
try:
|
||
12 years ago
|
cert_file_path = '%s/%s.pem' % (config_directory,cert_type)
|
||
11 years ago
|
body = response.read()
|
||
12 years ago
|
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():
|
||
|
|
||
|
module = AnsibleModule(
|
||
|
argument_spec=dict(
|
||
12 years ago
|
state=dict(required=True, choices=['present', 'absent']),
|
||
12 years ago
|
name=dict(required=False),
|
||
|
apikey=dict(required=True),
|
||
|
apiid=dict(required=True),
|
||
11 years ago
|
validate_certs = dict(default='yes', type='bool'),
|
||
12 years ago
|
)
|
||
|
)
|
||
|
|
||
|
state = module.params['state']
|
||
12 years ago
|
name= module.params['name']
|
||
12 years ago
|
apikey = module.params['api_key']
|
||
|
apiid = module.params['api_id']
|
||
12 years ago
|
|
||
|
if state == "present":
|
||
12 years ago
|
(rc, result) = create_meter(module, name, apiid, apikey)
|
||
12 years ago
|
|
||
12 years ago
|
if state == "absent":
|
||
|
(rc, result) = delete_meter(module, name, apiid, apikey)
|
||
12 years ago
|
|
||
|
if rc != 0:
|
||
|
module.fail_json(msg=result)
|
||
|
|
||
|
module.exit_json(status=result,changed=True)
|
||
|
|
||
11 years ago
|
# import module snippets
|
||
|
from ansible.module_utils.basic import *
|
||
11 years ago
|
from ansible.module_utils.urls import *
|
||
11 years ago
|
main()
|
||
11 years ago
|
|