How to use _decorate_request method in tempest

Best Python code snippet using tempest_python

lb_command.py

Source:lb_command.py Github

copy

Full Screen

...161 @return the LBCommandResult object created from the connectblox response.162 '''163 # the default command just pings the server164 request = self._create_request()165 self._decorate_request(request)166 response = self._send_request(request)167 bloxcommand_response = None168 if hasattr(response, 'transaction') and response.transaction.command:169 bloxcommand_response = response.transaction.command[0]170 return self.result_class.from_response(response, self, bloxcommand_response)171 @property172 def result_class(self):173 '''174 @return the class that should encapsulate the connectblox response for this command.175 '''176 # by default, strip off Command from this class and add Result to177 # find the result classname178 this_class_prefix = self.__class__.__name__[:-7]179 result_class_name = '%sResult' % this_class_prefix180 this_module = sys.modules[self.__class__.__module__]181 result_class_ = getattr(this_module, result_class_name)182 return result_class_183 @property184 def request_class(self):185 '''186 @return the class to construct connectblox requests for this command.187 '''188 return blox.connect.ConnectBlox_pb2.Request189 def initialize_transaction(self, request, workspace):190 '''191 Initializes the transaction object of this request with the values stored in192 self. See __init__ for a description of transaction properties.193 '''194 #commented out until timeout are supported by lb-server195# if self.timeout is not None:196# request.transaction.timeout = self.timeout197 if self.exclusive is not None:198 request.transaction.exclusive = self.exclusive199 if self.readonly is not None:200 request.transaction.readonly = self.readonly201 if self.commit_mode is not None:202 request.transaction.commit_mode = self.commit_mode203 if self.branch is not None:204 request.transaction.workspace = '%s@%s' % (workspace, self.branch)205 else:206 request.transaction.workspace = workspace207 def _create_request(self):208 '''209 @return a new request object (of type request_class()) configured according to210 this command.211 '''212 # create using the specific request class for this command213 request = self.request_class()214 # set some default values for it215 if self.loglevel is not None:216 request.log_level = self.loglevel217 if self.log is True:218 request.return_log = True219 if self.cwd is not None:220 request.current_working_directory = os.path.abspath(self.cwd)221 # potentially add monitoring commands222 self._add_monitor_commands(request)223 # add GUID to the request so we pass it to lb-server224 request.guid = self.guid225 return request226 def _add_monitor_commands(self, request):227 '''228 If this command was configured with monitoring, this function will add229 commands to the request to execute the monitoring.230 '''231 if isinstance(self.monitor, list):232 for predicate_name in self.monitor:233 # monitor assertions into a predicate234 cmd = request.transaction.command_after_fixpoint.add()235 cmd.query_predicate.delta = ASSERTIONS236 cmd.query_predicate.predicate.global_name.qualified_name = predicate_name237 # monitor retractions from a predicate238 cmd = request.transaction.command_after_fixpoint.add()239 cmd.query_predicate.delta = RETRACTIONS240 cmd.query_predicate.predicate.global_name.qualified_name = predicate_name241 def _decorate_request(self, request):242 '''243 A template method for sub-types to enrich the request after it's been created244 but before sending it to the connectblox server. The default implementation245 does nothing.246 @param request the connectblox protobuf request object being created for this command.247 '''248 pass249 def _decorate_transaction_request(self, blox_command, request):250 '''251 A template method for sub-types to enrich the blox_command when this command252 is going to be executed inside a transaction. The default implementation253 does nothing.254 @param blox_command the bloxcommand protobuf object corresponding to this lb command.255 @param request the connectblox protobuf request object being created for the whole256 transaction.257 '''258 pass259 def _send_request(self, request, ignore_exception=False):260 '''261 Send the request to the connectblox server.262 @param request the request to be sent (of type request_class())263 @param ignore_exception whether exceptions included in the connectblox response264 should be ignored. If False, this function will raise an265 exception with the contents of the connectblox exception.266 Otherwise, the response will be returned even if it contains267 an exception.268 @return the response object returned from the connectblox server.269 '''270 # at debug mode, print the request271 if LB_DEBUG:272 print('LB_DEBUG: connectblox request =\n' + text_format.MessageToString(request))273 # setting an alarm if there is a timeout274 if self.timeout is not None and self.timeout>0:275 util.set_alarm_handler(self.timeout)276 # rounds up to the second277 signal.alarm((self.timeout+999)/1000)278 #setting the guid to enable cleanup in case of interruption279 util.current_guid = self.guid280 # if we should serialize requests, acquire a lock first281 if self.serialize_requests:282 with self._backend_lock:283 response = self.conn.call(request)284 else:285 response = self.conn.call(request)286 # ad debug mode, print the response287 if LB_DEBUG:288 print('LB_DEBUG: connectblox response =\n' + text_format.MessageToString(response))289 #nothing to interrupt when the call has returned290 util.current_guid = None291 signal.alarm(0)292 # deal with exceptions in the response293 if not ignore_exception:294 if response.HasField("exception"):295 msg = response.exception.message296 if msg.startswith('ERROR '):297 msg = msg[6:]298 raise lb_exception.LBCommandError(msg)299 return response300 def _workspace_exists(self, name):301 '''302 TODO - remove this method, it makes no sense here.303 @return whether a workspace with this name exists.304 '''305 cmd = WorkspaceExistsCommand(name, conn = self.conn, log = self.log,306 loglevel = self.loglevel, monitor = self.monitor,307 priority = self.priority, isAdmin = False, serialize_requests = self.serialize_requests)308 result = cmd.run()309 return result.exists310 def _decorate_with_authenticator(self, workspaceName, req):311 """A helper function that adds an authentication token to the request.312 Use this when sending workspace delete requests."""313 # we only need to add an authenticator if the request goes through a regular tcp314 # connection (indicated by having a port). If a unix socket is used, then that is315 # not necessary.316 if hasattr(self.conn, "port"):317 port = self.conn.port318 secret_file = "%s/.lb-server.%s.secret" % (util.get_lb_deployment_home(), port)319 if os.path.isfile(secret_file):320 try:321 with open(secret_file, 'r') as f:322 secret = f.read()323 t = time.strftime('%s')324 token = "::%s::%s::%s::" % (t, str(workspaceName), secret)325 req.authenticator = t + "::" + hashlib.sha512(token).hexdigest()326 except IOError:327 print "Could not read authentication secret from file %s" % secret_file328class LBAdminCommand(LBCommand):329 '''330 An LBCommand specific for admin commands.331 '''332 def __init__(self, conn=None, log=False, loglevel=None, monitor=None,333 priority=None, isAdmin=True, serialize_requests=False, **kw):334 super(LBAdminCommand, self).__init__(335 conn=conn,log=log,loglevel=loglevel,monitor=monitor,336 priority=priority,isAdmin=isAdmin,serialize_requests=serialize_requests,337 **kw)338 @property339 def request_class(self):340 # an admin command must be encapsulated in a different class than a regular341 # command, so we override this method to return the proper class342 return blox.connect.ConnectBlox_pb2.AdminRequest343 def _create_request(self):344 # overwrite method in LBAdminCommand to avoid setting the loglevel and cwd345 # create using the specific request class for this command346 request = self.request_class()347 # Note: Admin requests do not support return_log348 # if self.log is True:349 # request.return_log = True350 #if self.cwd is not None:351 # request.current_working_directory = os.path.abspath(self.cwd)352 # potentially add monitoring commands353 self._add_monitor_commands(request)354 return request355class TransactionResult(LBCommandResult):356 '''357 An object to encapsulate the response of a transaction request.358 '''359 def __init__(self, response, lbcommand, bloxcommand_response):360 super(TransactionResult,self).__init__(response, lbcommand, bloxcommand_response)361 # mapping of response objects from protocol buffer to command result classes362 response_to_result_dict = {363 "AddBlockResponse":"AddBlockResult",364 "InstallLibraryResponse":"AddLibraryResult",365 "ExecInactiveBlockResponse":"ExecuteBlockResult",366 "ExecBlockResponse":"ExecuteLogicResult",367 "QueryPredicateResponse":"PrintPredicateResult",368 "UpdatePredicateResponse":None,369 "GetPredicateInfoResponse":"PredicateInfoResult",370 "GetPredicateInfoBulkResponse":None,371 "ImportProtoBufResponse":"ImportProtobufResult",372 "CompileBlockResponse":"CompileBlockResult",373 "InstallProjectResponse":"AddProjectResult",374 "RemoveBlockResponse":"RemoveBlockResult",375 "LogMessageResponse":"InsertLogMessageResult",376 "ReplaceBlockResponse":"ReplaceBlockResult",377 "ExportProtoBufResponse":"ExportProtobufResult",378 "ProtoAddSpecResponse":"ProtoAddSpecResult",379 "FaultInjection":None,380 "XMLAddSpecResponse":"XMLAddSpecResult",381 "ImportXMLDocResponse":"ImportXMLResult",382 "GetPredicatePopcountResponse":"PredicatePopcountResult",383 "GetProtocolDescriptorsResponse": "GetProtocolDescriptorsResult"384 }385 # decorate command response of each transaction command386 def parse_transaction_command_response(request_lbcommands, response_transaction_command, lbcommand_result_list):387 '''388 '''389 for i, bloxcommand_response in enumerate(response_transaction_command):390 descriptor, r = bloxcommand_response.ListFields()[0]391 result_class_name = response_to_result_dict[r.__class__.__name__]392 result_class = getattr(sys.modules[__name__], result_class_name)393 lbcommand_result_object = result_class(response, request_lbcommands[i], bloxcommand_response)394 lbcommand_result_object.name = descriptor.name395 lbcommand_result_list.append(lbcommand_result_object)396 # transaction commands before fix point397 self.transaction_cmds = []398 parse_transaction_command_response(lbcommand.commands, response.transaction.command, self.transaction_cmds)399 # transaction commands after fix point400 self.transaction_cmds_after_fixpoint = []401 parse_transaction_command_response(lbcommand.commands_after_fixpoint, response.transaction.command_after_fixpoint, self.transaction_cmds_after_fixpoint)402class TransactionCommand(LBCommand):403 '''404 An LBCommand that represents a transaction with multiple commands.405 '''406 def __init__(self, workspace, commands, commands_after_fixpoint, **kw):407 '''408 Initialize this transaction command.409 @param workspace410 @param commands a list of Namespaces that represent commands parsed from the command line. These are the411 objects returned by argparser after it parsed a command line. See lb_argparser. These are412 *not* LBCommand objects.413 @param commands_after_fixpoint a list with the same type of objects as the "commands" parameter, but with414 commands that should be executed after fixpoint.415 @param kw yey!416 '''417 super(TransactionCommand, self).__init__(**kw)418 # set attributes that are specific to transactions419 self.workspace = workspace420 self.cmdline_commands = commands421 self.cmdline_commands_after_fixpoint = commands_after_fixpoint422 def _decorate_request(self, request):423 '''424 Override to fill the request with commands.425 '''426 # set basic properties of the request427 self.initialize_transaction(request, self.workspace)428 def build_lb_command(cmdline_command, blox_command):429 '''430 Build an LBCommand object from information in the cmdline_command. Also decorate431 the blox_command object with information extracted from the built LBCommand.432 @return the LBCommand that was instantiated.433 '''434 cmdline_command.workspace = self.workspace435 # initialize the command from the cmdline. The class object is taked from the name in the cmd attribute of436 # the cmdline_command.437 lbcommand = getattr(sys.modules[__name__], "%sCommand" % vars(cmdline_command)['cmd']).from_args(None, cmdline_command)438 # make sure we keep the correspondence between lbcommand object and the blox_command protobuf object representing it439 lbcommand.blox_command = blox_command440 # decorate the protobuf command with information from the command441 lbcommand._decorate_transaction_request(blox_command, request)442 return lbcommand443 # build commands from cmdline_commands444 self.commands = []445 for cmdline_command in self.cmdline_commands:446 self.commands.append(build_lb_command(cmdline_command, request.transaction.command.add()))447 # build commands_after_fixpoint from cmdline_commands_after_fixpoint448 self.commands_after_fixpoint = []449 for cmdline_command in self.cmdline_commands_after_fixpoint:450 self.commands_after_fixpoint.append(build_lb_command(cmdline_command, request.transaction.command_after_fixpoint.add()))451 def run(self):452 '''453 TODO - is this useless?454 '''455 return super(TransactionCommand, self).run()456class CreateWorkspaceResult(LBCommandResult):457 def __init__(self, response, lbcommand, bloxcommand_response):458 super(CreateWorkspaceResult,self).__init__(response, lbcommand, bloxcommand_response)459 self.workspace_name = response.create.name460class CreateWorkspaceCommand(LBCommand):461 def __init__(self, name, unique, overwrite, libs=None, **kw):462 """ Command to create a new workspace463 """464 super(CreateWorkspaceCommand, self).__init__(**kw)465 self.name = name466 self.unique = unique467 self.overwrite = overwrite468 self.libs = libs469 def run(self):470 return super(CreateWorkspaceCommand, self).run()471 def _decorate_request(self, req):472 super(CreateWorkspaceCommand,self)._decorate_request(req)473 if self.name:474 req.create.name = self.name475 if self.libs != None:476 req.create.libraries = self.libs477 req.create.overwrite = self.overwrite478 if self.overwrite:479 self._decorate_with_authenticator(self.name, req)480 req.create.unique = self.unique481class ExecuteBlockResult(LBCommandResult):482 def __init__(self, response, lbcommand, bloxcommand_response):483 super(ExecuteBlockResult,self).__init__(response, lbcommand, bloxcommand_response)484 self.return_local = lbcommand.return_local485 for name, rel in zip(self.return_local, bloxcommand_response.exec_inactive.return_local):486 self.output_relations.append((name, rel))487 # This is a bit of a hack in that the ExecuteBlockCommand sometimes needs 2 bloxcommand488 # commands. So here it tries to get the corresponding response by getting the position489 # of the bloxcommand_reponse for the first command in the transaction and getting the next490 # response object. The problem is that it uses the command list, and exec block could491 # perhaps be in command_after_fixpoint. The way to fix this is to have a better link492 # between requests and responses.493 has_msgCls = hasattr(lbcommand, "msgCls") and lbcommand.msgCls is not None494 if has_msgCls and issubclass(lbcommand.msgCls, Message):495 pos = response.transaction.command.index(bloxcommand_response)496 proto_lbcommand = response.transaction.command[pos+1]497 msgInstance = lbcommand.msgCls()498 msgInstance.ParseFromString(proto_lbcommand.export_protobuf.message)499 self.message = msgInstance500 self.should_escape = lbcommand.escape501 self.output_csv = lbcommand.output_csv502 self.delimiter = lbcommand.delimiter503 self.exclude_ids = lbcommand.exclude_ids504class ExecuteBlockCommand(LBCommand):505 def __init__(self, name, workspace, escape, should_print=False, return_local=None, **kw):506 self.name = name507 self.workspace = workspace508 self.escape = escape509 self.return_local = []510 if (should_print != None) and (isinstance(should_print, list)):511 # This is kind of ridiculous mess, but different versions512 # require different formats for should_print, so we check513 # for a bunch of different variants. Clearly, there should514 # just be a clear parameter with a list of predicate515 # names/516 for rel in should_print:517 if rel == None:518 self.return_local.append("_")519 elif rel == []:520 self.return_local.append("_")521 elif isinstance(rel, basestring):522 self.return_local.append(rel)523 elif isinstance(rel, list):524 for predname in rel:525 self.return_local.append(predname)526 self.msgCls = kw.pop('msgCls', None)527 self.msgType = kw.pop('msgType', None)528 self.protocol = kw.pop('protocol', None)529 self.inputs = kw.pop('inputs',[])530 self.branch_bindings = kw.pop('branch_bindings',[])531 self.output_csv = kw.get('output_csv', False)532 self.delimiter = kw.get('delimiter', None)533 self.exclude_ids = kw.get('exclude_ids', False)534 super(ExecuteBlockCommand, self).__init__(**kw)535 def _decorate_request(self, request):536 self.initialize_transaction(request, self.workspace)537 self.bloxcommand = request.transaction.command.add()538 self._decorate_transaction_request(self.bloxcommand, request)539 for binding in self.branch_bindings:540 binding_command = request.transaction.command.add()541 binding_command.bind_branch_alias.alias = binding.alias542 binding_command.bind_branch_alias.branch_name = binding.branch_name543 def _decorate_transaction_request(self, bloxcommand, request):544 bloxcommand.exec_inactive.block_name = self.name545 if len(self.inputs) > 0:546 bloxcommand.exec_inactive.input.extend(self.inputs)547 has_msgCls = self.msgCls is not None548 has_msgType = self.msgType is not None549 has_protocol = self.protocol is not None550 if has_msgCls and has_msgType and has_protocol:551 bloxcommand = request.transaction.command.add()552 bloxcommand.export_protobuf.protocol = self.protocol553 bloxcommand.export_protobuf.type_name = self.msgType554 self._add_return_local_commands(request, bloxcommand)555 def _add_return_local_commands(self, request, bloxcommand):556 if isinstance(self.return_local,list):557 for pred in self.return_local:558 bloxcommand.exec_inactive.return_local.append(pred)559class ExecuteLogicResult(LBCommandResult):560 def __init__(self, response, lbcommand, bloxcommand_response):561 super(ExecuteLogicResult,self).__init__(response, lbcommand, bloxcommand_response)562 if hasattr(lbcommand, 'return_local'):563 self.return_local = lbcommand.return_local564 else:565 self.return_local = []566 for name, rel in zip(self.return_local, getattr(bloxcommand_response,'exec').return_local):567 self.output_relations.append((name, rel))568 self.should_escape = lbcommand.escape569 if isinstance(lbcommand.monitor,list):570 self.monitor_results = []571 i = 0572 for pred in lbcommand.monitor:573 rel1 = getattr(getattr(response.transaction.command_after_fixpoint[i], "query_predicate"),"relation")574 rel2 = getattr(getattr(response.transaction.command_after_fixpoint[i+1], "query_predicate"),"relation")575 self.monitor_results.append( (pred, (rel1, rel2)) );576 i = i + 2577 self.output_csv = lbcommand.output_csv578 self.delimiter = lbcommand.delimiter579 self.exclude_ids = lbcommand.exclude_ids580 if getattr(bloxcommand_response,'exec').problems is not None:581 self.errors = getattr(bloxcommand_response,'exec').problems582class ExecuteLogicCommand(LBCommand):583 def __init__(self, workspace, escape, blockname=None, language='lb', logic=None, should_print=False, file=None, block_name=None, **kw):584 if logic is None and file is None:585 raise lb_exception.LBCommandArgsError(LBCommand.FILE_OR_LOGIC_MSG)586 self.workspace = workspace587 if file is not None:588 logic = file.read()589 self.block_name = blockname590 self.language = blox.connect.BloxCommand_pb2.LB591 if language == 'lb':592 self.language = blox.connect.BloxCommand_pb2.LB593 elif language == 'lb0':594 self.language = blox.connect.BloxCommand_pb2.LB0595 else:596 raise lb_exception.LBCommandArgsError(LBCommand.LANG_MUST_BE_LOGIC_OR_LB0)597 self.logic = logic598 # only use block_name if blockname is None. I have no idea why there are 2 blockname parameters.599 if blockname is None:600 self.block_name = block_name601 self.branch_bindings = kw.pop('branch_bindings',[])602 self.return_local = []603 if (should_print != None) and (isinstance(should_print, list)):604 # This is kind of ridiculous mess, but different versions605 # require different formats for should_print, so we check606 # for a bunch of different variants. Clearly, there should607 # just be a clear parameter with a list of predicate608 # names/609 for rel in should_print:610 if rel == None:611 self.return_local.append("_")612 elif rel == []:613 self.return_local.append("_")614 elif isinstance(rel, basestring):615 self.return_local.append(rel)616 elif isinstance(rel, list):617 for predname in rel:618 self.return_local.append(predname)619 self.escape = escape620 self.output_csv = kw.get('output_csv', False)621 self.delimiter = kw.get('delimiter', None)622 self.exclude_ids = kw.get('exclude_ids', False)623 super(ExecuteLogicCommand, self).__init__(**kw)624 def _decorate_request(self, request):625 self.initialize_transaction(request, self.workspace)626 self.blox_command = request.transaction.command.add()627 self._decorate_transaction_request(self.blox_command, request)628 for binding in self.branch_bindings:629 binding_command = request.transaction.command.add()630 binding_command.bind_branch_alias.alias = binding.alias631 binding_command.bind_branch_alias.branch_name = binding.branch_name632 def _decorate_transaction_request(self, blox_command, request):633 cmdexec = getattr(blox_command, 'exec')634 cmdexec.language = self.language635 cmdexec.logic = self.logic636 if self.block_name is not None:637 cmdexec.block_name = self.block_name638 self._add_return_local_commands(cmdexec)639 def _add_return_local_commands(self, cmdexec):640 if isinstance(self.return_local,list):641 for pred in self.return_local:642 cmdexec.return_local.append(pred)643# query is same as exec with --print option644class QueryLogicResult(ExecuteLogicResult):645 pass646class QueryLogicCommand(ExecuteLogicCommand):647 def __init__(self, workspace, escape, logic=None, file=None, should_print=['_'], **kw):648 if should_print is None:649 should_print = ['_']650 kw['readonly'] = True651 super(QueryLogicCommand, self).__init__(workspace, escape, logic=logic, should_print = should_print, file=file, **kw)652class DeleteWorkspaceResult(LBCommandResult): pass653class DeleteWorkspaceCommand(LBCommand):654 def __init__(self, name, force=False, **kw):655 super(DeleteWorkspaceCommand, self).__init__(**kw)656 self.name = name657 self.force = force658 def _decorate_request(self, req):659 req.delete.name = self.name660 if self.force:661 req.delete.ignore_nonexistent = True662 self._decorate_with_authenticator(self.name, req)663class WorkspaceFilepathResult(LBCommandResult):664 def __init__(self, response, lbcommand, bloxcommand_response):665 super(WorkspaceFilepathResult,self).__init__(response, lbcommand, bloxcommand_response)666 self.filepath = response.get_ws_path.filepath667class WorkspaceFilepathCommand(LBCommand):668 def __init__(self, workspace, inverse=False, **kw):669 super(WorkspaceFilepathCommand, self).__init__(**kw)670 self.name = workspace671 self.inverse = inverse672 def _decorate_request(self, req):673 req.get_ws_path.name = self.name674 req.get_ws_path.inverse = self.inverse675class ListWorkspacesResult(LBAdminCommandResult):676 def __init__(self, response, lbcommand, bloxcommand_response):677 super(ListWorkspacesResult,self).__init__(response, lbcommand, bloxcommand_response)678 self.names = [name for name in response.list_workspaces.name]679class ListWorkspacesCommand(LBAdminCommand):680 def __init__(self, **kw):681 super(ListWorkspacesCommand, self).__init__(**kw)682 def _decorate_request(self, req):683 req.list_workspaces = True684class CreateBranchResult(LBCommandResult): pass685class CreateBranchCommand(LBCommand):686 def __init__(self, workspace, name, parent, overwrite=False, **kw):687 super(CreateBranchCommand, self).__init__(**kw)688 self.workspace = workspace689 self.name = name690 self.parent = parent691 self.overwrite = overwrite692 def _decorate_request(self, req):693 req.create_named_branch.workspace = self.workspace694 req.create_named_branch.branch = self.name695 if self.parent is not None:696 req.create_named_branch.from_branch = self.parent697 req.create_named_branch.overwrite = self.overwrite698class DeleteBranchResult(LBCommandResult): pass699class DeleteBranchCommand(LBCommand):700 def __init__(self, workspace, name, **kw):701 super(DeleteBranchCommand, self).__init__(**kw)702 self.workspace = workspace703 self.name = name704 def _decorate_request(self, req):705 req.close_named_branch.workspace = self.workspace706 req.close_named_branch.branch = self.name707class DefaultBranchResult(LBCommandResult): pass708class DefaultBranchCommand(LBCommand):709 def __init__(self, workspace, name, **kw):710 super(DefaultBranchCommand, self).__init__(**kw)711 self.workspace = workspace712 self.name = name713 def _decorate_request(self, req):714 req.revert_database.workspace = self.workspace715 req.revert_database.older_branch = self.name716 self._decorate_with_authenticator(self.workspace, req)717class ListBranchesResult(LBCommandResult):718 def __init__(self, response, lbcommand, bloxcommand_response):719 super(ListBranchesResult,self).__init__(response, lbcommand, bloxcommand_response)720 self.names = [name for name in response.get_branch_names.names]721class ListBranchesCommand(LBCommand):722 def __init__(self, workspace, all, **kw):723 super(ListBranchesCommand, self).__init__(**kw)724 self.workspace = workspace725 self.all = all726 def _decorate_request(self, req):727 req.get_branch_names.workspace = self.workspace728 req.get_branch_names.include_auto_versions = self.all729class VersionResult(LBCommandResult):730 def __init__(self, response, lbcommand, bloxcommand_response):731 super(VersionResult,self).__init__(response, lbcommand, bloxcommand_response)732 self.version = response.get_ws_version.version733 self.minor_version = response.get_ws_version.minor_version734 self.build_number = response.get_ws_version.build_number735class VersionCommand(LBCommand):736 def __init__(self, workspace, **kw):737 super(VersionCommand, self).__init__(**kw)738 if workspace == None:739 self.name = '/'740 else:741 self.name = workspace742 def _decorate_request(self, req):743 req.get_ws_version.name = self.name744class WorkspaceExistsResult(LBCommandResult):745 def __init__(self, response, lbcommand, bloxcommand_response):746 not_exists = (747 response.HasField('exception')748 and response.exception.HasField('message')749 and response.exception.message.startswith("Workspace '" + lbcommand.name + "' does not exist")750 )751 self.exists = not not_exists752 super(WorkspaceExistsResult,self).__init__(response, lbcommand)753class WorkspaceExistsCommand(LBCommand):754 def __init__(self, name, **kw):755 super(WorkspaceExistsCommand,self).__init__(**kw)756 self.name = name757 def run(self):758 req = self._create_request()759 self._decorate_request(req)760 # Ignore exception because that's what we use to know weather the ws exists or not761 res = self._send_request(req, True)762 return self.result_class.from_response(res, self)763 def _decorate_request(self, req):764 self.initialize_transaction(req, self.name)765class ExportWorkspaceResult(LBCommandResult): pass766class ExportWorkspaceCommand(LBCommand):767 def __init__(self, name, dest, overwrite=False, **kw):768 super(ExportWorkspaceCommand,self).__init__(**kw)769 self.name = name770 self.dest = dest771 self.overwrite = overwrite772 def _decorate_request(self, req):773 req.exportws.name = self.name774 req.exportws.dest_filepath = os.path.abspath(self.dest)775 req.exportws.overwrite = self.overwrite776class ImportWorkspaceResult(LBCommandResult):777 def __init__(self, response, lbcommand, bloxcommand_response):778 super(ImportWorkspaceResult, self).__init__(response, lbcommand, bloxcommand_response)779 self.name = response.importws.name780class ImportWorkspaceCommand(LBCommand):781 def __init__(self, name, src, overwrite=False, unique=False, **kw):782 super(ImportWorkspaceCommand,self).__init__(**kw)783 self.name = name784 self.src = src785 self.overwrite = overwrite786 self.unique = unique787 def _decorate_request(self, req):788 if self.name is not None:789 req.importws.name = self.name790 req.importws.src_filepath = os.path.abspath(self.src)791 req.importws.overwrite = self.overwrite792 req.importws.unique = self.unique793 self._decorate_with_authenticator(self.name, req)794class PredicateInfoResult(LBCommandResult):795 def __init__(self, response, lbcommand, bloxcommand_response):796 super(PredicateInfoResult,self).__init__(response, lbcommand, bloxcommand_response)797 pred_info = bloxcommand_response.pred_info798 self.pred_info = text_format.MessageToString(pred_info)799class PredicateInfoCommand(LBCommand):800 def __init__(self, name, workspace, all, **kw):801 super(PredicateInfoCommand, self).__init__(**kw)802 self.name = name803 self.workspace = workspace804 def _decorate_request(self, req):805 self.initialize_transaction(req, self.workspace)806 self.bloxcommand = req.transaction.command.add()807 req.transaction.readonly = True808 self._decorate_transaction_request(self.bloxcommand, req)809 def _decorate_transaction_request(self, cmd, req):810 cmd.pred_info.predicate.global_name.qualified_name = self.name[0]811class PredicateInfoBulkResult(LBCommandResult):812 def __init__(self, response, lbcommand, bloxcommand_response):813 super(PredicateInfoBulkResult,self).__init__(response, lbcommand, bloxcommand_response)814 pred_info = bloxcommand_response.pred_info_bulk815 self.pred_info = text_format.MessageToString(pred_info)816 self.pred_info_message = pred_info817class PredicateInfoBulkCommand(LBCommand):818 def __init__(self, name, workspace, all, **kw):819 super(PredicateInfoBulkCommand, self).__init__(**kw)820 if all:821 self.all = True822 else:823 self.all = False824 self.requested_names = name825 self.workspace = workspace826 def _decorate_request(self, req):827 self.initialize_transaction(req, self.workspace)828 self.bloxcommand = req.transaction.command.add()829 # we here add and delete omit to create the pred_info_bulk container830 # to have a request even if self.requested_names is empty (indicating831 # that all user-predicates should be returned.832 self.bloxcommand.pred_info_bulk.omit.add()833 del self.bloxcommand.pred_info_bulk.omit[:]834 if not self.all:835 for pred_name in self.requested_names:836 reqname = self.bloxcommand.pred_info_bulk.predicate.add()837 reqname.global_name.qualified_name = pred_name838# NEEDS_WORK: unused - remove? ("lb list" uses PredicatePopcount.)839class ListPredicatesResult(LBCommandResult):840 def __init__(self, response, lbcommand, bloxcommand_response):841 super(ListPredicatesResult,self).__init__(response, lbcommand, bloxcommand_response)842 rel = getattr(bloxcommand_response, 'exec').return_local[0]843 self.predicates_relation = rel844class ListPredicatesCommand(LBCommand):845 def __init__(self, workspace, **kw):846 super(ListPredicatesCommand,self).__init__(**kw)847 self.workspace = workspace848 def _decorate_request(self, req):849 self.initialize_transaction(req, self.workspace)850 self.bloxcommand = req.transaction.command.add()851 cmdexec = getattr(self.bloxcommand, 'exec')852 cmdexec.language = blox.connect.BloxCommand_pb2.LB853 cmdexec.logic = """854 _(name) <-855 system:Predicate:fullName[p] = name,856 !system:Predicate:displayName[p] = _.857 _(name) <-858 system:Predicate:displayName[_] = name.859 """860 cmdexec.return_local.append("_")861class PredicatePopcountResult(LBCommandResult):862 def __init__(self, response, lbcommand, bloxcommand_response):863 super(PredicatePopcountResult,self).__init__(response, lbcommand, bloxcommand_response)864 self.pred_popcount = bloxcommand_response.pred_popcount865class PredicatePopcountCommand(LBCommand):866 def __init__(self, workspace, predicate, name=None, include=None,867 exclude=None, only_predicate_names=False, estimate=False,868 include_default=False, **kw):869 super(PredicatePopcountCommand, self).__init__(**kw)870 self.workspace = workspace871 self.predicate = predicate872 if (self.predicate is None) and (name is not None) and (len(name) > 0):873 self.predicate = []874 if name is not None:875 for n in name:876 self.predicate.append(n)877 self.include = include878 self.exclude = exclude879 self.only_predicate_names = only_predicate_names880 self.estimate = estimate881 self.include_default = include_default882 def _decorate_request(self, req):883 self.initialize_transaction(req, self.workspace)884 self.bloxcommand = req.transaction.command.add()885 self._decorate_transaction_request(self.bloxcommand, req)886 req.transaction.readonly = True887 def _decorate_transaction_request(self, cmd, req):888 if self.only_predicate_names:889 cmd.pred_popcount.only_predicate_names = True890 if isinstance(self.predicate,list):891 cmd.pred_popcount.all = False892 for predname in self.predicate:893 pred = cmd.pred_popcount.predicate.add()894 pred.global_name.qualified_name = predname895 else:896 cmd.pred_popcount.all = True897 if self.include != None:898 cmd.pred_popcount.include_regexp = self.include899 if self.exclude != None:900 cmd.pred_popcount.exclude_regexp = self.exclude901 if self.estimate:902 cmd.pred_popcount.estimated = True903 if self.include_default:904 cmd.pred_popcount.include_default = True905# TODO - ListInactiveBlocks is currently not supported, so we use dlbatch scripts to list blocks906class ListInactiveBlocksResult(LBCommandResult):907 def __init__(self, response, lbcommand, bloxcommand_response):908 super(ListInactiveBlocksResult,self).__init__(response, lbcommand, bloxcommand_response)909 rel = getattr(bloxcommand_response, 'exec').return_local[0]910 self.blocks_relation = rel911class ListInactiveBlocksCommand(LBCommand):912 def __init__(self, workspace, active=True, inactive=True, **kw):913 super(ListInactiveBlocksCommand,self).__init__(**kw)914 self.workspace = workspace915 self.active = active916 self.inactive = inactive917 def _decorate_request(self, req):918 self.initialize_transaction(req, self.workspace)919 self.bloxcommand = req.transaction.command.add()920 cmdexec = getattr(self.bloxcommand, 'exec')921 if self.inactive and not self.active:922 cmdexec.language = blox.connect.BloxCommand_pb2.LB923 cmdexec.logic = """924 _(name) <-925 system:logic:logicBlockName[b] = name,926 !system:logic:Block:displayName[b] = _,927 !system:logic:ActiveBlock(b).928 _(displayName) <-929 system:logic:Block:displayName[b] = displayName,930 !system:logic:ActiveBlock(b).931 """932 else:933 if self.active and not self.inactive:934 cmdexec.logic += """935 _(name) <-936 system:logic:logicBlockName[b] = name,937 !system:logic:Block:displayName[b] = _,938 system:logic:ActiveBlock(b).939 _(displayName) <-940 system:logic:Block:displayName[b] = displayName,941 system:logic:ActiveBlock(b).942 """943 else:944 cmdexec.logic += """945 _(name) <-946 system:logic:logicBlockName[b] = name,947 !system:logic:Block:displayName[b] = _.948 _(displayName) <-949 system:logic:Block:displayName[b] = displayName.950 """951 cmdexec.return_local.append("_")952class PrintPredicateResult(LBCommandResult):953 def __init__(self, response, lbcommand, bloxcommand_response):954 super(PrintPredicateResult, self).__init__(response, lbcommand, bloxcommand_response)955 rel = bloxcommand_response.query_predicate.relation956 self.predicate_relation = rel957 self.should_escape = lbcommand.should_escape958 self.exclude_ids = lbcommand.exclude_ids959class PrintPredicateCommand(LBCommand):960 def __init__(self, name, workspace, should_escape=False, **kw):961 super(PrintPredicateCommand,self).__init__(**kw)962 self.name = name963 self.workspace = workspace964 self.should_escape = should_escape965 self.exclude_ids = kw.get('exclude_ids', False)966 def _decorate_request(self, req):967 self.initialize_transaction(req, self.workspace)968 self.bloxcommand = req.transaction.command.add()969 self._decorate_transaction_request(self.bloxcommand, req)970 req.transaction.readonly = True971 def _decorate_transaction_request(self, cmd, req):972 cmd.query_predicate.predicate.global_name.qualified_name = self.name973class AddBlockResult(LBCommandResult):974 def __init__(self, response, lbcommand, bloxcommand_response):975 super(AddBlockResult, self).__init__(response, lbcommand, bloxcommand_response)976 self.block_name = bloxcommand_response.add_block.block_name977 if bloxcommand_response.add_block.problems is not None:978 self.errors = bloxcommand_response.add_block.problems979class AddBlockCommand(LBCommand):980 def __init__(self, name, workspace, language='lb', logic=None, file=None, active=True, **kw):981 if logic is None and file is None:982 raise lb_exception.LBCommandArgsError(LBCommand.FILE_OR_LOGIC_MSG)983 self.workspace = workspace984 self.name = name985 if file is not None:986 logic = file.read()987 if self.name is None:988 file_basename = os.path.basename(file.name)989 self.name, ext = os.path.splitext(file_basename)990 self.language = blox.connect.BloxCommand_pb2.LB991 if language == 'lb':992 self.language = blox.connect.BloxCommand_pb2.LB993 elif language == 'lb0':994 self.language = blox.connect.BloxCommand_pb2.LB0995 else:996 raise lb_exception.LBCommandArgsError(LBCommand.LANG_MUST_BE_LOGIC_OR_LB0)997 self.logic = logic998 self.active = active999 super(AddBlockCommand,self).__init__(**kw)1000 def _decorate_request(self, req):1001 self.initialize_transaction(req, self.workspace)1002 self.bloxcommand = req.transaction.command.add()1003 self._decorate_transaction_request(self.bloxcommand, req)1004 def _decorate_transaction_request(self, cmd, req):1005 cmd.add_block.language = self.language1006 cmd.add_block.logic = self.logic1007 if self.name is not None:1008 cmd.add_block.block_name = self.name1009 cmd.add_block.active = self.active1010class CompileBlockResult(LBCommandResult):1011 def __init__(self, response, lbcommand, bloxcommand_response):1012 super(CompileBlockResult, self).__init__(response, lbcommand, bloxcommand_response)1013 if bloxcommand_response.compile_block.problems is not None:1014 self.errors = bloxcommand_response.compile_block.problems1015class CompileBlockCommand(LBCommand):1016 def __init__(self, name, workspace, logic=None, file=None, sort=ACTIVE, **kw):1017 if logic is None and file is None:1018 raise lb_exception.LBCommandArgsError(LBCommand.FILE_OR_LOGIC_MSG)1019 self.workspace = workspace1020 self.name = name1021 if file is not None:1022 logic = file.read()1023 if self.name is None:1024 file_basename = os.path.basename(file.name)1025 self.name, ext = os.path.splitext(file_basename)1026 self.language = blox.connect.BloxCommand_pb2.LB1027 self.logic = logic1028 self.sort = sort1029 super(CompileBlockCommand,self).__init__(**kw)1030 def _decorate_request(self, req):1031 self.initialize_transaction(req, self.workspace)1032 self.bloxcommand = req.transaction.command.add()1033 self._decorate_transaction_request(self.bloxcommand, req)1034 def _decorate_transaction_request(self, cmd, req):1035 cmd.compile_block.logic = self.logic1036 if self.name is not None:1037 cmd.compile_block.block_name = self.name1038 cmd.compile_block.sort = self.sort1039class ReplaceBlockResult(LBCommandResult): pass1040class ReplaceBlockCommand(LBCommand):1041 def __init__(self, blockname, workspace, logic, **kw):1042 self.blockname = blockname1043 self.workspace = workspace1044 file_ = kw.pop('file', None)1045 if file_ is not None:1046 logic = file_.read()1047 self.language = blox.connect.BloxCommand_pb2.LB1048 self.logic = logic1049 if file_ is None and logic is None:1050 raise lb_exception.LBCommandArgsError(LBCommand.FILE_OR_LOGIC_MSG)1051 super(ReplaceBlockCommand, self).__init__(**kw)1052 def _decorate_request(self, req):1053 self.initialize_transaction(req, self.workspace)1054 self.bloxcommand = req.transaction.command.add()1055 self._decorate_transaction_request(self.bloxcommand, req)1056 def _decorate_transaction_request(self, cmd):1057 cmd.replace_block.language = blox.connect.BloxCommand_pb2.LB1058 cmd.replace_block.logic = self.logic1059 if self.blockname is not None:1060 cmd.replace_block.block_name = self.blockname1061class RemoveBlockResult(LBCommandResult):1062 def __init__(self, response, lbcommand, bloxcommand_response):1063 self.block_names = lbcommand.blocknames1064 if bloxcommand_response.remove_block.problems is not None:1065 self.errors = bloxcommand_response.remove_block.problems1066class RemoveBlockCommand(LBCommand):1067 def __init__(self, blockname, workspace, **kw):1068 super(RemoveBlockCommand, self).__init__(**kw)1069 self.blocknames = blockname1070 self.workspace = workspace1071 def _decorate_request(self, req):1072 self.initialize_transaction(req, self.workspace)1073 self.bloxcommand = req.transaction.command.add()1074 self._decorate_transaction_request(self.bloxcommand, req)1075 def _decorate_transaction_request(self, bloxcommand, req):1076 bloxcommand.remove_block.block_name.extend(self.blocknames)1077class AddLibraryResult(LBCommandResult): pass1078class AddLibraryCommand(LBCommand):1079 def __init__(self, name, workspace, **kw):1080 super(AddLibraryCommand, self).__init__(**kw)1081 self.name = name1082 self.workspace = workspace1083 def _decorate_request(self, req):1084 self.initialize_transaction(req, self.workspace)1085 self.bloxcommand = req.transaction.command.add()1086 self._decorate_transaction_request(self.bloxcommand, req)1087 def _decorate_transaction_request(self, cmd, req):1088 cmd.install_library.name = self.name1089class AddProjectResult(LBCommandResult): pass1090class AddProjectCommand(LBCommand):1091 def __init__(self, workspace, norecurse, nocopy, libpath, **kw):1092 self.workspace = workspace1093 self.norecurse = norecurse1094 self.nocopy = nocopy1095 self.libpath = libpath1096 self.dir = kw.pop('dir', '.')1097 super(AddProjectCommand,self).__init__(**kw)1098 if self.cwd is None:1099 self.cwd = self.dir1100 def _decorate_request(self, req):1101 self.initialize_transaction(req, self.workspace)1102 self.bloxcommand = req.transaction.command.add()1103 self._decorate_transaction_request(self.bloxcommand, req)1104 def _decorate_transaction_request(self, cmd, req):1105 cmd.install_project.projDir = os.path.abspath(self.dir)1106 cmd.install_project.recurse = not self.norecurse1107 cmd.install_project.copy = not self.nocopy1108 cmd.install_project.lib_path = self.libpath1109class InsertLogMessageResult(LBCommandResult): pass1110class InsertLogMessageCommand(LBCommand):1111 def __init__(self, workspace, message, **kw):1112 super(InsertLogMessageCommand,self).__init__(**kw)1113 self.workspace = workspace1114 self.message = message1115 def _decorate_request(self, req):1116 self.initialize_transaction(req, self.workspace)1117 self.bloxcommand = req.transaction.command.add()1118 self._decorate_transaction_request(self.bloxcommand, req)1119 def _decorate_transaction_request(self, cmd, req):1120 cmd.log_message.message = self.message1121class ShutdownResult(LBAdminCommandResult):1122 def __init__(self, response, lbcommand, bloxcommand_response):1123 super(ShutdownResult, self).__init__(response, lbcommand, bloxcommand_response)1124 self.message = response.shutdown_server.message1125class ShutdownCommand(LBAdminCommand):1126 def _decorate_request(self, req):1127 req.client_id = '0'1128 req.shutdown_server.waitForProcesses = False1129class ServerLoglevelResult(LBAdminCommandResult):1130 def __init__(self, response, lbcommand, bloxcommand_response):1131 super(ServerLoglevelResult, self).__init__(response, lbcommand, bloxcommand_response)1132 self.message = response.message or 'OK'1133class ServerLoglevelCommand(LBAdminCommand):1134 def __init__(self, loglevel, **kw):1135 super(ServerLoglevelCommand, self).__init__(**kw)1136 self.loglevel = loglevel1137 def _decorate_request(self, req):1138 req.client_id = '0'1139 req.loglevel = self.loglevel1140class AbortTransactionResult(LBAdminCommandResult):1141 def __init__(self, response, lbcommand, bloxcommand_response):1142 super(AbortTransactionResult, self).__init__(response, lbcommand, bloxcommand_response)1143 self.res = response1144class AbortTransactionCommand(LBAdminCommand):1145 def __init__(self, workspace, tid, **kw):1146 super(AbortTransactionCommand, self).__init__(**kw)1147 self.tid = tid1148 self.workspace = workspace1149 def _decorate_request(self, req):1150 req.client_id = '0'1151 if self.workspace is not None:1152 req.abort_transaction.workspace = self.workspace1153 req.abort_transaction.tid = self.tid1154class CheckStatusResult(LBAdminCommandResult):1155 def __init__(self, response, lbcommand, bloxcommand_response):1156 super(CheckStatusResult, self).__init__(response, lbcommand, bloxcommand_response)1157 # print text_format.MessageToString(cmd.req)1158 self.res = response1159 self.server_status = True1160class CheckStatusCommand(LBAdminCommand):1161 def __init__(self, all, active, debug, workspace, **kw):1162 super(CheckStatusCommand, self).__init__(**kw)1163 self.active = active or all1164 self.all = all1165 self.debug = debug1166 # in lb interactive, workspace will be a single workspace string1167 if isinstance(workspace, str):1168 if workspace != "":1169 self.workspaces = [ workspace ]1170 else:1171 self.workspaces = [ ]1172 else:1173 self.workspaces = workspace1174 def _decorate_request(self, req):1175 for ws in self.workspaces:1176 req.status.workspaces.append(ws)1177 req.status.show_active_requests = self.active1178 req.status.show_queued_requests = self.all1179 req.status.add_debug_info = self.debug1180 req.client_id = '0'1181 self.req = req1182class CloneWorkspaceResult(LBCommandResult): pass1183class CloneWorkspaceCommand(LBCommand):1184 def __init__(self, source, target, overwrite, **kw):1185 super(CloneWorkspaceCommand,self).__init__(**kw)1186 self.source = source1187 self.target = target1188 self.overwrite = overwrite1189 self.kw = kw1190 def run(self):1191 tmpdir = tempfile.mktemp()1192 try:1193 ExportWorkspaceCommand(self.source, tmpdir, **self.kw).run()1194 ImportWorkspaceCommand(self.target, tmpdir, self.overwrite, False, **self.kw).run()1195 finally:1196 shutil.rmtree(tmpdir)1197 return self.target1198class ImportProtobufResult(LBCommandResult): pass1199class ImportProtobufCommand(LBCommand):1200 def __init__(self, workspace, protocol, msgType, **kw):1201 file_ = kw.pop('file', None)1202 self.workspace = workspace1203 self.protocol = protocol1204 self.msgType = msgType1205 if file_ is not None:1206 self.messageStr = file_.read()1207 super(ImportProtobufCommand,self).__init__(**kw)1208 def _decorate_request(self, req):1209 self.initialize_transaction(req, self.workspace)1210 self.bloxcommand = req.transaction.command.add()1211 self._decorate_transaction_request(self.bloxcommand, req)1212 def _decorate_transaction_request(self, cmd, req):1213 cmd.import_protobuf.protocol = self.protocol1214 cmd.import_protobuf.type_name = self.msgType1215 cmd.import_protobuf.message = self.messageStr1216class ExportProtobufResult(LBCommandResult):1217 def __init__(self, response, lbcommand, bloxcommand_response):1218 if hasattr(lbcommand,"fileName"):1219 file = open(lbcommand.fileName, "w")1220 file.write(bloxcommand_response.export_protobuf.message);1221 file.close()1222 else:1223 has_msgCls = hasattr(lbcommand, "msgCls") and lbcommand.msgCls is not None1224 if has_msgCls and issubclass(lbcommand.msgCls, Message):1225 msgInstance = lbcommand.msgCls()1226 msgInstance.ParseFromString(bloxcommand_response.export_protobuf.message)1227 self.message = msgInstance1228class ExportProtobufCommand(LBCommand):1229 def __init__(self, workspace, protocol, msgType, file=None, msgCls=None, **kw):1230 self.workspace = workspace1231 self.protocol = protocol1232 self.msgType = msgType1233 self.readonly = True1234 if file is not None:1235 self.fileName = file.name1236 self.msgCls = msgCls1237 super(ExportProtobufCommand,self).__init__(**kw)1238 def _decorate_request(self, req):1239 self.initialize_transaction(req, self.workspace)1240 self.bloxcommand = req.transaction.command.add()1241 self._decorate_transaction_request(self.bloxcommand, req)1242 def _decorate_transaction_request(self, cmd, req):1243 cmd.export_protobuf.protocol = self.protocol1244 cmd.export_protobuf.type_name = self.msgType1245class ProtoAddSpecResult(LBCommandResult): pass1246class ProtoAddSpecCommand(LBCommand):1247 def __init__(self, workspace, name, **kw):1248 file_ = kw.pop('file', None)1249 self.workspace = workspace1250 self.name = name1251 if file_ is not None:1252 self.descriptor = file_.read()1253 super(ProtoAddSpecCommand,self).__init__(**kw)1254 def _decorate_request(self, req):1255 self.initialize_transaction(req, self.workspace)1256 self.bloxcommand = req.transaction.command.add()1257 self._decorate_transaction_request(self.bloxcommand, req)1258 def _decorate_transaction_request(self, cmd, req):1259 cmd.proto_add_spec.name = self.name1260 cmd.proto_add_spec.descriptor_msg = self.descriptor1261class GetProtocolDescriptorsResult(LBCommandResult):1262 def __init__(self, response, lbcommand, bloxcommand_response):1263 for proto in bloxcommand_response.proto_get_descriptors.protocols:1264 file = open(proto.name + ".descriptor", "w")1265 file.write(proto.descriptor_data)1266 file.close1267 print "Descriptor for %s written to file %s.descriptor" % (proto.name, proto.name)1268class GetProtocolDescriptorsCommand(LBCommand):1269 def __init__(self, workspace, protocol, include=None, exclude=None, **kw):1270 self.workspace = workspace1271 self.protocol = protocol1272 self.include = include1273 self.exclude = exclude1274 super(GetProtocolDescriptorsCommand,self).__init__(**kw)1275 def _decorate_request(self, req):1276 self.initialize_transaction(req, self.workspace)1277 self.bloxcommand = req.transaction.command.add()1278 self._decorate_transaction_request(self.bloxcommand, req)1279 def _decorate_transaction_request(self, cmd, req):1280 if isinstance(self.protocol,list):1281 cmd.proto_get_descriptors.all = False1282 cmd.proto_get_descriptors.protocol_name.extend(self.protocol)1283 else:1284 cmd.proto_get_descriptors.all = True1285 if self.include != None:1286 cmd.proto_get_descriptors.include_regexp = self.include1287 if self.exclude != None:1288 cmd.proto_get_descriptors.exclude_regexp = self.exclude1289class ImportXMLResult(LBCommandResult): pass1290class ImportXMLCommand(LBCommand):1291 def __init__(self, workspace, schema=None, **kw):1292 self.workspace = workspace1293 self.schema = schema1294 file_ = kw.pop('file', None)1295 if file_ is not None:1296 self.document = file_.read()1297 super(ImportXMLCommand,self).__init__(**kw)1298 def _decorate_request(self, req):1299 self.initialize_transaction(req, self.workspace)1300 self.bloxcommand = req.transaction.command.add()1301 self._decorate_transaction_request(self.bloxcommand, req)1302 def _decorate_transaction_request(self, cmd, req):1303 cmd.import_xml_doc.document = self.document1304 if self.schema is not None:1305 cmd.import_xml_doc.schema = self.schema1306class RawCommand():1307 def __init__(self, conn, args, interactive, **kw):1308 arg_dict = vars(args)1309 if 'func' in arg_dict:1310 del arg_dict['func']1311 self.request = arg_dict["request"]1312 self.isAdmin = arg_dict["isAdmin"]1313 self.unixDomainSocket = arg_dict["unixDomainSocket"]1314 self.verbose = arg_dict["verbose"]1315 self.conn = conn1316 @property1317 def request_class(self):1318 return blox.connect.ConnectBlox_pb2.Request1319 @property1320 def admin_request_class(self):1321 return blox.connect.ConnectBlox_pb2.AdminRequest1322 def run(self):1323 req = None1324 if self.isAdmin:1325 req = self.admin_request_class()1326 else:1327 req = self.request_class()1328 # set default request id defaulting to 01329 req.client_id = '0'1330 text_format.Merge(self.request, req)1331 if self.verbose:1332 print "=" * 801333 print "Request:"1334 print "-" * 801335 print text_format.MessageToString(req)1336 print "-" * 801337 result = self.conn.call(req)1338 if self.verbose:1339 print "=" * 801340 print "Response:"1341 print "-" * 801342 print text_format.MessageToString(result)1343 if self.verbose:1344 print "-" * 801345class XMLAddSpecResult(LBCommandResult): pass1346class XMLAddSpecCommand(LBCommand):1347 def __init__(self, workspace, name, **kw):1348 file_ = kw.pop('file', None)1349 self.workspace = workspace1350 self.name = name1351 if file_ is not None:1352 self.descriptor = file_.read()1353 super(XMLAddSpecCommand,self).__init__(**kw)1354 def _decorate_request(self, req):1355 self.initialize_transaction(req, self.workspace)1356 self.bloxcommand = req.transaction.command.add()1357 self._decorate_transaction_request(self.bloxcommand, req)1358 def _decorate_transaction_request(self, cmd, req):1359 cmd.xml_add_spec.name = self.name1360 cmd.xml_add_spec.descriptor_msg = self.descriptor1361class GetWorkspaceInfoResult(LBCommandResult):1362 def __init__(self, response, lbcommand, bloxcommand_response):1363 super(GetWorkspaceInfoResult,self).__init__(response, lbcommand, bloxcommand_response)1364 self.info = response.get_workspace_info1365class GetWorkspaceInfoCommand(LBCommand):1366 def __init__(self, name, **kw):1367 super(GetWorkspaceInfoCommand, self).__init__(**kw)1368 self.name = name1369 def run(self):1370 return super(GetWorkspaceInfoCommand, self).run()1371 def _decorate_request(self, req):1372 super(GetWorkspaceInfoCommand,self)._decorate_request(req)1373 req.get_workspace_info.name = self.name1374class StartMirrorResult(LBCommandResult):1375 def __init__(self, response, lbcommand, bloxcommand_response):1376 super(StartMirrorResult,self).__init__(response, lbcommand, bloxcommand_response)1377class StartMirrorCommand(LBCommand):1378 def __init__(self, name, host, remote_port, remote_workspace, **kw):1379 super(StartMirrorCommand, self).__init__(**kw)1380 self.name = name1381 self.remote_host = host1382 self.remote_port = remote_port1383 self.remote_workspace = remote_workspace1384 def run(self):1385 return super(StartMirrorCommand, self).run()1386 def _decorate_request(self, req):1387 super(StartMirrorCommand,self)._decorate_request(req)1388 req.start_mirror.name = self.name1389 req.start_mirror.remote_host = self.remote_host1390 req.start_mirror.remote_port = self.remote_port1391 req.start_mirror.remote_workspace = self.remote_workspace1392class StopMirrorResult(LBCommandResult):1393 def __init__(self, response, lbcommand, bloxcommand_response):1394 super(StopMirrorResult,self).__init__(response, lbcommand, bloxcommand_response)1395class StopMirrorCommand(LBCommand):1396 def __init__(self, name, **kw):1397 super(StopMirrorCommand, self).__init__(**kw)1398 self.name = name1399 def run(self):1400 return super(StopMirrorCommand, self).run()1401 def _decorate_request(self, req):1402 super(StopMirrorCommand,self)._decorate_request(req)1403 req.stop_mirror.name = self.name1404class PromoteMirrorResult(LBCommandResult):1405 def __init__(self, response, lbcommand, bloxcommand_response):1406 super(PromoteMirrorResult,self).__init__(response, lbcommand, bloxcommand_response)1407class PromoteMirrorCommand(LBCommand):1408 def __init__(self, name, **kw):1409 super(PromoteMirrorCommand, self).__init__(**kw)1410 self.name = name1411 def run(self):1412 return super(PromoteMirrorCommand, self).run()1413 def _decorate_request(self, req):1414 super(PromoteMirrorCommand,self)._decorate_request(req)1415 req.promote_mirror.name = self.name1416class CopyRemoteWorkSpaceResult(LBCommandResult):1417 def __init__(self, response, lbcommand, bloxcommand_response):1418 super(CopyRemoteWorkSpaceResult,self).__init__(response, lbcommand, bloxcommand_response)1419class CopyRemoteWorkSpaceCommand(LBCommand):1420 def __init__(self, name, host, remote_port, remote_workspace, **kw):1421 super(CopyRemoteWorkSpaceCommand, self).__init__(**kw)1422 self.name = name1423 self.remote_host = host1424 self.remote_port = remote_port1425 self.remote_workspace = remote_workspace1426 def run(self):1427 return super(CopyRemoteWorkSpaceCommand, self).run()1428 def _decorate_request(self, req):1429 super(CopyRemoteWorkSpaceCommand,self)._decorate_request(req)1430 req.copy_remote_workspace.name = self.name1431 req.copy_remote_workspace.remote_host = self.remote_host1432 req.copy_remote_workspace.remote_port = self.remote_port1433 req.copy_remote_workspace.remote_workspace = self.remote_workspace1434class ExtractExampleResult(LBCommandResult):1435 def __init__(self, response, lbcommand, bloxcommand_response):1436 super(ExtractExampleResult,self).__init__(response, lbcommand, bloxcommand_response)1437 self.returned_data = response.execute_batch_script.returned_data1438class ExtractExampleCommand(LBCommand):1439 def __init__(self, name, predicate, transitive, monitor, defun, **kw):1440 super(ExtractExampleCommand, self).__init__(**kw)1441 self.workspace = name1442 script = "extractExample"1443 if predicate:1444 script += " --predicate "1445 script += predicate1446 if transitive:1447 script += " --transitive"1448 if monitor:1449 script += " --monitor"1450 if defun is not None:1451 for p in defun:1452 script += " --defun "1453 script += p1454 self.script = script1455 def run(self):1456 return super(ExtractExampleCommand, self).run()1457 def _decorate_request(self, req):1458 super(ExtractExampleCommand,self)._decorate_request(req)1459 req.execute_batch_script.workspace = self.workspace1460 req.execute_batch_script.transactional = True1461 req.execute_batch_script.return_data = True1462 req.execute_batch_script.script = self.script1463 self._decorate_with_authenticator(self.workspace, req)1464class ExecuteBatchScriptResult(LBCommandResult):1465 def __init__(self, response, lbcommand, bloxcommand_response):1466 super(ExecuteBatchScriptResult,self).__init__(response, lbcommand, bloxcommand_response)1467 self.output = response.execute_batch_script.output1468 self.returned_data = response.execute_batch_script.returned_data1469class ExecuteBatchScriptCommand(LBCommand):1470 def __init__(self, script, workspace, file, transactional, return_data=None, **kw):1471 super(ExecuteBatchScriptCommand, self).__init__(**kw)1472 if script is None and file is None:1473 raise lb_exception.LBCommandArgsError(1474 "Either a filename or script text must be supplied.")1475 self.workspace = workspace1476 self.transactional = transactional1477 self.return_data = return_data1478 if file is not None:1479 script = file.read()1480 self.script = script1481 def run(self):1482 return super(ExecuteBatchScriptCommand, self).run()1483 def _decorate_request(self, req):1484 super(ExecuteBatchScriptCommand,self)._decorate_request(req)1485 req.execute_batch_script.workspace = self.workspace1486 req.execute_batch_script.script = self.script1487 req.execute_batch_script.transactional = self.transactional1488 if self.return_data is not None:1489 req.execute_batch_script.return_data = self.return_data...

Full Screen

Full Screen

auth_token.py

Source:auth_token.py Github

copy

Full Screen

...117 #No claim(s) provided118 if self.delay_auth_decision:119 #Configured to allow downstream service to make final decision.120 #So mark status as Invalid and forward the request downstream121 self._decorate_request("X_IDENTITY_STATUS",122 "Invalid", env, proxy_headers)123 else:124 #Respond to client as appropriate for this auth protocol125 return self._reject_request(env, start_response)126 else:127 # this request is presenting claims. Let's validate them128 valid = self._validate_claims(claims)129 if not valid:130 # Keystone rejected claim131 if self.delay_auth_decision:132 # Downstream service will receive call still and decide133 self._decorate_request("X_IDENTITY_STATUS",134 "Invalid", env, proxy_headers)135 else:136 #Respond to client as appropriate for this auth protocol137 return self._reject_claims(env, start_response)138 else:139 self._decorate_request("X_IDENTITY_STATUS",140 "Confirmed", env, proxy_headers)141 #Collect information about valid claims142 if valid:143 claims = self._expound_claims(claims)144 # Store authentication data145 if claims:146 self._decorate_request('X_AUTHORIZATION', "Proxy %s" %147 claims['user'], env, proxy_headers)148 # For legacy compatibility before we had ID and Name149 self._decorate_request('X_TENANT',150 claims['tenant'], env, proxy_headers)151 # Services should use these152 self._decorate_request('X_TENANT_NAME',153 claims.get('tenant_name', claims['tenant']),154 env, proxy_headers)155 self._decorate_request('X_TENANT_ID',156 claims['tenant'], env, proxy_headers)157 self._decorate_request('X_USER',158 claims['user'], env, proxy_headers)159 if 'roles' in claims and len(claims['roles']) > 0:160 if claims['roles'] != None:161 roles = ''162 for role in claims['roles']:163 if len(roles) > 0:164 roles += ','165 roles += role166 self._decorate_request('X_ROLE',167 roles, env, proxy_headers)168 # NOTE(todd): unused169 self.expanded = True170 #Send request downstream171 return self._forward_request(env, start_response, proxy_headers)172 # NOTE(todd): unused173 def get_admin_auth_token(self, username, password):174 """175 This function gets an admin auth token to be used by this service to176 validate a user's token. Validate_token is a priviledged call so177 it needs to be authenticated by a service that is calling it178 """179 headers = {"Content-type": "application/json",180 "Accept": "application/json"}181 params = {"passwordCredentials": {"username": username,182 "password": password,183 "tenantId": "1"}}184 conn = httplib.HTTPConnection("%s:%s" \185 % (self.auth_host, self.auth_port))186 conn.request("POST", "/v2.0/tokens", json.dumps(params), \187 headers=headers)188 response = conn.getresponse()189 data = response.read()190 return data191 def _get_claims(self, env):192 """Get claims from request"""193 claims = env.get('HTTP_X_AUTH_TOKEN', env.get('HTTP_X_STORAGE_TOKEN'))194 return claims195 def _reject_request(self, env, start_response):196 """Redirect client to auth server"""197 return HTTPUnauthorized("Authentication required",198 [("WWW-Authenticate",199 "Keystone uri='%s'" % self.auth_location)])(env,200 start_response)201 def _reject_claims(self, env, start_response):202 """Client sent bad claims"""203 return HTTPUnauthorized()(env,204 start_response)205 def _validate_claims(self, claims):206 """Validate claims, and provide identity information isf applicable """207 # Step 1: We need to auth with the keystone service, so get an208 # admin token209 #TODO(ziad): Need to properly implement this, where to store creds210 # for now using token from ini211 #auth = self.get_admin_auth_token("admin", "secrete", "1")212 #admin_token = json.loads(auth)["auth"]["token"]["id"]213 # Step 2: validate the user's token with the auth service214 # since this is a priviledged op,m we need to auth ourselves215 # by using an admin token216 headers = {"Content-type": "application/json",217 "Accept": "application/json",218 "X-Auth-Token": self.admin_token}219 ##TODO(ziad):we need to figure out how to auth to keystone220 #since validate_token is a priviledged call221 #Khaled's version uses creds to get a token222 # "X-Auth-Token": admin_token}223 # we're using a test token from the ini file for now224 conn = http_connect(self.auth_host, self.auth_port, 'GET',225 '/v2.0/tokens/%s' % claims, headers=headers)226 resp = conn.getresponse()227 # data = resp.read()228 conn.close()229 if not str(resp.status).startswith('20'):230 # Keystone rejected claim231 return False232 else:233 #TODO(Ziad): there is an optimization we can do here. We have just234 #received data from Keystone that we can use instead of making235 #another call in _expound_claims236 return True237 def _expound_claims(self, claims):238 # Valid token. Get user data and put it in to the call239 # so the downstream service can use it240 headers = {"Content-type": "application/json",241 "Accept": "application/json",242 "X-Auth-Token": self.admin_token}243 ##TODO(ziad):we need to figure out how to auth to keystone244 #since validate_token is a priviledged call245 #Khaled's version uses creds to get a token246 # "X-Auth-Token": admin_token}247 # we're using a test token from the ini file for now248 conn = http_connect(self.auth_host, self.auth_port, 'GET',249 '/v2.0/tokens/%s' % claims, headers=headers)250 resp = conn.getresponse()251 data = resp.read()252 conn.close()253 if not str(resp.status).startswith('20'):254 raise LookupError('Unable to locate claims: %s' % resp.status)255 token_info = json.loads(data)256 roles = []257 role_refs = token_info["access"]["user"]["roles"]258 if role_refs != None:259 for role_ref in role_refs:260 # Nova looks for the non case-sensitive role 'Admin'261 # to determine admin-ness262 roles.append(role_ref["name"])263 try:264 tenant = token_info['access']['token']['tenant']['id']265 tenant_name = token_info['access']['token']['tenant']['name']266 except:267 tenant = None268 tenant_name = None269 if not tenant:270 tenant = token_info['access']['user'].get('tenantId')271 tenant_name = token_info['access']['user'].get('tenantName')272 verified_claims = {'user': token_info['access']['user']['username'],273 'tenant': tenant,274 'roles': roles}275 if tenant_name:276 verified_claims['tenantName'] = tenant_name277 return verified_claims278 def _decorate_request(self, index, value, env, proxy_headers):279 """Add headers to request"""280 proxy_headers[index] = value281 env["HTTP_%s" % index] = value282 def _forward_request(self, env, start_response, proxy_headers):283 """Token/Auth processed & claims added to headers"""284 self._decorate_request('AUTHORIZATION',285 "Basic %s" % self.service_pass, env, proxy_headers)286 #now decide how to pass on the call287 if self.app:288 # Pass to downstream WSGI component289 return self.app(env, start_response)290 #.custom_start_response)291 else:292 # We are forwarding to a remote service (no downstream WSGI app)293 req = Request(proxy_headers)294 parsed = urlparse(req.url)295 conn = http_connect(self.service_host,296 self.service_port,297 req.method,298 parsed.path,...

Full Screen

Full Screen

clients.py

Source:clients.py Github

copy

Full Screen

...58 'build_timeout': CONF.dns.build_timeout59 })60 return params61class BaseUnauthedProvider(auth.KeystoneAuthProvider):62 def _decorate_request(self, filters, method, url, headers=None, body=None,63 auth_data=None):64 result = super(BaseUnauthedProvider, self)._decorate_request(65 filters, method, url, headers=headers, body=body,66 auth_data=auth_data)67 url, headers, body = result68 try:69 del headers['X-Auth-Token']70 except KeyError:71 pass72 return url, headers, body73class KeystoneV2UnauthedProvider(auth.KeystoneV2AuthProvider,74 BaseUnauthedProvider):75 def _decorate_request(self, *args, **kwargs):76 return BaseUnauthedProvider._decorate_request(self, *args, **kwargs)77class KeystoneV3UnauthedProvider(auth.KeystoneV3AuthProvider,78 BaseUnauthedProvider):79 def _decorate_request(self, *args, **kwargs):...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run tempest automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful