Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 4.0

This page contains scripts that administrators can use to interact with FOAM via curl.

Code Block
borderStylesolid
titlechange_foam_fvinfo.py
#!/usr/bin/python

import urllib
import urllib2
import json
from optparse import OptionParser
import getpass
import sys
 
URL = "https://%s:3626/admin/%s"
 
def addCommonOpts (parser):
  parser.add_option("-n", "--name", dest="host", default="localhost")
  parser.add_option("--user", dest="C_user", default="foamadmin")
  parser.add_option("--passwd-file", dest="C_passwdfile", default=None)
 
def connect (url, user, passwd, data = None):
  try:
    passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
    passman.add_password(None, url, user, passwd)
    authhandler = urllib2.HTTPBasicAuthHandler(passman)
    opener = urllib2.build_opener(authhandler)
 
    if data is not None:
      data = json.dumps(data)
 
      h = {"Content-Type" : "application/json"}
      req = urllib2.Request(url, data, h)
      ph = opener.open(req)
      return ph.read()
    else:
      req = urllib2.Request(url)
      ph = opener.open(req)
      return ph.read()
 
  except urllib2.HTTPError, e:
    if e.code == 401:
      print "Basic auth failed: invalid password"
      sys.exit(1)
    elif e.code == 504:
      print "HTTP Error 504: Gateway Time-out"
      sys.exit(1)
    else:
      print e
  except RuntimeError, e:
    print e
 
def outputJSON (data, exit=True):
  j = json.loads(data)
  try:
    if j["retcode"] != 0:
      print j["msg"]
      if j["value"].has_key("missing"):
        print "Missing: %s" % (j["value"]["missing"])
        print "Incorrect Type: %s" % (j["value"]["wrong-type"])
      if exit:
        sys.exit(1)
      return
  except Exception:
    pass
 
  if j["value"] is None:
    sys.exit()
 
  json.dump(j["value"], sys.stdout, indent=1)
  print
  if exit:
    sys.exit()
 
def getPassword (opts):
  if opts.C_passwdfile is None:
    passwd = getpass.getpass("Password: ")
  else:
    passwd = open(opts.C_passwdfile, "r").read().strip()
  return passwd
 
if __name__ == '__main__':
  parser = OptionParser()
  addCommonOpts(parser)
  (opts, args) = parser.parse_args()
 
  passwd = getPassword(opts)
 
  fv_host = raw_input("FlowVisor Hostname: ")
  if not fv_host:
    print "You must supply a flowvisor hostname."
 
  fv_xmlrpc = raw_input("FlowVisor XMLRPC Port [8080]: ")
  if fv_xmlrpc:
    fv_xmlrpc = int(fv_xmlrpc)
  else:
    fv_xmlrpc = 8080
 
  fv_jsonrpc = raw_input("FlowVisor JSON RPC Port [8081]: ")
  if fv_jsonrpc:
    fv_jsonrpc = int(fv_jsonrpc)
  else:
    fv_jsonrpc = 8081
 
  fv_passwd = getpass.getpass("fvadmin user password: ")
 
  fv_info = {"host" : fv_host, "xmlrpc_port" : fv_xmlrpc, "json_port" : fv_jsonrpc,
             "passwd" : fv_passwd}
 
 
  url = URL % (opts.host, "set-config")
  obj = {"key" : "flowvisor_info", "value" : fv_info}
  data = connect(url, opts.C_user, passwd, obj)
  outputJSON(data)