11import json
2+ import os
23import typing as t
34from 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" ,
0 commit comments