Beispiel #1
0
def servers_action(module, target_state):
    changed = False
    set_token_headers(module)
    target_list = get_target_list(module, target_state)
    process_servers = ratify_server_list(module, target_list, target_state)

    first_response = []
    for ps in process_servers:
        if ps['status'] != state_api_remapping(target_state):
            ap = get_api_params(module, ps['id'], target_state)
            first_response.append(requests_wrapper(ap['endpoint'], ap['method'], data=ap['data'], module=module).json())
            changed = True

    if target_state == 'present':
        target_list = get_servers_id(module, target_list)
    if changed:
        process_servers = wait_for_status_change(module, target_list, target_state)

    if target_state in ['present', 'reset']:
        process_servers = prepare_result_present(first_response, target_state)

    return{
        'changed': changed,
        'servers': process_servers
    }
Beispiel #2
0
def cluster_action(module, target_state):
    changed = False
    set_token_headers(module)
    name = module.params['name']
    existing_clusters = get_existing_clusters(module)
    cluster = next(
        (cluster for cluster in existing_clusters if cluster['name'] == name),
        'absent')

    if target_state == 'present':
        if cluster == 'absent':
            changed = True
            if not module.check_mode:
                data = get_create_params(name, module)
                response_create = requests_wrapper(CLUSTER_API,
                                                   method='POST',
                                                   data=data,
                                                   module=module).json()
                cluster_id = response_create['id']
                cluster = wait_for_status_change(cluster_id, 'Ready',
                                                 response_create, module)
        else:
            check_immutable_arguments(IMMUTABLE_ARGUMENTS, cluster, module)

    if target_state == 'absent' and cluster != 'absent':
        changed = True
        if not module.check_mode:
            cluster = requests_wrapper(CLUSTER_API + cluster['id'],
                                       method='DELETE',
                                       module=module).json()

    if cluster == 'absent':
        cluster = 'The cluster [%s]' % name + ' is absent'

    return {'changed': changed, 'clusters': cluster}
Beispiel #3
0
def reservation_info(module):
    set_token_headers(module)
    data = {
        'productCategory': module.params['product_category'].upper()
    }
    reservations = requests_wrapper(RESERVATION_API, params=data, module=module).json()
    return{
        'reservations': reservations
    }
Beispiel #4
0
def ssh_key_info(module):
    set_token_headers(module)
    ssh_keys = requests_wrapper(SSH_API, module=module).json()
    filter_ssh_keys = []
    names = module.params['names']

    if names:
        [filter_ssh_keys.append(sh) for sh in ssh_keys if sh['name'] in names]
        ssh_keys = filter_ssh_keys

    return {'ssh_keys': ssh_keys}
Beispiel #5
0
def tag_info(module):
    set_token_headers(module)
    tags = requests_wrapper(TAG_API, module=module).json()
    filter_tags = []
    names = module.params['names']

    if names:
        [filter_tags.append(tag) for tag in tags if tag['name'] in names]
        tags = filter_tags

    return{
        'tags': tags
    }
def network_action(module, state):
    set_token_headers(module)
    changed = False
    existing_networks = get_existing_networks(module)
    new_network_name = module.params['name']
    target_network = next((network for network in existing_networks
                           if network['name'] == new_network_name), 'absent')

    if state == 'present':
        if target_network == 'absent':
            changed = True
            data = json.dumps({
                'name': new_network_name,
                'location': module.params['location'],
                'description': module.params['description'],
                'ipBlocks': module.params['ip_blocks']
            })
            if not module.check_mode:
                target_network = requests_wrapper(PUBLIC_NETWORK_API,
                                                  method='POST',
                                                  data=data).json()

        else:
            check_immutable_arguments(IMMUTABLE_ARGUMENTS, target_network,
                                      module)
            desc = target_network.get('description')
            if desc != module.params['description']:
                changed = True
                data = json.dumps({
                    'name': target_network['name'],
                    'description': module.params['description'],
                })
                if not module.check_mode:
                    target_network = requests_wrapper(PUBLIC_NETWORK_API +
                                                      target_network['id'],
                                                      method='PATCH',
                                                      data=data).json()

    if state == 'absent' and target_network != 'absent':
        changed = True
        if not module.check_mode:
            response = requests_wrapper(PUBLIC_NETWORK_API +
                                        target_network['id'],
                                        method='DELETE')
            target_network = 'The network [%s] has been deleted.' % new_network_name if len(
                response.text) == 0 else response.json()

    if target_network == 'absent':
        target_network = 'The network [%s]' % new_network_name + ' is absent'

    return {'changed': changed, 'public_networks': target_network}
Beispiel #7
0
def product_info(module):
    set_token_headers(module)

    params = {
        'productCode': module.params['product_code'],
        'productCategory': module.params['product_category'],
        'skuCode': module.params['sku_code'],
        'location': module.params['location'],
    }

    products = requests_wrapper(PRODUCT_API, params=params,
                                module=module).json()

    return {'products': products}
def tag_action(module, state):
    set_token_headers(module)
    changed = False
    existing_tags = get_existing_tags(module)
    new_tag_name = module.params['name']
    target_tag = next(
        (network
         for network in existing_tags if network['name'] == new_tag_name),
        'absent')

    if state == 'present':
        if target_tag == 'absent':
            changed = True
            data = json.dumps({
                'name': new_tag_name,
                'isBillingTag': module.params['is_billing_tag'],
                'description': module.params['description'],
            })
            if not module.check_mode:
                target_tag = requests_wrapper(TAG_API,
                                              method='POST',
                                              data=data).json()
        else:
            desc = target_tag.get('description')
            if desc != module.params['description'] or target_tag[
                    'isBillingTag'] != module.params['is_billing_tag']:
                changed = True
                data = json.dumps({
                    'name':
                    target_tag['name'],
                    'description':
                    module.params['description'],
                    'isBillingTag':
                    module.params['is_billing_tag']
                })
                if not module.check_mode:
                    target_tag = requests_wrapper(TAG_API + target_tag['id'],
                                                  method='PATCH',
                                                  data=data).json()

    if state == 'absent' and target_tag != 'absent':
        changed = True
        if not module.check_mode:
            target_tag = requests_wrapper(TAG_API + target_tag['id'],
                                          method='DELETE').json()

    if target_tag == 'absent':
        target_tag = 'The tag [%s]' % new_tag_name + ' is absent'

    return {'changed': changed, 'tags': target_tag}
def event_info(module):
    set_token_headers(module)
    data = {
        'from': module.params['from_date'],
        'to': module.params['to_date'],
        'limit': module.params['limit'],
        'order': module.params['order'],
        'username': module.params['username'],
        'verb': module.params['verb'],
        'uri': module.params['uri'],
    }
    events = requests_wrapper(EVENT_API, module=module, params=data).json()
    return{
        'events': events
    }
def reservation_action(module, state):
    set_token_headers(module)
    sku = module.params['sku']
    auto_renew = module.params['auto_renew']
    changed = False
    existing_reservations = get_existing_reservations(module)
    target_reservations = [
        er for er in existing_reservations if sku == er['sku']
    ]
    reservations = []

    if state == 'present':
        if module.params['convert']:
            if not target_reservations:
                raise Exception("Reservation with SKU %s doesn't exist." % sku)
            new_sku = module.params['convert']
            data = json.dumps({'sku': new_sku})

            for tr in target_reservations:
                CONVERT_ENDPOINT = RESERVATION_API + tr[
                    'id'] + '/actions/convert'
                reservations.append(
                    requests_wrapper(CONVERT_ENDPOINT,
                                     method='POST',
                                     data=data).json())
                changed = True
        else:
            if target_reservations:
                for tr in target_reservations:
                    if tr['autoRenew'] != auto_renew:
                        reservations.append(
                            change_autorenew_state(tr, auto_renew))
                        changed = True
                    else:
                        reservations.append(tr)
            else:
                data = json.dumps({'sku': sku})
                reservations = requests_wrapper(RESERVATION_API,
                                                method='POST',
                                                data=data).json()
                if reservations['autoRenew'] != auto_renew:
                    reservations = change_autorenew_state(
                        reservations, auto_renew)
                changed = True

    return {'changed': changed, 'reservations': reservations}
Beispiel #11
0
def private_network_info(module):
    set_token_headers(module)
    params = {'location': module.params['location']}
    private_networks = requests_wrapper(PRIVATE_NETWORK_API,
                                        params=params,
                                        module=module).json()
    filter_private_networks = []
    names = module.params['names']

    if names:
        [
            filter_private_networks.append(pn) for pn in private_networks
            if pn['name'] in names
        ]
        private_networks = filter_private_networks

    return {'private_networks': private_networks}
Beispiel #12
0
def ssh_keys_action(module, state):
    set_token_headers(module)
    changed = False
    existing_keys = get_existing_keys(module)
    new_key_name = module.params['name']
    target_key = next(
        (key for key in existing_keys if key['name'] == new_key_name),
        'absent')

    if state == 'present':
        if target_key == 'absent':
            changed = True
            data = json.dumps({
                'name': new_key_name,
                'default': module.params['default'],
                'key': module.params['ssh_key']
            })
            if not module.check_mode:
                target_key = requests_wrapper(SSH_API,
                                              method='POST',
                                              data=data).json()
        else:
            check_immutable_arguments(IMMUTABLE_ARGUMENTS, target_key, module)
            if target_key['default'] != module.params['default']:
                changed = True
                data = json.dumps({
                    'name': target_key['name'],
                    'default': module.params['default']
                })
                if not module.check_mode:
                    target_key = requests_wrapper(SSH_API + target_key['id'],
                                                  method='PUT',
                                                  data=data).json()

    if state == 'absent' and target_key != 'absent':
        changed = True
        data = json.dumps({'ssh_key_id': target_key['id']})
        if not module.check_mode:
            target_key = requests_wrapper(SSH_API + target_key['id'],
                                          method='DELETE',
                                          data=data).json()

    if target_key == 'absent':
        target_key = 'The SSH Key [%s]' % new_key_name + ' is absent'

    return {'changed': changed, 'ssh_key': target_key}
def server_info(module):
    set_token_headers(module)
    servers = requests_wrapper(SERVER_API, module=module).json()
    filter_servers = []
    server_ids = module.params['server_ids']
    hostnames = module.params['hostnames']

    if server_ids:
        [filter_servers.append(es) for es in servers if es['id'] in server_ids]
        servers = filter_servers

    if hostnames:
        [filter_servers.append(es) for es in servers if es['hostname'] in hostnames]
        servers = filter_servers

    return{
        'servers': servers
    }
Beispiel #14
0
def servers_action(module, target_state):
    changed = False
    set_token_headers(module)
    existing_servers = get_existing_servers(module)
    target_list = get_target_list(module, target_state, existing_servers)
    process_servers = ratify_server_list(target_list, target_state,
                                         existing_servers)

    first_response = []
    for ps in process_servers:
        if ps['status'] not in state_api_remapping(target_state):
            changed = True
            if not module.check_mode:
                ap = get_api_params(module, ps['id'], target_state)
                first_response.append(
                    requests_wrapper(ap['endpoint'],
                                     ap['method'],
                                     data=ap['data'],
                                     module=module).json())

    if target_state == 'present':
        existing_servers = get_existing_servers(module)
        [
            target_list.remove(ps['hostname']) for ps in present_servers
            if ps['hostname'] in target_list
        ]
        target_list = get_servers_id(target_list, existing_servers,
                                     target_state)

    if not module.check_mode:
        if changed:
            process_servers = wait_for_status_change(module, target_list,
                                                     target_state,
                                                     first_response)
        if target_state in ['present', 'reset']:
            process_servers = prepare_result_present(first_response,
                                                     target_state)

    if target_state == 'present':
        process_servers += present_servers

    return {'changed': changed, 'servers': process_servers}
def ip_blocks_action(module, state):
    set_token_headers(module)
    changed = False
    count = module.params['count']
    if count and count < 1:
        raise Exception('The count cannot be less than 0')
    cidr_block_size = module.params['cidr_block_size']
    location = module.params['location']
    description = module.params['description']
    existing_ips = get_existing_ip_blocks(module)
    ip_blocks_result = []

    if state == 'present':
        match_ips, count = get_matched_ip_blocks(existing_ips, cidr_block_size, location, count)
        if count > 0:
            changed = True
            ip_blocks_result = create_ip_blocks(cidr_block_size, location, description, count, module)
        elif count == 0:
            ip_blocks_result = match_ips
        else:
            ids = find_deletion_candidates(match_ips, abs(count))
            ip_blocks_result, changed = delete_ip_blocks(ids, abs(count), module, changed)

    if state == 'absent':
        count = 1
        target_ip_block = next((ip for ip in existing_ips if ip['id'] == module.params['ip_block_id']), 'absent')
        if target_ip_block != 'absent':
            if not module.check_mode:
                ip_blocks_result, changed = delete_ip_blocks([target_ip_block], count, module, changed)
            else:
                ip_blocks_result, changed = delete_ip_blocks([target_ip_block], count, module, changed)
        else:
            ip_blocks_result = 'The IP Block with Id %s' % module.params['ip_block_id'] + ' is absent.'

    return{
        'changed': changed,
        'ip_blocks': ip_blocks_result
    }
def ssh_keys_action(module, state):
    set_token_headers(module)
    changed = False
    existing_keys = get_existing_keys(module)
    new_key_name = module.params['name']
    target_key = next(
        (key for key in existing_keys if key['name'] == new_key_name),
        'missing')

    if state == 'present':
        if target_key == 'missing':
            data = json.dumps({
                'name': new_key_name,
                'default': module.params['default'],
                'key': module.params['ssh_key']
            })
            target_key = requests_wrapper(BASE_API, method='POST',
                                          data=data).json()
            changed = True
        else:
            if target_key['default'] != module.params['default']:
                data = json.dumps({
                    'name': target_key['name'],
                    'default': module.params['default']
                })
                target_key = requests_wrapper(BASE_API + target_key['id'],
                                              method='PUT',
                                              data=data).json()
                changed = True

    if state == 'absent' and target_key != 'missing':
        data = json.dumps({'ssh_key_id': target_key['id']})
        target_key = requests_wrapper(BASE_API + target_key['id'],
                                      method='DELETE',
                                      data=data).json()
        changed = True

    return {'changed': changed, 'ssh_key': target_key}
def product_availabilities_info(module):
    set_token_headers(module)

    params = {
        'productCategory':
        module.params['product_category'],
        'productCode':
        module.params['product_code'],
        'showOnlyMinQuantityAvailable':
        module.params['show_only_min_quantity_available'],
        'location':
        module.params['location'],
        'solution':
        module.params['solution'],
        'minQuantity':
        module.params['min_quantity'],
    }

    product_availabilities = requests_wrapper(PRODUCT_AVAILABILITY_API,
                                              params=params,
                                              module=module).json()

    return {'product_availabilities': product_availabilities}
Beispiel #18
0
def cluster_info(module):
    set_token_headers(module)
    clusters = requests_wrapper(CLUSTER_API, module=module).json()
    return {'clusters': clusters}
def ip_blocks_info(module):
    set_token_headers(module)
    ip_blocks = requests_wrapper(IP_API, module=module).json()
    return {'ip_blocks': ip_blocks}