...
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?
Expand |
---|
We broke this up in a few steps so it's easier to link together. 1) Let's create a Resource Holder called "Ned" Code Block |
---|
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 Code 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 Code Block |
---|
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 Code Block |
---|
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" Code Block |
---|
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" Code Block |
---|
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 Code Block |
---|
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 Code Block |
---|
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)) |
|
...