Search code examples
apiibm-cloud-infrastructure

SoftLayer API to know and total and available IPs in a VLAN


SoftLayer API to know and total and available IPs in a VLAN

Hello,

Which API can be used to know the total IPs and the used/usable IPs of a VLAN if I know the VLAN ID.

One way I could figure out is I can get subnets of a VLAN and then in subnet details I can see total and usable IPs with "totalIpAddresses,usableIpAddressCount" attributes . But then I will have to get sum of total and usable IPs for a VLAN since a VLAN has multiple subnets. Not sure if this is the correct way.

Thanks


Solution

  • To get the information for the vlan about its subnets with the total used/usable IPs, try the following Python Script.

    This script will help to get the exact number of free slots from subnets inside the vlan.

    """
    This script retrieves the Total Ip Addresses, Usable Ip Address Count and Free Slots for an specific Vlan Id
    
    Important manual pages:
    http://sldn.softlayer.com/reference/services/SoftLayer_Account/getNetworkVlans
    http://sldn.softlayer.com/article/object-masks
    http://sldn.softlayer.com/article/object-filters
    
    License: http://sldn.softlayer.com/article/License
    Author: SoftLayer Technologies, Inc. <sldn@softlayer.com>
    """
    import SoftLayer
    from prettytable import PrettyTable
    
    # Declare your SoftLayer username and apiKey
    username = 'set me'
    apikey = 'set me'
    
    # Define the vlan Id
    vlanId = 318734
    
    # Contact To SoftLayer
    client = SoftLayer.Client(username=username, api_key=apikey)
    
    # Declare an Object Mask to get additional information
    object_mask = 'mask[primaryRouter,primarySubnet[datacenter[name]],subnets[billingItem, subnetType,networkIdentifier, cidr, totalIpAddresses, usableIpAddressCount, ipAddresses[ipAddress, isReserved, virtualGuest, hardware]]]'
    # Declare an Object Filter to get information from specific vlan
    filter = {'networkVlans': {'id': {'operation': vlanId}}}
    
    
    result = client['SoftLayer_Account'].getNetworkVlans(mask=object_mask, filter=filter)
    x = PrettyTable(["Vlan Id", "Vlan Number", "Subnet", "Total Ip Addresses", "Usable Ip Address Count","Free Slots"])
    
    count = 0
    for vlan in result:
        for subnet in vlan['subnets']:
            for item in subnet['ipAddresses']:
                if item['isReserved'] == True:
                    count = count + 1
                if 'hardware' in item:
                    count = count + 1
                if 'virtualGuest' in item:
                    count = count + 1
            if (subnet['usableIpAddressCount'] - count) > 0:
                if subnet['subnetType'] == 'PRIMARY' or subnet['subnetType'] == 'ADDITIONAL_PRIMARY':
                    x.add_row([vlan['id'], str('%s  %s' % (vlan['primaryRouter']['hostname'], vlan['vlanNumber'])), str('%s/%s' % (subnet['networkIdentifier'], subnet['cidr'])), subnet['totalIpAddresses'], subnet['usableIpAddressCount'], (subnet['usableIpAddressCount'] - count)])
            count = 0
    print(x)
    

    References: SoftLayer_Account::getNetworkVlans