|
1 | 1 | """Profile management for multiple Workato environments.""" |
2 | 2 |
|
| 3 | +import asyncio |
3 | 4 | import contextlib |
4 | 5 | import json |
5 | 6 | import os |
|
9 | 10 | from urllib.parse import urlparse |
10 | 11 |
|
11 | 12 | import asyncclick as click |
| 13 | +import certifi |
12 | 14 | import inquirer |
13 | 15 | import keyring |
14 | 16 |
|
15 | 17 | from keyring.backend import KeyringBackend |
16 | 18 | from keyring.compat import properties |
17 | 19 | from keyring.errors import KeyringError, NoKeyringError |
18 | 20 |
|
| 21 | +from workato_platform_cli import Workato |
| 22 | +from workato_platform_cli.cli.utils.token_input import get_token_with_smart_paste |
| 23 | +from workato_platform_cli.client.workato_api.configuration import Configuration |
| 24 | + |
19 | 25 | from .models import AVAILABLE_REGIONS, ProfileData, ProfilesConfig, RegionInfo |
20 | 26 |
|
21 | 27 |
|
@@ -489,3 +495,62 @@ async def select_region_interactive( |
489 | 495 | return RegionInfo(region="custom", name="Custom URL", url=custom_url) |
490 | 496 |
|
491 | 497 | return selected_region |
| 498 | + |
| 499 | + async def create_profile_interactive( |
| 500 | + self, profile_name: str |
| 501 | + ) -> tuple[ProfileData, str]: |
| 502 | + """Create a new profile with interactive prompts for region and token. |
| 503 | +
|
| 504 | + Performs region selection, token input, credential validation, and returns |
| 505 | + the validated profile data and token. |
| 506 | +
|
| 507 | + Args: |
| 508 | + profile_name: Name of the profile being created |
| 509 | +
|
| 510 | + Returns: |
| 511 | + tuple[ProfileData, str]: The validated profile data and API token |
| 512 | +
|
| 513 | + Raises: |
| 514 | + click.ClickException: If setup is cancelled, token is empty, |
| 515 | + or validation fails |
| 516 | + """ |
| 517 | + # Step 1: Select region |
| 518 | + click.echo("📍 Select your Workato region") |
| 519 | + selected_region = await self.select_region_interactive(profile_name) |
| 520 | + |
| 521 | + if not selected_region: |
| 522 | + raise click.ClickException("Setup cancelled") |
| 523 | + |
| 524 | + # Step 2: Get API token |
| 525 | + token = await asyncio.to_thread( |
| 526 | + get_token_with_smart_paste, |
| 527 | + prompt_text="API token", |
| 528 | + ) |
| 529 | + if not token.strip(): |
| 530 | + raise click.ClickException("API token cannot be empty") |
| 531 | + |
| 532 | + # Step 3: Test authentication and get workspace info |
| 533 | + click.echo("🔄 Validating credentials...") |
| 534 | + api_config = Configuration( |
| 535 | + access_token=token, host=selected_region.url, ssl_ca_cert=certifi.where() |
| 536 | + ) |
| 537 | + |
| 538 | + try: |
| 539 | + async with Workato(configuration=api_config) as workato_api_client: |
| 540 | + user_info = await workato_api_client.users_api.get_workspace_details() |
| 541 | + except Exception as e: |
| 542 | + raise click.ClickException(f"Authentication failed: {e}") from e |
| 543 | + |
| 544 | + # Step 4: Create profile data |
| 545 | + if not selected_region.url: |
| 546 | + raise click.ClickException("Region URL is required") |
| 547 | + |
| 548 | + profile_data = ProfileData( |
| 549 | + region=selected_region.region, |
| 550 | + region_url=selected_region.url, |
| 551 | + workspace_id=user_info.id, |
| 552 | + ) |
| 553 | + |
| 554 | + click.echo(f"✅ Authenticated as: {user_info.name}") |
| 555 | + |
| 556 | + return profile_data, token |
0 commit comments