def create_network(module, oneandone_conn): """ Create new private network module : AnsibleModule object oneandone_conn: authenticated oneandone object Returns a dictionary containing a 'changed' attribute indicating whether any network was added. """ name = module.params.get('name') description = module.params.get('description') network_address = module.params.get('network_address') subnet_mask = module.params.get('subnet_mask') datacenter = module.params.get('datacenter') wait = module.params.get('wait') wait_timeout = module.params.get('wait_timeout') wait_interval = module.params.get('wait_interval') if datacenter is not None: datacenter_id = get_datacenter(oneandone_conn, datacenter) if datacenter_id is None: module.fail_json( msg='datacenter %s not found.' % datacenter) try: _check_mode(module, True) network = oneandone_conn.create_private_network( private_network=oneandone.client.PrivateNetwork( name=name, description=description, network_address=network_address, subnet_mask=subnet_mask, datacenter_id=datacenter_id )) if wait: wait_for_resource_creation_completion( oneandone_conn, OneAndOneResources.private_network, network['id'], wait_timeout, wait_interval) network = get_private_network(oneandone_conn, network['id'], True) changed = True if network else False _check_mode(module, False) return (changed, network) except Exception as e: module.fail_json(msg=str(e))
def create_network(module, oneandone_conn): """ Create new private network module : AnsibleModule object oneandone_conn: authenticated oneandone object Returns a dictionary containing a 'changed' attribute indicating whether any network was added. """ name = module.params.get('name') description = module.params.get('description') network_address = module.params.get('network_address') subnet_mask = module.params.get('subnet_mask') datacenter = module.params.get('datacenter') wait = module.params.get('wait') wait_timeout = module.params.get('wait_timeout') wait_interval = module.params.get('wait_interval') if datacenter is not None: datacenter_id = get_datacenter(oneandone_conn, datacenter) if datacenter_id is None: module.fail_json( msg='datacenter %s not found.' % datacenter) try: _check_mode(module, True) network = oneandone_conn.create_private_network( private_network=oneandone.client.PrivateNetwork( name=name, description=description, network_address=network_address, subnet_mask=subnet_mask, datacenter_id=datacenter_id )) if wait: wait_for_resource_creation_completion( oneandone_conn, OneAndOneResources.private_network, network['id'], wait_timeout, wait_interval) network = get_private_network(oneandone_conn, network['id'], True) changed = True if network else False _check_mode(module, False) return (changed, network) except Exception as e: module.fail_json(msg=str(e))
def create_firewall_policy(module, oneandone_conn): """ Create a new firewall policy. module : AnsibleModule object oneandone_conn: authenticated oneandone object """ try: name = module.params.get('name') description = module.params.get('description') rules = module.params.get('rules') wait = module.params.get('wait') wait_timeout = module.params.get('wait_timeout') wait_interval = module.params.get('wait_interval') firewall_rules = [] for rule in rules: firewall_rule = oneandone.client.FirewallPolicyRule( protocol=rule['protocol'], port_from=rule['port_from'], port_to=rule['port_to'], source=rule['source']) firewall_rules.append(firewall_rule) firewall_policy_obj = oneandone.client.FirewallPolicy( name=name, description=description ) _check_mode(module, True) firewall_policy = oneandone_conn.create_firewall_policy( firewall_policy=firewall_policy_obj, firewall_policy_rules=firewall_rules ) if wait: wait_for_resource_creation_completion( oneandone_conn, OneAndOneResources.firewall_policy, firewall_policy['id'], wait_timeout, wait_interval) firewall_policy = get_firewall_policy(oneandone_conn, firewall_policy['id'], True) # refresh changed = True if firewall_policy else False _check_mode(module, False) return (changed, firewall_policy) except Exception as e: module.fail_json(msg=str(e))
def _create_server(module, oneandone_conn, hostname, description, fixed_instance_size_id, vcore, cores_per_processor, ram, hdds, datacenter_id, appliance_id, ssh_key, private_network_id, firewall_policy_id, load_balancer_id, monitoring_policy_id, server_type, wait, wait_timeout, wait_interval): try: existing_server = get_server(oneandone_conn, hostname) if existing_server: if module.check_mode: return False return None if module.check_mode: return True server = oneandone_conn.create_server( oneandone.client.Server( name=hostname, description=description, fixed_instance_size_id=fixed_instance_size_id, vcore=vcore, cores_per_processor=cores_per_processor, ram=ram, appliance_id=appliance_id, datacenter_id=datacenter_id, rsa_key=ssh_key, private_network_id=private_network_id, firewall_policy_id=firewall_policy_id, load_balancer_id=load_balancer_id, monitoring_policy_id=monitoring_policy_id, server_type=server_type,), hdds) if wait: wait_for_resource_creation_completion( oneandone_conn, OneAndOneResources.server, server['id'], wait_timeout, wait_interval) server = oneandone_conn.get_server(server['id']) # refresh return server except Exception as ex: module.fail_json(msg=str(ex))
def create_public_ip(module, oneandone_conn): """ Create new public IP module : AnsibleModule object oneandone_conn: authenticated oneandone object Returns a dictionary containing a 'changed' attribute indicating whether any public IP was added. """ reverse_dns = module.params.get('reverse_dns') datacenter = module.params.get('datacenter') ip_type = module.params.get('type') wait = module.params.get('wait') wait_timeout = module.params.get('wait_timeout') wait_interval = module.params.get('wait_interval') if datacenter is not None: 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) try: _check_mode(module, True) public_ip = oneandone_conn.create_public_ip( reverse_dns=reverse_dns, ip_type=ip_type, datacenter_id=datacenter_id) if wait: wait_for_resource_creation_completion(oneandone_conn, OneAndOneResources.public_ip, public_ip['id'], wait_timeout, wait_interval) public_ip = oneandone_conn.get_public_ip(public_ip['id']) changed = True if public_ip else False return (changed, public_ip) except Exception as e: module.fail_json(msg=str(e))
def create_public_ip(module, oneandone_conn): """ Create new public IP module : AnsibleModule object oneandone_conn: authenticated oneandone object Returns a dictionary containing a 'changed' attribute indicating whether any public IP was added. """ reverse_dns = module.params.get('reverse_dns') datacenter = module.params.get('datacenter') ip_type = module.params.get('type') wait = module.params.get('wait') wait_timeout = module.params.get('wait_timeout') wait_interval = module.params.get('wait_interval') if datacenter is not None: 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) try: _check_mode(module, True) public_ip = oneandone_conn.create_public_ip( reverse_dns=reverse_dns, ip_type=ip_type, datacenter_id=datacenter_id) if wait: wait_for_resource_creation_completion(oneandone_conn, OneAndOneResources.public_ip, public_ip['id'], wait_timeout, wait_interval) public_ip = oneandone_conn.get_public_ip(public_ip['id']) changed = True if public_ip else False return (changed, public_ip) except Exception as e: module.fail_json(msg=str(e))
def update_public_ip(module, oneandone_conn): """ Update a public IP module : AnsibleModule object oneandone_conn: authenticated oneandone object Returns a dictionary containing a 'changed' attribute indicating whether any public IP was changed. """ reverse_dns = module.params.get('reverse_dns') public_ip_id = module.params.get('public_ip_id') wait = module.params.get('wait') wait_timeout = module.params.get('wait_timeout') wait_interval = module.params.get('wait_interval') public_ip = get_public_ip(oneandone_conn, public_ip_id, True) if public_ip is None: _check_mode(module, False) module.fail_json( msg='public IP %s not found.' % public_ip_id) try: _check_mode(module, True) public_ip = oneandone_conn.modify_public_ip( ip_id=public_ip['id'], reverse_dns=reverse_dns) if wait: wait_for_resource_creation_completion(oneandone_conn, OneAndOneResources.public_ip, public_ip['id'], wait_timeout, wait_interval) public_ip = oneandone_conn.get_public_ip(public_ip['id']) changed = True if public_ip else False return (changed, public_ip) except Exception as e: module.fail_json(msg=str(e))
def update_public_ip(module, oneandone_conn): """ Update a public IP module : AnsibleModule object oneandone_conn: authenticated oneandone object Returns a dictionary containing a 'changed' attribute indicating whether any public IP was changed. """ reverse_dns = module.params.get('reverse_dns') public_ip_id = module.params.get('public_ip_id') wait = module.params.get('wait') wait_timeout = module.params.get('wait_timeout') wait_interval = module.params.get('wait_interval') public_ip = get_public_ip(oneandone_conn, public_ip_id, True) if public_ip is None: _check_mode(module, False) module.fail_json( msg='public IP %s not found.' % public_ip_id) try: _check_mode(module, True) public_ip = oneandone_conn.modify_public_ip( ip_id=public_ip['id'], reverse_dns=reverse_dns) if wait: wait_for_resource_creation_completion(oneandone_conn, OneAndOneResources.public_ip, public_ip['id'], wait_timeout, wait_interval) public_ip = oneandone_conn.get_public_ip(public_ip['id']) changed = True if public_ip else False return (changed, public_ip) except Exception as e: module.fail_json(msg=str(e))
def create_monitoring_policy(module, oneandone_conn): """ Creates a new monitoring policy. module : AnsibleModule object oneandone_conn: authenticated oneandone object """ try: name = module.params.get('name') description = module.params.get('description') email = module.params.get('email') agent = module.params.get('agent') thresholds = module.params.get('thresholds') ports = module.params.get('ports') processes = module.params.get('processes') wait = module.params.get('wait') wait_timeout = module.params.get('wait_timeout') wait_interval = module.params.get('wait_interval') _monitoring_policy = oneandone.client.MonitoringPolicy(name, description, email, agent, ) _monitoring_policy.specs['agent'] = str(_monitoring_policy.specs['agent']).lower() 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) _ports = [] for port in ports: _port = oneandone.client.Port( protocol=port['protocol'], port=port['port'], alert_if=port['alert_if'], email_notification=str(port['email_notification']).lower()) _ports.append(_port) _processes = [] for process in processes: _process = oneandone.client.Process( process=process['process'], alert_if=process['alert_if'], email_notification=str(process['email_notification']).lower()) _processes.append(_process) _check_mode(module, True) monitoring_policy = oneandone_conn.create_monitoring_policy( monitoring_policy=_monitoring_policy, thresholds=_thresholds, ports=_ports, processes=_processes ) if wait: wait_for_resource_creation_completion( oneandone_conn, OneAndOneResources.monitoring_policy, monitoring_policy['id'], wait_timeout, wait_interval) changed = True if monitoring_policy else False _check_mode(module, False) return (changed, monitoring_policy) except Exception as ex: module.fail_json(msg=str(ex))
def create_load_balancer(module, oneandone_conn): """ Create a new load_balancer. module : AnsibleModule object oneandone_conn: authenticated oneandone object """ try: name = module.params.get('name') description = module.params.get('description') health_check_test = module.params.get('health_check_test') health_check_interval = module.params.get('health_check_interval') health_check_path = module.params.get('health_check_path') health_check_parse = module.params.get('health_check_parse') persistence = module.params.get('persistence') persistence_time = module.params.get('persistence_time') method = module.params.get('method') datacenter = module.params.get('datacenter') rules = module.params.get('rules') wait = module.params.get('wait') wait_timeout = module.params.get('wait_timeout') wait_interval = module.params.get('wait_interval') load_balancer_rules = [] datacenter_id = None if datacenter is not None: datacenter_id = get_datacenter(oneandone_conn, datacenter) if datacenter_id is None: module.fail_json( msg='datacenter %s not found.' % datacenter) for rule in rules: load_balancer_rule = oneandone.client.LoadBalancerRule( protocol=rule['protocol'], port_balancer=rule['port_balancer'], port_server=rule['port_server'], source=rule['source']) load_balancer_rules.append(load_balancer_rule) _check_mode(module, True) load_balancer_obj = oneandone.client.LoadBalancer( health_check_path=health_check_path, health_check_parse=health_check_parse, name=name, description=description, health_check_test=health_check_test, health_check_interval=health_check_interval, persistence=persistence, persistence_time=persistence_time, method=method, datacenter_id=datacenter_id ) load_balancer = oneandone_conn.create_load_balancer( load_balancer=load_balancer_obj, load_balancer_rules=load_balancer_rules ) if wait: wait_for_resource_creation_completion(oneandone_conn, OneAndOneResources.load_balancer, load_balancer['id'], wait_timeout, wait_interval) load_balancer = get_load_balancer(oneandone_conn, load_balancer['id'], True) # refresh changed = True if load_balancer else False _check_mode(module, False) return (changed, load_balancer) except Exception as ex: module.fail_json(msg=str(ex))
def create_monitoring_policy(module, oneandone_conn): """ Creates a new monitoring policy. module : AnsibleModule object oneandone_conn: authenticated oneandone object """ try: name = module.params.get('name') description = module.params.get('description') email = module.params.get('email') agent = module.params.get('agent') thresholds = module.params.get('thresholds') ports = module.params.get('ports') processes = module.params.get('processes') wait = module.params.get('wait') wait_timeout = module.params.get('wait_timeout') wait_interval = module.params.get('wait_interval') _monitoring_policy = oneandone.client.MonitoringPolicy( name, description, email, agent, ) _monitoring_policy.specs['agent'] = str( _monitoring_policy.specs['agent']).lower() 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) _ports = [] for port in ports: _port = oneandone.client.Port( protocol=port['protocol'], port=port['port'], alert_if=port['alert_if'], email_notification=str(port['email_notification']).lower()) _ports.append(_port) _processes = [] for process in processes: _process = oneandone.client.Process( process=process['process'], alert_if=process['alert_if'], email_notification=str(process['email_notification']).lower()) _processes.append(_process) _check_mode(module, True) monitoring_policy = oneandone_conn.create_monitoring_policy( monitoring_policy=_monitoring_policy, thresholds=_thresholds, ports=_ports, processes=_processes) if wait: wait_for_resource_creation_completion( oneandone_conn, OneAndOneResources.monitoring_policy, monitoring_policy['id'], wait_timeout, wait_interval) changed = True if monitoring_policy else False _check_mode(module, False) return (changed, monitoring_policy) except Exception as ex: module.fail_json(msg=str(ex))
def create_load_balancer(module, oneandone_conn): """ Create a new load_balancer. module : AnsibleModule object oneandone_conn: authenticated oneandone object """ try: name = module.params.get('name') description = module.params.get('description') health_check_test = module.params.get('health_check_test') health_check_interval = module.params.get('health_check_interval') health_check_path = module.params.get('health_check_path') health_check_parse = module.params.get('health_check_parse') persistence = module.params.get('persistence') persistence_time = module.params.get('persistence_time') method = module.params.get('method') datacenter = module.params.get('datacenter') rules = module.params.get('rules') wait = module.params.get('wait') wait_timeout = module.params.get('wait_timeout') wait_interval = module.params.get('wait_interval') load_balancer_rules = [] datacenter_id = None if datacenter is not None: datacenter_id = get_datacenter(oneandone_conn, datacenter) if datacenter_id is None: module.fail_json( msg='datacenter %s not found.' % datacenter) for rule in rules: load_balancer_rule = oneandone.client.LoadBalancerRule( protocol=rule['protocol'], port_balancer=rule['port_balancer'], port_server=rule['port_server'], source=rule['source']) load_balancer_rules.append(load_balancer_rule) _check_mode(module, True) load_balancer_obj = oneandone.client.LoadBalancer( health_check_path=health_check_path, health_check_parse=health_check_parse, name=name, description=description, health_check_test=health_check_test, health_check_interval=health_check_interval, persistence=persistence, persistence_time=persistence_time, method=method, datacenter_id=datacenter_id ) load_balancer = oneandone_conn.create_load_balancer( load_balancer=load_balancer_obj, load_balancer_rules=load_balancer_rules ) if wait: wait_for_resource_creation_completion(oneandone_conn, OneAndOneResources.load_balancer, load_balancer['id'], wait_timeout, wait_interval) load_balancer = get_load_balancer(oneandone_conn, load_balancer['id'], True) # refresh changed = True if load_balancer else False _check_mode(module, False) return (changed, load_balancer) except Exception as ex: module.fail_json(msg=str(ex))