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))
Exemple #2
0
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_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_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)