|
| 1 | +# Copyright The Lightning team. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import sys |
| 16 | + |
| 17 | +import click |
| 18 | +import rich |
| 19 | +from lightning_cloud.openapi import ProjectIdDataConnectionsBody |
| 20 | +from rich.live import Live |
| 21 | +from rich.spinner import Spinner |
| 22 | +from rich.text import Text |
| 23 | + |
| 24 | +from lightning_app.utilities.app_helpers import Logger |
| 25 | +from lightning_app.utilities.cli_helpers import _error_and_exit |
| 26 | +from lightning_app.utilities.cloud import _get_project |
| 27 | +from lightning_app.utilities.network import LightningClient |
| 28 | + |
| 29 | +logger = Logger(__name__) |
| 30 | + |
| 31 | + |
| 32 | +@click.argument("name", required=True) |
| 33 | +@click.argument("region", required=True) |
| 34 | +@click.argument("source", required=True) |
| 35 | +@click.argument("destination", required=False) |
| 36 | +@click.argument("project_name", required=False) |
| 37 | +def connect_data( |
| 38 | + name: str, |
| 39 | + region: str, |
| 40 | + source: str, |
| 41 | + destination: str = "", |
| 42 | + project_name: str = "", |
| 43 | +) -> None: |
| 44 | + """Create a new data connection.""" |
| 45 | + |
| 46 | + if sys.platform == "win32": |
| 47 | + _error_and_exit("Data connection isn't supported on windows. Open an issue on Github.") |
| 48 | + |
| 49 | + with Live(Spinner("point", text=Text("pending...", style="white")), transient=True) as live: |
| 50 | + |
| 51 | + live.stop() |
| 52 | + |
| 53 | + client = LightningClient() |
| 54 | + projects = client.projects_service_list_memberships() |
| 55 | + |
| 56 | + project_id = None |
| 57 | + |
| 58 | + for project in projects.memberships: |
| 59 | + |
| 60 | + if project.name == project_name: |
| 61 | + project_id = project.project_id |
| 62 | + break |
| 63 | + |
| 64 | + if project_id is None: |
| 65 | + project_id = _get_project(client).project_id |
| 66 | + |
| 67 | + if not source.startswith("s3://"): |
| 68 | + return _error_and_exit( |
| 69 | + "Only public S3 folders are supported for now. Please, open a Github issue with your use case." |
| 70 | + ) |
| 71 | + |
| 72 | + try: |
| 73 | + _ = client.data_connection_service_create_data_connection( |
| 74 | + body=ProjectIdDataConnectionsBody( |
| 75 | + name=name, |
| 76 | + region=region, |
| 77 | + source=source, |
| 78 | + destination=destination, |
| 79 | + ), |
| 80 | + project_id=project_id, |
| 81 | + ) |
| 82 | + |
| 83 | + # Note: Expose through lightning show data {DATA_NAME} |
| 84 | + # response = client.data_connection_service_list_data_connection_artifacts( |
| 85 | + # project_id=project_id, |
| 86 | + # id=response.id, |
| 87 | + # ) |
| 88 | + # print(response) |
| 89 | + except Exception: |
| 90 | + _error_and_exit("The data connection creation failed.") |
| 91 | + |
| 92 | + rich.print(f"[green]Succeeded[/green]: You have created a new data connection {name}.") |
0 commit comments