Skip to content

Commit c76fbe3

Browse files
committed
Cloud API: Use CRATEDB_CLOUD_SUBSCRIPTION_ID environment variable
1 parent f794fd9 commit c76fbe3

File tree

3 files changed

+51
-6
lines changed

3 files changed

+51
-6
lines changed

cratedb_toolkit/cluster/croud.py

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import json
2+
import os
23
import typing as t
34
from pathlib import Path
45

@@ -11,7 +12,25 @@ class CloudManager:
1112
A wrapper around the CrateDB Cloud API through the `croud` package, providing common methods.
1213
"""
1314

14-
def get_cluster_list(self):
15+
def list_subscriptions(self):
16+
"""
17+
Get list of clusters
18+
19+
croud clusters list --format=json
20+
"""
21+
from croud.__main__ import command_tree
22+
from croud.subscriptions.commands import subscriptions_list
23+
24+
call = CroudCall(
25+
fun=subscriptions_list,
26+
specs=command_tree["subscriptions"]["commands"]["list"]["extra_args"],
27+
arguments=[],
28+
)
29+
30+
wr = CroudWrapper(call=call)
31+
return wr.invoke()
32+
33+
def list_clusters(self):
1534
"""
1635
Get list of clusters
1736
@@ -66,7 +85,7 @@ def create_project(self, name: str):
6685
wr = CroudWrapper(call=call)
6786
return wr.invoke()
6887

69-
def deploy_cluster(self, name: str, project_id: str):
88+
def deploy_cluster(self, name: str, project_id: str, subscription_id: str = None):
7089
"""
7190
Deploy cluster.
7291
@@ -85,11 +104,32 @@ def deploy_cluster(self, name: str, project_id: str):
85104
from croud.__main__ import command_tree
86105
from croud.clusters.commands import clusters_deploy
87106

107+
# TODO: Refactor elsewhere.
108+
subscription_id = subscription_id or os.environ.get("CRATEDB_CLOUD_SUBSCRIPTION_ID")
109+
110+
# Automatically use subscription, if there is only a single one. Otherwise, croak.
111+
if subscription_id is None:
112+
subscriptions = self.list_subscriptions()
113+
if not subscriptions:
114+
raise ValueError("Not selecting a subscription automatically, because there are none.")
115+
if len(subscriptions) > 1:
116+
subscriptions_text = json.dumps(subscriptions, indent=2)
117+
raise ValueError(
118+
"Not selecting a subscription automatically, because there is more than one in your " # noqa: S608
119+
"account. Please select one from this list by choosing the relevant UUID from the "
120+
f"`id` attribute, and supply it to the `CRATEDB_CLOUD_SUBSCRIPTION_ID` environment "
121+
f"variable:\n{subscriptions_text}"
122+
)
123+
subscription_id = subscriptions[0]["id"]
124+
125+
if subscription_id is None:
126+
raise ValueError("Failed to obtain a subscription identifier")
127+
88128
call = CroudCall(
89129
fun=clusters_deploy,
90130
specs=command_tree["clusters"]["commands"]["deploy"]["extra_args"],
91131
arguments=[
92-
"--subscription-id=ba8592d2-b85b-4b21-b1c1-fbb99bc3c419",
132+
f"--subscription-id={subscription_id}",
93133
f"--project-id={project_id}",
94134
"--tier=default",
95135
"--product-name=crfree",

cratedb_toolkit/cluster/util.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,18 @@ def get_cluster_info(cluster_id: str) -> ClusterInformation:
1414

1515
def get_cluster_by_name(cluster_name: str) -> ClusterInformation:
1616
cm = CloudManager()
17-
cluster_list = cm.get_cluster_list()
17+
cluster_list = cm.list_clusters()
1818
for cluster in cluster_list:
1919
if cluster["name"] == cluster_name:
2020
return ClusterInformation(cloud=cluster)
2121
raise CroudException(f"Cluster not found: {cluster_name}")
2222

2323

24-
def deploy_cluster(cluster_name: str) -> ClusterInformation:
24+
def deploy_cluster(cluster_name: str, subscription_id: str = None) -> ClusterInformation:
2525
cm = CloudManager()
26+
# TODO: Only create new project when needed. Otherwise, use existing project.
2627
project = cm.create_project(name=cluster_name)
2728
project_id = project["id"]
2829
logger.info(f"Created project: {project_id}")
29-
cluster_info = cm.deploy_cluster(name=cluster_name, project_id=project_id)
30+
cluster_info = cm.deploy_cluster(name=cluster_name, project_id=project_id, subscription_id=subscription_id)
3031
return cluster_info

examples/cloud_cluster.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818
# Log in to CrateDB Cloud.
1919
croud login --idp azuread
2020
21+
# Optional: If your account uses multiple subscriptions, you will need
22+
# to select a specific one for invoking the cluster deployment operation.
23+
export CRATEDB_CLOUD_SUBSCRIPTION_ID=f33a2f55-17d1-4f21-8130-b6595d7c52db
24+
2125
# Initialize a cluster instance.
2226
python examples/cloud_cluster.py --cluster-name Hotzenplotz
2327

0 commit comments

Comments
 (0)