mirror of
https://github.com/puppetlabs/vmfloaty.git
synced 2026-01-25 21:28:40 -05:00
(#34) Raise TokenError on operations that require tokens
Prior to this commit, the Pooler class would raise an exception if the token provided was nil and it attempted to make a request with Faraday. This fixes that by catching when nil tokens are provided and instead raising a TokenError to be caught by the consumer.
This commit is contained in:
parent
08760d8c0b
commit
b51a549fe5
3 changed files with 71 additions and 5 deletions
|
|
@ -177,7 +177,12 @@ class Vmfloaty
|
|||
token = options.token || config['token']
|
||||
|
||||
if lifetime || tags
|
||||
modify_req = Pooler.modify(verbose, url, hostname, token, lifetime, tags)
|
||||
begin
|
||||
modify_req = Pooler.modify(verbose, url, hostname, token, lifetime, tags)
|
||||
rescue TokenError => e
|
||||
STDERR.puts e
|
||||
exit 1
|
||||
end
|
||||
|
||||
if modify_req["ok"]
|
||||
puts "Successfully modified vm #{hostname}."
|
||||
|
|
@ -189,7 +194,13 @@ class Vmfloaty
|
|||
end
|
||||
|
||||
if disk
|
||||
disk_req = Pooler.disk(verbose, url, hostname, token, disk)
|
||||
begin
|
||||
disk_req = Pooler.disk(verbose, url, hostname, token, disk)
|
||||
rescue TokenError => e
|
||||
STDERR.puts e
|
||||
exit 1
|
||||
end
|
||||
|
||||
if disk_req["ok"]
|
||||
puts "Successfully updated disk space of vm #{hostname}."
|
||||
else
|
||||
|
|
@ -290,7 +301,13 @@ class Vmfloaty
|
|||
hostname = args[0]
|
||||
token = options.token ||= config['token']
|
||||
|
||||
snapshot_req = Pooler.snapshot(verbose, url, hostname, token)
|
||||
begin
|
||||
snapshot_req = Pooler.snapshot(verbose, url, hostname, token)
|
||||
rescue TokenError => e
|
||||
STDERR.puts e
|
||||
exit 1
|
||||
end
|
||||
|
||||
pp snapshot_req
|
||||
end
|
||||
end
|
||||
|
|
@ -315,7 +332,13 @@ class Vmfloaty
|
|||
STDERR.puts "Two snapshot arguments were given....using snapshot #{snapshot_sha}"
|
||||
end
|
||||
|
||||
revert_req = Pooler.revert(verbose, url, hostname, token, snapshot_sha)
|
||||
begin
|
||||
revert_req = Pooler.revert(verbose, url, hostname, token, snapshot_sha)
|
||||
rescue TokenError => e
|
||||
STDERR.puts e
|
||||
exit 1
|
||||
end
|
||||
|
||||
pp revert_req
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -55,6 +55,10 @@ class Pooler
|
|||
end
|
||||
|
||||
def self.modify(verbose, url, hostname, token, lifetime, tags)
|
||||
if token.nil?
|
||||
raise TokenError, "Token provided was nil. Request cannot be made to modify vm"
|
||||
end
|
||||
|
||||
modify_body = {}
|
||||
if lifetime
|
||||
modify_body['lifetime'] = lifetime
|
||||
|
|
@ -76,6 +80,10 @@ class Pooler
|
|||
end
|
||||
|
||||
def self.disk(verbose, url, hostname, token, disk)
|
||||
if token.nil?
|
||||
raise TokenError, "Token provided was nil. Request cannot be made to modify vm"
|
||||
end
|
||||
|
||||
conn = Http.get_conn(verbose, url)
|
||||
conn.headers['X-AUTH-TOKEN'] = token
|
||||
|
||||
|
|
@ -129,6 +137,10 @@ class Pooler
|
|||
end
|
||||
|
||||
def self.snapshot(verbose, url, hostname, token)
|
||||
if token.nil?
|
||||
raise TokenError, "Token provided was nil. Request cannot be made to snapshot vm"
|
||||
end
|
||||
|
||||
conn = Http.get_conn(verbose, url)
|
||||
conn.headers['X-AUTH-TOKEN'] = token
|
||||
|
||||
|
|
@ -138,6 +150,10 @@ class Pooler
|
|||
end
|
||||
|
||||
def self.revert(verbose, url, hostname, token, snapshot_sha)
|
||||
if token.nil?
|
||||
raise TokenError, "Token provided was nil. Request cannot be made to revert vm"
|
||||
end
|
||||
|
||||
conn = Http.get_conn(verbose, url)
|
||||
conn.headers['X-AUTH-TOKEN'] = token
|
||||
|
||||
|
|
|
|||
|
|
@ -90,6 +90,10 @@ describe Pooler do
|
|||
@modify_response_body_fail = "{\"ok\":false}"
|
||||
end
|
||||
|
||||
it "raises a TokenError if token provided is nil" do
|
||||
expect{ Pooler.modify(false, @vmpooler_url, 'myfakehost', nil, 12, nil) }.to raise_error(TokenError)
|
||||
end
|
||||
|
||||
it "modifies the TTL of a vm" do
|
||||
stub_request(:put, "#{@vmpooler_url}/vm/fq6qlpjlsskycq6").
|
||||
with(:body => {"{\"lifetime\":12}"=>true},
|
||||
|
|
@ -116,7 +120,7 @@ describe Pooler do
|
|||
end
|
||||
end
|
||||
|
||||
describe "#staus" do
|
||||
describe "#status" do
|
||||
before :each do
|
||||
#smaller version
|
||||
@status_response_body = "{\"capacity\":{\"current\":716,\"total\":717,\"percent\": 99.9},\"status\":{\"ok\":true,\"message\":\"Battle station fully armed and operational.\"}}"
|
||||
|
|
@ -188,5 +192,28 @@ describe Pooler do
|
|||
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
|
||||
|
||||
it "raises a TokenError if no token was provided" do
|
||||
expect{ Pooler.revert(false, @vmpooler_url, 'myfakehost', nil, 'shaaaaaaa') }.to raise_error(TokenError)
|
||||
end
|
||||
end
|
||||
|
||||
describe "#disk" do
|
||||
before :each do
|
||||
@disk_response_body_success = "{\"ok\":true}"
|
||||
@disk_response_body_fail = "{\"ok\":false}"
|
||||
end
|
||||
|
||||
it "makes a request to extend disk space of a vm" do
|
||||
stub_request(:post, "#{@vmpooler_url}/vm/fq6qlpjlsskycq6/disk/12").
|
||||
with(:headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Content-Length'=>'0', 'User-Agent'=>'Faraday v0.9.2', 'X-Auth-Token'=>'mytokenfile'}). to_return(:status => 200, :body => @disk_response_body_success, :headers => {})
|
||||
|
||||
disk_req = Pooler.disk(false, @vmpooler_url, 'fq6qlpjlsskycq6', 'mytokenfile', 12)
|
||||
expect(disk_req["ok"]).to be true
|
||||
end
|
||||
|
||||
it "raises a TokenError if no token was provided" do
|
||||
expect{ Pooler.disk(false, @vmpooler_url, 'myfakehost', nil, 12) }.to raise_error(TokenError)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue