Previous Topic: Discovery URIs


Curl and Python Examples

This section contains the following topics:

Authentication Examples

Grid List Examples

Details for a Single Grid

Grid Create Examples

Grid Operation Examples

Grid Update Examples

Grid Delete Examples

Authentication Examples

The following examples illustrate how to create an authentication session in the BFC API.

Curl Example:

%export BFC_HOST=10.10.x.x
%curl  -k https://$BFC_HOST:8443/BFC/1.0/login -X POST \
-d  "{\"username\":\"core/admin\",\"password\":\"changeme\"}" \
-H "Content-Type:application/json"
RESPONSE= <session token>  i.e. "d147eb4031217717c5af68edf33cd40c"%
%export BFC_SESSION= d147eb4031217717c5af68edf33cd40c

Python Example:

from httplib import HTTPSConnection
import json
bfcHost = "10.10.x.x"
conn = HTTPSConnection(bfcHost + ':8443')
conn.connect()
conn.request(method='POST', url='/BFC/1.0/login', 
             body='{"username":"admin","password":"changeme"}', 
             headers={'Content-Type':'application/json'})
session = json.loads(conn.getresponse().read())
print session

Grid List Examples

The following examples illustrate how to retrieve information about a set of grids.

Curl Example:

curl -k -v https://$BFC_HOST:8443/BFC/grids \
  -H "Content-Type:application/json" \
  -H "Authorization:$BFC_SESSION"

Python Example:

session = get_session()
conn = HTTPSConnection(bfcHost + ':8443')
conn.connect()
conn.request(method='GET',
             url='/BFC/grids',
             headers={'Content-Type':'application/json',
                      'Authorization':session})
grids = json.loads(conn.getresponse().read())
print grids

Details for a Single Grid

The following examples illustrate how to retrieve information about a single grid.

Curl Example:

curl -k -v https://$BFC_HOST:8443/BFC/grids/$GRID_NAME \
  -H "Content-Type:application/json" \
  -H "Authorization:$BFC_SESSION"

Python Example:

session = get_session()
gridName = 'productionApiTest'
conn = HTTPSConnection(bfcHost + ':8443')
conn.connect()
conn.request(method='GET',
             url='/BFC/grids/' + gridName,
             headers={'Content-Type':'application/json',
                      'Authorization':session})
grid = json.loads(conn.getresponse().read())
print grid

Grid Create Examples

The following examples illustrate how to create a grid in the BFC API.

Curl Example:

curl -k -v -X POST "https://$BFC_HOST:8443/BFC/grids?numAppIPs=3" \
  -H "Content-Type:application/json" \
  -H "Authorization:$BFC_SESSION" \
  -d "{\"grid\":{\"name\":\"productionApiTest\",
                 \"xen_config\":{\"grid_server_configuration\":{\"min\":1,
                                                   \"target\":1,
                                                   \"max\":1,
                                                   \"selection_criteria\":[]}},
                 \"grid_controller_user\":\"cadev@ca.com\",
                 \"grid_controller_password\":\"myPass\",
                 \"description\":\"Xen grid for demo\"}}"

Python Example:

session = get_session()
conn = HTTPSConnection(bfcHost + ':8443')
conn.connect()
conn.request(method='POST',
             url='/BFC/grids?numAppIPs=2',
             headers={'Content-Type':'application/json',
                      'Authorization':session},
             body = json.dumps({"grid":{"name":"productionApiTest",
                 "xen_config":{"grid_server_configuration":{
                        "min":1,
                        "target":1,
                        "max":2,
                        "selection_criteria":[]}},
                 "grid_controller_user":"cadev@ca.com",
                 "grid_controller_password":"c0tr011er",
                 "description":"Xen grid for demo"}}))

resp = conn.getresponse()
if resp.status > 201:
    print 'Failed grid create - '
    print json.loads(resp.read())
else:
    print 'Grid created'

Grid Operation Examples

You can perform the following operations on the grid: start, stop, reboot, clearFailure, applyHotfixes, removeNodes. The following examples illustrate how to start a grid in the BFC API. Replace the start parameter with another parameter for the other grid operations.

Curl Example:

curl -k -v -X POST https://$BFC_HOST:8443/BFC/grids/$GRID_NAME/operations/start \
  -H "Content-Type:application/json" \
  -H "Authorization:$BFC_SESSION"

Python Example:

session = get_session()
conn = HTTPSConnection(bfcHost + ':8443')
gridName = 'productionApiTest'
operation = 'start'
conn.connect()
conn.request(method='POST',
             url='/BFC/grids/' + gridName + "/operations/" + operation ,
             headers={'Content-Type':'application/json',
                      'Authorization':session})

resp = conn.getresponse()
if resp.status > 202:
    print 'Failed grid operation  - '
    print json.loads(resp.read())
else:
    print 'Operation successful'

Grid Update Examples

The following examples illustrate a grid update in which the description changes and the xen target node count changes from 1 to 2.

Curl Example:

curl -k -v -X PUT https://$BFC_HOST:8443/BFC/grids/$GRID_NAME \
  -H "Content-Type:application/json" \
  -H "Authorization:$BFC_SESSION" \
  -d "{\"grid\":{\"description\":\"New Description\",
                 \"xen_config\":{\"grid_server_configuration\":{\"min\":1,
                                                   \"target\":2,
                                                   \"max\":2,
						   \"selection_criteria\":[]}}}}"

Python Example:

session = get_session()
conn = HTTPSConnection(bfcHost + ':8443')
gridName = 'productionApiTest'
conn.connect()
conn.request(method='PUT',
             url='/BFC/grids/' + gridName,
             headers={'Content-Type':'application/json',
                      'Authorization':session},
             body = json.dumps({"grid":{
                 "xen_config":{"grid_server_configuration":{
                        "min":1,
                        "target":2,
                        "max":2,
                        "selection_criteria":[]}},
                 "description":"New Description"}}))

resp = conn.getresponse()
if resp.status > 201:
    print 'Failed grid create - '
    print json.loads(resp.read())
else:
    print 'Grid created'

Grid Delete Examples

The following examples illustrate how to delete a grid using the BFC API.

Curl Example:

curl -k -v -X DELETE https://$BFC_HOST:8443/BFC/grids/$GRID_NAME \
  -H "Content-Type:application/json" \
  -H "Authorization:$BFC_SESSION"

Python Example:

session = get_session()
gridName = 'productionApiTest'
conn = HTTPSConnection(bfcHost + ':8443')
conn.connect()
conn.request(method='DELETE',
             url='/BFC/grids/' + gridName,
             headers={'Content-Type':'application/json',
                      'Authorization':session})
resp = conn.getresponse()
if resp.status > 202:
    print 'Failed grid delete - '
    print json.loads(resp.read())
else:
    print 'Grid deleted'