def test_successful(self): """Tests calling WorkspaceConfiguration constructor successfully with all information.""" # No exception is success config = WorkspaceConfiguration({ 'broker': { 'type': 'host', 'host_path': '/host/path', }, }) config.validate_broker()
def test_bare_min(self): """Tests calling WorkspaceConfiguration constructor with bare minimum JSON.""" # No exception is success config = WorkspaceConfiguration({ 'broker': { 'type': 'host', 'host_path': '/the/path', }, }) config.validate_broker()
def validate_workspace(self, name, json_config): """Validates a new workspace prior to attempting a save :param name: The identifying name of a Workspace to validate :type name: string :param json_config: The Workspace configuration :type json_config: dict :returns: A list of warnings discovered during validation. :rtype: list[:class:`storage.configuration.workspace_configuration.ValidationWarning`] :raises :class:`storage.configuration.exceptions.InvalidWorkspaceConfiguration`: If the configuration is invalid """ warnings = [] # Validate the configuration, no exception is success config = WorkspaceConfiguration(json_config) # Check for issues when changing an existing workspace configuration try: workspace = Workspace.objects.get(name=name) if (json_config['broker'] and workspace.json_config['broker'] and json_config['broker']['type'] != workspace.json_config['broker']['type']): warnings.append(ValidationWarning('broker_type', 'Changing the broker type may disrupt queued/running jobs.')) except Workspace.DoesNotExist: pass # Add broker-specific warnings warnings.extend(config.validate_broker()) return warnings
def get_broker(self): """Returns the configured broker for this workspace :returns: The configured broker :rtype: :class:`storage.brokers.broker.Broker` """ if not hasattr(self, '_broker'): ws_config = WorkspaceConfiguration(self.json_config) ws_config.validate_broker() broker_config = self.json_config['broker'] broker_type = broker_config['type'] broker = get_broker(broker_type) broker.load_configuration(broker_config) self._broker = broker return self._broker
def edit_workspace(self, workspace_id, title=None, description=None, json_config=None, base_url=None, is_active=None): """Edits the given Workspace and saves the changes in the database. All database changes occur in an atomic transaction. An argument of None for a field indicates that the field should not change. :param workspace_id: The unique identifier of the Workspace to edit :type workspace_id: int :param title: The human-readable name of this Workspace :type title: string :param description: A description of this Workspace :type description: string :param json_config: The Workspace configuration :type json_config: dict :param base_url: The URL prefix used to download files stored in the Workspace. :type base_url: string :param is_active: Whether or not the Workspace is available for use. :type is_active: bool :raises :class:`storage.configuration.exceptions.InvalidWorkspaceConfiguration`: If the configuration is invalid """ workspace = Workspace.objects.get(pk=workspace_id) # Validate the configuration, no exception is success if json_config: config = WorkspaceConfiguration(json_config) config.validate_broker() workspace.json_config = config.get_dict() # Update editable fields if title: workspace.title = title if description: workspace.description = description if base_url: workspace.base_url = base_url if is_active is not None: workspace.is_active = is_active workspace.save()
def create_workspace(self, name, title, description, json_config, base_url=None, is_active=True): """Creates a new Workspace with the given configuration and returns the new Workspace model. The Workspace model will be saved in the database and all changes to the database will occur in an atomic transaction. :param name: The identifying name of this Workspace :type name: string :param title: The human-readable name of this Workspace :type title: string :param description: A description of this Workspace :type description: string :param json_config: The Workspace configuration :type json_config: dict :param base_url: The URL prefix used to download files stored in the Workspace. :type base_url: string :param is_active: Whether or not the Workspace is available for use. :type is_active: bool :returns: The new Workspace :rtype: :class:`storage.models.Workspace` :raises :class:`storage.configuration.exceptions.InvalidWorkspaceConfiguration`: If the configuration is invalid """ # Validate the configuration, no exception is success config = WorkspaceConfiguration(json_config) config.validate_broker() workspace = Workspace() workspace.name = name workspace.title = title workspace.description = description workspace.json_config = config.get_dict() workspace.base_url = base_url workspace.is_active = is_active workspace.save() return workspace
def _detect_workspaces(self, workspace_list): """Parses, validates, and returns workspace information for the given workspaces :param workspace_list: The workspace list that was given :type workspace_list: [dict] :return: All workspaces by given name with associated broker and volume_path :rtype: dict """ workspaces = {} for workspace in workspace_list: name = workspace.keys()[0] wrkspc = WorkspaceConfiguration(workspace[name]) wrkspc.validate_broker() valid_wrkspc = wrkspc.get_dict() workspaces[name] = { 'broker': get_broker(valid_wrkspc['broker']['type']), 'volume_path': valid_wrkspc['broker']['host_path'] } return workspaces