2013-11-02 18:52:32 +00:00
|
|
|
#Copyright 2013 IBM All rights reserved
|
|
|
|
|
|
|
|
# This module implements client/server messages emitted from plugins.
|
|
|
|
# Things are defined here to 'encourage' developers to coordinate information
|
|
|
|
# format. This is also how different data formats are supported
|
2013-11-03 14:52:43 +00:00
|
|
|
import confluent.exceptions as exc
|
2013-11-02 18:52:32 +00:00
|
|
|
import json
|
|
|
|
|
2014-02-06 18:13:16 +00:00
|
|
|
|
2013-11-02 18:52:32 +00:00
|
|
|
class ConfluentMessage(object):
|
2013-11-03 17:30:22 +00:00
|
|
|
defaultvalue = ''
|
|
|
|
defaulttype = 'text'
|
2013-11-02 18:52:32 +00:00
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
raise NotImplementedError("Must be subclassed!")
|
|
|
|
|
|
|
|
def json(self):
|
|
|
|
# This will create the canonical json representation of this message
|
2013-11-03 22:07:17 +00:00
|
|
|
jsonsnippet = json.dumps(self.kvpairs, separators=(',', ':'))[1:-1]
|
|
|
|
return jsonsnippet
|
2013-11-02 18:52:32 +00:00
|
|
|
|
2014-02-11 00:36:18 +00:00
|
|
|
def raw(self):
|
2014-01-28 16:18:00 +00:00
|
|
|
"""Return pythonic representation of the response.
|
|
|
|
|
|
|
|
Used by httpapi while assembling data prior to json serialization"""
|
|
|
|
return self.kvpairs
|
|
|
|
|
2013-11-02 18:52:32 +00:00
|
|
|
def strip_node(self, node):
|
|
|
|
self.kvpairs = self.kvpairs[node]
|
|
|
|
|
|
|
|
def html(self):
|
|
|
|
#this is used to facilitate the api explorer feature
|
2013-11-02 22:37:26 +00:00
|
|
|
snippet = ""
|
|
|
|
for key in self.kvpairs.iterkeys():
|
2013-11-03 04:25:12 +00:00
|
|
|
val = self.kvpairs[key]
|
2013-11-03 17:30:22 +00:00
|
|
|
value = self.defaultvalue
|
|
|
|
type = self.defaulttype
|
2014-02-23 00:12:42 +00:00
|
|
|
notes = []
|
2014-02-20 00:10:58 +00:00
|
|
|
if val is not None and 'value' in val:
|
2013-11-03 04:25:12 +00:00
|
|
|
value = val['value']
|
2014-02-23 00:12:42 +00:00
|
|
|
if 'inheritedfrom' in val:
|
|
|
|
notes.append('Inherited from %s' % val['inheritedfrom'])
|
|
|
|
if 'expression' in val:
|
|
|
|
notes.append('Derived from expression "%s"' % val['expression'])
|
2014-03-03 19:48:46 +00:00
|
|
|
elif val is not None and 'expression' in val and 'broken' in val:
|
|
|
|
value = "*BROKEN*"
|
|
|
|
notes.append('Derived from expression "%s"' % val['expression'])
|
|
|
|
notes.append('Broken because of %s' % val['broken'])
|
2014-02-20 01:24:51 +00:00
|
|
|
elif val is not None and 'expression' in val:
|
|
|
|
value = val['expression']
|
2014-01-28 16:18:00 +00:00
|
|
|
if value is None:
|
|
|
|
value = ''
|
2014-02-20 00:10:58 +00:00
|
|
|
if val is not None and value == '' and 'isset' in val and val['isset'] is True:
|
2014-02-11 00:36:18 +00:00
|
|
|
# an encrypted value, put some *** to show it is set
|
|
|
|
# in the explorer
|
2014-02-23 00:12:42 +00:00
|
|
|
if 'inheritedfrom' in val:
|
|
|
|
notes.append('Inherited from %s' % val['inheritedfrom'])
|
2014-02-11 00:36:18 +00:00
|
|
|
value = '********'
|
2013-11-13 15:36:43 +00:00
|
|
|
if isinstance(val, list):
|
|
|
|
snippet += key + ":"
|
2014-02-13 20:55:26 +00:00
|
|
|
if len(val) == 0:
|
|
|
|
snippet += ('<input type="{0}" name="{1}" value="" '
|
|
|
|
' "title="{2}">'
|
|
|
|
).format(type, key, self.desc)
|
2013-11-13 15:36:43 +00:00
|
|
|
for v in val:
|
2014-02-06 18:13:16 +00:00
|
|
|
snippet += ('<input type="{0}" name="{1}" value="{2}" '
|
2014-02-11 00:36:18 +00:00
|
|
|
' "title="{3}">'
|
2014-02-13 17:58:03 +00:00
|
|
|
).format(type, key, v, self.desc)
|
2014-02-06 18:13:16 +00:00
|
|
|
snippet += (
|
2014-02-11 00:36:18 +00:00
|
|
|
'<input type="{0}" name="{1}" value="" title="{2}">'
|
2014-02-06 18:13:16 +00:00
|
|
|
'<input type="checkbox" name="restexplorerhonorkey" '
|
2014-02-13 17:58:03 +00:00
|
|
|
'value="{1}">').format(type, key, self.desc)
|
2013-11-13 15:36:43 +00:00
|
|
|
return snippet
|
2014-02-06 18:13:16 +00:00
|
|
|
snippet += (key + ":" +
|
|
|
|
'<input type="{0}" name="{1}" value="{2}" '
|
2014-02-11 00:36:18 +00:00
|
|
|
'title="{3}"><input type="checkbox" '
|
2014-02-06 18:13:16 +00:00
|
|
|
'name="restexplorerhonorkey" value="{1}">'
|
2014-02-13 17:58:03 +00:00
|
|
|
).format(type, key, value, self.desc)
|
2014-02-23 00:12:42 +00:00
|
|
|
if len(notes) > 0:
|
|
|
|
snippet += '(' + ','.join(notes) + ')'
|
2013-11-02 22:37:26 +00:00
|
|
|
return snippet
|
2013-11-03 15:12:50 +00:00
|
|
|
|
2014-02-06 18:13:16 +00:00
|
|
|
|
2014-01-28 16:18:00 +00:00
|
|
|
class DeletedResource(ConfluentMessage):
|
2014-02-13 21:57:03 +00:00
|
|
|
def __init__(self, resource):
|
2014-01-28 16:18:00 +00:00
|
|
|
self.kvpairs = {}
|
2013-11-03 15:12:50 +00:00
|
|
|
|
2014-02-06 18:13:16 +00:00
|
|
|
|
2013-11-03 17:30:22 +00:00
|
|
|
class ConfluentChoiceMessage(ConfluentMessage):
|
|
|
|
|
|
|
|
def html(self):
|
|
|
|
snippet = ""
|
|
|
|
for key in self.kvpairs.iterkeys():
|
|
|
|
val = self.kvpairs[key]
|
|
|
|
snippet += key + ':<select name="%s">' % key
|
|
|
|
for opt in self.valid_values:
|
|
|
|
snippet += opt + ":"
|
|
|
|
if opt == val['value']:
|
|
|
|
snippet += '<option value="%s" selected>%s</option>' % (
|
|
|
|
opt, opt)
|
|
|
|
else:
|
|
|
|
snippet += '<option value="%s">%s</option>' % (opt, opt)
|
|
|
|
snippet += '</select>'
|
|
|
|
snippet += '<input type="checkbox" name="restexplorerhonorkey" '
|
|
|
|
snippet += 'value="%s">' % (key)
|
|
|
|
return snippet
|
|
|
|
|
2014-02-06 18:13:16 +00:00
|
|
|
|
2013-11-03 18:16:28 +00:00
|
|
|
class LinkRelation(ConfluentMessage):
|
2014-02-09 22:35:49 +00:00
|
|
|
def json(self):
|
2014-01-28 16:18:00 +00:00
|
|
|
"""Provide json_hal style representation of the relation.
|
|
|
|
|
|
|
|
This currently only makes sense for the socket api.
|
|
|
|
"""
|
2014-02-06 18:13:16 +00:00
|
|
|
return {self.rel: '{ "href": "%s" }' % self.href}
|
2013-11-03 22:07:17 +00:00
|
|
|
|
2014-02-10 00:26:48 +00:00
|
|
|
def raw(self):
|
2014-01-28 16:18:00 +00:00
|
|
|
"""Provide python structure of the relation.
|
|
|
|
|
|
|
|
This currently is only sensible to consume from httpapi.
|
|
|
|
"""
|
2014-02-06 18:13:16 +00:00
|
|
|
return {self.rel: {"href": self.href}}
|
2014-01-28 16:18:00 +00:00
|
|
|
|
2013-11-03 18:16:28 +00:00
|
|
|
def html(self):
|
2014-01-28 16:18:00 +00:00
|
|
|
"""Provide an html representation of the link relation.
|
|
|
|
|
|
|
|
This is used by the API explorer aspect of httpapi"""
|
2014-02-06 18:13:16 +00:00
|
|
|
return '<a href="{0}" rel="{1}">{0}</a>'.format(self.href, self.rel)
|
|
|
|
# return '<a href="%s" rel="%s">%s</a><input type="submit"
|
|
|
|
# name="restexprerorop" value="delete:%s"' % (self.href, self.rel,
|
|
|
|
# self.href, self.href)
|
2013-11-03 18:16:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ChildCollection(LinkRelation):
|
2014-01-28 16:18:00 +00:00
|
|
|
def __init__(self, collname, candelete=False):
|
2013-11-03 18:16:28 +00:00
|
|
|
self.rel = 'item'
|
|
|
|
self.href = collname
|
2014-01-28 16:18:00 +00:00
|
|
|
self.candelete = candelete
|
|
|
|
|
|
|
|
def html(self):
|
|
|
|
if self.candelete:
|
2014-02-06 18:13:16 +00:00
|
|
|
return ('<a href="{0}" rel="{1}">{0}</a> . . . . . . . . . . . . '
|
|
|
|
'<button type="submit" name="restexplorerop" '
|
|
|
|
'value="delete" formaction="{0}">delete'
|
|
|
|
'</button>').format(self.href, self.rel)
|
2014-01-28 16:18:00 +00:00
|
|
|
else:
|
2014-02-06 18:13:16 +00:00
|
|
|
return '<a href="{0}" rel="{0}">{0}</a>'.format(self.href)
|
|
|
|
|
2013-11-03 17:30:22 +00:00
|
|
|
|
2013-11-03 14:52:43 +00:00
|
|
|
def get_input_message(path, operation, inputdata, nodes=None):
|
2013-11-07 19:39:34 +00:00
|
|
|
if path[0] == 'power' and path[1] == 'state' and operation != 'retrieve':
|
2013-11-03 14:52:43 +00:00
|
|
|
return InputPowerMessage(path, nodes, inputdata)
|
2013-11-07 19:39:34 +00:00
|
|
|
elif path[0] == 'attributes' and operation != 'retrieve':
|
2014-02-22 23:12:21 +00:00
|
|
|
return InputAttributes(path, inputdata, nodes)
|
2014-02-23 01:41:15 +00:00
|
|
|
elif path == ['boot', 'nextdevice'] and operation != 'retrieve':
|
2013-11-07 21:33:48 +00:00
|
|
|
return InputBootDevice(path, nodes, inputdata)
|
2013-11-03 15:12:50 +00:00
|
|
|
elif inputdata:
|
|
|
|
raise exc.InvalidArgumentException()
|
|
|
|
|
2013-11-03 14:52:43 +00:00
|
|
|
|
2013-11-03 15:12:50 +00:00
|
|
|
class InputAttributes(ConfluentMessage):
|
2013-11-03 14:52:43 +00:00
|
|
|
|
2014-02-22 23:12:21 +00:00
|
|
|
def __init__(self, path, inputdata, nodes=None):
|
2013-11-03 15:12:50 +00:00
|
|
|
self.nodeattribs = {}
|
|
|
|
nestedmode = False
|
|
|
|
if not inputdata:
|
|
|
|
raise exc.InvalidArgumentException
|
2014-02-18 18:22:07 +00:00
|
|
|
if nodes is None:
|
|
|
|
self.attribs = inputdata
|
2014-02-20 01:24:51 +00:00
|
|
|
for attrib in self.attribs:
|
2014-03-02 18:01:19 +00:00
|
|
|
if type(self.attribs[attrib]) in (str, unicode):
|
|
|
|
try:
|
|
|
|
# ok, try to use format against the string
|
|
|
|
# store back result to the attribute to
|
|
|
|
# handle things like '{{' and '}}'
|
|
|
|
# if any weird sort of error should
|
|
|
|
# happen, it means the string has something
|
|
|
|
# that formatter is looking to fulfill, but
|
|
|
|
# is unable to do so, meaning it is an expression
|
|
|
|
tv = self.attribs[attrib].format()
|
|
|
|
self.attribs[attrib] = tv
|
|
|
|
except:
|
|
|
|
# this means format() actually thought there was work
|
|
|
|
# that suggested parameters, push it in as an
|
|
|
|
# expression
|
|
|
|
self.attribs[attrib] = {
|
|
|
|
'expression': self.attribs[attrib]}
|
2014-02-18 18:22:07 +00:00
|
|
|
return
|
2013-11-03 15:12:50 +00:00
|
|
|
for node in nodes:
|
|
|
|
if node in inputdata:
|
|
|
|
nestedmode = True
|
|
|
|
self.nodeattribs[node] = inputdata[node]
|
|
|
|
if nestedmode:
|
|
|
|
for key in inputdata:
|
|
|
|
if key not in nodes:
|
|
|
|
raise exc.InvalidArgumentException
|
|
|
|
else:
|
|
|
|
for node in nodes:
|
|
|
|
self.nodeattribs[node] = inputdata
|
2013-11-03 14:52:43 +00:00
|
|
|
|
2013-11-03 15:21:54 +00:00
|
|
|
def get_attributes(self, node):
|
|
|
|
if node not in self.nodeattribs:
|
|
|
|
return {}
|
2014-02-20 01:24:51 +00:00
|
|
|
nodeattr = self.nodeattribs[node]
|
|
|
|
for attr in nodeattr:
|
2014-03-02 18:01:19 +00:00
|
|
|
if type(nodeattr[attr]) in (str, unicode):
|
|
|
|
try:
|
|
|
|
# as above, use format() to see if string follows
|
|
|
|
# expression, store value back in case of escapes
|
|
|
|
tv = nodeattr[attr].format()
|
|
|
|
nodeattr[attr] = tv
|
|
|
|
except:
|
|
|
|
# an expression string will error if format() done
|
|
|
|
# use that as cue to put it into config as an expr
|
|
|
|
nodeattr[attr] = {'expression': nodeattr[attr]}
|
2014-02-20 01:24:51 +00:00
|
|
|
return nodeattr
|
2013-11-03 15:21:54 +00:00
|
|
|
|
2013-11-03 14:52:43 +00:00
|
|
|
|
|
|
|
class InputPowerMessage(ConfluentMessage):
|
2013-11-03 17:30:22 +00:00
|
|
|
valid_values = set([
|
2013-11-03 15:12:50 +00:00
|
|
|
'on',
|
|
|
|
'off',
|
|
|
|
'reset',
|
|
|
|
'boot',
|
|
|
|
])
|
2013-11-03 14:52:43 +00:00
|
|
|
|
|
|
|
def __init__(self, path, nodes, inputdata):
|
|
|
|
self.powerbynode = {}
|
|
|
|
if not inputdata:
|
|
|
|
raise exc.InvalidArgumentException()
|
2014-02-10 23:38:15 +00:00
|
|
|
if 'state' not in inputdata:
|
2013-11-03 14:52:43 +00:00
|
|
|
#assume we have nested information
|
|
|
|
for key in nodes:
|
|
|
|
if key not in inputdata:
|
|
|
|
raise exc.InvalidArgumentException()
|
|
|
|
datum = inputdata[key]
|
2014-02-10 23:38:15 +00:00
|
|
|
if ('state' not in datum or
|
|
|
|
datum['state'] not in self.valid_values):
|
2013-11-03 14:52:43 +00:00
|
|
|
raise exc.InvalidArgumentException()
|
2014-02-10 23:38:15 +00:00
|
|
|
self.powerbynode[key] = datum['state']
|
|
|
|
else: # we have a state argument not by node
|
2013-11-03 14:52:43 +00:00
|
|
|
datum = inputdata
|
2014-02-10 23:38:15 +00:00
|
|
|
if ('state' not in datum or
|
|
|
|
datum['state'] not in self.valid_values):
|
2013-11-03 14:52:43 +00:00
|
|
|
raise exc.InvalidArgumentException()
|
|
|
|
for node in nodes:
|
2014-02-10 23:38:15 +00:00
|
|
|
self.powerbynode[node] = datum['state']
|
2013-11-03 14:52:43 +00:00
|
|
|
|
|
|
|
def powerstate(self, node):
|
|
|
|
return self.powerbynode[node]
|
2013-11-02 18:52:32 +00:00
|
|
|
|
|
|
|
|
2013-11-07 21:33:48 +00:00
|
|
|
class BootDevice(ConfluentChoiceMessage):
|
|
|
|
valid_values = set([
|
|
|
|
'network',
|
|
|
|
'hd',
|
|
|
|
'setup',
|
|
|
|
'default',
|
|
|
|
'cd',
|
|
|
|
])
|
|
|
|
|
|
|
|
def __init__(self, node, device):
|
|
|
|
if device not in self.valid_values:
|
2014-02-06 18:13:16 +00:00
|
|
|
raise Exception("Invalid boot device argument passed in:" + device)
|
2013-11-07 21:33:48 +00:00
|
|
|
self.kvpairs = {
|
|
|
|
node: {
|
2014-02-23 01:41:15 +00:00
|
|
|
'nextdevice': {'value': device},
|
2013-11-07 21:33:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-06 18:13:16 +00:00
|
|
|
|
2013-11-07 21:33:48 +00:00
|
|
|
class InputBootDevice(BootDevice):
|
|
|
|
def __init__(self, path, nodes, inputdata):
|
|
|
|
self.bootdevbynode = {}
|
|
|
|
if not inputdata:
|
|
|
|
raise exc.InvalidArgumentException()
|
2014-02-23 01:41:15 +00:00
|
|
|
if 'nextdevice' not in inputdata:
|
2013-11-07 21:33:48 +00:00
|
|
|
for key in nodes:
|
|
|
|
if key not in inputdata:
|
|
|
|
raise exc.InvalidArgumentException()
|
|
|
|
datum = inputdata[key]
|
2014-02-10 23:38:15 +00:00
|
|
|
if ('state' not in datum or
|
|
|
|
datum['state'] not in self.valid_values):
|
2013-11-07 21:33:48 +00:00
|
|
|
raise exc.InvalidArgumenTException()
|
2014-02-23 01:41:15 +00:00
|
|
|
self.bootdevbynode[key] = datum['nextdevice']
|
2013-11-07 21:33:48 +00:00
|
|
|
else:
|
|
|
|
datum = inputdata
|
2014-02-23 01:41:15 +00:00
|
|
|
if ('nextdevice' not in datum or
|
|
|
|
datum['nextdevice'] not in self.valid_values):
|
2013-11-07 21:33:48 +00:00
|
|
|
raise exc.InvalidArgumentException()
|
|
|
|
for node in nodes:
|
2014-02-23 01:41:15 +00:00
|
|
|
self.bootdevbynode[node] = datum['nextdevice']
|
2013-11-07 21:33:48 +00:00
|
|
|
|
|
|
|
def bootdevice(self, node):
|
|
|
|
return self.bootdevbynode[node]
|
|
|
|
|
|
|
|
|
2013-11-03 17:30:22 +00:00
|
|
|
class PowerState(ConfluentChoiceMessage):
|
|
|
|
valid_values = set([
|
|
|
|
'on',
|
|
|
|
'off',
|
|
|
|
'reset',
|
|
|
|
'boot',
|
|
|
|
])
|
2013-11-02 18:52:32 +00:00
|
|
|
|
2013-11-07 21:33:48 +00:00
|
|
|
def __init__(self, node, state):
|
2013-11-02 18:52:32 +00:00
|
|
|
self.kvpairs = {
|
|
|
|
node: {
|
2014-02-10 23:38:15 +00:00
|
|
|
'state': {'value': state},
|
2013-11-02 18:52:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-06 18:13:16 +00:00
|
|
|
|
2013-11-02 22:24:04 +00:00
|
|
|
class Attributes(ConfluentMessage):
|
2014-02-13 17:58:03 +00:00
|
|
|
def __init__(self, node=None, kv=None, desc=''):
|
2014-01-28 16:18:00 +00:00
|
|
|
self.desc = desc
|
2013-11-03 04:25:12 +00:00
|
|
|
nkv = {}
|
|
|
|
for key in kv.iterkeys():
|
2014-03-01 15:48:08 +00:00
|
|
|
if type(kv[key]) in (str, unicode):
|
2014-02-20 00:10:58 +00:00
|
|
|
nkv[key] = {'value': kv[key]}
|
|
|
|
else:
|
|
|
|
nkv[key] = kv[key]
|
2014-01-28 16:18:00 +00:00
|
|
|
if node is None:
|
|
|
|
self.kvpairs = nkv
|
|
|
|
else:
|
|
|
|
self.kvpairs = {
|
|
|
|
node: nkv
|
|
|
|
}
|
2013-11-02 18:52:32 +00:00
|
|
|
|
2014-02-06 18:13:16 +00:00
|
|
|
|
2013-11-13 15:36:43 +00:00
|
|
|
class ListAttributes(ConfluentMessage):
|
2014-02-13 20:55:26 +00:00
|
|
|
def __init__(self, node=None, kv=None, desc=''):
|
2014-02-13 17:58:03 +00:00
|
|
|
self.desc = desc
|
2014-02-13 20:55:26 +00:00
|
|
|
if node is None:
|
|
|
|
self.kvpairs = kv
|
|
|
|
else:
|
2014-02-18 18:22:07 +00:00
|
|
|
self.kvpairs = {node: kv}
|
2013-11-13 15:36:43 +00:00
|
|
|
|
2014-02-06 18:13:16 +00:00
|
|
|
|
2013-11-02 22:24:04 +00:00
|
|
|
class CryptedAttributes(Attributes):
|
2013-11-03 17:30:22 +00:00
|
|
|
defaulttype = 'password'
|
|
|
|
|
2014-02-13 17:58:03 +00:00
|
|
|
def __init__(self, node=None, kv=None, desc=''):
|
2013-11-02 22:24:04 +00:00
|
|
|
# for now, just keep the dictionary keys and discard crypt value
|
2014-01-28 16:18:00 +00:00
|
|
|
self.desc = desc
|
2013-11-03 04:25:12 +00:00
|
|
|
nkv = {}
|
|
|
|
for key in kv.iterkeys():
|
2014-02-13 17:58:03 +00:00
|
|
|
nkv[key] = {'isset': False}
|
|
|
|
try:
|
|
|
|
if kv[key] is not None and kv[key]['cryptvalue'] != '':
|
|
|
|
nkv[key] = {'isset': True}
|
2014-02-23 00:12:42 +00:00
|
|
|
nkv[key]['inheritedfrom'] = kv[key]['inheritedfrom']
|
2014-02-13 17:58:03 +00:00
|
|
|
except KeyError:
|
|
|
|
pass
|
2014-01-28 16:18:00 +00:00
|
|
|
if node is None:
|
|
|
|
self.kvpairs = nkv
|
|
|
|
else:
|
|
|
|
self.kvpairs = {
|
|
|
|
node: nkv
|
|
|
|
}
|