cancel
Showing results for 
Search instead for 
Did you mean: 

Using Python to add a node to a pool

SOLVED
Highlighted
Occasional Contributor

Using Python to add a node to a pool

Hi,

I am trying to write a Python script that will source the IP address of a new machine and then add it as a node to a pool, I am really new to Python and still getting my head around it.

So far this is what I have, at the moment I am just entering the IP address as a command line argument

#!/usr/bin/python

import requests

import json

import sys

pool_name = sys.argv[1]

add_node = sys.argv[2]

url = 'https://<URL>/' + pool_name 

jsontype = {'content-type': 'application/json'}

client = requests.Session()

client.auth = ('<username>', '<password>')

client.verify = 0

response = client.get(url)

pools = json.loads(response.content)

nodes = pools['properties']['basic']['nodes']

pools['properties']['basic']['nodes'].append(add_node)


client.put(url, <place I am stuck in>, headers = jsontype)

As you can see I have got to the point where I have everything I want but am having trouble putting the updated array back to the REST API.

If anyone can point me in the right direction that would be great.

Thanks,

Darren

2 REPLIES
Occasional Contributor

Re: Using Python to add a node to a pool

I have updated my script, this all looks fine but I get a <Response [400]>.

#!/usr/bin/python

import requests

import json

import sys

pool_name = sys.argv[1]

#add_node = sys.argv[2]

add_node = u'10.10.10.1:80'

url = '<uri>' + pool_name 

jsontype = {'content-type': 'application/json'}

client = requests.Session() 

client.auth = ('admin', 'admin') 

client.verify = 0 

 

response = client.get(url) 

pools = json.loads(response.content)

nodes = pools['properties']['basic']['nodes']

#updatepool['properties']['basic']['nodes'].append(add_node)

data = nodes

data.append(add_node)

print client.put(url,json.dumps(data), headers=jsontype)

Occasional Contributor

Re: Using Python to add a node to a pool

Got it to work, here my script for adding nodes using Python

#!/usr/bin/python

import requests

import json

import sys

pool_name = sys.argv[1]

add_node = unicode(sys.argv[2])


url = 'https://<uri>:9070/api/tm/1.0/config/active/pools/' + pool_name 

jsontype = {'content-type': 'application/json'}

client = requests.Session() 

client.auth = ('<username>', <password>') 

client.verify = 0 

 

response = client.get(url) 

pools = json.loads(response.content)

nodes = pools['properties']['basic']['nodes']

data = nodes

data.append(add_node)

print client.put(url,json.dumps(pools), headers=jsontype)

print nodes