Skip to content

Commit 6325f0c

Browse files
committed
Update restart command to support service restart.
The API recently added the ability to restart both the postgres service and the full server. Here we're adding the ability to specify which to restart. By default, the `restart` command will restart the postgres service. However, using the `--full` flag will perform a full restart of the server.
1 parent cafe831 commit 6325f0c

6 files changed

Lines changed: 88 additions & 16 deletions

File tree

spec/cb/completion_spec.cr

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,13 @@ describe CB::Completion do
401401

402402
result = parse("cb restart abc ")
403403
result.should have_option "--confirm"
404+
result.should have_option "--full"
405+
406+
result = parse("cb restart abc --full ")
407+
result.should have_option "--confirm"
408+
409+
result = parse("cb restart abc --full --confirm ")
410+
result.empty?.should be_true
404411
end
405412

406413
it "completes detach" do
@@ -409,6 +416,9 @@ describe CB::Completion do
409416

410417
result = parse("cb detach abc ")
411418
result.should have_option "--confirm"
419+
420+
result = parse "cb detach abc --confirm "
421+
result.empty?.should be_true
412422
end
413423

414424
it "completes role" do

spec/cb/restart_spec.cr

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
require "../spec_helper"
2+
3+
private class RestartTestClient < CB::Client
4+
def get_cluster(id : String)
5+
ClusterDetail.new(
6+
id: id,
7+
team_id: "teamid",
8+
name: "source cluster",
9+
state: "na",
10+
created_at: Time.utc(2016, 2, 15, 10, 20, 30),
11+
is_ha: false,
12+
major_version: 12,
13+
plan_id: "memory-4",
14+
cpu: 4,
15+
memory: 111,
16+
oldest_backup: nil,
17+
provider_id: "aws",
18+
region_id: "us-east-2",
19+
network_id: "nfpvoqooxzdrriu6w3bhqo55c4",
20+
storage: 1234
21+
)
22+
end
23+
24+
def restart_cluster(id, service : String)
25+
end
26+
end
27+
28+
describe CB::Restart do
29+
it "validates that required arguments are present" do
30+
action = CB::Restart.new(RestartTestClient.new(TEST_TOKEN))
31+
32+
msg = /Missing required argument/
33+
expect_cb_error(msg) { action.validate }
34+
35+
action.cluster_id = "pkdpq6yynjgjbps4otxd7il2u4"
36+
action.validate.should eq true
37+
end
38+
39+
it "#run prints confirmation" do
40+
action = CB::Restart.new(RestartTestClient.new(TEST_TOKEN))
41+
action.output = IO::Memory.new
42+
43+
action.cluster_id = "pkdpq6yynjgjbps4otxd7il2u4"
44+
action.confirmed = true
45+
46+
action.call
47+
48+
action.output.to_s.should eq "Cluster #{action.cluster_id} restarted.\n"
49+
end
50+
end

src/cb/client.cr

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -352,8 +352,9 @@ class CB::Client
352352
delete "clusters/#{id}"
353353
end
354354

355-
def restart_cluster(id)
356-
put "clusters/#{id}/restart", ""
355+
# https://crunchybridgeapi.docs.apiary.io/#reference/0/clustersclusteridrestart/restart-cluster
356+
def restart_cluster(id, service : String)
357+
put "clusters/#{id}/restart", {service: service}
357358
end
358359

359360
jrecord Plan, id : String, display_name : String

src/cb/completion.cr

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,16 @@ class CB::Completion
4848
single_cluster_suggestion
4949
when "create"
5050
create
51+
when "detach"
52+
detach
5153
when "firewall"
5254
firewall
5355
when "logdest"
5456
logdest
5557
when "psql"
5658
psql
57-
when "restart", "detach"
58-
restart_or_detach
59+
when "restart"
60+
restart
5961
when "role"
6062
role
6163
when "team", "teams"
@@ -403,10 +405,16 @@ class CB::Completion
403405
suggest
404406
end
405407

406-
# `restart and `detach` are the only commands that offer `--confirm` as their
407-
# own option (other than `--help`). If that expands to other commands, then
408-
# perhaps we'll need to refactor this one a little to be more 'general'.
409-
def restart_or_detach
408+
def restart
409+
return cluster_suggestions if @args.size == 2
410+
411+
suggest = [] of String
412+
suggest << "--confirm\tconfirm cluster #{@args.first}" unless has_full_flag? :confirm
413+
suggest << "--full\tfull server restart" unless has_full_flag? :full
414+
suggest
415+
end
416+
417+
def detach
410418
return cluster_suggestions if @args.size == 2
411419

412420
if last_arg?("--confirm")
@@ -758,6 +766,7 @@ class CB::Completion
758766
full << :billing_email if has_full_flag? "--billing-email"
759767
full << :account if has_full_flag? "--account"
760768
full << :email if has_full_flag? "--email"
769+
full << :full if has_full_flag? "--full"
761770
full
762771
end
763772

src/cb/restart.cr

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ require "./action"
22

33
class CB::Restart < CB::Action
44
eid_setter cluster_id
5-
property confirmed : Bool = false
5+
bool_setter confirmed
6+
bool_setter full
67

78
def run
89
validate
@@ -17,12 +18,14 @@ class CB::Restart < CB::Action
1718
if c.name == response
1819
self.confirmed = true
1920
else
20-
raise Error.new "Response did not match, did not restart the cluster"
21+
raise Error.new "Response did not match, did not restart the cluster."
2122
end
2223
end
2324

24-
client.restart_cluster cluster_id
25-
output.puts "Cluster #{c.id.colorize.t_id} restarted"
25+
service = full ? "server" : "postgres"
26+
27+
client.restart_cluster cluster_id, service
28+
output.puts "Cluster #{c.id.colorize.t_id} restarted."
2629
end
2730

2831
def validate

src/cli.cr

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -194,11 +194,10 @@ op = OptionParser.new do |parser|
194194

195195
parser.on("restart", "Restart a cluster") do
196196
restart = set_action Restart
197-
parser.banner = "cb restart <cluster id> [--confirm]"
197+
parser.banner = "cb restart <cluster id> [--confirm] [--full]"
198198

199-
parser.on("--confirm", "Confirm cluster restart") do
200-
restart.confirmed = true
201-
end
199+
parser.on("--confirm", "Confirm cluster restart") { restart.confirmed = true }
200+
parser.on("--full", "Full restart of server") { restart.full = true }
202201

203202
parser.unknown_args do |args|
204203
restart.cluster_id = get_id_arg.call(args)

0 commit comments

Comments
 (0)