diff --git a/lib/uffizzi/cli/cluster.rb b/lib/uffizzi/cli/cluster.rb index 5e3bcd2d..1c8d9fe2 100644 --- a/lib/uffizzi/cli/cluster.rb +++ b/lib/uffizzi/cli/cluster.rb @@ -245,12 +245,14 @@ def handle_sleep_command(project_slug, command_args) return handle_missing_cluster_name_error if cluster_name.nil? response = scale_down_cluster(ConfigFile.read_option(:server), project_slug, cluster_name) + return ResponseHelper.handle_failed_response(response) unless ResponseHelper.ok?(response) - if ResponseHelper.ok?(response) - Uffizzi.ui.say("Cluster #{cluster_name} was successfully scaled down") - else - ResponseHelper.handle_failed_response(response) - end + spinner = TTY::Spinner.new("[:spinner] Scaling down cluster #{cluster_name}...", format: :dots) + spinner.auto_spin + ClusterService.wait_cluster_scale_down(project_slug, cluster_name) + + spinner.success + Uffizzi.ui.say("Cluster #{cluster_name} was successfully scaled down") end def handle_wake_command(project_slug, command_args) diff --git a/lib/uffizzi/services/cluster_service.rb b/lib/uffizzi/services/cluster_service.rb index 484eaeb9..e7cbd6a1 100644 --- a/lib/uffizzi/services/cluster_service.rb +++ b/lib/uffizzi/services/cluster_service.rb @@ -1,11 +1,13 @@ # frozen_string_literal: true require 'uffizzi/clients/api/api_client' +require 'byebug' class ClusterService CLUSTER_STATE_DEPLOYING_NAMESPACE = 'deploying_namespace' CLUSTER_STATE_DEPLOYING = 'deploying' CLUSTER_STATE_DEPLOYED = 'deployed' + CLUSTER_STATE_SCALING_DOWN = 'scaling_down' CLUSTER_STATE_SCALING_UP = 'scaling_up' CLUSTER_FAILED_SCALING_UP = 'failed_scaling_up' CLUSTER_STATE_FAILED_DEPLOY_NAMESPACE = 'failed_deploy_namespace' @@ -32,6 +34,10 @@ def scaling_up?(cluster_state) cluster_state === CLUSTER_STATE_SCALING_UP end + def scaling_down?(cluster_state) + cluster_state === CLUSTER_STATE_SCALING_DOWN + end + def failed_scaling_up?(cluster_state) cluster_state === CLUSTER_FAILED_SCALING_UP end @@ -69,6 +75,22 @@ def wait_cluster_scale_up(project_slug, cluster_name) end end + def wait_cluster_scale_down(project_slug, cluster_name) + loop do + params = { + cluster_name: cluster_name, + } + response = get_cluster(Uffizzi::ConfigFile.read_option(:server), project_slug, params) + return Uffizzi::ResponseHelper.handle_failed_response(response) unless Uffizzi::ResponseHelper.ok?(response) + + cluster_data = response.dig(:body, :cluster) + + return unless scaling_down?(cluster_data[:state]) + + sleep(3) + end + end + def generate_name name = Faker::Internet.domain_word[0..CLUSTER_NAME_MAX_LENGTH] diff --git a/test/fixtures/files/uffizzi/uffizzi_cluster_scaled_down.json b/test/fixtures/files/uffizzi/uffizzi_cluster_scaled_down.json new file mode 100644 index 00000000..7bedd348 --- /dev/null +++ b/test/fixtures/files/uffizzi/uffizzi_cluster_scaled_down.json @@ -0,0 +1,10 @@ +{ + "cluster": { + "id": 1, + "name": "uffizzi-test-cluster-vcluster", + "state": "scaled_down", + "created_at": "2023-07-15T15:33:41.590Z", + "host": "some-host.cluster.com", + "kubeconfig": "YXBpVmVyc2lvbjogdjEKY2x1c3RlcnM6Ci0gY2x1c3RlcjoKICAgIGNlcnRpZmljYXRlLWF1dGhvcml0eS1kYXRhOiBMUzB0TFMxQ1JVZEpUaUJEUlZKVVNVWkpRMEZVUlMwdExTMHRDazFKU1VKa2VrTkRRVkl5WjBGM1NVSkJaMGxDUVVSQlMwSm5aM0ZvYTJwUFVGRlJSRUZxUVdwTlUwVjNTSGRaUkZaUlVVUkVRbWh5VFROTmRHTXlWbmtLWkcxV2VVeFhUbWhSUkVVeVQwUlpORTVFUlhkTlJFRjNTR2hqVGsxcVRYZE9ha1V4VFZSUk1VNXFVWGRYYUdOT1RYcE5kMDVxUlhsTlZGRXhUbXBSZHdwWGFrRnFUVk5GZDBoM1dVUldVVkZFUkVKb2NrMHpUWFJqTWxaNVpHMVdlVXhYVG1oUlJFVXlUMFJaTkU1RVJYZE5SRUYzVjFSQlZFSm5ZM0ZvYTJwUENsQlJTVUpDWjJkeGFHdHFUMUJSVFVKQ2QwNURRVUZTZ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ZGaE1NblpMVVdZNFRVNXhhR3B2VUhKdVJrTkhPVEZyVldSa2NYcGhPQ3M0WkRsSVpERTVVemR2UVc5SFEwTnhSMU5OTkRrS1FYZEZTRzlWVVVSUlowRkZUM2RyWmxscFZHMVpaakE1VGtZMmRXdzNUazFZU2xKcVVtUTBSakpRTlZseWVWcFVWM2xqYkhOMVZtbEhZemhWYldWb01BcG9MMGxQZDNCV1Uyb3lUMVprUjFwdGNVOW9ielF2UlhFdlowWnNOSFk0U0cxUlBUMEtMUzB0TFMxRlRrUWdSVU1nVUZKSlZrRlVSU0JMUlZrdExTMHRMUW89Cg==" + } +} diff --git a/test/uffizzi/cli/cluster_test.rb b/test/uffizzi/cli/cluster_test.rb index 99356c89..ad83ff4a 100644 --- a/test/uffizzi/cli/cluster_test.rb +++ b/test/uffizzi/cli/cluster_test.rb @@ -479,9 +479,13 @@ def test_scale_down_cluster cluster_name: cluster_name, ) + cluster_get_body = json_fixture('files/uffizzi/uffizzi_cluster_scaled_down.json') + stubbed_uffizzi_cluster_get_request = stub_get_cluster_request(cluster_get_body, @project_slug) + @cluster.sleep(cluster_name) assert_requested(stubbed_uffizzi_cluster_scale_down_request) + assert_requested(stubbed_uffizzi_cluster_get_request) end def test_scale_down_cluster_from_context @@ -494,10 +498,13 @@ def test_scale_down_cluster_from_context @project_slug, cluster_name: cluster_name, ) + cluster_get_body = json_fixture('files/uffizzi/uffizzi_cluster_scaled_down.json') + stubbed_uffizzi_cluster_get_request = stub_get_cluster_request(cluster_get_body, @project_slug) @cluster.sleep assert_requested(stubbed_uffizzi_cluster_scale_down_request) + assert_requested(stubbed_uffizzi_cluster_get_request) end def test_scale_up_cluster