|
| 1 | +""" |
| 2 | +Public API for managing execution environments on Posit Connect. |
| 3 | +""" |
| 4 | + |
| 5 | +from __future__ import annotations |
| 6 | + |
| 7 | +from typing import Optional, Union |
| 8 | + |
| 9 | +from .api import RSConnectClient, RSConnectServer, SPCSConnectServer |
| 10 | +from .models import ( |
| 11 | + EnvironmentCreateInput, |
| 12 | + EnvironmentInstallation, |
| 13 | + EnvironmentInstallations, |
| 14 | + EnvironmentPermissionInput, |
| 15 | + EnvironmentPermissionV1, |
| 16 | + EnvironmentUpdateInput, |
| 17 | + EnvironmentV1, |
| 18 | + EnvironmentVolumeMount, |
| 19 | +) |
| 20 | + |
| 21 | + |
| 22 | +def list_environments( |
| 23 | + connect_server: Union[RSConnectServer, SPCSConnectServer], |
| 24 | +) -> list[EnvironmentV1]: |
| 25 | + with RSConnectClient(connect_server) as client: |
| 26 | + return client.environment_list() |
| 27 | + |
| 28 | + |
| 29 | +def get_environment( |
| 30 | + connect_server: Union[RSConnectServer, SPCSConnectServer], |
| 31 | + guid: str, |
| 32 | +) -> EnvironmentV1: |
| 33 | + with RSConnectClient(connect_server) as client: |
| 34 | + return client.environment_get(guid) |
| 35 | + |
| 36 | + |
| 37 | +def create_environment( |
| 38 | + connect_server: Union[RSConnectServer, SPCSConnectServer], |
| 39 | + image: str, |
| 40 | + title: Optional[str] = None, |
| 41 | + description: Optional[str] = None, |
| 42 | + matching: Optional[str] = None, |
| 43 | + supervisor: Optional[str] = None, |
| 44 | + python: Optional[list[EnvironmentInstallation]] = None, |
| 45 | + quarto: Optional[list[EnvironmentInstallation]] = None, |
| 46 | + r: Optional[list[EnvironmentInstallation]] = None, |
| 47 | + tensorflow: Optional[list[EnvironmentInstallation]] = None, |
| 48 | + volume_mounts: Optional[list[EnvironmentVolumeMount]] = None, |
| 49 | + user_guids: Optional[list[str]] = None, |
| 50 | + group_guids: Optional[list[str]] = None, |
| 51 | +) -> EnvironmentV1: |
| 52 | + body: EnvironmentCreateInput = { |
| 53 | + "cluster_name": "Kubernetes", |
| 54 | + "name": image, |
| 55 | + } |
| 56 | + if title is not None: |
| 57 | + body["title"] = title |
| 58 | + if description is not None: |
| 59 | + body["description"] = description |
| 60 | + if matching is not None: |
| 61 | + body["matching"] = matching |
| 62 | + if supervisor is not None: |
| 63 | + body["supervisor"] = supervisor |
| 64 | + if python is not None: |
| 65 | + body["python"] = _make_installations(python) |
| 66 | + if quarto is not None: |
| 67 | + body["quarto"] = _make_installations(quarto) |
| 68 | + if r is not None: |
| 69 | + body["r"] = _make_installations(r) |
| 70 | + if tensorflow is not None: |
| 71 | + body["tensorflow"] = _make_installations(tensorflow) |
| 72 | + if volume_mounts is not None: |
| 73 | + body["volume_mounts"] = volume_mounts |
| 74 | + |
| 75 | + with RSConnectClient(connect_server) as client: |
| 76 | + result = client.environment_create(body) |
| 77 | + if user_guids is not None or group_guids is not None: |
| 78 | + _sync_permissions(client, result["guid"], user_guids, group_guids) |
| 79 | + return client.environment_get(result["guid"]) |
| 80 | + |
| 81 | + |
| 82 | +def update_environment( |
| 83 | + connect_server: Union[RSConnectServer, SPCSConnectServer], |
| 84 | + guid: str, |
| 85 | + title: Optional[str] = None, |
| 86 | + description: Optional[str] = None, |
| 87 | + matching: Optional[str] = None, |
| 88 | + supervisor: Optional[str] = None, |
| 89 | + python: Optional[list[EnvironmentInstallation]] = None, |
| 90 | + quarto: Optional[list[EnvironmentInstallation]] = None, |
| 91 | + r: Optional[list[EnvironmentInstallation]] = None, |
| 92 | + tensorflow: Optional[list[EnvironmentInstallation]] = None, |
| 93 | + volume_mounts: Optional[list[EnvironmentVolumeMount]] = None, |
| 94 | + user_guids: Optional[list[str]] = None, |
| 95 | + group_guids: Optional[list[str]] = None, |
| 96 | +) -> EnvironmentV1: |
| 97 | + with RSConnectClient(connect_server) as client: |
| 98 | + existing = client.environment_get(guid) |
| 99 | + |
| 100 | + body: EnvironmentUpdateInput = { |
| 101 | + "title": title if title is not None else existing["title"], |
| 102 | + "description": description if description is not None else existing["description"], |
| 103 | + "matching": matching if matching is not None else existing["matching"], |
| 104 | + "supervisor": supervisor if supervisor is not None else existing["supervisor"], |
| 105 | + "python": _make_installations(python) if python is not None else existing["python"], |
| 106 | + "quarto": _make_installations(quarto) if quarto is not None else existing["quarto"], |
| 107 | + "r": _make_installations(r) if r is not None else existing["r"], |
| 108 | + "tensorflow": _make_installations(tensorflow) if tensorflow is not None else existing["tensorflow"], |
| 109 | + "volume_mounts": volume_mounts if volume_mounts is not None else existing["volume_mounts"], |
| 110 | + } |
| 111 | + |
| 112 | + result = client.environment_update(guid, body) |
| 113 | + |
| 114 | + if user_guids is not None or group_guids is not None: |
| 115 | + _sync_permissions(client, guid, user_guids, group_guids) |
| 116 | + return client.environment_get(guid) |
| 117 | + |
| 118 | + return result |
| 119 | + |
| 120 | + |
| 121 | +def delete_environment( |
| 122 | + connect_server: Union[RSConnectServer, SPCSConnectServer], |
| 123 | + guid: str, |
| 124 | +) -> None: |
| 125 | + with RSConnectClient(connect_server) as client: |
| 126 | + client.environment_delete(guid) |
| 127 | + |
| 128 | + |
| 129 | +def _make_installations(items: list[EnvironmentInstallation]) -> EnvironmentInstallations: |
| 130 | + return {"installations": items} |
| 131 | + |
| 132 | + |
| 133 | +def _sync_permissions( |
| 134 | + client: RSConnectClient, |
| 135 | + env_guid: str, |
| 136 | + user_guids: Optional[list[str]], |
| 137 | + group_guids: Optional[list[str]], |
| 138 | +) -> list[EnvironmentPermissionV1]: |
| 139 | + existing = client.environment_permission_list(env_guid) |
| 140 | + |
| 141 | + desired_users = set(user_guids or []) |
| 142 | + desired_groups = set(group_guids or []) |
| 143 | + |
| 144 | + existing_users = {p["user_guid"]: p for p in existing if p["user_guid"] is not None} |
| 145 | + existing_groups = {p["group_guid"]: p for p in existing if p["group_guid"] is not None} |
| 146 | + |
| 147 | + results: list[EnvironmentPermissionV1] = [] |
| 148 | + for g in desired_users - set(existing_users.keys()): |
| 149 | + body: EnvironmentPermissionInput = {"user_guid": g} |
| 150 | + results.append(client.environment_permission_add(env_guid, body)) |
| 151 | + for g in desired_groups - set(existing_groups.keys()): |
| 152 | + body = {"group_guid": g} |
| 153 | + results.append(client.environment_permission_add(env_guid, body)) |
| 154 | + |
| 155 | + for g in set(existing_users.keys()) - desired_users: |
| 156 | + client.environment_permission_delete(env_guid, existing_users[g]["guid"]) |
| 157 | + for g in set(existing_groups.keys()) - desired_groups: |
| 158 | + client.environment_permission_delete(env_guid, existing_groups[g]["guid"]) |
| 159 | + |
| 160 | + return results |
0 commit comments