You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 9 Next »

 If you want to get a jumpstart on common API use cases, you came to the right place! Expand the text areas below for walkthroughs and code samples of API calls...

Context: I unassigned an IP address and now it's in the Holding Tank. Now I want to assign an IP from the Holding Tank. I don't want to unassign an IP randomly, in case it is allocated to a Resource. What are my options?

There are 3 options:

1) If you know the specific IP, you can use use the ipam-get api call to determine if it is in Holding:

/api/v1/api.php?target=ipam&action=get&cidr=1.2.3.4/32

{
  id:1234,
  cidr:"1.2.3.4",
  ...
  resource_name:"Holding"
}


2) If you want to show all blocks/IPs in Holding, you can use the following ipam-get API call:

/api/v1/api.php?target=ipam&action=get&resourceQuery={"name":"Holding"}


3) If you know the block is in Holding, you can issue another ipam-unassign API call to move it from Holding to Available:

/api/v1/api.php?target=ipam&action=unassign&block=1.2.3.4/32

Context: I need to create a Resource Holder, assign them an IP block, then subassign some IPs out of that block to two new Resource Holders. What does this look like in Python?

We broke this up in a few steps so it's easier to link together.

1) Let's create a Resource Holder called "Ned"

query_string    = 'target=resource&action=add&meta[type]=entry&meta[section]=resource-holder&meta[name]=Ned'
query_string    += '&apiKey=' + api_key
hash            = base64.b64encode( hmac.new(api_secret_key, query_string, hashlib.sha256).digest() )
url                = base_url + '?' + query_string + '&hash=' + hash
print 'Create Ned resource holder'
print url, "\n"
data = json.load(urllib2.urlopen(url))
ned_resource_id = data['data']['id']

2) Now let's add the 213.29.27.0/24 IP block

query_string =    'target=ipam&action=add&rir=RIPE&block=213.29.27.0/24'
query_string    += '&apiKey=' + api_key
hash            = base64.b64encode( hmac.new(api_secret_key, query_string, hashlib.sha256).digest() )
url                = base_url + '?' + query_string + '&hash=' + hash
print 'Create 213.29.27.0/24 block'
print url, "\n"
data = json.load(urllib2.urlopen(url))

3) With the block in the system, we can assign 213.29.27.0/24 to "Ned" the Resource Holder

query_string =    "target=ipam&action=directAssign&block=213.29.27.0/24&resourceId=%d" % (ned_resource_id)
query_string    += '&apiKey=' + api_key
hash            = base64.b64encode( hmac.new(api_secret_key, query_string, hashlib.sha256).digest() )
url                = base_url + '?' + query_string + '&hash=' + hash
print 'Assign 213.29.27.0/24 block to Ned'
print url, "\n"
data = json.load(urllib2.urlopen(url))

4) Since we plan on assigning IPs out of this block, we should enable subassignments for 213.29.27.0/24

query_string =    'target=ipam&action=update&block=213.29.27.0/24&allowSubAssignments=true'
query_string    += '&apiKey=' + api_key
hash            = base64.b64encode( hmac.new(api_secret_key, query_string, hashlib.sha256).digest() )
url                = base_url + '?' + query_string + '&hash=' + hash
print 'Update 213.29.27.0/24 to allow sub assignments'
print url, "\n"
data = json.load(urllib2.urlopen(url))

5) Now let's create a Resource Holder "Tara"

query_string =    "target=resource&action=add&meta[type]=entry&meta[section]=resource-holder&meta[name]=Tara&meta[parent_id]=%d" % (ned_resource_id)
query_string    += '&apiKey=' + api_key
hash            = base64.b64encode( hmac.new(api_secret_key, query_string, hashlib.sha256).digest() )
url                = base_url + '?' + query_string + '&hash=' + hash
print 'Create Tara resource holder'
print url, "\n"
data = json.load(urllib2.urlopen(url))
tara_resource_id = data['data']['id']

6) To keep it interesting, let's create another Resource Holder "Una"

query_string =    "target=resource&action=add&meta[type]=entry&meta[section]=resource-holder&meta[name]=Una&meta[parent_id]=%d" % (ned_resource_id)
query_string    += '&apiKey=' + api_key
hash            = base64.b64encode( hmac.new(api_secret_key, query_string, hashlib.sha256).digest() )
url                = base_url + '?' + query_string + '&hash=' + hash
print 'Create Una resource holder'
print url, "\n"
data = json.load(urllib2.urlopen(url))
una_resource_id = data['data']['id']

7) Assign a /28 block from Ned's 213.29.27.0/24 to Tara

query_string =    "target=ipam&action=smartAssign&type=ipv4&rir=RIPE&mask=28&&resourceId=%d&assignedResourceId=%d" % (tara_resource_id, ned_resource_id)
query_string    += '&apiKey=' + api_key
hash            = base64.b64encode( hmac.new(api_secret_key, query_string, hashlib.sha256).digest() )
url                = base_url + '?' + query_string + '&hash=' + hash
print 'Assign block from Ned\'s 213.29.27.0/24 to Tara'
print url, "\n"
data = json.load(urllib2.urlopen(url))

8) Then assign another /28 block from Ned's 213.29.27.0/24 to Una

query_string =    "target=ipam&action=smartAssign&type=ipv4&rir=RIPE&mask=28&&resourceId=%d&assignedResourceId=%d" % (una_resource_id, ned_resource_id)
query_string    += '&apiKey=' + api_key
hash            = base64.b64encode( hmac.new(api_secret_key, query_string, hashlib.sha256).digest() )
url                = base_url + '?' + query_string + '&hash=' + hash
print 'Assign block from Ned\'s 213.29.27.0/24 to Una'
print url, "\n"
data = json.load(urllib2.urlopen(url))


 

  • No labels