2014-04-07 16:43:39 -04:00
|
|
|
# vim: tabstop=4 shiftwidth=4 softtabstop=4
|
|
|
|
|
|
|
|
# Copyright 2014 IBM Corporation
|
2015-03-13 14:13:11 -04:00
|
|
|
# Copyright 2015 Lenovo
|
2014-04-07 16:43:39 -04:00
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
2013-09-04 15:44:28 -04:00
|
|
|
# concept here that mapping from the resource tree and arguments go to
|
|
|
|
# specific python class signatures. The intent is to require
|
|
|
|
# plugin authors to come here if they *really* think they need new 'commands'
|
|
|
|
# and hopefully curtail deviation by each plugin author
|
|
|
|
|
|
|
|
# have to specify a standard place for cfg selection of *which* plugin
|
|
|
|
# as well a standard to map api requests to python funcitons
|
2014-02-06 13:13:16 -05:00
|
|
|
# e.g. <nodeelement>/power/state maps to some plugin
|
|
|
|
# HardwareManager.get_power/set_power selected by hardwaremanagement.method
|
2013-09-04 15:44:28 -04:00
|
|
|
# plugins can advertise a set of names if there is a desire for readable things
|
|
|
|
# exceptions to handle os images
|
|
|
|
# endpoints point to a class... usually, the class should have:
|
|
|
|
# -create
|
|
|
|
# -retrieve
|
|
|
|
# -update
|
|
|
|
# -delete
|
|
|
|
# functions. Console is special and just get's passed through
|
|
|
|
# see API.txt
|
|
|
|
|
2015-07-31 17:07:40 -04:00
|
|
|
import confluentd.alerts as alerts
|
|
|
|
import confluentd.config.attributes as attrscheme
|
|
|
|
import confluentd.interface.console as console
|
|
|
|
import confluentd.exceptions as exc
|
|
|
|
import confluentd.messages as msg
|
|
|
|
import confluentd.noderange as noderange
|
2015-07-30 17:08:52 -04:00
|
|
|
try:
|
2015-07-31 17:07:40 -04:00
|
|
|
import confluentd.shellmodule as shellmodule
|
2015-07-30 17:08:52 -04:00
|
|
|
except ImportError:
|
|
|
|
pass
|
2015-03-13 17:12:31 -04:00
|
|
|
import itertools
|
2013-09-04 15:44:28 -04:00
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
|
|
|
|
pluginmap = {}
|
|
|
|
|
2015-04-27 16:57:52 -04:00
|
|
|
|
2015-01-15 11:28:22 -05:00
|
|
|
def seek_element(currplace, currkey):
|
|
|
|
try:
|
|
|
|
return currplace[currkey]
|
|
|
|
except TypeError:
|
|
|
|
if isinstance(currplace, PluginCollection):
|
|
|
|
# we hit a plugin curated collection, all children
|
|
|
|
# are up to the plugin to comprehend
|
|
|
|
return currplace
|
|
|
|
raise
|
2014-02-06 13:13:16 -05:00
|
|
|
|
2015-04-27 16:57:52 -04:00
|
|
|
|
2013-11-07 14:39:34 -05:00
|
|
|
def nested_lookup(nestdict, key):
|
2014-02-22 22:08:46 -05:00
|
|
|
try:
|
2015-01-15 11:28:22 -05:00
|
|
|
return reduce(seek_element, key, nestdict)
|
2015-04-27 16:57:52 -04:00
|
|
|
except TypeError:
|
2014-02-22 22:08:46 -05:00
|
|
|
raise exc.NotFoundException("Invalid element requested")
|
2013-09-04 15:44:28 -04:00
|
|
|
|
2014-02-06 13:13:16 -05:00
|
|
|
|
2013-09-04 15:44:28 -04:00
|
|
|
def load_plugins():
|
|
|
|
# To know our plugins directory, we get the parent path of 'bin'
|
2014-02-06 13:13:16 -05:00
|
|
|
path = os.path.dirname(os.path.realpath(__file__))
|
2014-05-06 16:45:51 -04:00
|
|
|
plugintop = os.path.realpath(os.path.join(path, 'plugins'))
|
2013-09-04 15:44:28 -04:00
|
|
|
plugins = set()
|
2013-11-02 17:45:55 -04:00
|
|
|
for plugindir in os.listdir(plugintop):
|
2014-02-06 13:13:16 -05:00
|
|
|
plugindir = os.path.join(plugintop, plugindir)
|
2013-11-02 17:45:55 -04:00
|
|
|
if not os.path.isdir(plugindir):
|
2013-09-04 15:44:28 -04:00
|
|
|
continue
|
2013-11-02 17:45:55 -04:00
|
|
|
sys.path.append(plugindir)
|
2015-04-27 16:57:52 -04:00
|
|
|
# two passes, to avoid adding both py and pyc files
|
2013-11-02 17:45:55 -04:00
|
|
|
for plugin in os.listdir(plugindir):
|
|
|
|
if plugin.startswith('.'):
|
|
|
|
continue
|
2014-05-27 15:02:53 -04:00
|
|
|
(plugin, plugtype) = os.path.splitext(plugin)
|
|
|
|
if plugtype == '.sh':
|
|
|
|
pluginmap[plugin] = shellmodule.Plugin(
|
|
|
|
os.path.join(plugindir, plugin + '.sh'))
|
2015-07-06 14:41:15 -04:00
|
|
|
elif "__init__" not in plugin:
|
2014-05-27 15:02:53 -04:00
|
|
|
plugins.add(plugin)
|
|
|
|
for plugin in plugins:
|
2013-11-02 17:45:55 -04:00
|
|
|
tmpmod = __import__(plugin)
|
|
|
|
if 'plugin_names' in tmpmod.__dict__:
|
|
|
|
for name in tmpmod.plugin_names:
|
|
|
|
pluginmap[name] = tmpmod
|
|
|
|
else:
|
|
|
|
pluginmap[plugin] = tmpmod
|
2013-09-04 15:44:28 -04:00
|
|
|
|
2013-11-03 13:16:28 -05:00
|
|
|
|
2015-06-16 16:04:40 -04:00
|
|
|
rootcollections = ['noderange/', 'nodes/', 'nodegroups/', 'users/', 'events/']
|
2014-02-06 13:13:16 -05:00
|
|
|
|
2013-11-07 14:39:34 -05:00
|
|
|
|
|
|
|
class PluginRoute(object):
|
|
|
|
def __init__(self, routedict):
|
|
|
|
self.routeinfo = routedict
|
2014-04-18 17:13:50 -04:00
|
|
|
|
2015-04-27 16:57:52 -04:00
|
|
|
|
2015-01-15 11:28:22 -05:00
|
|
|
class PluginCollection(object):
|
|
|
|
def __init__(self, routedict):
|
|
|
|
self.routeinfo = routedict
|
|
|
|
|
2013-11-07 14:39:34 -05:00
|
|
|
# _ prefix indicates internal use (e.g. special console scheme) and should not
|
|
|
|
# be enumerated in any collection
|
|
|
|
noderesources = {
|
2015-05-29 11:17:44 -04:00
|
|
|
'attributes': {
|
|
|
|
'all': PluginRoute({'handler': 'attributes'}),
|
|
|
|
'current': PluginRoute({'handler': 'attributes'}),
|
|
|
|
},
|
|
|
|
'boot': {
|
|
|
|
'nextdevice': PluginRoute({
|
|
|
|
'pluginattrs': ['hardwaremanagement.method'],
|
|
|
|
'default': 'ipmi',
|
|
|
|
}),
|
|
|
|
},
|
2015-05-29 17:04:25 -04:00
|
|
|
'configuration': {
|
|
|
|
'management_controller': {
|
|
|
|
'alerts': {
|
|
|
|
'destinations': PluginCollection({
|
|
|
|
'pluginattrs': ['hardwaremanagement.method'],
|
|
|
|
'default': 'ipmi',
|
|
|
|
}),
|
2015-07-06 14:41:15 -04:00
|
|
|
},
|
|
|
|
'users': PluginCollection({
|
|
|
|
'pluginattrs': ['hardwaremanagement.method'],
|
|
|
|
'default': 'ipmi',
|
|
|
|
}),
|
2015-07-28 14:25:51 -04:00
|
|
|
'net_interfaces': PluginCollection({
|
|
|
|
'pluginattrs': ['hardwaremanagement.method'],
|
|
|
|
'default': 'ipmi',
|
|
|
|
}),
|
2015-08-27 08:38:23 -03:00
|
|
|
'reset': PluginRoute({
|
|
|
|
'pluginattrs': ['hardwaremanagement.method'],
|
|
|
|
'default': 'ipmi',
|
|
|
|
}),
|
2015-09-08 12:24:47 -03:00
|
|
|
'identifier': PluginRoute({
|
|
|
|
'pluginattrs': ['hardwaremanagement.method'],
|
|
|
|
'default': 'ipmi',
|
|
|
|
}),
|
2015-05-29 17:04:25 -04:00
|
|
|
}
|
|
|
|
},
|
2013-11-07 14:39:34 -05:00
|
|
|
'_console': {
|
|
|
|
'session': PluginRoute({
|
2014-04-04 12:36:46 -04:00
|
|
|
'pluginattrs': ['console.method'],
|
2013-11-07 14:39:34 -05:00
|
|
|
}),
|
|
|
|
},
|
|
|
|
'console': {
|
2015-04-27 16:57:52 -04:00
|
|
|
# this is a dummy value, http or socket must handle special
|
2013-11-07 14:39:34 -05:00
|
|
|
'session': PluginRoute({}),
|
|
|
|
},
|
2015-05-15 16:49:52 -04:00
|
|
|
'events': {
|
2015-05-29 17:04:25 -04:00
|
|
|
'hardware': {
|
|
|
|
'log': PluginRoute({
|
|
|
|
'pluginattrs': ['hardwaremanagement.method'],
|
|
|
|
'default': 'ipmi',
|
2015-06-16 16:04:40 -04:00
|
|
|
}),
|
|
|
|
'decode': PluginRoute({
|
|
|
|
'pluginattrs': ['hardwaremanagement.method'],
|
|
|
|
'default': 'ipmi',
|
|
|
|
}),
|
2015-05-29 17:04:25 -04:00
|
|
|
},
|
2015-05-15 16:49:52 -04:00
|
|
|
},
|
2014-04-15 14:59:36 -04:00
|
|
|
'health': {
|
|
|
|
'hardware': PluginRoute({
|
|
|
|
'pluginattrs': ['hardwaremanagement.method'],
|
|
|
|
'default': 'ipmi',
|
|
|
|
}),
|
|
|
|
},
|
2014-11-25 13:57:31 -05:00
|
|
|
'identify': PluginRoute({
|
|
|
|
'pluginattrs': ['hardwaremanagement.method'],
|
|
|
|
'default': 'ipmi',
|
|
|
|
}),
|
2015-04-27 16:57:52 -04:00
|
|
|
'inventory': {
|
|
|
|
'hardware': {
|
|
|
|
'all': PluginCollection({
|
|
|
|
'pluginattrs': ['hardwaremanagement.method'],
|
|
|
|
'default': 'ipmi',
|
|
|
|
}),
|
|
|
|
},
|
2015-08-19 08:13:55 -03:00
|
|
|
'firmware': {
|
|
|
|
'all': PluginCollection({
|
|
|
|
'pluginattrs': ['hardwaremanagement.method'],
|
|
|
|
'default': 'ipmi',
|
|
|
|
}),
|
|
|
|
},
|
2015-04-27 16:57:52 -04:00
|
|
|
},
|
2015-05-29 11:17:44 -04:00
|
|
|
'power': {
|
|
|
|
'state': PluginRoute({
|
|
|
|
'pluginattrs': ['hardwaremanagement.method'],
|
|
|
|
'default': 'ipmi',
|
|
|
|
}),
|
|
|
|
},
|
2015-01-15 11:28:22 -05:00
|
|
|
'sensors': {
|
|
|
|
'hardware': {
|
|
|
|
'all': PluginCollection({
|
|
|
|
'pluginattrs': ['hardwaremanagement.method'],
|
|
|
|
'default': 'ipmi',
|
|
|
|
}),
|
|
|
|
'temperature': PluginCollection({
|
|
|
|
'pluginattrs': ['hardwaremanagement.method'],
|
|
|
|
'default': 'ipmi',
|
|
|
|
}),
|
|
|
|
'power': PluginCollection({
|
|
|
|
'pluginattrs': ['hardwaremanagement.method'],
|
|
|
|
'default': 'ipmi',
|
|
|
|
}),
|
|
|
|
'fans': PluginCollection({
|
|
|
|
'pluginattrs': ['hardwaremanagement.method'],
|
|
|
|
'default': 'ipmi',
|
|
|
|
}),
|
|
|
|
},
|
2015-08-20 10:26:29 -03:00
|
|
|
'led': {
|
2015-08-24 14:43:38 -03:00
|
|
|
'all': PluginCollection({
|
2015-08-20 10:26:29 -03:00
|
|
|
'pluginattrs': ['hardwaremanagement.method'],
|
|
|
|
'default': 'ipmi',
|
|
|
|
})
|
2015-08-24 14:43:38 -03:00
|
|
|
},
|
2015-01-15 11:28:22 -05:00
|
|
|
},
|
2013-11-07 14:39:34 -05:00
|
|
|
}
|
|
|
|
|
2014-02-18 13:22:07 -05:00
|
|
|
nodegroupresources = {
|
|
|
|
'attributes': {
|
|
|
|
'all': PluginRoute({'handler': 'attributes'}),
|
|
|
|
'current': PluginRoute({'handler': 'attributes'}),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2014-02-06 13:13:16 -05:00
|
|
|
|
2014-04-10 17:05:57 -04:00
|
|
|
def create_user(inputdata, configmanager):
|
|
|
|
try:
|
|
|
|
username = inputdata['name']
|
|
|
|
del inputdata['name']
|
|
|
|
except (KeyError, ValueError):
|
|
|
|
raise exc.InvalidArgumentException()
|
|
|
|
configmanager.create_user(username, attributemap=inputdata)
|
|
|
|
|
|
|
|
|
|
|
|
def update_user(name, attribmap, configmanager):
|
|
|
|
try:
|
|
|
|
configmanager.set_user(name, attribmap)
|
|
|
|
except ValueError:
|
|
|
|
raise exc.InvalidArgumentException()
|
|
|
|
|
2014-04-18 17:13:50 -04:00
|
|
|
|
2014-04-10 17:05:57 -04:00
|
|
|
def show_user(name, configmanager):
|
|
|
|
userobj = configmanager.get_user(name)
|
|
|
|
rv = {}
|
|
|
|
for attr in attrscheme.user.iterkeys():
|
|
|
|
rv[attr] = None
|
2014-07-27 19:23:32 -04:00
|
|
|
if attr == 'password':
|
2014-04-10 17:05:57 -04:00
|
|
|
if 'cryptpass' in userobj:
|
2014-07-27 19:23:32 -04:00
|
|
|
rv['password'] = {'cryptvalue': True}
|
|
|
|
yield msg.CryptedAttributes(kv={'password': rv['password']},
|
2014-04-18 17:13:50 -04:00
|
|
|
desc=attrscheme.user[attr][
|
|
|
|
'description'])
|
2014-04-10 17:05:57 -04:00
|
|
|
else:
|
|
|
|
if attr in userobj:
|
|
|
|
rv[attr] = userobj[attr]
|
|
|
|
yield msg.Attributes(kv={attr: rv[attr]},
|
|
|
|
desc=attrscheme.user[attr]['description'])
|
|
|
|
|
|
|
|
|
2013-11-02 13:06:48 -04:00
|
|
|
def stripnode(iterablersp, node):
|
|
|
|
for i in iterablersp:
|
2014-11-25 13:57:31 -05:00
|
|
|
if i is None:
|
|
|
|
raise exc.NotImplementedException("Not Implemented")
|
2013-11-02 13:06:48 -04:00
|
|
|
i.strip_node(node)
|
|
|
|
yield i
|
|
|
|
|
2014-02-06 13:13:16 -05:00
|
|
|
|
2014-04-10 17:05:57 -04:00
|
|
|
def iterate_collections(iterable, forcecollection=True):
|
2013-11-03 14:57:58 -05:00
|
|
|
for coll in iterable:
|
2014-04-10 17:05:57 -04:00
|
|
|
if forcecollection and coll[-1] != '/':
|
2014-04-18 17:13:50 -04:00
|
|
|
coll += '/'
|
2014-01-28 11:18:00 -05:00
|
|
|
yield msg.ChildCollection(coll, candelete=True)
|
2013-11-03 14:57:58 -05:00
|
|
|
|
2014-02-06 13:13:16 -05:00
|
|
|
|
2013-11-07 14:39:34 -05:00
|
|
|
def iterate_resources(fancydict):
|
|
|
|
for resource in fancydict.iterkeys():
|
|
|
|
if resource.startswith("_"):
|
|
|
|
continue
|
|
|
|
if not isinstance(fancydict[resource], PluginRoute): # a resource
|
|
|
|
resource += '/'
|
|
|
|
yield msg.ChildCollection(resource)
|
|
|
|
|
2014-02-06 13:13:16 -05:00
|
|
|
|
2014-04-11 10:07:35 -04:00
|
|
|
def delete_user(user, configmanager):
|
|
|
|
configmanager.del_user(user)
|
|
|
|
yield msg.DeletedResource(user)
|
|
|
|
|
2014-04-18 17:13:50 -04:00
|
|
|
|
2014-02-13 16:57:03 -05:00
|
|
|
def delete_nodegroup_collection(collectionpath, configmanager):
|
|
|
|
if len(collectionpath) == 2: # just the nodegroup
|
|
|
|
group = collectionpath[-1]
|
|
|
|
configmanager.del_groups([group])
|
|
|
|
yield msg.DeletedResource(group)
|
|
|
|
else:
|
|
|
|
raise Exception("Not implemented")
|
|
|
|
|
2014-04-18 17:13:50 -04:00
|
|
|
|
2014-01-28 11:18:00 -05:00
|
|
|
def delete_node_collection(collectionpath, configmanager):
|
2014-02-06 13:13:16 -05:00
|
|
|
if len(collectionpath) == 2: # just node
|
2014-01-28 11:18:00 -05:00
|
|
|
node = collectionpath[-1]
|
2014-02-13 17:07:05 -05:00
|
|
|
configmanager.del_nodes([node])
|
2014-02-13 16:57:03 -05:00
|
|
|
yield msg.DeletedResource(node)
|
2014-01-28 11:18:00 -05:00
|
|
|
else:
|
|
|
|
raise Exception("Not implemented")
|
|
|
|
|
2014-02-06 13:13:16 -05:00
|
|
|
|
2014-02-18 13:22:07 -05:00
|
|
|
def enumerate_nodegroup_collection(collectionpath, configmanager):
|
|
|
|
nodegroup = collectionpath[1]
|
|
|
|
if not configmanager.is_nodegroup(nodegroup):
|
|
|
|
raise exc.NotFoundException("Invalid element requested")
|
|
|
|
del collectionpath[0:2]
|
|
|
|
collection = nested_lookup(nodegroupresources, collectionpath)
|
|
|
|
return iterate_resources(collection)
|
|
|
|
|
2014-04-18 17:13:50 -04:00
|
|
|
|
2013-11-07 14:39:34 -05:00
|
|
|
def enumerate_node_collection(collectionpath, configmanager):
|
2014-04-18 17:13:50 -04:00
|
|
|
if collectionpath == ['nodes']: # it is just '/node/', need to list nodes
|
2015-03-25 09:57:25 -04:00
|
|
|
allnodes = list(configmanager.list_nodes())
|
|
|
|
try:
|
|
|
|
allnodes.sort(key=noderange.humanify_nodename)
|
|
|
|
except TypeError:
|
|
|
|
allnodes.sort()
|
|
|
|
return iterate_collections(allnodes)
|
2015-03-13 14:13:11 -04:00
|
|
|
nodeorrange = collectionpath[1]
|
|
|
|
if collectionpath[0] == 'nodes' and not configmanager.is_node(nodeorrange):
|
2014-02-09 19:30:46 -05:00
|
|
|
raise exc.NotFoundException("Invalid element requested")
|
2015-03-13 14:13:11 -04:00
|
|
|
collection = nested_lookup(noderesources, collectionpath[2:])
|
|
|
|
if len(collectionpath) == 2 and collectionpath[0] == 'noderange':
|
|
|
|
collection['nodes'] = {}
|
2014-02-22 22:08:46 -05:00
|
|
|
if not isinstance(collection, dict):
|
|
|
|
raise exc.NotFoundException("Invalid element requested")
|
2013-11-07 14:39:34 -05:00
|
|
|
return iterate_resources(collection)
|
2013-11-03 13:16:28 -05:00
|
|
|
|
|
|
|
|
2014-02-14 16:43:00 -05:00
|
|
|
def create_group(inputdata, configmanager):
|
|
|
|
try:
|
|
|
|
groupname = inputdata['name']
|
|
|
|
del inputdata['name']
|
|
|
|
attribmap = {groupname: inputdata}
|
|
|
|
except KeyError:
|
2014-02-22 18:12:21 -05:00
|
|
|
raise exc.InvalidArgumentException()
|
2014-05-08 14:39:44 -04:00
|
|
|
try:
|
|
|
|
configmanager.add_group_attributes(attribmap)
|
|
|
|
except ValueError as e:
|
|
|
|
raise exc.InvalidArgumentException(str(e))
|
2014-02-14 16:43:00 -05:00
|
|
|
|
|
|
|
|
2014-01-28 11:18:00 -05:00
|
|
|
def create_node(inputdata, configmanager):
|
|
|
|
try:
|
|
|
|
nodename = inputdata['name']
|
|
|
|
del inputdata['name']
|
2014-02-06 13:13:16 -05:00
|
|
|
attribmap = {nodename: inputdata}
|
2014-01-28 11:18:00 -05:00
|
|
|
except KeyError:
|
2014-05-08 14:39:44 -04:00
|
|
|
raise exc.InvalidArgumentException('name not specified')
|
|
|
|
try:
|
|
|
|
configmanager.add_node_attributes(attribmap)
|
|
|
|
except ValueError as e:
|
|
|
|
raise exc.InvalidArgumentException(str(e))
|
2014-01-28 11:18:00 -05:00
|
|
|
|
|
|
|
|
2013-11-03 13:16:28 -05:00
|
|
|
def enumerate_collections(collections):
|
2013-11-11 09:15:17 -05:00
|
|
|
for collection in collections:
|
2013-11-03 13:16:28 -05:00
|
|
|
yield msg.ChildCollection(collection)
|
|
|
|
|
2014-01-28 11:18:00 -05:00
|
|
|
|
2015-03-13 14:13:11 -04:00
|
|
|
def handle_nodegroup_request(configmanager, inputdata,
|
|
|
|
pathcomponents, operation):
|
|
|
|
iscollection = False
|
2015-04-27 16:57:52 -04:00
|
|
|
routespec = None
|
2015-03-13 14:13:11 -04:00
|
|
|
if len(pathcomponents) < 2:
|
|
|
|
if operation == "create":
|
|
|
|
inputdata = msg.InputAttributes(pathcomponents, inputdata)
|
|
|
|
create_group(inputdata.attribs, configmanager)
|
2015-03-25 10:01:44 -04:00
|
|
|
allgroups = list(configmanager.get_groups())
|
|
|
|
try:
|
|
|
|
allgroups.sort(key=noderange.humanify_nodename)
|
|
|
|
except TypeError:
|
|
|
|
allgroups.sort()
|
|
|
|
return iterate_collections(allgroups)
|
2015-03-13 14:13:11 -04:00
|
|
|
elif len(pathcomponents) == 2:
|
|
|
|
iscollection = True
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
routespec = nested_lookup(nodegroupresources, pathcomponents[2:])
|
|
|
|
if isinstance(routespec, dict):
|
|
|
|
iscollection = True
|
|
|
|
elif isinstance(routespec, PluginCollection):
|
|
|
|
iscollection = False # it is a collection, but plugin defined
|
|
|
|
except KeyError:
|
|
|
|
raise exc.NotFoundException("Invalid element requested")
|
|
|
|
if iscollection:
|
|
|
|
if operation == "delete":
|
|
|
|
return delete_nodegroup_collection(pathcomponents,
|
2015-04-27 16:57:52 -04:00
|
|
|
configmanager)
|
2015-03-13 14:13:11 -04:00
|
|
|
elif operation == "retrieve":
|
|
|
|
return enumerate_nodegroup_collection(pathcomponents,
|
2015-04-27 16:57:52 -04:00
|
|
|
configmanager)
|
2015-03-13 14:13:11 -04:00
|
|
|
else:
|
|
|
|
raise Exception("TODO")
|
|
|
|
plugroute = routespec.routeinfo
|
|
|
|
inputdata = msg.get_input_message(
|
|
|
|
pathcomponents[2:], operation, inputdata)
|
|
|
|
if 'handler' in plugroute: # fixed handler definition
|
|
|
|
hfunc = getattr(pluginmap[plugroute['handler']], operation)
|
|
|
|
return hfunc(
|
|
|
|
nodes=None, element=pathcomponents,
|
|
|
|
configmanager=configmanager,
|
|
|
|
inputdata=inputdata)
|
|
|
|
raise Exception("unknown case encountered")
|
|
|
|
|
|
|
|
|
|
|
|
def handle_node_request(configmanager, inputdata, operation,
|
2015-06-16 16:04:40 -04:00
|
|
|
pathcomponents, autostrip=True):
|
2015-03-13 14:13:11 -04:00
|
|
|
iscollection = False
|
2015-03-13 17:12:31 -04:00
|
|
|
routespec = None
|
|
|
|
if pathcomponents[0] == 'noderange':
|
|
|
|
if len(pathcomponents) > 3 and pathcomponents[2] == 'nodes':
|
|
|
|
# transform into a normal looking node request
|
|
|
|
# this does mean we don't see if it is a valid
|
|
|
|
# child, but that's not a goal for the noderange
|
|
|
|
# facility anyway
|
|
|
|
isnoderange = False
|
|
|
|
pathcomponents = pathcomponents[2:]
|
|
|
|
else:
|
|
|
|
isnoderange = True
|
|
|
|
else:
|
|
|
|
isnoderange = False
|
2015-03-13 14:13:11 -04:00
|
|
|
try:
|
2015-03-13 17:12:31 -04:00
|
|
|
nodeorrange = pathcomponents[1]
|
|
|
|
if not isnoderange and not configmanager.is_node(nodeorrange):
|
2015-03-13 14:13:11 -04:00
|
|
|
raise exc.NotFoundException("Invalid Node")
|
2015-03-13 17:12:31 -04:00
|
|
|
if isnoderange:
|
|
|
|
try:
|
|
|
|
nodes = noderange.NodeRange(nodeorrange, configmanager).nodes
|
2015-03-25 09:57:25 -04:00
|
|
|
except Exception as e:
|
|
|
|
raise exc.NotFoundException("Invalid Noderange: " + str(e))
|
2015-03-13 17:12:31 -04:00
|
|
|
else:
|
|
|
|
nodes = (nodeorrange,)
|
2015-03-13 14:13:11 -04:00
|
|
|
except IndexError: # doesn't actually have a long enough path
|
2015-03-13 17:12:31 -04:00
|
|
|
# this is enumerating a list of nodes or just empty noderange
|
|
|
|
if isnoderange and operation == "retrieve":
|
|
|
|
return iterate_collections([])
|
|
|
|
elif isnoderange or operation == "delete":
|
2015-03-13 14:13:11 -04:00
|
|
|
raise exc.InvalidArgumentException()
|
|
|
|
if operation == "create":
|
|
|
|
inputdata = msg.InputAttributes(pathcomponents, inputdata)
|
|
|
|
create_node(inputdata.attribs, configmanager)
|
2015-03-25 09:57:25 -04:00
|
|
|
allnodes = list(configmanager.list_nodes())
|
|
|
|
try:
|
|
|
|
allnodes.sort(key=noderange.humanify_nodename)
|
|
|
|
except TypeError:
|
|
|
|
allnodes.sort()
|
|
|
|
return iterate_collections(allnodes)
|
2015-05-29 17:04:25 -04:00
|
|
|
if (isnoderange and len(pathcomponents) == 3 and
|
|
|
|
pathcomponents[2] == 'nodes'):
|
2015-03-13 17:12:31 -04:00
|
|
|
# this means that it's a list of relevant nodes
|
2015-03-25 09:57:25 -04:00
|
|
|
nodes = list(nodes)
|
|
|
|
try:
|
|
|
|
nodes.sort(key=noderange.humanify_nodename)
|
|
|
|
except TypeError:
|
|
|
|
nodes.sort()
|
2015-03-13 17:12:31 -04:00
|
|
|
return iterate_collections(nodes)
|
2015-03-13 14:13:11 -04:00
|
|
|
if len(pathcomponents) == 2:
|
|
|
|
iscollection = True
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
routespec = nested_lookup(noderesources, pathcomponents[2:])
|
|
|
|
except KeyError:
|
|
|
|
raise exc.NotFoundException("Invalid element requested")
|
|
|
|
if isinstance(routespec, dict):
|
|
|
|
iscollection = True
|
|
|
|
elif isinstance(routespec, PluginCollection):
|
|
|
|
iscollection = False # it is a collection, but plugin defined
|
|
|
|
if iscollection:
|
|
|
|
if operation == "delete":
|
|
|
|
return delete_node_collection(pathcomponents, configmanager)
|
|
|
|
elif operation == "retrieve":
|
|
|
|
return enumerate_node_collection(pathcomponents, configmanager)
|
|
|
|
else:
|
|
|
|
raise Exception("TODO here")
|
|
|
|
del pathcomponents[0:2]
|
2015-03-13 17:12:31 -04:00
|
|
|
passvalues = []
|
2015-03-13 14:13:11 -04:00
|
|
|
plugroute = routespec.routeinfo
|
|
|
|
inputdata = msg.get_input_message(
|
2015-05-29 17:04:25 -04:00
|
|
|
pathcomponents, operation, inputdata, nodes, isnoderange)
|
2015-03-13 17:12:31 -04:00
|
|
|
if 'handler' in plugroute: # fixed handler definition, easy enough
|
2015-03-13 14:13:11 -04:00
|
|
|
hfunc = getattr(pluginmap[plugroute['handler']], operation)
|
2015-04-27 16:57:52 -04:00
|
|
|
passvalue = hfunc(
|
2015-03-13 17:12:31 -04:00
|
|
|
nodes=nodes, element=pathcomponents,
|
2015-03-13 14:13:11 -04:00
|
|
|
configmanager=configmanager,
|
|
|
|
inputdata=inputdata)
|
2015-03-13 17:12:31 -04:00
|
|
|
if isnoderange:
|
|
|
|
return passvalue
|
|
|
|
else:
|
|
|
|
return stripnode(passvalue, nodes[0])
|
2015-03-13 14:13:11 -04:00
|
|
|
elif 'pluginattrs' in plugroute:
|
|
|
|
nodeattr = configmanager.get_node_attributes(
|
2015-03-13 17:12:31 -04:00
|
|
|
nodes, plugroute['pluginattrs'])
|
2015-03-13 14:13:11 -04:00
|
|
|
plugpath = None
|
|
|
|
if 'default' in plugroute:
|
|
|
|
plugpath = plugroute['default']
|
2015-03-17 11:05:28 -04:00
|
|
|
nodesbyhandler = {}
|
2015-03-13 17:12:31 -04:00
|
|
|
for node in nodes:
|
|
|
|
for attrname in plugroute['pluginattrs']:
|
|
|
|
if attrname in nodeattr[node]:
|
|
|
|
plugpath = nodeattr[node][attrname]['value']
|
|
|
|
if plugpath is not None:
|
|
|
|
hfunc = getattr(pluginmap[plugpath], operation)
|
2015-03-17 11:05:28 -04:00
|
|
|
if hfunc in nodesbyhandler:
|
|
|
|
nodesbyhandler[hfunc].append(node)
|
|
|
|
else:
|
|
|
|
nodesbyhandler[hfunc] = [node]
|
2015-03-26 09:53:34 -04:00
|
|
|
for hfunc in nodesbyhandler:
|
2015-03-17 11:05:28 -04:00
|
|
|
passvalues.append(hfunc(
|
|
|
|
nodes=nodesbyhandler[hfunc], element=pathcomponents,
|
|
|
|
configmanager=configmanager,
|
|
|
|
inputdata=inputdata))
|
2015-06-16 16:04:40 -04:00
|
|
|
if isnoderange or not autostrip:
|
2015-03-13 17:12:31 -04:00
|
|
|
return itertools.chain(*passvalues)
|
|
|
|
elif isinstance(passvalues[0], console.Console):
|
|
|
|
return passvalues[0]
|
|
|
|
else:
|
|
|
|
return stripnode(passvalues[0], nodes[0])
|
2015-03-13 14:13:11 -04:00
|
|
|
|
2015-04-27 16:57:52 -04:00
|
|
|
|
2015-06-16 16:04:40 -04:00
|
|
|
def handle_path(path, operation, configmanager, inputdata=None, autostrip=True):
|
2014-04-18 17:13:50 -04:00
|
|
|
"""Given a full path request, return an object.
|
2013-09-04 15:44:28 -04:00
|
|
|
|
|
|
|
The plugins should generally return some sort of iterator.
|
|
|
|
An exception is made for console/session, which should return
|
2013-10-12 21:13:22 -04:00
|
|
|
a class with connect(), read(), write(bytes), and close()
|
2014-04-18 17:13:50 -04:00
|
|
|
"""
|
2013-11-07 14:39:34 -05:00
|
|
|
pathcomponents = path.split('/')
|
|
|
|
del pathcomponents[0] # discard the value from leading /
|
|
|
|
if pathcomponents[-1] == '':
|
|
|
|
del pathcomponents[-1]
|
2014-02-06 13:13:16 -05:00
|
|
|
if not pathcomponents: # root collection list
|
2013-11-03 13:16:28 -05:00
|
|
|
return enumerate_collections(rootcollections)
|
2015-03-13 14:13:11 -04:00
|
|
|
elif pathcomponents[0] == 'noderange':
|
2015-03-13 17:12:31 -04:00
|
|
|
return handle_node_request(configmanager, inputdata, operation,
|
2015-06-16 16:04:40 -04:00
|
|
|
pathcomponents, autostrip)
|
2014-07-25 14:32:04 -04:00
|
|
|
elif pathcomponents[0] == 'nodegroups':
|
2015-03-13 14:13:11 -04:00
|
|
|
return handle_nodegroup_request(configmanager, inputdata,
|
2015-04-27 16:57:52 -04:00
|
|
|
pathcomponents,
|
|
|
|
operation)
|
2014-04-04 08:58:39 -04:00
|
|
|
elif pathcomponents[0] == 'nodes':
|
2015-04-27 16:57:52 -04:00
|
|
|
# single node request of some sort
|
2015-03-13 14:13:11 -04:00
|
|
|
return handle_node_request(configmanager, inputdata,
|
2015-06-16 16:04:40 -04:00
|
|
|
operation, pathcomponents, autostrip)
|
2014-04-10 17:05:57 -04:00
|
|
|
elif pathcomponents[0] == 'users':
|
2015-04-27 16:57:52 -04:00
|
|
|
# TODO: when non-administrator accounts exist,
|
2014-04-11 10:07:35 -04:00
|
|
|
# they must only be allowed to see their own user
|
2014-04-10 17:05:57 -04:00
|
|
|
try:
|
|
|
|
user = pathcomponents[1]
|
2014-04-18 17:13:50 -04:00
|
|
|
except IndexError: # it's just users/
|
2014-04-10 17:05:57 -04:00
|
|
|
if operation == 'create':
|
|
|
|
inputdata = msg.get_input_message(
|
|
|
|
pathcomponents, operation, inputdata)
|
|
|
|
create_user(inputdata.attribs, configmanager)
|
2014-04-18 17:13:50 -04:00
|
|
|
return iterate_collections(configmanager.list_users(),
|
|
|
|
forcecollection=False)
|
2014-04-11 11:30:17 -04:00
|
|
|
if user not in configmanager.list_users():
|
|
|
|
raise exc.NotFoundException("Invalid user %s" % user)
|
2014-04-10 17:05:57 -04:00
|
|
|
if operation == 'retrieve':
|
|
|
|
return show_user(user, configmanager)
|
|
|
|
elif operation == 'delete':
|
2014-04-11 10:07:35 -04:00
|
|
|
return delete_user(user, configmanager)
|
2014-04-10 17:05:57 -04:00
|
|
|
elif operation == 'update':
|
2014-04-11 10:07:35 -04:00
|
|
|
inputdata = msg.get_input_message(
|
|
|
|
pathcomponents, operation, inputdata)
|
2014-04-10 17:05:57 -04:00
|
|
|
update_user(user, inputdata.attribs, configmanager)
|
|
|
|
return show_user(user, configmanager)
|
2015-06-16 16:04:40 -04:00
|
|
|
elif pathcomponents[0] == 'events':
|
|
|
|
try:
|
|
|
|
element = pathcomponents[1]
|
|
|
|
except IndexError:
|
|
|
|
if operation != 'retrieve':
|
|
|
|
raise exc.InvalidArgumentException('Target is read-only')
|
|
|
|
return (msg.ChildCollection('decode'),)
|
|
|
|
if element != 'decode':
|
|
|
|
raise exc.NotFoundException()
|
|
|
|
if operation == 'update':
|
|
|
|
return alerts.decode_alert(inputdata, configmanager)
|
2013-11-02 13:06:48 -04:00
|
|
|
else:
|
2013-11-02 17:32:48 -04:00
|
|
|
raise exc.NotFoundException()
|