Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • DM/dm-docs
  • hammonds/dm-docs
  • hparraga/dm-docs
3 results
Show changes
Showing
with 660 additions and 0 deletions
#!/usr/bin/env python
from dm.ds_web_service.api.experimentRestApi import ExperimentRestApi
from dm.common.exceptions.invalidRequest import InvalidRequest
from dsWebServiceSessionCli import DsWebServiceSessionCli
class AddExperimentCli(DsWebServiceSessionCli):
def __init__(self):
DsWebServiceSessionCli.__init__(self)
self.addOption('', '--name', dest='name', help='Experiment name.')
self.addOption('', '--type-id', dest='typeId', help='Experiment type id.')
self.addOption('', '--description', dest='description', help='Experiment description.')
def checkArgs(self):
if self.options.name is None:
raise InvalidRequest('Experiment name must be provided.')
if self.options.typeId is None:
raise InvalidRequest('Experiment type id must be provided.')
def getName(self):
return self.options.name
def getTypeId(self):
return self.options.typeId
def getDescription(self):
return self.options.description
def runCommand(self):
self.parseArgs(usage="""
dm-get-experiment --name=NAME --type-id=TYPEID
[--description=DESCRIPTION]
Description:
Add new experiment to the DM database.
""")
self.checkArgs()
api = ExperimentRestApi(self.getLoginUsername(), self.getLoginPassword(), self.getServiceHost(), self.getServicePort(), self.getServiceProtocol())
experiment = api.addExperiment(self.getName(), self.getTypeId(), self.getDescription())
print experiment.getDisplayString(self.getDisplayKeys(), self.getDisplayFormat())
#######################################################################
# Run command.
if __name__ == '__main__':
cli = AddExperimentCli()
cli.run()
#!/usr/bin/env python
from dm.common.cli.dmRestCli import DmRestCli
from dm.common.utility.configurationManager import ConfigurationManager
class DsWebServiceCli(DmRestCli):
""" DM DS web service cli class. """
def __init__(self, validArgCount=0):
DmRestCli.__init__(self, validArgCount)
def getDefaultServiceHost(self):
return ConfigurationManager.getInstance().getDsWebServiceHost()
def getDefaultServicePort(self):
return ConfigurationManager.getInstance().getDsWebServicePort()
`
#!/usr/bin/env python
from dm.common.cli.dmRestSessionCli import DmRestSessionCli
from dm.common.utility.osUtility import OsUtility
from dm.common.utility.configurationManager import ConfigurationManager
class DsWebServiceSessionCli(DmRestSessionCli):
""" DM DS web service session cli class. """
DEFAULT_SESSION_CACHE_FILE = OsUtility.getUserHomeDir() + '/.dm/.ds.session.cache'
def __init__(self, validArgCount=0):
DmRestSessionCli.__init__(self, validArgCount)
ConfigurationManager.getInstance().setSessionCacheFile(DsWebServiceSessionCli.DEFAULT_SESSION_CACHE_FILE)
def getDefaultServiceHost(self):
return ConfigurationManager.getInstance().getDsWebServiceHost()
def getDefaultServicePort(self):
return ConfigurationManager.getInstance().getDsWebServicePort()
#!/usr/bin/env python
from dm.common.exceptions.invalidRequest import InvalidRequest
from dm.ds_web_service.api.userRestApi import ExperimentRestApi
from dsWebServiceSessionCli import DsWebServiceSessionCli
class GetExperimentCli(DsWebServiceSessionCli):
def __init__(self):
DsWebServiceSessionCli.__init__(self)
self.addOption('', '--id', dest='id', help='Experiment id. Either id or name must be provided. If both are provided, id takes precedence.')
self.addOption('', '--name', dest='name', help='Experiment name. Either id or name must be provided. If both are provided, id takes precedence.')
def checkArgs(self):
if self.options.id is None and self.options.username is None:
raise InvalidRequest('Either user id or username must be provided.')
def getId(self):
return self.options.id
def getName(self):
return self.options.name
def runCommand(self):
self.parseArgs(usage="""
dm-get-user --id=ID|--name=NAME
Description:
Retrieves experiment information.
""")
self.checkArgs()
api = ExperimentRestApi(self.getLoginUsername(), self.getLoginPassword(), self.getServiceHost(), self.getServicePort(), self.getServiceProtocol())
if self.getId() is not None:
experiment = api.getExperimentById(self.getId())
else:
experiment = api.getExperimentByName(self.getName())
print experiment.getDisplayString(self.getDisplayKeys(), self.getDisplayFormat())
#######################################################################
# Run command.
if __name__ == '__main__':
cli = GetExperimentCli()
cli.run()
#!/usr/bin/env python
from dm.ds_web_service.api.experimentRestApi import ExperimentRestApi
from dsWebServiceSessionCli import DsWebServiceSessionCli
class GetExperimentTypesCli(DsWebServiceSessionCli):
def __init__(self):
DsWebServiceSessionCli.__init__(self)
def runCommand(self):
self.parseArgs(usage="""
dm-get-experiment-types
Description:
Retrieves list of known experiment types.
""")
api = ExperimentRestApi(self.getLoginUsername(), self.getLoginPassword(), self.getServiceHost(), self.getServicePort(), self.getServiceProtocol())
experimentTypes = api.getExperimentTypes()
for experimentType in experimentTypes:
print experimentType.getDisplayString(self.getDisplayKeys(), self.getDisplayFormat())
#######################################################################
# Run command.
if __name__ == '__main__':
cli = GetExperimentTypesCli()
cli.run()
#!/usr/bin/env python
from dm.ds_web_service.api.experimentRestApi import ExperimentRestApi
from dsWebServiceSessionCli import DsWebServiceSessionCli
class GetExperimentsCli(DsWebServiceSessionCli):
def __init__(self):
DsWebServiceSessionCli.__init__(self)
def runCommand(self):
self.parseArgs(usage="""
dm-get-experiments
Description:
Retrieves list of known experiments.
""")
api = ExperimentRestApi(self.getLoginUsername(), self.getLoginPassword(), self.getServiceHost(), self.getServicePort(), self.getServiceProtocol())
experiments = api.getExperiments()
for experiment in experiments:
print experiment.getDisplayString(self.getDisplayKeys(), self.getDisplayFormat())
#######################################################################
# Run command.
if __name__ == '__main__':
cli = GetExperimentsCli()
cli.run()
#!/usr/bin/env python
from dm.common.exceptions.invalidRequest import InvalidRequest
from dm.ds_web_service.api.userRestApi import UserRestApi
from dsWebServiceSessionCli import DsWebServiceSessionCli
class GetUserCli(DsWebServiceSessionCli):
def __init__(self):
DsWebServiceSessionCli.__init__(self)
self.addOption('', '--id', dest='id', help='User id. Either id or username must be provided. If both are provided, id takes precedence.')
self.addOption('', '--username', dest='username', help='User username. Either id or username must be provided. If both are provided, id takes precedence.')
def checkArgs(self):
if self.options.id is None and self.options.username is None:
raise InvalidRequest('Either user id or username must be provided.')
def getId(self):
return self.options.id
def getUsername(self):
return self.options.username
def runCommand(self):
self.parseArgs(usage="""
dm-get-user --id=ID|--username=USERNAME
Description:
Retrieves user information.
""")
self.checkArgs()
api = UserRestApi(self.getLoginUsername(), self.getLoginPassword(), self.getServiceHost(), self.getServicePort(), self.getServiceProtocol())
if self.getId() is not None:
userInfo = api.getUserById(self.getId())
else:
userInfo = api.getUserByUsername(self.getUsername())
print userInfo.getDisplayString(self.getDisplayKeys(), self.getDisplayFormat())
#######################################################################
# Run command.
if __name__ == '__main__':
cli = GetUserCli()
cli.run()
#!/usr/bin/env python
from dm.ds_web_service.api.userRestApi import UserRestApi
from dsWebServiceSessionCli import DsWebServiceSessionCli
class GetUsersCli(DsWebServiceSessionCli):
def __init__(self):
DsWebServiceSessionCli.__init__(self)
def runCommand(self):
self.parseArgs(usage="""
dm-get-users
Description:
Retrieves list of registered users.
""")
api = UserRestApi(self.getLoginUsername(), self.getLoginPassword(), self.getServiceHost(), self.getServicePort(), self.getServiceProtocol())
users = api.getUsers()
for user in users:
print user.getDisplayString(self.getDisplayKeys(), self.getDisplayFormat())
#######################################################################
# Run command.
if __name__ == '__main__':
cli = GetUsersCli()
cli.run()
#!/usr/bin/env python
from dm.ds_web_service.api.experimentRestApi import ExperimentRestApi
from dm.common.exceptions.invalidRequest import InvalidRequest
from dsWebServiceSessionCli import DsWebServiceSessionCli
class StartExperimentCli(DsWebServiceSessionCli):
def __init__(self):
DsWebServiceSessionCli.__init__(self)
self.addOption('', '--name', dest='name', help='Experiment name.')
def checkArgs(self):
if self.options.name is None:
raise InvalidRequest('Experiment name must be provided.')
def getName(self):
return self.options.name
def runCommand(self):
self.parseArgs(usage="""
dm-start-experiment --name=NAME
Description:
Updates experiment start time in the DM database and prepares experiment data directory.
""")
self.checkArgs()
api = ExperimentRestApi(self.getLoginUsername(), self.getLoginPassword(), self.getServiceHost(), self.getServicePort(), self.getServiceProtocol())
experiment = api.startExperiment(self.getName())
print experiment.getDisplayString(self.getDisplayKeys(), self.getDisplayFormat())
#######################################################################
# Run command.
if __name__ == '__main__':
cli = StartExperimentCli()
cli.run()
#!/usr/bin/env python
from dm.ds_web_service.api.experimentRestApi import ExperimentRestApi
from dm.common.exceptions.invalidRequest import InvalidRequest
from dsWebServiceSessionCli import DsWebServiceSessionCli
class StopExperimentCli(DsWebServiceSessionCli):
def __init__(self):
DsWebServiceSessionCli.__init__(self)
self.addOption('', '--name', dest='name', help='Experiment name.')
def checkArgs(self):
if self.options.name is None:
raise InvalidRequest('Experiment name must be provided.')
def getName(self):
return self.options.name
def runCommand(self):
self.parseArgs(usage="""
dm-stop-experiment --name=NAME
Description:
Updates experiment end date in the DM database and checks experiment data permissions.
""")
self.checkArgs()
api = ExperimentRestApi(self.getLoginUsername(), self.getLoginPassword(), self.getServiceHost(), self.getServicePort(), self.getServiceProtocol())
experiment = api.stopExperiment(self.getName())
print experiment.getDisplayString(self.getDisplayKeys(), self.getDisplayFormat())
#######################################################################
# Run command.
if __name__ == '__main__':
cli = StopExperimentCli()
cli.run()
#!/usr/bin/env python
from dm.common.constants import dmRole
from dm.common.objects.authorizationPrincipal import AuthorizationPrincipal
from dm.common.db.api.userDbApi import UserDbApi
from dm.common.service.auth.authorizationPrincipalRetriever import AuthorizationPrincipalRetriever
from dm.ds_web_service.api.dsRestApiFactory import DsRestApiFactory
class DsAuthPrincipalRetriever(AuthorizationPrincipalRetriever):
def __init__(self):
AuthorizationPrincipalRetriever.__init__(self, self.__class__.__name__)
self.authApi = DsRestApiFactory.getAuthRestApi()
def getAuthorizationPrincipal(self, username):
principal = self.authApi.getAuthorizationPrincipal(username)
return principal
#######################################################################
# Testing.
if __name__ == '__main__':
pass
#!/usr/bin/env python
#
# Auth route descriptor.
#
from dm.common.utility.configurationManager import ConfigurationManager
from authSessionController import AuthSessionController
class AuthRouteDescriptor:
@classmethod
def getRoutes(cls):
contextRoot = ConfigurationManager.getInstance().getContextRoot()
authSessionController = AuthSessionController()
routes = [
# Get authorization principal
{
'name' : 'getAuthorizationPrincipal',
'path' : '%s/authorizationPrincipals/:(username)' % contextRoot,
'controller' : authSessionController,
'action' : 'getAuthorizationPrincipal',
'method' : [ 'GET' ]
},
]
return routes
#authenticateUser!/usr/bin/env python
import cherrypy
from dm.common.service.dmSessionController import DmSessionController
from dm.ds_web_service.service.impl.authSessionControllerImpl import AuthSessionControllerImpl
class AuthSessionController(DmSessionController):
def __init__(self):
DmSessionController.__init__(self)
self.authSessionControllerImpl = AuthSessionControllerImpl()
@cherrypy.expose
@DmSessionController.require(DmSessionController.isLoggedIn())
@DmSessionController.execute
def getAuthorizationPrincipal(self, username, **kwargs):
if not len(username):
raise InvalidRequest('Invalid username provided.')
response = self.authSessionControllerImpl.getAuthorizationPrincipal(username).getFullJsonRep()
self.logger.debug('Returning authorization principal for %s' % (username))
return response
#!/usr/bin/env python
#
# DM DS Web Service
#
####################################################################
from dm.common.service.dmRestWebServiceBase import DmRestWebServiceBase
from dm.common.utility.dmModuleManager import DmModuleManager
from dm.common.utility.configurationManager import ConfigurationManager
from dm.ds_web_service.service.impl.storageManager import StorageManager
from dsWebServiceRouteMapper import DsWebServiceRouteMapper
####################################################################
class DsWebService(DmRestWebServiceBase):
def __init__(self):
DmRestWebServiceBase.__init__(self, DsWebServiceRouteMapper)
def initDmModules(self):
self.logger.debug('Initializing dm modules')
# Add modules that will be started.
moduleManager = DmModuleManager.getInstance()
moduleManager.addModule(StorageManager.getInstance())
self.logger.debug('Initialized dm modules')
def getDefaultServerHost(self):
return ConfigurationManager.getInstance().getServiceHost()
def getDefaultServerPort(self):
return ConfigurationManager.getInstance().getServicePort()
####################################################################
# Run service
if __name__ == '__main__':
ConfigurationManager.getInstance().setServiceName('ds-web-service')
service = DsWebService();
service.run()
#!/usr/bin/env python
#
# Route mapper for DM DS web service.
#
import sys
import os
import cherrypy
from dm.common.utility.loggingManager import LoggingManager
from dm.common.utility.configurationManager import ConfigurationManager
from dm.common.service.loginRouteDescriptor import LoginRouteDescriptor
from userRouteDescriptor import UserRouteDescriptor
from experimentRouteDescriptor import ExperimentRouteDescriptor
from authRouteDescriptor import AuthRouteDescriptor
class DsWebServiceRouteMapper:
@classmethod
def setupRoutes(cls):
""" Setup RESTFul routes. """
logger = LoggingManager.getInstance().getLogger(cls.__name__)
contextRoot = ConfigurationManager.getInstance().getContextRoot()
logger.debug('Using context root: %s' % contextRoot)
# Get routes.
routes = LoginRouteDescriptor.getRoutes()
routes += AuthRouteDescriptor.getRoutes()
routes += UserRouteDescriptor.getRoutes()
routes += ExperimentRouteDescriptor.getRoutes()
# Add routes to dispatcher.
d = cherrypy.dispatch.RoutesDispatcher()
for route in routes:
logger.debug('Connecting route: %s' % route)
d.connect(route['name'], route['path'], action=route['action'], controller=route['controller'], conditions=dict(method=route['method']))
return d
#!/usr/bin/env python
#
# User route descriptor.
#
from dm.common.utility.configurationManager import ConfigurationManager
from experimentSessionController import ExperimentSessionController
class ExperimentRouteDescriptor:
@classmethod
def getRoutes(cls):
contextRoot = ConfigurationManager.getInstance().getContextRoot()
# Static instances shared between different routes
experimentSessionController = ExperimentSessionController()
# Define routes.
routes = [
# Get experiment types
{
'name' : 'getExperimentTypes',
'path' : '%s/experimentTypes' % contextRoot,
'controller' : experimentSessionController,
'action' : 'getExperimentTypes',
'method' : ['GET']
},
# Get experiments
{
'name' : 'getExperiments',
'path' : '%s/experiments' % contextRoot,
'controller' : experimentSessionController,
'action' : 'getExperiments',
'method' : ['GET']
},
# Get experiment
{
'name' : 'getExperimentById',
'path' : '%s/experiments/:(id)' % contextRoot,
'controller' : experimentSessionController,
'action' : 'getExperimentById',
'method' : ['GET']
},
# Get experiment
{
'name' : 'getExperimentByName',
'path' : '%s/experimentsByName/:(name)' % contextRoot,
'controller' : experimentSessionController,
'action' : 'getExperimentByName',
'method' : ['GET']
},
# Add experiment
{
'name' : 'addExperiment',
'path' : '%s/experiments' % contextRoot,
'controller' : experimentSessionController,
'action' : 'addExperiment',
'method' : ['POST']
},
# Start experiment
{
'name' : 'startExperiment',
'path' : '%s/experiments/start' % contextRoot,
'controller' : experimentSessionController,
'action' : 'startExperiment',
'method' : ['PUT']
},
# Stop experiment
{
'name' : 'stopExperiment',
'path' : '%s/experiments/stop' % contextRoot,
'controller' : experimentSessionController,
'action' : 'stopExperiment',
'method' : ['PUT']
},
]
return routes
#!/usr/bin/env python
import cherrypy
from dm.common.service.dmSessionController import DmSessionController
from dm.common.exceptions.invalidRequest import InvalidRequest
from dm.common.utility.encoder import Encoder
from dm.ds_web_service.service.impl.experimentSessionControllerImpl import ExperimentSessionControllerImpl
class ExperimentSessionController(DmSessionController):
def __init__(self):
DmSessionController.__init__(self)
self.experimentSessionControllerImpl = ExperimentSessionControllerImpl()
@cherrypy.expose
@DmSessionController.require(DmSessionController.isLoggedIn())
@DmSessionController.execute
def getExperimentTypes(self, **kwargs):
return self.listToJson(self.experimentSessionControllerImpl.getExperimentTypes())
@cherrypy.expose
@DmSessionController.require(DmSessionController.isLoggedIn())
@DmSessionController.execute
def getExperiments(self, **kwargs):
return self.listToJson(self.experimentSessionControllerImpl.getExperiments())
@cherrypy.expose
@DmSessionController.require(DmSessionController.isLoggedIn())
@DmSessionController.execute
def getExperimentByName(self, name, **kwargs):
response = self.experimentSessionControllerImpl.getExperimentByName(name).getFullJsonRep()
self.logger.debug('Returning: %s' % response)
return response
@cherrypy.expose
@DmSessionController.require(DmSessionController.isLoggedIn())
@DmSessionController.execute
def getExperimentById(self, id, **kwargs):
response = self.experimentSessionControllerImpl.getExperimentByid(id).getFullJsonRep()
self.logger.debug('Returning: %s' % response)
return response
@cherrypy.expose
@DmSessionController.require(DmSessionController.isLoggedIn())
@DmSessionController.execute
def addExperiment(self, **kwargs):
name = kwargs.get('name')
if name is None or not len(name):
raise InvalidRequest('Missing experiment name.')
name = Encoder.decode(name)
experimentTypeId = kwargs.get('experimentTypeId')
if experimentTypeId is None:
raise InvalidRequest('Missing experiment type id.')
description = kwargs.get('description')
if description is not None:
description = Encoder.decode(description)
response = self.experimentSessionControllerImpl.addExperiment(name, experimentTypeId, description).getFullJsonRep()
self.logger.debug('Returning: %s' % response)
return response
@cherrypy.expose
@DmSessionController.require(DmSessionController.isLoggedIn())
@DmSessionController.execute
def startExperiment(self, **kwargs):
name = kwargs.get('name')
if name is None or not len(name):
raise InvalidRequest('Missing experiment name.')
name = Encoder.decode(name)
response = self.experimentSessionControllerImpl.startExperiment(name).getFullJsonRep()
self.logger.debug('Returning: %s' % response)
return response
@cherrypy.expose
@DmSessionController.require(DmSessionController.isLoggedIn())
@DmSessionController.execute
def stopExperiment(self, **kwargs):
name = kwargs.get('name')
if name is None or not len(name):
raise InvalidRequest('Missing experiment name.')
name = Encoder.decode(name)
response = self.experimentSessionControllerImpl.stopExperiment(name).getFullJsonRep()
self.logger.debug('Returning: %s' % response)
return response