Skip to content

API Examples

Za Wilgustus edited this page Dec 21, 2015 · 24 revisions

This page will document how to use the BigIP API:

Simple example of CRUD calls

Each of the following example builds on the previous.

Create

from f5.bigip import BigIP

# Create NAT

```Python
bigip = BigIP("HOSTNAME", "USERNAME", "PASSWORD")
nat_obj_1 = bigip.ltm.nat(partition='TestPartition', name='Nat1', translationAddress='1.2.3.4', originatingAddress='1.2.3.5')

Get an existing NAT

new_version_nat_obj_1 = nat_obj_1.read()

Update an existing NAT

nat_obj_1.update({KEY_TO_UPDATE1: NEW_VALUE1, KEY_TO_UPDATE2:NEW_VALUE2, ...})

Delete a Nat

nat_obj_1.delete()

nat_obj = bigip.ltm.create_nat() nats = bigip.ltm.get_nats() -- pool = bigip.ltm.create_pool() pools = bigip.ltm.get_pools() nat_obj = nats_obj.create(name='mynat', partition='Common', translationAddress='192.168.2.1', originatingAddress='192.168.1.1')

print nat_obj.name; # Print an attribute that we set print nat_obj.trafficGroup; # Print an attribute that BIGIP set by default

### READ
```Python
# Refresh the nat object with the current settings on the BIGIP
nat_obj.read()

# We can also read all of the nats on the device
nat_objs = bigip.ltm.nats.read(partition='Common')
for n in nat_objs:
    print n.name

UPDATE

# Set an attribute and update it
nat_obj.trafficGroup = "Common/newgroup"
nat_obj.update()

# Update using key/value pairs
nat_obj.update(trafficGroup='Common/anothernewgroup')

# Update attribute and override with update (key/value pairs always override current settings)
nat_obj.arp = 'Enabled'
nat_obj.update(arp='Disabled')

DELETE

nat_obj.delete()

# This should raise an exception
print nat_obj.name

##Example of BigIP Resource Creation The intent is that the client using the f5-common-python REST API can access BigIP uri's as Python objects. For example, a user that wants to create a new Nat instance on the BigIP device can use any of the three following equivalent patterns:

Preamble:

from f5.bigip import BigIP

bigip = BigIP("HOSTNAME", "USERNAME", "PASSWORD")

all examples should start with the preamble.

####1. Instantiate the object and the create it using keys/value pairs. In this example the user can create an empty nat object. When the user calls create() with the key/value pairs those keys are set as attributes for the object and then sent to the BIGIP device as the HTTP POST request's data. This will either successfully create the object on the BIGIP or raise an exception.

Note: Since this is using HTTP POST calling create() with a name and partition combination that already exists on the BIGIP will throw an exception.

nats =  bigip.ltm.nat
nat_obj = nats.create(parition="Common", name="SOMEUSER", originatingAddress='192.168.1.1', translationAddress='192.168.2.1')

The local Python object contains a complete representation of the JSON returned by calling HTTP GET against the corresponding uri on the BigIP. Elements of the JSON object are attributes of the Python object. This implies a second method for instantiating an ltm/nat service on the BigIP:

####3. One line creation of a new object In this example the nat object is instantiated and created in the same call. As with previous examples this will either create a new nat object on the BIGIP or throw an error.

nat_obj = bigip.ltm.nats.create(
    folder="Common",
    name="SOMEUSER",
    translationAddress='192.168.1.1',
    originatingAddress='192.168.2.1')

The Transaction Model

The API operates on the device using the "Create, Read, Update, Delete" meta-pattern (implemented via HTTP verbs). Data is serialized, and transmitted, as JSON objects, and manipulated by the client as corresponding Python objects.

Transactions with the Device's REST Server either raise an IncompleteTransaction Exception (Not Yet Implemented), or result in the calling Python object being updated with the JSON returned from the transaction.

Plural Resources

Because the BigIP REST Server provides uri's that map to "singular" resource types (e.g. "ltm/nat" as opposed to "ltm/nats", it is necessary to make a design decision in the client API about how to handle plural resources. We have opted to include logic for "plural" operations in the parent Resource of the singular object.

For example invoke the following to get all ltm nats from the Device REST Server:

nats = bigip.ltm.nats.read()
Clone this wiki locally