vADC Docs

Tech Tip: Creating a new service with the REST API and Python

by on ‎03-25-2013 11:14 AM (1,403 Views)

This script illustrates how to create a new Stingray service that forwards traffic to www.riverbed.com.  The service consists of the following three configuration objects:

  • A virtual server, listening on port 8080, that runs a rule, then selects a pool
  • A rule that calls http.setHeader( "Host", "www.riverbed.com" ); to 'correct' the host header in the request
  • A pool that directs traffic to www.riverbed.com:80

Screen Shot 2013-03-25 at 18.03.31.png

Note that when you provision configuration objects in Stingray using REST, most of the properties can take sensible default values.  You only need to specify the values you explictly want to set.


import requests


import json



# FIXME: make sure that the URL below is correct for your Stingray deployment


url = 'https://stingray:9070/api/tm/1.0/config/active'



name = 'web site'



# Pool configuration


poolname = name + ' pool'


pool = {


   'properties' : {


      'basic' : { 'nodes' : [ 'www.riverbed.com:80' ] }


   }


}



# rule configuration (this is a HERE document)


rulename = name + ' rule'


rule = '''


http.setHeader( "Host", "www.riverbed.com" );


'''



# vserver configuration


vsname = name


vs = {


   'properties' : {


      'basic' : {


         'port' : 8080, 'protocol' : 'HTTP', 'pool' : poolname, 'request_rules' : [ rulename ], 'enabled' : True


      }


   }


}



# FIXME: update the authentication information in the three REST calls below


print requests.put( url + '/pools/' + poolname,  data=json.dumps( pool ), auth=('admin','admin'), verify=False, headers={'content-type': 'application/json'} )


print requests.put( url + '/rules/' + rulename,  data=rule,               auth=('admin','admin'), verify=False )


print requests.put( url + '/vservers/' + vsname, data=json.dumps( vs ),   auth=('admin','admin'), verify=False, headers={'content-type': 'application/json'} )


Note the order in which the objects are created: the virtual server references the pool and rule, so to avoid errors, we create the rule and pool first.

You can delete this service in a similar fashion:


import requests



url = 'https://stingray:9070/api/tm/1.0/config/active'



name = 'web site'



poolname = name + ' pool'


rulename = name + ' rule'


vsname = name



print requests.delete( url + '/vservers/' + vsname,auth=('admin','admin'),verify=False,headers={'content-type': 'application/json'} )


print requests.delete( url + '/rules/' + rulename,auth=('admin','admin'),verify=False,headers={'content-type': 'application/json'} )


print requests.delete( url + '/pools/' + poolname,auth=('admin','admin'),verify=False,headers={'content-type': 'application/json'} )


As a matter of style, you can use Python string formats and a requests Session object to present the code in a slightly different fashion:


import requests


import json



name = 'web site'



# Pool configuration


poolname = name + ' pool'


pool = {


   'properties' : {


      'basic' : { 'nodes' : [ 'www.riverbed.com:80' ] }


   }


}



# rule configuration (this is a HERE document)


rulename = name + ' rule'


rule = '''


http.setHeader( "Host", "www.riverbed.com" );


'''



# vserver configuration


vsname = name


vs = {


   'properties' : {


      'basic' : {


         'port' : 8080, 'protocol' : 'HTTP', 'pool' : poolname, 'request_rules' : [ rulename ], 'enabled' : True


      }


   }


}



# FIXME: make sure that the URL is correct for your Stingray deployment


url = 'https://stingray-1:9070/api/tm/1.0/config/active/%s/%s'


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



#FIXME: ensure that the authentication details are appropriate


s = requests.Session()


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


s.verify = False



print s.put( url % ( 'pools', poolname),  data = json.dumps( pool ), headers = jsontype )


print s.put( url % ( 'rules', rulename),  data = rule )


print s.put( url % ( 'vservers', vsname), data = json.dumps( vs ), headers = jsontype )


The method you use is of course just a matter of style and personal taste.

For more details on using Python's requests library, check out Tech Tip: Using the RESTful Control API with Python - Overview