def get_address_data(configuration_id, ip6_address):
    """
    Get the BAM data for a given ip6_address in the given configuration.

    :param configuration_id: The ID of the configuration in which the ip6_address resides.
    :param ip6_address: The IP address in string form.
    :return: The IP address data in JSON, using result template format.
    """

    # Regular expression to match IPv6 format
    result = get_result_template()
    if not is_valid_ipv6_address(ip6_address):
        result['message'] = 'IP address is invalid.'
        result['status'] = FAIL
        return result

    # Retrieve the configuration object
    configuration = g.user.get_api().get_entity_by_id(configuration_id)

    # Check if IP address exists within a network. Fail if not.
    try:
        configuration.get_ip_ranged_by_ip(Entity.IP6Network, ip6_address)
        result['message'] = 'IPv6 Address is available'
        result['status'] = SUCCESS
        result['data'] = {'state': None, 'mac_address': None, 'name': None}
        return result
    except PortalException:
        result[
            'message'] = 'IP address is not in a network or network does not exist'
        result['status'] = FAIL
        return result
Example #2
0
def release_ip(view):
    """release ip in list input"""
    try:
        args = request.args
        message_check = validate_require_param(args, {'iplist': False})
        if len(message_check) > 0:
            err_message = convert_message_to_string(message_check)
            return Response(err_message, status=400)

        ip_list = args['iplist']

        config = g.user.get_api()._api_client.service.getEntityByName(
            0, view, Entity.Configuration)
        if config['type'] is None:
            raise Exception('View \'{0}\' does not exist'.format(view))
        config_id = config['id']

        ip_list = ip_list.split(',')
        for ip in ip_list:
            ip = ip.strip()
            if util.is_valid_ipv4_address(ip):
                ip_address = g.user.get_api(
                )._api_client.service.getIP4Address(config_id, ip)
            elif util.is_valid_ipv6_address(ip):
                ip_address = g.user.get_api(
                )._api_client.service.getIP6Address(config_id, ip)
            else:
                raise Exception('Ip address \'{0}\' is invalid'.format(ip))
            id = ip_address['id']
            if id is not 0:
                g.user.get_api()._api_client.service.delete(id)
        return Response(status=200)
    except Exception as ex:
        return Response(ex.__str__(), status=400)
Example #3
0
def validate_ip(pool_cidr):
    message = ''
    ip = pool_cidr.split('/')[0]
    if util.is_valid_ipv4_address(ip):
        pass
    elif util.is_valid_ipv6_address(ip):
        pass
    else:
        message = 'Pool \'{0}\' is invalid'.format(pool_cidr)
    return message
Example #4
0
def assign_gateway(parent_id, gateway_ip):
    """assign gateway for entity"""
    options_message = []
    try:
        ip = gateway_ip.split('/')[0]
        if util.is_valid_ipv4_address(ip):
            entity = g.user.get_api()._api_client.service.getEntityById(
                parent_id)
            properties = properties_to_map(entity['properties'])
            properties['gateway'] = gateway_ip
            entity['properties'] = map_to_properties(properties)
            g.user.get_api()._api_client.service.update(entity)
        elif util.is_valid_ipv6_address(ip):
            addip6_gateway(parent_id, gateway_ip)
        else:
            options_message.append(
                'Gateway Ip \'{0}\' is invalid'.format(gateway_ip))
            return options_message
    except Exception as ex:
        options_message.append(
            'Assign gateway \'{0}\' is failed, exception: {1}'.format(
                gateway_ip, ex.message))
    return options_message
Example #5
0
def create_pool():
    """
    Payload request = {
        "view":"DNACenter",
        "poolName":"DNACPool",
        "poolCidr":"10.0.0.0/8",
        "DHCPServerip":[],
        "DNSServerip":[],
        "ClientOptions":[{"key":"option43", "keyValue":"172.0.0.1"}]
    }
    :return: None
    """
    args = request.get_json()
    new_entity_id = None
    # create pool
    try:
        message_check = validate_require_param(args, {
            'view': False,
            'poolName': False,
            'poolCidr': True
        })
        if len(message_check) > 0:
            err_message = convert_message_to_string(message_check)
            return Response(err_message, status=400)

        # get parameters
        pool_name = args['poolName']
        pool_cidr = args['poolCidr']
        config_name = args['view']

        config_id = create_configuration(config_name)

        subpool = get_child_ip(config_id, pool_cidr)
        if subpool is not None:
            if subpool == pool_cidr:
                raise IpPoolAlreadyExistsException(
                    'A pool \'{0}\' already exists'.format(pool_cidr))
            else:
                raise IpPoolInUseException(
                    'A parent pool \'{0}\' has child pool \'{1}\''.format(
                        pool_cidr, subpool))

        ip = pool_cidr.split('/')[0]
        if util.is_valid_ipv4_address(ip):
            new_entity_id = create_ipv4_block(config_id, pool_cidr, pool_name)
        if util.is_valid_ipv6_address(ip):
            new_entity_id = create_ipv6_block(config_id, pool_cidr, pool_name)

    except IpPoolInUseException as inus_err:
        return Response(inus_err.description, status=405)
    except IpPoolAlreadyExistsException as exist_err:
        return Response(exist_err.description, status=405)
    except Exception as ex:
        return Response(ex.__str__(), status=400)

    keys = args.keys()
    options_message = []

    if new_entity_id is not None and 'DHCPServerip' in keys:
        dhcp_server = args['DHCPServerip']
        interfaces = get_server_interface_id(config_id)
        options_message += add_dhcp_servers(config_name, new_entity_id,
                                            dhcp_server, interfaces)

    if new_entity_id is not None and 'DNSServerip' in keys:
        dns_servers = args['DNSServerip']
        dns_view_name = config_name + '_view'
        interfaces = get_server_interface_id(config_id)
        options_message += add_dns_servers(config_id, config_name,
                                           new_entity_id, dns_servers,
                                           interfaces, dns_view_name)

    if new_entity_id is not None and 'ClientOptions' in keys:
        client_options = args['ClientOptions']
        is_ipv4 = True
        ip = pool_cidr.split('/')[0]
        if util.is_valid_ipv6_address(ip):
            is_ipv4 = False
        options_message += add_client_options(client_options, new_entity_id,
                                              is_ipv4)

    if len(options_message) > 0:
        return Response(convert_message_to_string(options_message), status=200)

    return Response(status=200)
Example #6
0
def create_subpool():
    """
    Payload request = {
        "view":"DNACenter",
        "subpoolName":"SanJosePool",
        "poolCidr":"10.0.0.0/16",
        "gatewayip":"10.0.0.1",
        "DHCPServerip":[],
        "DNSServerip":[],
        "ClientOptions":[{"key":"option43", "keyValue":"172.0.0.1"}]
    }
    :return: None
    """
    args = request.get_json()
    g.user.logger.debug(args)
    new_entity_id = None
    # create pool
    try:
        # get parameters
        message_check = validate_require_param(args, {
            'view': False,
            'subpoolName': False,
            'poolCidr': True
        })
        if len(message_check) > 0:
            err_message = convert_message_to_string(message_check)
            return Response(err_message, status=400)

        pool_cidr = args['poolCidr']
        subpool_name = args['subpoolName']
        config_name = args['view']

        config_id = create_configuration(config_name)

        subpool = get_child_ip(config_id, pool_cidr)
        if subpool is not None:
            if subpool == pool_cidr:
                raise IpSubpoolAlreadyExistsException(
                    'A pool \'{0}\' already exists'.format(pool_cidr))
            else:
                raise IpSubpoolNotEmptyException(
                    'A parent pool \'{0}\' has child pool \'{1}\''.format(
                        pool_cidr, subpool))

        ip = pool_cidr.split('/')[0]
        if util.is_valid_ipv4_address(ip):
            if 'gatewayip' in args.keys():
                new_entity_id = create_ipv4_network(config_id, pool_cidr,
                                                    subpool_name,
                                                    args['gatewayip'])
            else:
                new_entity_id = create_ipv4_network(config_id, pool_cidr,
                                                    subpool_name)
                gateway_id = get_gateway_under_network(new_entity_id)
                if gateway_id is not None:
                    g.user.get_api()._api_client.service.delete(gateway_id)

        elif util.is_valid_ipv6_address(ip):
            new_entity_id = create_ipv6_network(config_id, pool_cidr,
                                                subpool_name)
            if 'gatewayip' in args.keys():
                addip6_gateway(new_entity_id, args['gatewayip'])

    except IpSubpoolNotEmptyException as not_empty_err:
        return Response(not_empty_err.description, status=405)
    except IpSubpoolAlreadyExistsException as exist_err:
        return Response(exist_err.description, status=405)
    except Exception as ex:
        return Response(ex.__str__(), status=400)

    keys = args.keys()
    options_message = []
    if new_entity_id is not None and 'DHCPServerip' in keys:
        dhcp_server = args['DHCPServerip']
        interfaces = get_server_interface_id(config_id)
        options_message += add_dhcp_servers(config_name, new_entity_id,
                                            dhcp_server, interfaces)

    if new_entity_id is not None and 'DNSServerip' in keys:
        dns_servers = args['DNSServerip']
        dns_view_name = config_name + '_view'
        interfaces = get_server_interface_id(config_id)
        options_message += add_dns_servers(config_id, config_name,
                                           new_entity_id, dns_servers,
                                           interfaces, dns_view_name)

    if new_entity_id is not None and 'ClientOptions' in keys:
        client_options = args['ClientOptions']
        is_ipv4 = True
        ip = pool_cidr.split('/')[0]
        if util.is_valid_ipv6_address(ip):
            is_ipv4 = False
        options_message += add_client_options(client_options, new_entity_id,
                                              is_ipv4)

    if len(options_message) > 0:
        return Response(convert_message_to_string(options_message), status=200)

    return Response(status=200)