Python Rest

Requests

this replace urllib3 / urllib2 because it is more simplest
official documentation on http://docs.python-requests.org/en/latest/

use the certificates to authenticate from
http://stackoverflow.com/questions/17576324/python-requests-ssl-error-for-client-side-cert

import requests
cert_file_path = "cert.pem"
key_file_path = "key.pem"

url = "https://example.com/resource"
params = {"param_1": "value_1", "param_2": "value_2"}
cert = (cert_file_path, key_file_path)
r = requests.get(url, params=params, cert=cert, verify=False)

Unirest

http://unirest.io/python.html
another library multi language

Library for puppet

https://github.com/daradib/pypuppet
a library to call the rest api for puppet , it use urllib2

instaclustr.com restapi usage

I need to use the "Provisioning API" of intracluster to automate some actions https://support.instaclustr.com/hc/en-us/articles/213671118-Provisioning-API
To do that I used python requests and json library

prerequisite you need to install the requests library

 pip install requests
Downloading/unpacking requests
  Downloading requests-2.11.1-py2.py3-none-any.whl (514kB): 514kB downloaded

some examples:

BASIC GET REQUEST

extract the cluster list

import requests
url = 'https://api.instaclustr.com/provisioning/v1/'
username = 'xxxx'
password = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'

r = requests.get(url , auth=(username, password))
print "Status Code is:"
print r.status_code
print "Body is:"
print r.json()

result
Status Code is:
200
Body is:
[{u'runningNodeCount': 3, u'name': u'myfirstcluster', u'nodeCount': 3, u'cassandraVersion': u'apache-cassandra-2.1.11', u'derivedStatus': u'RUNNING', u'id': u'AAAAAA-bbbbbb-4456-cccccc66666'}, {u'runningNodeCount': 3, u'name': u'mysecondcluster', u'nodeCount': 3, u'cassandraVersion': u'apache-cassandra-2.1.11', u'derivedStatus': u'RUNNING', u'id': u'cccccccc-bbbbbb-4456-ccddddd66666'}]

GET REQUEST WITH JSON ACCESS

extract the private ip address of the cluster

import requests
import simplejson as json

serviceurl = "https://api.instaclustr.com/provisioning/v1/"
clusterid = "AAAAAA-bbbbbb-4456-cccccc66666"
finalurl = (serviceurl+clusterid)
username = 'XXXXXX'
password = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXX'

r = requests.get(finalurl , auth=(username, password))
print "Status Code is:"
print r.status_code
print "Body is:"
result = r.json()
print(result['dataCentres'][0]['nodes'][0]['privateAddress'])
print(result['dataCentres'][0]['nodes'][1]['privateAddress'])
print(result['dataCentres'][0]['nodes'][2]['privateAddress'])

result
Status Code is:
202
Body is:
10.22.36.9
10.22.143.39
10.22.91.5

POST REQUEST

create a vpc peer

import requests
import json
from requests.auth import HTTPBasicAuth
serviceurl = 'https://api.instaclustr.com/provisioning/v1/vpc-peering/'
datacenterid = "iiiiiiii-0f28-00000-9772-77c20aaaaaa"
finalurl = (serviceurl+datacenterid)
username = 'xxxxxxx'
password = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'

print finalurl
payload = {"peerVpcId": "vpc-aaabbb111222", "peerAccountId": "01234567890", "peerSubnet": "10.0.0.0/16"}

r = requests.post(finalurl , auth=HTTPBasicAuth(username, password), data=json.dumps(payload) )

print "Status Code is:"
print r.status_code
print "Body is:"
print r.json()
Salvo diversa indicazione, il contenuto di questa pagina è sotto licenza Creative Commons Attribution-ShareAlike 3.0 License