Solarwinds IPAM CRUD/Update using Orion/SDK

Note to myself for future use. Did this while troubleshooting Solarwinds IPAM IP reservation automation.

import orionsdk
import requests
from requests.adapters import HTTPAdapter
from requests.packages.urllib3.util.retry import Retry
from pprint import pprint

def retry_session(retries=3,
                  backoff_factor=0.3,
                  status_forcelist=(500, 502, 504)):
    session = requests.Session()
    retry = Retry(
        total=retries,
        read=retries,
        connect=retries,
        backoff_factor=backoff_factor,
        status_forcelist=status_forcelist)
    adapter = HTTPAdapter(max_retries=retry)
    session.mount('http://', adapter)
    session.mount('https://', adapter)
    return session
class VPNSolarWinds():
    def __init__(self,**kwargs):
        try:
            self.swis = orionsdk.SwisClient(kwargs["host"],
                                        kwargs["user"],
                                        kwargs["password"],
                                        session=retry_session())   #verify="server.pem",
        except Exception as e:
            print("connectionError: {}".format(e))
            
obj = VPNSolarWinds(user="admin",password="password@123",host="10.10.10.10") #sample lab solarwinds ipam tool


query = """ SELECT 
                ipn.subnetid,
                ipn.IPAddress, 
                ipn.Status, 
                ipn.Alias, 
                ipn.MAC, 
                ipn.DnsBackward, 
                ipn.DhcpClientName, 
                ipn.SysName, 
                ipn.Description, 
                ipn.Contact, 
                ipn.Location, 
                ipn.SysObjectID, 
                ipn.Vendor, 
                ipn.VendorIcon, 
                ipn.MachineType, 
                ipn.Comments, 
                ipn.ResponseTime, 
                ipn.LastBoot, 
                ipn.LastSync, 
                ipn.LastCredential, 
                ipn.AllocPolicy, 
                ipn.SkipScan, 
                ipn.LeaseExpires, 
                ipn.DnsBy, 
                ipn.MacBy, 
                ipn.StatusBy, 
                ipn.SystemDataBy,
                ipn.Uri 
            FROM IPAM.IPNode ipn JOIN IPAM.Subnet sbn 
            ON ipn.subnetid=sbn.subnetid 
            WHERE sbn.DisplayName='{subnet_name}' AND ipn.IPAddress='{ip}'""".format(subnet_name="some-subnet",ip="10.20.20.20")
# Added "Uri" to the above column names            


output = obj.swis.query(query)
uri = (output['results'][0]['Uri'])
# obj.swis.update(uri, Status='Reserved')
obj.swis.update(uri, Status='Used')
output_after = obj.swis.query(query)
status = (output['results'][0]['Status'])
print("Status:", status)
    
# pprint(results)
Reference: https://github.com/solarwinds/OrionSDK/wiki/IPAM-4.5.x-API#crud-operations-for-ip-address
#IPAM IP RESERVATION STATUS.
#Value Name
#0 Unknown
#1 Used
#2 Available
#4 Reserved
#8 Transient
#16 Blocked

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s