Infoblox Client for Python

repository·master·Indexed 40 days ago

https://github.com/infobloxopen/infoblox-client

A Python client for interacting with Infoblox NIOS via the WAPI. It provides a low-level connector for raw dictionary access and a high-level object-oriented API for managing NIOS resources such as Network Views, DNS Zones, and Host Records. As of version 0.6.2, all NIOS objects are supported.

Tokens
3.3K
Snippets
15
Records
17
Agent score
100%

What's inside Infoblox Client

  1. Use the high-level API with NIOS objects

    master

    The high-level API uses specialized object classes (e.g., NetworkView, Network, HostRecord) to perform CRUD operations. This approach parses NIOS responses back into Python objects.

    from infoblox_client import connector
    from infoblox_client import objects
    
    opts = {'host': '192.168.1.10', 'username': 'admin', 'password': 'admin'}
    conn = connector.Connector(opts)
    
    # Create a network view and network
    nview = objects.NetworkView.create(conn, name='my_view')
    network = objects.Network.create(conn, network_view='my_view', cidr='192.168.1.0/24')
    
    # Create a DNS view and zone
    view = objects.DNSView.create(conn, network_view='my_view', name='my_dns_view')
    zone = objects.DNSZone.create(conn, view='my_dns_view', fqdn='my_zone.com')
    
    # Create a host record with an IP object
    my_ip = objects.IP.create(ip='192.168.1.25', mac='aa:bb:cc:11:22:33')
    hr = objects.HostRecord.create(conn, view='my_dns_view', name='my_host_record.my_zone.com', ip=my_ip)
  2. Run end-to-end (E2E) tests

    master

    The E2E tests are designed to validate functionality against an existing, running WAPI instance. To run them, you must first export the connection parameters for your WAPI instance as environment variables, then execute the tests using a test runner like unittest.

    Warning: Do not run these tests against a production WAPI instance.

    export WAPI_HOST=<WAPI HOST IP> WAPI_USER=<WAPI USERNAME> WAPI_PASS=<WAPI PASSWORD>
    python3 -m unittest e2e_tests.test_objects
  3. Search by regular expression using the '~' operator

    master

    Partial match searching via regular expressions is currently supported only by the low-level API. To perform a regex search, append the tilde character (~) to the field name in your query dictionary.

    Note: Not all fields support regular expression searching. You should consult the wapidoc for a complete list of searchable fields.

    Examples

    Find all networks starting with '10.10.':

    conn = connector.Connector(opts)
    nw = conn.get_object('network', {'network~': '10.10.'})

    Find all host records starting with '10.10.':

    conn = connector.Connector(opts)
    hr = conn.get_object('record:host', {'ipv4addr~': '10.10.'})
  4. Create a FixedAddress with MS Server DHCP reservation

    master

    Use objects.FixedAddress.create_check_exists to create a fixed address. For Microsoft Server DHCP reservations, provide a dictionary for the ms_server parameter containing the _struct key set to 'msdhcpserver' and the ipv4addr.

    obj, created = objects.FixedAddress.create_check_exists(
        connector=conn,
        ip='192.168.100.100',
        mac='aa:bb:cc:11:22:33',
        comment='My DHCP reservation',
        name='My hostname',
        network_view='default',
        ms_server={'_struct': 'msdhcpserver', 'ipv4addr': '192.168.0.0'}
    )
  5. Search for objects using extensible attributes

    master

    You can search for objects based on their extensible attributes using the get_object method on a Connector instance. To include both the standard object fields and the extensible attributes in the response, include 'default' and 'extattrs' in the return_fields list.

    When constructing the search criteria for an extensible attribute, use the syntax f"*{extensible_attribute}:~": value within the arguments passed to get_object. This mimics the WAPI _return_fields+ behavior, ensuring you receive the basic fields in addition to the specific ones requested.

    from infoblox_client.connector import Connector
    
    # Setup connection
    opts = {'host': '192.168.1.10', 'username': 'admin', 'password': 'admin'}
    conn = Connector(opts)
    
    # Search for a 'network' object where the extensible attribute 'CustomerCode' is 'Infoblox'
    # The syntax f"*{extensible_attribute}:~" is used for the search criteria
    extensible_args = [
        "network",
        {
            "*CustomerCode:~": "Infoblox",
        }
    ]
    
    kwargs = {
        'return_fields': [
            'default',
            'extattrs',
        ]
    }
    
    result = conn.get_object(*extensible_args, **kwargs)
    print(result)
  6. Create HostRecords with Extensible Attributes (EA)

    master

    You can assign Extensible Attributes to a HostRecord using the extattrs parameter. You can also enable inheritance for attributes by setting use_for_ea_inheritance=True on the IP object.

    # Create host record with Extensible Attributes (EA)
    ea = objects.EA({'Tenant ID': tenantid, 'CMP Type': cmptype, 'Cloud API Owned': True})
    host = objects.HostRecord.create(conn, name='new_host', ip=my_ip, extattrs=ea)
    
    # Create host record with inherited Extensible Attributes (EA)
    my_ip = objects.IP.create(ip='192.168.1.25', mac='aa:bb:cc:11:22:33', use_for_ea_inheritance=True)
    hr = objects.HostRecord.create(conn, view='my_dns_view', name='my_host_record.my_zone.com', ip=my_ip)
  7. Allocate next available IP and create HostRecord

    master

    Use objects.IPAllocation.next_available_ip_from_cidr to find the next available IP in a specific CIDR, then use that IP to create a host record.

    next_ip = objects.IPAllocation.next_available_ip_from_cidr('default', '10.0.0.0/24')
    my_ip = objects.IP.create(ip=next_ip, mac='aa:bb:cc:11:22:33', configure_for_dhcp=True)
    host = objects.HostRecord.create(conn, name='some.valid.fqdn', view='Internal', ip=my_ip)