From 1d0fc42c80cb917c456f67925cbf360ad8595663 Mon Sep 17 00:00:00 2001 From: Brian Cain Date: Sat, 17 Sep 2016 16:07:21 -0700 Subject: [PATCH] (#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. --- lib/vmfloaty.rb | 24 ++++++++++++++---------- lib/vmfloaty/pooler.rb | 4 ++++ lib/vmfloaty/version.rb | 2 +- spec/vmfloaty/pooler_spec.rb | 4 ++++ vmfloaty.gemspec | 2 +- 5 files changed, 24 insertions(+), 12 deletions(-) diff --git a/lib/vmfloaty.rb b/lib/vmfloaty.rb index c70bd82..41e5d37 100644 --- a/lib/vmfloaty.rb +++ b/lib/vmfloaty.rb @@ -74,7 +74,7 @@ class Vmfloaty end command :list do |c| - c.syntax = 'floaty list [hostname]' + c.syntax = 'floaty list [options]' c.summary = 'Shows a list of available vms from the pooler' c.description = '' c.example 'Filter the list on centos', 'floaty list centos --url http://vmpooler.example.com' @@ -117,7 +117,7 @@ class Vmfloaty end command :query do |c| - c.syntax = 'floaty query [options]' + c.syntax = 'floaty query [hostname] [options]' c.summary = 'Get information about a given vm' c.description = '' c.example 'Get information about a sample host', 'floaty query hostname --url http://vmpooler.example.com' @@ -134,7 +134,7 @@ class Vmfloaty end command :modify do |c| - c.syntax = 'floaty modify [hostname]' + c.syntax = 'floaty modify [hostname] [options]' c.summary = 'Modify a vms tags, TTL, and disk space' c.description = '' 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 @@ class Vmfloaty end command :snapshot do |c| - c.syntax = 'floaty snapshot [options]' + c.syntax = 'floaty snapshot [hostname] [options]' c.summary = 'Takes a snapshot of a given vm' c.description = '' 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 @@ class Vmfloaty end command :revert do |c| - c.syntax = 'floaty revert [options]' + c.syntax = 'floaty revert [hostname] [snapshot] [options]' c.summary = 'Reverts a vm to a specified snapshot' c.description = '' - c.example 'Reverts to a snapshot for a given host', 'floaty revert myvm.example.com --url http://vmpooler.example.com --token a9znth9dn01t416hrguu56ze37t790bl --snapshot n4eb4kdtp7rwv4x158366vd9jhac8btq' + c.example 'Reverts to a snapshot for a given host', 'floaty revert myvm.example.com n4eb4kdtp7rwv4x158366vd9jhac8btq --url http://vmpooler.example.com --token a9znth9dn01t416hrguu56ze37t790bl' c.option '--verbose', 'Enables verbose output' c.option '--url STRING', String, 'URL of vmpooler' c.option '--token STRING', String, 'Token for vmpooler' @@ -286,7 +286,11 @@ class Vmfloaty url = options.url ||= config['url'] hostname = args[0] token = options.token || config['token'] - snapshot_sha = options.snapshot + snapshot_sha = args[1] || options.snapshot + + if args[1] && options.snapshot + STDERR.puts "Two snapshot arguments were given....using snapshot #{snapshot_sha}" + end revert_req = Pooler.revert(verbose, url, hostname, token, snapshot_sha) pp revert_req @@ -326,10 +330,10 @@ class Vmfloaty end command :token do |c| - c.syntax = 'floaty token [get | delete | status]' + c.syntax = 'floaty token [get | delete | status] [token]' c.summary = 'Retrieves or deletes a token' c.description = '' - c.example '', '' + c.example 'Gets a token from the pooler', 'floaty token get' c.option '--verbose', 'Enables verbose output' c.option '--url STRING', String, 'URL of vmpooler' c.option '--user STRING', String, 'User to authenticate with' @@ -338,7 +342,7 @@ class Vmfloaty verbose = options.verbose || config['verbose'] action = args.first url = options.url ||= config['url'] - token = options.token ||= config['token'] + token = args[1] ||= options.token ||= config['token'] user = options.user ||= config['user'] case action diff --git a/lib/vmfloaty/pooler.rb b/lib/vmfloaty/pooler.rb index c6e2d4e..c515d65 100644 --- a/lib/vmfloaty/pooler.rb +++ b/lib/vmfloaty/pooler.rb @@ -134,6 +134,10 @@ class Pooler conn = Http.get_conn(verbose, url) conn.headers['X-AUTH-TOKEN'] = token + if snapshot_sha.nil? + raise "Snapshot SHA provided was nil, could not revert #{hostname}" + end + response = conn.post "vm/#{hostname}/snapshot/#{snapshot_sha}" res_body = JSON.parse(response.body) res_body diff --git a/lib/vmfloaty/version.rb b/lib/vmfloaty/version.rb index b6aaa64..041ac44 100644 --- a/lib/vmfloaty/version.rb +++ b/lib/vmfloaty/version.rb @@ -1,6 +1,6 @@ class Version - @version = '0.6.1' + @version = '0.6.2' def self.get @version diff --git a/spec/vmfloaty/pooler_spec.rb b/spec/vmfloaty/pooler_spec.rb index c9b4b94..881e883 100644 --- a/spec/vmfloaty/pooler_spec.rb +++ b/spec/vmfloaty/pooler_spec.rb @@ -174,5 +174,9 @@ describe Pooler do revert_req = Pooler.revert(false, @vmpooler_url, 'fq6qlpjlsskycq6', 'mytokenfile', 'dAfewKNfaweLKNve') expect(revert_req["ok"]).to be true end + + it "doesn't make a request to revert a vm if snapshot is not provided" do + expect{ Pooler.revert(false, @vmpooler_url, 'fq6qlpjlsskycq6', 'mytokenfile', nil) }.to raise_error(RuntimeError, "Snapshot SHA provided was nil, could not revert fq6qlpjlsskycq6") + end end end diff --git a/vmfloaty.gemspec b/vmfloaty.gemspec index 25430c3..817f34e 100644 --- a/vmfloaty.gemspec +++ b/vmfloaty.gemspec @@ -1,6 +1,6 @@ Gem::Specification.new do |s| s.name = 'vmfloaty' - s.version = '0.6.1' + s.version = '0.6.2' s.authors = ['Brian Cain'] s.email = ['brian.cain@puppetlabs.com'] s.license = 'Apache'