Skip to content

Commit 1d0fc42

Browse files
committed
(#31) Improve how users can provide arguments to commands
Prior this commit, some commands did not accept some options as an argument and instead only worked through a flag. This commit updates that behavior to allow users to specify some options through arguments, while leaving the ability to continue to specify those options through flags. Command line arguments take precedence over flags. It also fixes an issue where if a snapshot sha was nil, it would submit a request to take a snapshot to vmpooler.
1 parent b07139b commit 1d0fc42

File tree

5 files changed

+24
-12
lines changed

5 files changed

+24
-12
lines changed

Diff for: lib/vmfloaty.rb

+14-10
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ def run
7474
end
7575

7676
command :list do |c|
77-
c.syntax = 'floaty list [hostname]'
77+
c.syntax = 'floaty list [options]'
7878
c.summary = 'Shows a list of available vms from the pooler'
7979
c.description = ''
8080
c.example 'Filter the list on centos', 'floaty list centos --url http://vmpooler.example.com'
@@ -117,7 +117,7 @@ def run
117117
end
118118

119119
command :query do |c|
120-
c.syntax = 'floaty query [options]'
120+
c.syntax = 'floaty query [hostname] [options]'
121121
c.summary = 'Get information about a given vm'
122122
c.description = ''
123123
c.example 'Get information about a sample host', 'floaty query hostname --url http://vmpooler.example.com'
@@ -134,7 +134,7 @@ def run
134134
end
135135

136136
command :modify do |c|
137-
c.syntax = 'floaty modify [hostname]'
137+
c.syntax = 'floaty modify [hostname] [options]'
138138
c.summary = 'Modify a vms tags, TTL, and disk space'
139139
c.description = ''
140140
c.example 'Modifies myhost1 to have a TTL of 12 hours and adds a custom tag', 'floaty modify myhost1 --lifetime 12 --url https://myurl --token mytokenstring --tags \'{"tag":"myvalue"}\''
@@ -254,7 +254,7 @@ def run
254254
end
255255

256256
command :snapshot do |c|
257-
c.syntax = 'floaty snapshot [options]'
257+
c.syntax = 'floaty snapshot [hostname] [options]'
258258
c.summary = 'Takes a snapshot of a given vm'
259259
c.description = ''
260260
c.example 'Takes a snapshot for a given host', 'floaty snapshot myvm.example.com --url http://vmpooler.example.com --token a9znth9dn01t416hrguu56ze37t790bl'
@@ -273,10 +273,10 @@ def run
273273
end
274274

275275
command :revert do |c|
276-
c.syntax = 'floaty revert [options]'
276+
c.syntax = 'floaty revert [hostname] [snapshot] [options]'
277277
c.summary = 'Reverts a vm to a specified snapshot'
278278
c.description = ''
279-
c.example 'Reverts to a snapshot for a given host', 'floaty revert myvm.example.com --url http://vmpooler.example.com --token a9znth9dn01t416hrguu56ze37t790bl --snapshot n4eb4kdtp7rwv4x158366vd9jhac8btq'
279+
c.example 'Reverts to a snapshot for a given host', 'floaty revert myvm.example.com n4eb4kdtp7rwv4x158366vd9jhac8btq --url http://vmpooler.example.com --token a9znth9dn01t416hrguu56ze37t790bl'
280280
c.option '--verbose', 'Enables verbose output'
281281
c.option '--url STRING', String, 'URL of vmpooler'
282282
c.option '--token STRING', String, 'Token for vmpooler'
@@ -286,7 +286,11 @@ def run
286286
url = options.url ||= config['url']
287287
hostname = args[0]
288288
token = options.token || config['token']
289-
snapshot_sha = options.snapshot
289+
snapshot_sha = args[1] || options.snapshot
290+
291+
if args[1] && options.snapshot
292+
STDERR.puts "Two snapshot arguments were given....using snapshot #{snapshot_sha}"
293+
end
290294

291295
revert_req = Pooler.revert(verbose, url, hostname, token, snapshot_sha)
292296
pp revert_req
@@ -326,10 +330,10 @@ def run
326330
end
327331

328332
command :token do |c|
329-
c.syntax = 'floaty token [get | delete | status]'
333+
c.syntax = 'floaty token [get | delete | status] [token]'
330334
c.summary = 'Retrieves or deletes a token'
331335
c.description = ''
332-
c.example '', ''
336+
c.example 'Gets a token from the pooler', 'floaty token get'
333337
c.option '--verbose', 'Enables verbose output'
334338
c.option '--url STRING', String, 'URL of vmpooler'
335339
c.option '--user STRING', String, 'User to authenticate with'
@@ -338,7 +342,7 @@ def run
338342
verbose = options.verbose || config['verbose']
339343
action = args.first
340344
url = options.url ||= config['url']
341-
token = options.token ||= config['token']
345+
token = args[1] ||= options.token ||= config['token']
342346
user = options.user ||= config['user']
343347

344348
case action

Diff for: lib/vmfloaty/pooler.rb

+4
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,10 @@ def self.revert(verbose, url, hostname, token, snapshot_sha)
134134
conn = Http.get_conn(verbose, url)
135135
conn.headers['X-AUTH-TOKEN'] = token
136136

137+
if snapshot_sha.nil?
138+
raise "Snapshot SHA provided was nil, could not revert #{hostname}"
139+
end
140+
137141
response = conn.post "vm/#{hostname}/snapshot/#{snapshot_sha}"
138142
res_body = JSON.parse(response.body)
139143
res_body

Diff for: lib/vmfloaty/version.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
class Version
3-
@version = '0.6.1'
3+
@version = '0.6.2'
44

55
def self.get
66
@version

Diff for: spec/vmfloaty/pooler_spec.rb

+4
Original file line numberDiff line numberDiff line change
@@ -174,5 +174,9 @@
174174
revert_req = Pooler.revert(false, @vmpooler_url, 'fq6qlpjlsskycq6', 'mytokenfile', 'dAfewKNfaweLKNve')
175175
expect(revert_req["ok"]).to be true
176176
end
177+
178+
it "doesn't make a request to revert a vm if snapshot is not provided" do
179+
expect{ Pooler.revert(false, @vmpooler_url, 'fq6qlpjlsskycq6', 'mytokenfile', nil) }.to raise_error(RuntimeError, "Snapshot SHA provided was nil, could not revert fq6qlpjlsskycq6")
180+
end
177181
end
178182
end

Diff for: vmfloaty.gemspec

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Gem::Specification.new do |s|
22
s.name = 'vmfloaty'
3-
s.version = '0.6.1'
3+
s.version = '0.6.2'
44
s.authors = ['Brian Cain']
55
s.email = ['[email protected]']
66
s.license = 'Apache'

0 commit comments

Comments
 (0)