admin.py
Go to the documentation of this file.
1 
2 from samweb_client import json, convert_from_unicode, Error
3 from samweb_client.client import samweb_method
4 from samweb_client.http_client import escape_url_path, SAMWebHTTPError, HTTPNotFound
5 
6 @samweb_method
7 def listApplications(samweb, **queryCriteria):
8  result = samweb.getURL('/values/applications', queryCriteria)
9  return convert_from_unicode(result.json())
10 
11 @samweb_method
12 def addApplication(samweb, family, name, version):
13  return samweb.postURL('/values/applications', {"family":family, "name":name, "version":version}, secure=True, role='*').text.rstrip()
14 
15 @samweb_method
16 def listUsers(samweb):
17  result = samweb.getURL('/users')
18  return convert_from_unicode(result.json())
19 
20 @samweb_method
21 def _describeUser(samweb, username, format=None):
22  params = {}
23  if format:
24  params['format'] = format
25  return samweb.getURL('/users/name/%s' % escape_url_path(username), params)
26 
27 @samweb_method
28 def describeUser(samweb, username):
29  result = samweb._describeUser(username)
30  return convert_from_unicode(result.json())
31 
32 @samweb_method
33 def describeUserText(samweb, username):
34  result = samweb._describeUser(username, format='plain')
35  return result.text.rstrip()
36 
37 @samweb_method
38 def addUser(samweb, username, firstname=None, lastname=None, email=None, uid=None, groups=None):
39 
40  userdata = { 'username' : username }
41  if firstname: userdata['first_name'] = firstname
42  if lastname: userdata['last_name'] = lastname
43  if email: userdata['email'] = email
44  if uid is not None:
45  try:
46  uid = int(uid)
47  except ValueError:
48  raise ArgumentError("Invalid value for uid: %s" % uid)
49  else:
50  userdata['uid'] = uid
51  if groups:
52  userdata["groups"] = groups
53  return samweb.postURL('/users', data=json.dumps(userdata), content_type='application/json', secure=True, role='*').text.rstrip()
54 
55 @samweb_method
56 def modifyUser(samweb, username, **args):
57  return samweb.postURL('/users/name/%s' % escape_url_path(username), data=json.dumps(args), content_type='application/json', secure=True).text.rstrip()
58 
59 @samweb_method
60 def getAvailableValues(samweb):
61  """ get the available names that can be used with listValues, addValues """
62  return convert_from_unicode(samweb.getURL('/values?list=generic').json())
63 
64 @samweb_method
65 def listValues(samweb, vtype):
66  """ list values from database. This method tries to be generic, so vtype is
67  passed directly to the web server
68  arguments:
69  vtype: string with values to return (ie data_tiers, groups)
70  """
71  try:
72  return convert_from_unicode(samweb.getURL('/values/%s' % escape_url_path(vtype)).json())
73  except HTTPNotFound, ex:
74  raise Error("Unknown value type '%s'" % vtype)
75 
76 @samweb_method
77 def addValue(samweb, vtype, *args, **kwargs):
78  """ Add a new value to the database
79  arguments:
80  vtype: string giving the type of value to add ( ie data_tiers, groups)
81  *args: arguments to pass to server
82  **kwargs: keyword arguments to pass to server
83  """
84  # build the provided arguments into a parameter list. The args sequence is turned into multiple
85  # "value" parameters
86  postdata = dict(kwargs)
87  if args: postdata["value"] = [str(i) for i in args]
88  try:
89  return samweb.postURL('/values/%s' % escape_url_path(vtype), postdata, secure=True, role='*')
90  except HTTPNotFound, ex:
91  raise Error("Unknown value type '%s'" % vtype)
92 
93 @samweb_method
94 def listParameters(samweb):
95  """ list defined parameters """
96  return convert_from_unicode(samweb.getURL('/values/parameters').json())
97 
98 @samweb_method
99 def listParameterValues(samweb, param):
100  """ list the values for the given parameter
101  arguments:
102  param: parameter name
103  """
104  result = samweb.getURL('/values/parameters/%s?format=json' % escape_url_path(param))
105  return convert_from_unicode(result.json())
106 
107 @samweb_method
108 def addParameter(samweb, name, data_type, category=None):
109  """ Add a new parameter to the database
110  arguments:
111  name: Parameter name (either just the name, or category.name)
112  datatype: data type for new parameter
113  """
114  data = { 'name' : name, 'data_type' : data_type }
115  if category: data['category'] = category
116  return samweb.postURL('/values/parameters', data, secure=True, role='*')
117 
118 @samweb_method
119 def listDataDisks(samweb):
120  """ list defined data disks """
121  return convert_from_unicode(samweb.getURL('/values/data_disks').json())
122 
123 @samweb_method
124 def addDataDisk(samweb, mount_point):
125  """ Add a new data disk to the database
126  arguments:
127  mount_point: The mount point for the new disk
128  """
129  return samweb.postURL('/values/data_disks', {'mountpoint': mount_point}, secure=True, role='*')
130 
def _describeUser(samweb, username, format=None)
Definition: admin.py:21
def modifyUser(samweb, username, args)
Definition: admin.py:56
def addParameter(samweb, name, data_type, category=None)
Definition: admin.py:108
def listDataDisks(samweb)
Definition: admin.py:119
def getAvailableValues(samweb)
Definition: admin.py:60
def addApplication(samweb, family, name, version)
Definition: admin.py:12
def describeUser(samweb, username)
Definition: admin.py:28
def addDataDisk(samweb, mount_point)
Definition: admin.py:124
def listApplications(samweb, queryCriteria)
Definition: admin.py:7
def describeUserText(samweb, username)
Definition: admin.py:33
def addValue(samweb, vtype, args, kwargs)
Definition: admin.py:77
def convert_from_unicode(input)
Definition: __init__.py:14
def listValues(samweb, vtype)
Definition: admin.py:65
def listParameters(samweb)
Definition: admin.py:94
def listParameterValues(samweb, param)
Definition: admin.py:99
def listUsers(samweb)
Definition: admin.py:16
def addUser(samweb, username, firstname=None, lastname=None, email=None, uid=None, groups=None)
Definition: admin.py:38