|
3 | 3 |
|
4 | 4 | from __future__ import annotations |
5 | 5 |
|
6 | | -from typing import Dict, Optional |
| 6 | +from typing import Dict, List, Optional |
7 | 7 |
|
8 | 8 | from pyatlan.client.constants import ( |
| 9 | + CREATE_OAUTH_CLIENT, |
9 | 10 | DELETE_OAUTH_CLIENT, |
10 | 11 | GET_OAUTH_CLIENT_BY_ID, |
11 | 12 | GET_OAUTH_CLIENTS, |
12 | 13 | UPDATE_OAUTH_CLIENT, |
13 | 14 | ) |
14 | | -from pyatlan.model.oauth_clients import OAuthClient, OAuthClientResponse |
| 15 | +from pyatlan.model.oauth_clients import ( |
| 16 | + OAuthClient, |
| 17 | + OAuthClientCreateResponse, |
| 18 | + OAuthClientRequest, |
| 19 | + OAuthClientResponse, |
| 20 | +) |
| 21 | +from pyatlan.model.role import AtlanRole |
15 | 22 |
|
16 | 23 |
|
17 | 24 | class OAuthClientGetAll: |
@@ -158,3 +165,91 @@ def prepare_request(client_id: str) -> tuple: |
158 | 165 | {"client_id": client_id} |
159 | 166 | ).format_path_with_params() |
160 | 167 | return endpoint, None |
| 168 | + |
| 169 | + |
| 170 | +class OAuthClientCreate: |
| 171 | + """Shared logic for creating OAuth clients.""" |
| 172 | + |
| 173 | + @staticmethod |
| 174 | + def resolve_role_name(role: str, available_roles: List[AtlanRole]) -> str: |
| 175 | + """ |
| 176 | + Resolve the user-provided role to the actual API role name. |
| 177 | +
|
| 178 | + The user provides a role description (e.g., 'Admin', 'Member', 'Admins (Connections)') |
| 179 | + and we find the corresponding role name (e.g., '$admin', '$member', '$admin_connections') |
| 180 | + to send in the API payload. |
| 181 | +
|
| 182 | + :param role: user-provided role description |
| 183 | + :param available_roles: list of available roles from the API |
| 184 | + :returns: the actual API role name (e.g., '$admin') |
| 185 | + :raises ValueError: if the role description is not found |
| 186 | + """ |
| 187 | + role_lower = role.lower().strip() |
| 188 | + |
| 189 | + # Build lookup: lowercased description -> role name |
| 190 | + desc_to_name: Dict[str, str] = {} |
| 191 | + available_descriptions: List[str] = [] |
| 192 | + |
| 193 | + for r in available_roles: |
| 194 | + if r.description and r.name: |
| 195 | + desc_to_name[r.description.lower()] = r.name |
| 196 | + available_descriptions.append(r.description) |
| 197 | + |
| 198 | + # Match against description |
| 199 | + if role_lower in desc_to_name: |
| 200 | + return desc_to_name[role_lower] |
| 201 | + |
| 202 | + # No match found - raise error with available descriptions |
| 203 | + raise ValueError( |
| 204 | + f"Role '{role}' not found. Available roles: {', '.join(sorted(available_descriptions))}" |
| 205 | + ) |
| 206 | + |
| 207 | + @staticmethod |
| 208 | + def build_roles_filter() -> str: |
| 209 | + """ |
| 210 | + Build the filter string to fetch workspace and admin-subrole level roles. |
| 211 | +
|
| 212 | + :returns: JSON filter string |
| 213 | + """ |
| 214 | + import json |
| 215 | + |
| 216 | + return json.dumps( |
| 217 | + {"$or": [{"level": "workspace"}, {"level": "admin-subrole"}]} |
| 218 | + ) |
| 219 | + |
| 220 | + @staticmethod |
| 221 | + def prepare_request( |
| 222 | + display_name: str, |
| 223 | + role: str, |
| 224 | + description: Optional[str] = None, |
| 225 | + persona_qns: Optional[List[str]] = None, |
| 226 | + ) -> tuple: |
| 227 | + """ |
| 228 | + Prepare the request for creating an OAuth client. |
| 229 | +
|
| 230 | + Note: The role should already be resolved to the actual API value |
| 231 | + (e.g., '$admin') before calling this method. |
| 232 | +
|
| 233 | + :param display_name: human-readable name for the OAuth client |
| 234 | + :param role: role assigned to the OAuth client (must be the actual API value like '$admin') |
| 235 | + :param description: optional explanation of the OAuth client |
| 236 | + :param persona_qns: qualified names of personas to associate with the OAuth client |
| 237 | + :returns: tuple of (endpoint, request_dict) |
| 238 | + """ |
| 239 | + request = OAuthClientRequest( |
| 240 | + display_name=display_name, |
| 241 | + description=description, |
| 242 | + role=role, |
| 243 | + persona_qns=persona_qns, |
| 244 | + ) |
| 245 | + return CREATE_OAUTH_CLIENT.format_path_with_params(), request |
| 246 | + |
| 247 | + @staticmethod |
| 248 | + def process_response(raw_json: Dict) -> OAuthClientCreateResponse: |
| 249 | + """ |
| 250 | + Process the API response into an OAuthClientCreateResponse. |
| 251 | +
|
| 252 | + :param raw_json: raw response from the API |
| 253 | + :returns: the created OAuthClientCreateResponse (includes client_secret) |
| 254 | + """ |
| 255 | + return OAuthClientCreateResponse(**raw_json) |
0 commit comments