Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ repos:
exclude: ^(ansys/rep/client/jms/resource/|ansys/rep/client/auth/resource/)

- repo: https://github.com/pycqa/isort
rev: 5.10.1
rev: 5.11.5
hooks:
- id: isort
exclude: ^(ansys/rep/client/jms/resource/|ansys/rep/client/auth/resource/)
Expand Down
23 changes: 15 additions & 8 deletions ansys/rep/client/jms/api/base.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import json
import logging
from typing import List
from typing import List, Type

from requests import Session

Expand All @@ -10,7 +10,9 @@
log = logging.getLogger(__name__)


def get_objects(session: Session, url: str, obj_type: Object, as_objects=True, **query_params):
def get_objects(
session: Session, url: str, obj_type: Type[Object], as_objects=True, **query_params
):

rest_name = obj_type.Meta.rest_name
url = f"{url}/{rest_name}"
Expand All @@ -29,7 +31,7 @@ def get_objects(session: Session, url: str, obj_type: Object, as_objects=True, *


def get_object(
session: Session, url: str, obj_type: Object, id: str, as_object=True, **query_params
session: Session, url: str, obj_type: Type[Object], id: str, as_object=True, **query_params
):

rest_name = obj_type.Meta.rest_name
Expand Down Expand Up @@ -80,16 +82,21 @@ def create_objects(


def update_objects(
session: Session, url: str, objects: List[Object], as_objects=True, **query_params
session: Session,
url: str,
objects: List[Object],
obj_type: Type[Object],
as_objects=True,
**query_params,
):
if not objects:
return []

are_same = [o.__class__ == objects[0].__class__ for o in objects[1:]]
if objects is None:
raise ClientError("objects can't be None")

are_same = [o.__class__ == obj_type for o in objects]
if not all(are_same):
raise ClientError("Mixed object types")

obj_type = objects[0].__class__
rest_name = obj_type.Meta.rest_name

url = f"{url}/{rest_name}"
Expand Down
14 changes: 12 additions & 2 deletions ansys/rep/client/jms/api/jms_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,9 @@ def update_evaluators(
self, evaluators: List[Evaluator], as_objects=True, **query_params
) -> List[Evaluator]:
"""Update evaluators configuration"""
return update_objects(self.client.session, self.url, evaluators, as_objects, **query_params)
return update_objects(
self.client.session, self.url, evaluators, Evaluator, as_objects, **query_params
)

################################################################
# Task Definition Templates
Expand Down Expand Up @@ -146,7 +148,14 @@ def update_task_definition_templates(
templates (list of :class:`ansys.rep.client.jms.TaskDefinitionTemplate`):
A list of task definition templates
"""
return update_objects(self.client.session, self.url, templates, as_objects, **query_params)
return update_objects(
self.client.session,
self.url,
templates,
TaskDefinitionTemplate,
as_objects,
*query_params,
)

def delete_task_definition_templates(self, templates: List[TaskDefinitionTemplate]):
"""Delete existing task definition templates
Expand Down Expand Up @@ -180,6 +189,7 @@ def update_task_definition_template_permissions(
self.client.session,
f"{self.url}/task_definition_templates/{template_id}",
permissions,
Permission,
as_objects,
)

Expand Down
36 changes: 21 additions & 15 deletions ansys/rep/client/jms/api/project_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import logging
import os
from pathlib import Path
from typing import Callable, List
from typing import Callable, List, Type

from cachetools import TTLCache, cached
from marshmallow.utils import missing
Expand Down Expand Up @@ -170,7 +170,7 @@ def create_parameter_definitions(
def update_parameter_definitions(
self, parameter_definitions: List[ParameterDefinition], as_objects=True
) -> List[ParameterDefinition]:
return self._update_objects(parameter_definitions, as_objects)
return self._update_objects(parameter_definitions, ParameterDefinition, as_objects)

def delete_parameter_definitions(self, parameter_definitions: List[ParameterDefinition]):
return self._delete_objects(parameter_definitions)
Expand All @@ -188,7 +188,7 @@ def create_parameter_mappings(
def update_parameter_mappings(
self, parameter_mappings: List[ParameterMapping], as_objects=True
) -> List[ParameterMapping]:
return self._update_objects(parameter_mappings, as_objects=as_objects)
return self._update_objects(parameter_mappings, ParameterMapping, as_objects=as_objects)

def delete_parameter_mappings(self, parameter_mappings: List[ParameterMapping]):
return self._delete_objects(parameter_mappings)
Expand All @@ -206,7 +206,7 @@ def create_task_definitions(
def update_task_definitions(
self, task_definitions: List[TaskDefinition], as_objects=True
) -> List[TaskDefinition]:
return self._update_objects(task_definitions, as_objects=as_objects)
return self._update_objects(task_definitions, TaskDefinition, as_objects=as_objects)

def delete_task_definitions(self, task_definitions: List[TaskDefinition]):
return self._delete_objects(task_definitions)
Expand All @@ -224,7 +224,7 @@ def create_job_definitions(
def update_job_definitions(
self, job_definitions: List[JobDefinition], as_objects=True
) -> List[JobDefinition]:
return self._update_objects(job_definitions, as_objects=as_objects)
return self._update_objects(job_definitions, JobDefinition, as_objects=as_objects)

def delete_job_definitions(self, job_definitions: List[JobDefinition]):
return self._delete_objects(job_definitions)
Expand Down Expand Up @@ -267,7 +267,7 @@ def update_jobs(self, jobs: List[Job], as_objects=True) -> List[Job]:
Returns:
List of :class:`ansys.rep.client.jms.Job` or list of dict if `as_objects` is True
"""
return self._update_objects(jobs, as_objects=as_objects)
return self._update_objects(jobs, Job, as_objects=as_objects)

def delete_jobs(self, jobs: List[Job]):
"""Delete existing jobs
Expand Down Expand Up @@ -298,7 +298,7 @@ def get_tasks(self, as_objects=True, **query_params) -> List[Task]:
return self._get_objects(Task, as_objects=as_objects, **query_params)

def update_tasks(self, tasks: List[Task], as_objects=True) -> List[Task]:
return self._update_objects(tasks, as_objects=as_objects)
return self._update_objects(tasks, Task, as_objects=as_objects)

################################################################
# Selections
Expand All @@ -313,7 +313,7 @@ def create_job_selections(
def update_job_selections(
self, selections: List[JobSelection], as_objects=True
) -> List[JobSelection]:
return self._update_objects(selections, as_objects=as_objects)
return self._update_objects(selections, JobSelection, as_objects=as_objects)

def delete_job_selections(self, selections: List[JobSelection]):
return self._delete_objects(selections)
Expand All @@ -327,7 +327,7 @@ def create_algorithms(self, algorithms: List[Algorithm], as_objects=True) -> Lis
return self._create_objects(algorithms, as_objects=as_objects)

def update_algorithms(self, algorithms: List[Algorithm], as_objects=True) -> List[Algorithm]:
return self._update_objects(algorithms, as_objects=as_objects)
return self._update_objects(algorithms, Algorithm, as_objects=as_objects)

def delete_algorithms(self, algorithms: List[Algorithm]):
return self._delete_objects(algorithms)
Expand All @@ -338,7 +338,7 @@ def get_permissions(self, as_objects=True) -> List[Permission]:
return self._get_objects(Permission, as_objects=as_objects, fields=None)

def update_permissions(self, permissions: List[Permission], as_objects=True):
return self._update_objects(permissions, as_objects=as_objects)
return self._update_objects(permissions, Permission, as_objects=as_objects)

################################################################
# License contexts
Expand All @@ -357,7 +357,7 @@ def create_license_contexts(self, as_objects=True) -> List[LicenseContext]:
return objects

def update_license_contexts(self, license_contexts, as_objects=True) -> List[LicenseContext]:
return self._update_objects(self, license_contexts, as_objects=as_objects)
return self._update_objects(self, license_contexts, LicenseContext, as_objects=as_objects)

def delete_license_contexts(self):
rest_name = LicenseContext.Meta.rest_name
Expand All @@ -371,8 +371,12 @@ def _get_objects(self, obj_type: Object, as_objects=True, **query_params):
def _create_objects(self, objects: List[Object], as_objects=True, **query_params):
return create_objects(self.client.session, self.url, objects, as_objects, **query_params)

def _update_objects(self, objects: List[Object], as_objects=True, **query_params):
return update_objects(self.client.session, self.url, objects, as_objects, **query_params)
def _update_objects(
self, objects: List[Object], obj_type: Type[Object], as_objects=True, **query_params
):
return update_objects(
self.client.session, self.url, objects, obj_type, as_objects, **query_params
)

def _delete_objects(self, objects: List[Object]):
delete_objects(self.client.session, self.url, objects)
Expand Down Expand Up @@ -440,7 +444,7 @@ def create_files(project_api: ProjectApi, files, as_objects=True) -> List[File]:

# (4) Update corresponding file resources in JMS with hashes of uploaded files
created_files = update_objects(
project_api.client.session, project_api.url, created_files, as_objects=as_objects
project_api.client.session, project_api.url, created_files, File, as_objects=as_objects
)

return created_files
Expand All @@ -450,7 +454,9 @@ def update_files(project_api: ProjectApi, files: List[File], as_objects=True) ->
# Upload files first if there are any src parameters
_upload_files(project_api, files)
# Update file resources in JMS
return update_objects(project_api.client.session, project_api.url, files, as_objects=as_objects)
return update_objects(
project_api.client.session, project_api.url, files, File, as_objects=as_objects
)


def _download_file(
Expand Down
14 changes: 4 additions & 10 deletions tests/auth/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
import logging
import uuid

from keycloak.exceptions import KeycloakError

from ansys.rep.client import Client
from ansys.rep.client.auth import AuthApi, User
from tests.rep_test import REPTestCase
Expand Down Expand Up @@ -58,7 +56,7 @@ def test_auth_client(self):
usernames = [x.username for x in users]
self.assertNotIn(new_user.username, usernames)

def test_auth_api_exceptions(self):
def test_get_users(self):

api = AuthApi(Client(self.rep_url, username=self.username, password=self.password))
users = api.get_users()
Expand All @@ -78,18 +76,14 @@ def test_auth_api_exceptions(self):
last_name="User",
)
new_user = api.create_user(new_user)
users = api.get_users()

# use non-admin user to get users
api_non_admin = AuthApi(
Client(self.rep_url, username=username, password="test_auth_client")
)
except_obj = None
try:
users = api_non_admin.get_users()
except KeycloakError as e:
except_obj = e

self.assertEqual(except_obj.response_code, 403)
users2 = api_non_admin.get_users()
self.assertEqual(len(users), len(users2))

api.delete_user(new_user)
users = api.get_users()
Expand Down
21 changes: 21 additions & 0 deletions tests/jms/test_task_definition_templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,27 @@ def test_template_permissions(self):
# Delete user
auth_api.delete_user(user1)

def test_template_permissions_update(self):

client = self.client()
jms_api = JmsApi(client)

# create new template and check default permissions
template = TaskDefinitionTemplate(name="my_template", version=uuid.uuid4())
template = jms_api.create_task_definition_templates([template])[0]
permissions = jms_api.get_task_definition_template_permissions(template_id=template.id)
self.assertEqual(len(permissions), 1)
self.assertEqual(permissions[0].permission_type, "user")

# remove permissions
permissions = []
permissions = jms_api.update_task_definition_template_permissions(
template_id=template.id, permissions=permissions
)
self.assertEqual(len(permissions), 0)
permissions = jms_api.get_task_definition_template_permissions(template_id=template.id)
self.assertEqual(len(permissions), 0)

def test_template_anyone_permission(self):

client = self.client()
Expand Down