11import abc
2+ import itertools
23import json
34import pydoc
45
78from click import style
89from halo import halo
910
10- from gradient import exceptions , DeploymentsClient
11- from gradient .api_sdk import sdk_exceptions , utils , models
11+ from gradient import exceptions , DeploymentsClient , AutoscalingMetric , AutoscalingDefinition
12+ from gradient .api_sdk import sdk_exceptions , utils
1213from gradient .api_sdk .config import config
1314from gradient .api_sdk .utils import concatenate_urls
1415from gradient .cli_constants import CLI_PS_CLIENT_NAME
@@ -27,6 +28,29 @@ def _get_client(self, api_key, logger):
2728 return client
2829
2930
31+ class HandleAutoscalingOptions (object ):
32+ def _handle_autoscaling_options (self , kwargs ):
33+ autoscaling_metrics_and_resources = []
34+ metrics = kwargs .pop ("metrics" , None ) or []
35+ resources = kwargs .pop ("resources" , None ) or []
36+ for metric_dict in itertools .chain (resources , metrics ):
37+ metric = AutoscalingMetric (
38+ type = metric_dict ["type" ],
39+ name = metric_dict ["name" ],
40+ value_type = metric_dict ["value_type" ],
41+ value = metric_dict ["value" ],
42+ )
43+ autoscaling_metrics_and_resources .append (metric )
44+
45+ autoscaling_definition = AutoscalingDefinition (
46+ min_instance_count = kwargs .pop ("min_instance_count" , None ),
47+ max_instance_count = kwargs .pop ("max_instance_count" , None ),
48+ scale_cooldown_period = kwargs .pop ("scale_cooldown_period" , None ),
49+ metrics = autoscaling_metrics_and_resources ,
50+ )
51+ kwargs ["autoscaling" ] = autoscaling_definition
52+
53+
3054class HandleWorkspaceMixin (object ):
3155 def _handle_workspace (self , instance_dict ):
3256 handler = self .workspace_handler .handle (instance_dict )
@@ -37,14 +61,15 @@ def _handle_workspace(self, instance_dict):
3761 instance_dict ["workspace_url" ] = handler
3862
3963
40- class CreateDeploymentCommand (BaseDeploymentCommand , HandleWorkspaceMixin ):
64+ class CreateDeploymentCommand (HandleAutoscalingOptions , BaseDeploymentCommand , HandleWorkspaceMixin ):
4165 def __init__ (self , workspace_handler , * args , ** kwargs ):
4266 super (CreateDeploymentCommand , self ).__init__ (* args , ** kwargs )
4367 self .workspace_handler = workspace_handler
4468
4569 def execute (self , ** kwargs ):
4670 self ._handle_auth (kwargs )
4771 self ._handle_workspace (kwargs )
72+ self ._handle_autoscaling_options (kwargs )
4873 with halo .Halo (text = "Creating new deployment" , spinner = "dots" ):
4974 deployment_id = self .client .create (** kwargs )
5075
@@ -131,13 +156,14 @@ def execute(self, **kwargs):
131156 self .logger .log ("Deployment deleted" )
132157
133158
134- class UpdateDeploymentCommand (BaseDeploymentCommand , HandleWorkspaceMixin ):
159+ class UpdateDeploymentCommand (HandleAutoscalingOptions , BaseDeploymentCommand , HandleWorkspaceMixin ):
135160 def __init__ (self , workspace_handler , * args , ** kwargs ):
136161 super (UpdateDeploymentCommand , self ).__init__ (* args , ** kwargs )
137162 self .workspace_handler = workspace_handler
138163
139164 def execute (self , deployment_id , ** kwargs ):
140165 self ._handle_workspace (kwargs )
166+ self ._handle_autoscaling_options (kwargs )
141167
142168 with halo .Halo (text = "Updating deployment data" , spinner = "dots" ):
143169 self .client .update (deployment_id , ** kwargs )
@@ -151,6 +177,7 @@ def _get_table_data(self, instance):
151177 :param models.Deployment instance:
152178 """
153179 tags_string = ", " .join (instance .tags )
180+ autoscaling_metrics_string = self .get_autoscaling_metrics_string (instance )
154181
155182 data = (
156183 ("ID" , instance .id ),
@@ -166,9 +193,21 @@ def _get_table_data(self, instance):
166193 ("API type" , instance .api_type ),
167194 ("Cluster ID" , instance .cluster_id ),
168195 ("Tags" , tags_string ),
196+ ("Min Instance Count" , getattr (instance .autoscaling , "min_instance_count" , "" )),
197+ ("Max Instance Count" , getattr (instance .autoscaling , "max_instance_count" , "" )),
198+ ("Scale Cooldown Period" , getattr (instance .autoscaling , "scale_cooldown_period" , "" )),
199+ ("Autoscaling Metrics" , autoscaling_metrics_string ),
169200 )
170201 return data
171202
203+ def get_autoscaling_metrics_string (self , instance ):
204+ if not instance .autoscaling or not instance .autoscaling .metrics :
205+ return ""
206+
207+ s = "\n " .join ("{}/{}:{}" .format (m .name , m .value_type , m .value )
208+ for m in instance .autoscaling .metrics )
209+ return s
210+
172211
173212class DeploymentAddTagsCommand (BaseDeploymentCommand ):
174213 def execute (self , deployment_id , * args , ** kwargs ):
0 commit comments