def is_loaded(self, project_id: Optional[str] = None, project_name: Optional[str] = None) -> bool: """Check if project is loaded, by passing project ID or name, returns True or False. Args: project_id: Project ID project_name: Project name """ if project_id is None and project_name is None: helper.exception_handler( "Please specify either 'project_name' or 'project_id' argument." ) if project_id is None: project_list = Project._list_project_ids(self.connection, name=project_name) if project_list: project_id = project_list[0] else: msg = f"There is no project with the given name: '{project_name}'" raise ValueError(msg) nodes = self.list_nodes(project=project_id) loaded = False for node in nodes: status = node['projects'][0]['status'] loaded = True if status == 'loaded' else False if loaded: break return loaded
def revoke_from(self, members: Union["UserOrGroup", List["UserOrGroup"]], project: Union["Project", str]) -> None: """Remove users/user groups from a Security Role. Args: members(list): List of objects or IDs of Users or User Groups which will be removed from this Security Role. project(Project, str): Project object or name to which this removal will apply. """ from mstrio.server.project import Project from mstrio.users_and_groups.user import User from mstrio.users_and_groups.user_group import UserGroup if isinstance(project, Project): project_id = project.id project_name = project.name elif isinstance(project, str): project_list = Project._list_projects(connection=self.connection, to_dictionary=True, name=project) if project_list: project_id = project_list[0]['id'] project_name = project_list[0]['name'] else: helper.exception_handler( f"Project name '{project}' does not exist.") else: helper.exception_handler( "Project parameter must be of type str or Project.", TypeError) # create list of objects from strings/objects/lists members_list = members if isinstance(members, list) else [members] members_list = [ obj.id if isinstance(obj, (User, UserGroup)) else str(obj) for obj in members_list ] existing_ids = [ obj['id'] for obj in self.list_members(project_name=project_name) ] succeeded = list(set(members_list).intersection(set(existing_ids))) failed = list(set(members_list) - set(succeeded)) value = {"projectId": project_id, "memberIds": members_list} self._update_nested_properties( objects=value, path="members", op='remove', ) if succeeded and config.verbose: logger.info( f"Revoked Security Role '{self.name}' from {succeeded}") if failed and config.verbose: logger.warning( f"Security Role '{self.name}' does not have member(s) {failed}" )
def create_project(self, name: str, description: Optional[str] = None, force: bool = False) -> Optional["Project"]: """Create a new project on the environment. Args: name: Name of Project. description: Description of Application. force: If `True`, overrides the prompt. """ return Project._create(self.connection, name, description, force)
def load_project(self, project: Union[str, "Project"], on_nodes: Optional[Union[str, List[str]]] = None) -> None: """Request to load the project onto the chosen cluster nodes. If nodes are not specified, the project will be loaded on all nodes. Args: project: name or object of project which will be loaded on_nodes: name of node or nodes, if not passed, project will be loaded on all of the nodes """ from mstrio.server.project import Project project_name = project.name if isinstance(project, Project) else project project = Project._list_projects(self.connection, name=project_name)[0] project.load(on_nodes=on_nodes)
def list_loaded_projects(self, to_dictionary: bool = False, **filters) -> Union[List["Project"], List[dict]]: """Return list of all loaded project objects or project dicts if `to_dictionary=True` that the user has access to. Optionally filter the Projects by specifying the `filters` keyword arguments. Args: to_dictionary: If True, returns list of project dicts **filters: Available filter parameters: ['acg', 'id', 'name', 'status', 'alias', 'description', 'date_created', 'date_modified', 'owner'] """ return Project._list_loaded_projects( connection=self.connection, to_dictionary=to_dictionary, **filters, )
def unload_project( self, project: Union[str, "Project"], on_nodes: Optional[Union[str, List[str]]] = None) -> None: """Request to unload the project from the chosen cluster nodes. If nodes are not specified, the project will be unloaded on all nodes. The unload action cannot be performed until all jobs and connections for project are completed. Once these processes have finished, pending project will be automatically unloaded. Args: project: name or object of project which will be unloaded on_nodes: name of node or nodes, if not passed, project will be unloaded on all of the nodes """ from mstrio.server.project import Project project_name = project.name if isinstance(project, Project) else project project = Project._list_projects(self.connection, name=project_name)[0] project.unload(on_nodes=on_nodes)
def list_projects(self, to_dictionary: bool = False, limit: Optional[int] = None, **filters) -> Union[List["Project"], List[dict]]: """Return list of project objects or project dicts if `to_dictionary=True`. Optionally filter the Projects by specifying the `filters` keyword arguments. Args: to_dictionary: If True returns list of project dicts. limit: limit the number of elements returned. If `None`, all objects are returned. **filters: Available filter parameters: ['name', 'id', 'description', 'date_created', 'date_modified', 'owner']. """ return Project._list_projects( connection=self.connection, to_dictionary=to_dictionary, limit=limit, **filters, )
def _project_id_check(connection, project_id, project_name): """Check if the project name exists and returns the project ID. Args: connection(object): MicroStrategy connection object project_id: Project ID project_name: Project name """ if project_id is None and project_name is None: msg = ("Please specify either 'project_name' or 'project_id' " "parameter in the constructor.") helper.exception_handler(msg) if project_id is None: project_loaded_list = Project._list_loaded_projects( connection, to_dictionary=True, name=project_name) try: project_id = project_loaded_list[0]['id'] except IndexError: helper.exception_handler( f"There is no project with the given name: '{project_name}'", exception_type=ValueError) return project_id