|
| 1 | +"""Code used to convert from/to apispec and models.""" |
| 2 | + |
| 3 | +from pathlib import PurePosixPath |
| 4 | + |
| 5 | +from renku_data_services.base_models.core import RESET, ResetType |
| 6 | +from renku_data_services.session import apispec, models |
| 7 | + |
| 8 | + |
| 9 | +def environment_update_from_patch(data: apispec.EnvironmentPatch) -> models.EnvironmentUpdate: |
| 10 | + """Create an update object from an apispec or any other pydantic model.""" |
| 11 | + data_dict = data.model_dump(exclude_unset=True, mode="json") |
| 12 | + working_directory: PurePosixPath | None = None |
| 13 | + if data.working_directory is not None: |
| 14 | + working_directory = PurePosixPath(data.working_directory) |
| 15 | + mount_directory: PurePosixPath | None = None |
| 16 | + if data.mount_directory is not None: |
| 17 | + mount_directory = PurePosixPath(data.mount_directory) |
| 18 | + # NOTE: If the args or command are present in the data_dict and they are None they were passed in by the user. |
| 19 | + # The None specifically passed by the user indicates that the value should be removed from the DB. |
| 20 | + args = RESET if "args" in data_dict and data_dict["args"] is None else data.args |
| 21 | + command = RESET if "command" in data_dict and data_dict["command"] is None else data.command |
| 22 | + return models.EnvironmentUpdate( |
| 23 | + name=data.name, |
| 24 | + description=data.description, |
| 25 | + container_image=data.container_image, |
| 26 | + default_url=data.default_url, |
| 27 | + port=data.port, |
| 28 | + working_directory=working_directory, |
| 29 | + mount_directory=mount_directory, |
| 30 | + uid=data.uid, |
| 31 | + gid=data.gid, |
| 32 | + args=args, |
| 33 | + command=command, |
| 34 | + ) |
| 35 | + |
| 36 | + |
| 37 | +def launcher_update_from_patch( |
| 38 | + data: apispec.SessionLauncherPatch, |
| 39 | + current_launcher: models.SessionLauncher | None = None, |
| 40 | +) -> models.SessionLauncherUpdate: |
| 41 | + """Create an update object from an apispec or any other pydantic model.""" |
| 42 | + data_dict = data.model_dump(exclude_unset=True, mode="json") |
| 43 | + environment: str | models.EnvironmentUpdate | models.UnsavedEnvironment | None = None |
| 44 | + if ( |
| 45 | + isinstance(data.environment, apispec.EnvironmentPatchInLauncher) |
| 46 | + and current_launcher is not None |
| 47 | + and current_launcher.environment.environment_kind == models.EnvironmentKind.GLOBAL |
| 48 | + and data.environment.environment_kind == apispec.EnvironmentKind.CUSTOM |
| 49 | + ): |
| 50 | + # This means that the global environment is being swapped for a custom one, |
| 51 | + # so we have to create a brand new environment, but we have to validate here. |
| 52 | + validated_env = apispec.EnvironmentPostInLauncher.model_validate(data_dict["environment"]) |
| 53 | + environment = models.UnsavedEnvironment( |
| 54 | + name=validated_env.name, |
| 55 | + description=validated_env.description, |
| 56 | + container_image=validated_env.container_image, |
| 57 | + default_url=validated_env.default_url, |
| 58 | + port=validated_env.port, |
| 59 | + working_directory=PurePosixPath(validated_env.working_directory), |
| 60 | + mount_directory=PurePosixPath(validated_env.mount_directory), |
| 61 | + uid=validated_env.uid, |
| 62 | + gid=validated_env.gid, |
| 63 | + environment_kind=models.EnvironmentKind(validated_env.environment_kind.value), |
| 64 | + args=validated_env.args, |
| 65 | + command=validated_env.command, |
| 66 | + ) |
| 67 | + elif isinstance(data.environment, apispec.EnvironmentPatchInLauncher): |
| 68 | + environment = environment_update_from_patch(data.environment) |
| 69 | + elif isinstance(data.environment, apispec.EnvironmentIdOnlyPatch): |
| 70 | + environment = data.environment.id |
| 71 | + resource_class_id: int | None | ResetType = None |
| 72 | + if "resource_class_id" in data_dict and data_dict["resource_class_id"] is None: |
| 73 | + # NOTE: This means that the resource class set in the DB should be removed so that the |
| 74 | + # default resource class currently set in the CRC will be used. |
| 75 | + resource_class_id = RESET |
| 76 | + else: |
| 77 | + resource_class_id = data_dict.get("resource_class_id") |
| 78 | + return models.SessionLauncherUpdate( |
| 79 | + name=data_dict.get("name"), |
| 80 | + description=data_dict.get("description"), |
| 81 | + environment=environment, |
| 82 | + resource_class_id=resource_class_id, |
| 83 | + ) |
0 commit comments