Skip to content

Commit fc4c2c8

Browse files
authored
Refactor configuration and profile usage (#4)
1 parent 20d5c55 commit fc4c2c8

6 files changed

Lines changed: 169 additions & 110 deletions

File tree

README.md

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,6 @@ CLI `cloud` commands require a [Griptape Cloud](https://www.griptape.ai/griptape
2424

2525
## Griptape Cloud
2626

27-
Set environment variables:
28-
29-
```shell
30-
export GRIPTAPE_CLOUD_API_KEY=<api_key>
31-
```
32-
33-
To override the Griptape Cloud endpoint used, set the following environment variable:
34-
35-
```shell
36-
export GRIPTAPE_CLOUD_ENDPOINT_URL="https://<endpoint>"
37-
```
38-
3927
Configure the Cloud:
4028

4129
```shell

griptape/cli/commands/cloud.py

Lines changed: 54 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from griptape.cli.core.app_packager import AppPackager
66
from griptape.cli.core.cloud_client import CloudClient
77
from griptape.cli.core.utils.constants import DEFAULT_ENDPOINT_URL
8+
from griptape.cli.core.utils.endpoint_utils import is_valid_endpoint
89
from InquirerPy import inquirer
910

1011

@@ -15,18 +16,31 @@ def cloud(ctx):
1516

1617

1718
@cloud.command(name="configure")
18-
@click.option(
19-
"--endpoint-url",
20-
"-e",
21-
type=str,
22-
required=False,
23-
help="Override default endpoint url",
24-
default=DEFAULT_ENDPOINT_URL,
25-
)
26-
def configure(endpoint_url: str) -> None:
19+
def configure() -> None:
2720
try:
21+
api_key = inquirer.text("Enter your Griptape Cloud API key: ").execute()
22+
23+
endpoint_choices = ["https://api.cloud.griptape.ai", "Other"]
24+
endpoint_url = inquirer.select(
25+
message="Select the endpoint you want to use: ",
26+
choices=endpoint_choices,
27+
).execute()
28+
if endpoint_url == "Other":
29+
valid_endpoint = False
30+
while not valid_endpoint:
31+
endpoint_url = inquirer.text(
32+
message="Enter the endpoint you want to use: ",
33+
default=DEFAULT_ENDPOINT_URL,
34+
).execute()
35+
if is_valid_endpoint(endpoint_url):
36+
valid_endpoint = True
37+
else:
38+
echo(
39+
f"Invalid endpoint url detected: {endpoint_url}. \nPlease set a valid endpoint url with the format: https://api.cloud<-stage>.griptape.ai."
40+
)
41+
2842
counter = 0
29-
cloud_client = CloudClient(endpoint_url=endpoint_url)
43+
cloud_client = CloudClient(api_key=api_key, endpoint_url=endpoint_url)
3044
organizations = cloud_client.list_organizations().json()["organizations"]
3145

3246
org_choices = [
@@ -66,8 +80,10 @@ def configure(endpoint_url: str) -> None:
6680

6781
cloud_client._write_gt_cloud_profile(
6882
profile_name=profile_name,
83+
api_key=api_key,
6984
organization_id=org["organization_id"],
7085
environment_id=env["environment_id"],
86+
endpoint_url=endpoint_url,
7187
)
7288

7389
echo(f"Successfully configured profile {profile_name}!")
@@ -78,19 +94,19 @@ def configure(endpoint_url: str) -> None:
7894

7995
@cloud.command(name="list-organizations")
8096
@click.option(
81-
"--endpoint-url",
82-
"-e",
97+
"--profile",
98+
"-p",
8399
type=str,
100+
help="Griptape Cloud profile name",
101+
default=None,
84102
required=False,
85-
help="Override default endpoint url",
86-
default=DEFAULT_ENDPOINT_URL,
87103
)
88-
def list_organizations(endpoint_url: str):
104+
def list_organizations(profile: str):
89105
"""
90106
List Griptape Cloud Organizations.
91107
"""
92108

93-
cloud_client = CloudClient(endpoint_url=endpoint_url)
109+
cloud_client = CloudClient(profile=profile)
94110
response = cloud_client.list_organizations()
95111
echo(response.json())
96112

@@ -100,23 +116,23 @@ def list_organizations(endpoint_url: str):
100116
"--organization-id",
101117
"-o",
102118
type=str,
103-
required=True,
119+
required=False,
104120
help="Organization ID to list environments for",
105121
)
106122
@click.option(
107-
"--endpoint-url",
108-
"-e",
123+
"--profile",
124+
"-p",
109125
type=str,
126+
help="Griptape Cloud profile name",
127+
default=None,
110128
required=False,
111-
help="Override default endpoint url",
112-
default=DEFAULT_ENDPOINT_URL,
113129
)
114-
def list_environments(organization_id: str, endpoint_url: str):
130+
def list_environments(organization_id: str, profile: str):
115131
"""
116132
List Griptape Cloud Environments.
117133
"""
118134

119-
cloud_client = CloudClient(endpoint_url=endpoint_url)
135+
cloud_client = CloudClient(profile=profile)
120136
response = cloud_client.list_environments(organization_id=organization_id)
121137
echo(response.json())
122138

@@ -126,23 +142,23 @@ def list_environments(organization_id: str, endpoint_url: str):
126142
"--environment-id",
127143
"-n",
128144
type=str,
129-
required=True,
145+
required=False,
130146
help="Environment ID to list apps for",
131147
)
132148
@click.option(
133-
"--endpoint-url",
134-
"-e",
149+
"--profile",
150+
"-p",
135151
type=str,
152+
help="Griptape Cloud profile name",
153+
default=None,
136154
required=False,
137-
help="Override default endpoint url",
138-
default=DEFAULT_ENDPOINT_URL,
139155
)
140-
def list_apps(environment_id: str, endpoint_url: str):
156+
def list_apps(environment_id: str, profile: str):
141157
"""
142158
List Griptape Cloud Apps.
143159
"""
144160

145-
cloud_client = CloudClient(endpoint_url=endpoint_url)
161+
cloud_client = CloudClient(profile=profile)
146162
response = cloud_client.list_apps(environment_id=environment_id)
147163
echo(response.json())
148164

@@ -155,14 +171,6 @@ def list_apps(environment_id: str, endpoint_url: str):
155171
help="Griptape Cloud app name",
156172
required=True,
157173
)
158-
@click.option(
159-
"--endpoint-url",
160-
"-e",
161-
type=str,
162-
required=False,
163-
help="Override default endpoint url",
164-
default=DEFAULT_ENDPOINT_URL,
165-
)
166174
@click.option(
167175
"--environment-id",
168176
"-v",
@@ -180,15 +188,15 @@ def list_apps(environment_id: str, endpoint_url: str):
180188
required=False,
181189
)
182190
def create_app(
183-
name: str, endpoint_url: str, environment_id: Optional[str], profile: Optional[str]
191+
name: str, environment_id: Optional[str], profile: Optional[str]
184192
) -> None:
185193
"""
186194
Create a Griptape App on the Griptape Cloud.
187195
"""
188196
app_data = {
189197
"name": name,
190198
}
191-
cloud_client = CloudClient(endpoint_url=endpoint_url, profile=profile)
199+
cloud_client = CloudClient(profile=profile)
192200
response = cloud_client.create_app(app_data=app_data, environment_id=environment_id)
193201
echo(response.json())
194202

@@ -210,19 +218,19 @@ def create_app(
210218
show_default=True,
211219
)
212220
@click.option(
213-
"--endpoint-url",
214-
"-e",
221+
"--profile",
222+
"-p",
215223
type=str,
224+
help="Griptape Cloud profile name",
225+
default=None,
216226
required=False,
217-
help="Override default endpoint url",
218-
default=DEFAULT_ENDPOINT_URL,
219227
)
220-
def create_deployment(app_id: str, directory: str, endpoint_url: str) -> None:
228+
def create_deployment(app_id: str, directory: str, profile: str) -> None:
221229
"""
222230
Deploy a Griptape App to Griptape Cloud.
223231
"""
224232

225-
cloud_client = CloudClient(endpoint_url=endpoint_url)
233+
cloud_client = CloudClient(profile=profile)
226234

227235
app_packager = AppPackager(
228236
app_directory=directory,
@@ -235,8 +243,6 @@ def create_deployment(app_id: str, directory: str, endpoint_url: str) -> None:
235243
response = cloud_client.create_deployment(
236244
deployment_data=deployment_data, app_id=app_id
237245
)
238-
if response.status_code != 202:
239-
raise Exception(response.json())
240246
echo(response.json())
241247
except Exception as e:
242248
echo(f"Unable to create deployment: {e}", err=True)

0 commit comments

Comments
 (0)