Last active
August 5, 2016 06:55
-
-
Save yudai09/78d14656ee63ca5ae2f04dd38458a5cc to your computer and use it in GitHub Desktop.
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
#!/usr/bin/python | |
# -*- coding: utf-8 -*- | |
# this ansible module can maintain niftycloud loadbalancer | |
# see https://github.com/yudai09/niftycloud/tree/master/niftycloud/computing/loadbalancer | |
import json | |
try: | |
import niftycloud.computing.loadbalancer | |
HAS_NIFTYCLOUD_MODULE = True | |
except ImportError: | |
HAS_NIFTYCLOUD_MODULE = False | |
def main(): | |
module = AnsibleModule( | |
argument_spec = dict( | |
name = dict(required=True, default=None), | |
instance = dict(required=True, default=None), | |
load_balancer_port = dict(required=True, default=None), | |
instance_port = dict(required=True, default=None), | |
state = dict(required=True, default=None, choices=['present', 'absent']), | |
aws_access_key_id = dict(required=True, default=None), | |
aws_secret_access_key = dict(required=True, default=None), | |
region = dict(default=None) | |
) | |
) | |
load_balancer_name = module.params.get('name') | |
instance_name= module.params.get('instance') | |
load_balancer_port = module.params.get('load_balancer_port') | |
instance_port = module.params.get('instance_port') | |
state = module.params.get('state') | |
aws_access_key_id = module.params.get('aws_access_key_id') | |
aws_secret_access_key = module.params.get('aws_secret_access_key') | |
region = module.params.get('region') | |
if not HAS_NIFTYCLOUD_MODULE: | |
module.fail_json(rc=1, msg='Please install niftycloud module before using this module') | |
try: | |
global conn | |
conn = niftycloud.computing.loadbalancer.connect_to_region( | |
region, | |
aws_access_key_id=aws_access_key_id, | |
aws_secret_access_key=aws_secret_access_key, | |
) | |
lb = get_load_balancer(load_balancer_name, | |
int(load_balancer_port), | |
int(instance_port)) | |
if lb is None: | |
module.fail_json(rc=1, msg='loadbalancer (%s) not found' % (load_balancer_name)) | |
if state == 'present': | |
changed = register_instance_to_load_balancer(lb, instance_name) | |
module.exit_json(changed=changed, | |
loadbalancer=jsonize_loadbalancer(lb)) | |
else: # state == 'absent': | |
changed = deregister_instance_from_load_balancer(lb, instance_name) | |
module.exit_json(changed=changed, | |
loadbalancer=jsonize_loadbalancer(lb)) | |
except Exception, e: | |
module.fail_json(rc=1, msg='Failed to maintain loadbalancer, %s' % (e)) | |
def get_load_balancer(load_balancer_name, | |
load_balancer_port, | |
instance_port): | |
global conn | |
lbs = conn.get_all_load_balancers( | |
load_balancer_names=[load_balancer_name]) | |
for lb in lbs: | |
listener = lb.listeners[0] | |
if listener.load_balancer_port == load_balancer_port \ | |
and listener.instance_port == instance_port: | |
return lb | |
return None | |
def register_instance_to_load_balancer(lb, instance_name): | |
changed = False | |
if instance_name in [instance.id for instance in lb.instances]: | |
# instance is already registered to loadbalancer | |
changed = False | |
else: | |
lb.register_instances([instance_name]) | |
changed = True | |
return changed | |
def deregister_instance_from_load_balancer(lb, instance_name): | |
changed = False | |
if instance_name not in [instance.id for instance in lb.instances]: | |
# instance not registered to loadbalancer | |
changed = False | |
else: | |
lb.deregister_instances([instance_name]) | |
changed = True | |
return changed | |
def jsonize_loadbalancer(lb): | |
return {"name": lb.name, | |
"instances": "%s" % (lb.instances), | |
"listeners": "%s" % (lb.listeners)} | |
# import module snippets | |
from ansible.module_utils.basic import * | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment