def remove_monitoring_policy(module, oneandone_conn): """ Removes a monitoring policy. module : AnsibleModule object oneandone_conn: authenticated oneandone object """ try: mp_id = module.params.get('name') monitoring_policy_id = get_monitoring_policy(oneandone_conn, mp_id) if module.check_mode: if monitoring_policy_id is None: _check_mode(module, False) _check_mode(module, True) monitoring_policy = oneandone_conn.delete_monitoring_policy( monitoring_policy_id) changed = True if monitoring_policy else False return (changed, { 'id': monitoring_policy['id'], 'name': monitoring_policy['name'] }) except Exception as ex: module.fail_json(msg=str(ex))
def _add_processes(module, oneandone_conn, monitoring_policy_id, processes): """ Adds new processes to a monitoring policy. """ try: monitoring_policy_processes = [] for _process in processes: monitoring_policy_process = oneandone.client.Process( process=_process['process'], alert_if=_process['alert_if'], email_notification=_process['email_notification']) monitoring_policy_processes.append(monitoring_policy_process) if module.check_mode: mp_id = get_monitoring_policy(oneandone_conn, monitoring_policy_id) if (monitoring_policy_processes and mp_id): return True return False monitoring_policy = oneandone_conn.add_process( monitoring_policy_id=monitoring_policy_id, processes=monitoring_policy_processes) return monitoring_policy except Exception as ex: module.fail_json(msg=str(ex))
def update_monitoring_policy(module, oneandone_conn): """ Updates a monitoring_policy based on input arguments. Monitoring policy ports, processes and servers can be added/removed to/from a monitoring policy. Monitoring policy name, description, email, thresholds for cpu, ram, disk, transfer and internal_ping can be updated as well. module : AnsibleModule object oneandone_conn: authenticated oneandone object """ try: monitoring_policy_id = module.params.get('monitoring_policy') name = module.params.get('name') description = module.params.get('description') email = module.params.get('email') thresholds = module.params.get('thresholds') add_ports = module.params.get('add_ports') update_ports = module.params.get('update_ports') remove_ports = module.params.get('remove_ports') add_processes = module.params.get('add_processes') update_processes = module.params.get('update_processes') remove_processes = module.params.get('remove_processes') add_servers = module.params.get('add_servers') remove_servers = module.params.get('remove_servers') changed = False monitoring_policy = get_monitoring_policy(oneandone_conn, monitoring_policy_id, True) if monitoring_policy is None: _check_mode(module, False) _monitoring_policy = oneandone.client.MonitoringPolicy( name=name, description=description, email=email) _thresholds = None if thresholds: threshold_entities = [ 'cpu', 'ram', 'disk', 'internal_ping', 'transfer' ] _thresholds = [] for treshold in thresholds: key = treshold.keys()[0] if key in threshold_entities: _threshold = oneandone.client.Threshold( entity=key, warning_value=treshold[key]['warning']['value'], warning_alert=str( treshold[key]['warning']['alert']).lower(), critical_value=treshold[key]['critical']['value'], critical_alert=str( treshold[key]['critical']['alert']).lower()) _thresholds.append(_threshold) if name or description or email or thresholds: _check_mode(module, True) monitoring_policy = oneandone_conn.modify_monitoring_policy( monitoring_policy_id=monitoring_policy['id'], monitoring_policy=_monitoring_policy, thresholds=_thresholds) changed = True if add_ports: if module.check_mode: _check_mode( module, _add_ports(module, oneandone_conn, monitoring_policy['id'], add_ports)) monitoring_policy = _add_ports(module, oneandone_conn, monitoring_policy['id'], add_ports) changed = True if update_ports: chk_changed = False for update_port in update_ports: if module.check_mode: chk_changed |= _modify_port(module, oneandone_conn, monitoring_policy['id'], update_port['id'], update_port) _modify_port(module, oneandone_conn, monitoring_policy['id'], update_port['id'], update_port) monitoring_policy = get_monitoring_policy(oneandone_conn, monitoring_policy['id'], True) changed = True if remove_ports: chk_changed = False for port_id in remove_ports: if module.check_mode: chk_changed |= _delete_monitoring_policy_port( module, oneandone_conn, monitoring_policy['id'], port_id) _delete_monitoring_policy_port(module, oneandone_conn, monitoring_policy['id'], port_id) _check_mode(module, chk_changed) monitoring_policy = get_monitoring_policy(oneandone_conn, monitoring_policy['id'], True) changed = True if add_processes: monitoring_policy = _add_processes(module, oneandone_conn, monitoring_policy['id'], add_processes) _check_mode(module, monitoring_policy) changed = True if update_processes: chk_changed = False for update_process in update_processes: if module.check_mode: chk_changed |= _modify_process(module, oneandone_conn, monitoring_policy['id'], update_process['id'], update_process) _modify_process(module, oneandone_conn, monitoring_policy['id'], update_process['id'], update_process) _check_mode(module, chk_changed) monitoring_policy = get_monitoring_policy(oneandone_conn, monitoring_policy['id'], True) changed = True if remove_processes: chk_changed = False for process_id in remove_processes: if module.check_mode: chk_changed |= _delete_monitoring_policy_process( module, oneandone_conn, monitoring_policy['id'], process_id) _delete_monitoring_policy_process(module, oneandone_conn, monitoring_policy['id'], process_id) _check_mode(module, chk_changed) monitoring_policy = get_monitoring_policy(oneandone_conn, monitoring_policy['id'], True) changed = True if add_servers: monitoring_policy = _attach_monitoring_policy_server( module, oneandone_conn, monitoring_policy['id'], add_servers) _check_mode(module, monitoring_policy) changed = True if remove_servers: chk_changed = False for _server_id in remove_servers: server_id = get_server(oneandone_conn, _server_id) if module.check_mode: chk_changed |= _detach_monitoring_policy_server( module, oneandone_conn, monitoring_policy['id'], server_id) _detach_monitoring_policy_server(module, oneandone_conn, monitoring_policy['id'], server_id) _check_mode(module, chk_changed) monitoring_policy = get_monitoring_policy(oneandone_conn, monitoring_policy['id'], True) changed = True return (changed, monitoring_policy) except Exception as ex: module.fail_json(msg=str(ex))
def create_server(module, oneandone_conn): """ Create new server module : AnsibleModule object oneandone_conn: authenticated oneandone object Returns a dictionary containing a 'changed' attribute indicating whether any server was added, and a 'servers' attribute with the list of the created servers' hostname, id and ip addresses. """ hostname = module.params.get('hostname') description = module.params.get('description') auto_increment = module.params.get('auto_increment') count = module.params.get('count') fixed_instance_size = module.params.get('fixed_instance_size') vcore = module.params.get('vcore') cores_per_processor = module.params.get('cores_per_processor') ram = module.params.get('ram') hdds = module.params.get('hdds') datacenter = module.params.get('datacenter') appliance = module.params.get('appliance') ssh_key = module.params.get('ssh_key') private_network = module.params.get('private_network') monitoring_policy = module.params.get('monitoring_policy') firewall_policy = module.params.get('firewall_policy') load_balancer = module.params.get('load_balancer') server_type = module.params.get('server_type') wait = module.params.get('wait') wait_timeout = module.params.get('wait_timeout') wait_interval = module.params.get('wait_interval') datacenter_id = get_datacenter(oneandone_conn, datacenter) if datacenter_id is None: _check_mode(module, False) module.fail_json(msg='datacenter %s not found.' % datacenter) fixed_instance_size_id = None if fixed_instance_size: fixed_instance_size_id = get_fixed_instance_size( oneandone_conn, fixed_instance_size) if fixed_instance_size_id is None: _check_mode(module, False) module.fail_json(msg='fixed_instance_size %s not found.' % fixed_instance_size) appliance_id = get_appliance(oneandone_conn, appliance) if appliance_id is None: _check_mode(module, False) module.fail_json(msg='appliance %s not found.' % appliance) private_network_id = None if private_network: private_network_id = get_private_network(oneandone_conn, private_network) if private_network_id is None: _check_mode(module, False) module.fail_json(msg='private network %s not found.' % private_network) monitoring_policy_id = None if monitoring_policy: monitoring_policy_id = get_monitoring_policy(oneandone_conn, monitoring_policy) if monitoring_policy_id is None: _check_mode(module, False) module.fail_json(msg='monitoring policy %s not found.' % monitoring_policy) firewall_policy_id = None if firewall_policy: firewall_policy_id = get_firewall_policy(oneandone_conn, firewall_policy) if firewall_policy_id is None: _check_mode(module, False) module.fail_json(msg='firewall policy %s not found.' % firewall_policy) load_balancer_id = None if load_balancer: load_balancer_id = get_load_balancer(oneandone_conn, load_balancer) if load_balancer_id is None: _check_mode(module, False) module.fail_json(msg='load balancer %s not found.' % load_balancer) if auto_increment: hostnames = _auto_increment_hostname(count, hostname) descriptions = _auto_increment_description(count, description) else: hostnames = [hostname] * count descriptions = [description] * count hdd_objs = [] if hdds: for hdd in hdds: hdd_objs.append( oneandone.client.Hdd(size=hdd['size'], is_main=hdd['is_main'])) servers = [] for index, name in enumerate(hostnames): server = _create_server(module=module, oneandone_conn=oneandone_conn, hostname=name, description=descriptions[index], fixed_instance_size_id=fixed_instance_size_id, vcore=vcore, cores_per_processor=cores_per_processor, ram=ram, hdds=hdd_objs, datacenter_id=datacenter_id, appliance_id=appliance_id, ssh_key=ssh_key, private_network_id=private_network_id, monitoring_policy_id=monitoring_policy_id, firewall_policy_id=firewall_policy_id, load_balancer_id=load_balancer_id, server_type=server_type, wait=wait, wait_timeout=wait_timeout, wait_interval=wait_interval) if server: servers.append(server) changed = False if servers: for server in servers: if server: _check_mode(module, True) _check_mode(module, False) servers = [_insert_network_data(_server) for _server in servers] changed = True _check_mode(module, False) return (changed, servers)