From 2e24a455a3131223f14289c9b376386ea161d2df Mon Sep 17 00:00:00 2001 From: Brian Cain Date: Tue, 3 May 2016 14:50:39 -0700 Subject: [PATCH] (#19) Update vmfloaty to expect /api/v1 in URL for disk endpoint This commit updates floaty to use a new endpoint to modify vms. Now you can use the modify command to extend the disk space of a given vm. The downside is this new endpoint only exists at /api/v1 on the pooler, and the way Faraday works required an update to removing a leading slash for each request endpoint. Users should update their URL in the floaty dot file to include a /api/v1 at the end of the pooler url --- README.md | 8 ++++---- lib/vmfloaty.rb | 31 ++++++++++++++++++++++++------- lib/vmfloaty/auth.rb | 6 +++--- lib/vmfloaty/pooler.rb | 24 +++++++++++++++++------- lib/vmfloaty/version.rb | 2 +- vmfloaty.gemspec | 2 +- vmfloaty.yml.example | 3 ++- 7 files changed, 52 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index eeff6b0..a0cff46 100644 --- a/README.md +++ b/README.md @@ -47,7 +47,7 @@ gem install vmfloaty Grabbing a token for authenticated pooler requests: ``` -floaty token get --user me --url https://vmpooler.mycompany.net +floaty token get --user me --url https://vmpooler.mycompany.net/api/v1 ``` This command will then ask you to log in. If successful, it will return a token that you can save either in a dotfile or use with other cli commands. @@ -55,7 +55,7 @@ This command will then ask you to log in. If successful, it will return a token Grabbing vms: ``` -floaty get centos-7-x86_64=2 debian-7-x86_64=1 windows-10=3 --token mytokenstring --url https://vmpooler.mycompany.net +floaty get centos-7-x86_64=2 debian-7-x86_64=1 windows-10=3 --token mytokenstring --url https://vmpooler.mycompany.net/api/v1 ``` ### vmfloaty dotfile @@ -64,7 +64,7 @@ If you do not wish to continuely specify various config options with the cli, yo ```yaml #file at /Users/me/.vmfloaty.yml -url: 'http://vmpooler.mycompany.net' +url: 'http://vmpooler.mycompany.net/api/v1' user: 'brian' token: 'tokenstring' ``` @@ -143,7 +143,7 @@ end if __FILE__ == $0 verbose = true - url = 'https://vmpooler.mycompany.net' + url = 'https://vmpooler.mycompany.net/api/v1' token = aquire_token(verbose, url) os = ARGV[0] diff --git a/lib/vmfloaty.rb b/lib/vmfloaty.rb index 1d61733..31137e8 100644 --- a/lib/vmfloaty.rb +++ b/lib/vmfloaty.rb @@ -125,29 +125,46 @@ class Vmfloaty command :modify do |c| c.syntax = 'floaty modify [hostname]' - c.summary = 'Modify a vms tags and TTL' + 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"}\'' c.option '--verbose', 'Enables verbose output' c.option '--url STRING', String, 'URL of vmpooler' c.option '--token STRING', String, 'Token for vmpooler' c.option '--lifetime INT', Integer, 'VM TTL (Integer, in hours)' + c.option '--disk INT', Integer, 'Increases VM disk space (Integer, in gb)' c.option '--tags STRING', String, 'free-form VM tagging (json)' c.action do |args, options| verbose = options.verbose || config['verbose'] url = options.url ||= config['url'] hostname = args[0] lifetime = options.lifetime + disk = options.disk tags = JSON.parse(options.tags) if options.tags token = options.token || config['token'] - modify_req = Pooler.modify(verbose, url, hostname, token, lifetime, tags) - if modify_req["ok"] - puts "Successfully modified vm #{hostname}." + if lifetime || tags + modify_req = Pooler.modify(verbose, url, hostname, token, lifetime, tags) + + if modify_req["ok"] + puts "Successfully modified vm #{hostname}." + else + STDERR.puts "Could not modify given host #{hostname} at #{url}." + puts modify_req + exit 1 + end + end + + if disk + disk_req = Pooler.disk(verbose, url, hostname, token, disk) + if disk_req["ok"] + puts "Successfully updated disk space of vm #{hostname}." + else + STDERR.puts "Could not modify given host #{hostname} at #{url}." + puts disk_req + exit 1 + end else - STDERR.puts "Could not modify given host #{hostname} at #{url}." - puts modify_req - exit 1 end end end diff --git a/lib/vmfloaty/auth.rb b/lib/vmfloaty/auth.rb index a973ae1..2e74b9b 100644 --- a/lib/vmfloaty/auth.rb +++ b/lib/vmfloaty/auth.rb @@ -6,7 +6,7 @@ class Auth def self.get_token(verbose, url, user, password) conn = Http.get_conn_with_auth(verbose, url, user, password) - resp = conn.post "/token" + resp = conn.post "token" res_body = JSON.parse(resp.body) if res_body["ok"] @@ -26,7 +26,7 @@ class Auth conn = Http.get_conn_with_auth(verbose, url, user, password) - response = conn.delete "/token/#{token}" + response = conn.delete "token/#{token}" res_body = JSON.parse(response.body) if res_body["ok"] return res_body @@ -45,7 +45,7 @@ class Auth conn = Http.get_conn(verbose, url) - response = conn.get "/token/#{token}" + response = conn.get "token/#{token}" res_body = JSON.parse(response.body) if res_body["ok"] diff --git a/lib/vmfloaty/pooler.rb b/lib/vmfloaty/pooler.rb index 6106a85..acc42f2 100644 --- a/lib/vmfloaty/pooler.rb +++ b/lib/vmfloaty/pooler.rb @@ -6,7 +6,7 @@ class Pooler def self.list(verbose, url, os_filter=nil) conn = Http.get_conn(verbose, url) - response = conn.get '/vm' + response = conn.get 'vm' response_body = JSON.parse(response.body) if os_filter @@ -37,7 +37,7 @@ class Pooler raise "No operating systems provided to obtain. See `floaty get --help` for more information on how to get VMs." end - response = conn.post "/vm/#{os_string}" + response = conn.post "vm/#{os_string}" res_body = JSON.parse(response.body) if res_body["ok"] @@ -60,7 +60,7 @@ class Pooler conn.headers['X-AUTH-TOKEN'] = token response = conn.put do |req| - req.url "/vm/#{hostname}" + req.url "vm/#{hostname}" req.body = modify_body.to_json end @@ -68,6 +68,16 @@ class Pooler res_body end + def self.disk(verbose, url, hostname, token, disk) + conn = Http.get_conn(verbose, url) + conn.headers['X-AUTH-TOKEN'] = token + + response = conn.post "vm/#{hostname}/disk/#{disk}" + + res_body = JSON.parse(response.body) + res_body + end + def self.delete(verbose, url, hosts, token) conn = Http.get_conn(verbose, url) @@ -77,7 +87,7 @@ class Pooler hosts.each do |host| puts "Scheduling host #{host} for deletion" - response = conn.delete "/vm/#{host}" + response = conn.delete "vm/#{host}" res_body = JSON.parse(response.body) if res_body['ok'] puts "Deletion for vm #{host} successfully scheduled" @@ -107,7 +117,7 @@ class Pooler def self.query(verbose, url, hostname) conn = Http.get_conn(verbose, url) - response = conn.get "/vm/#{hostname}" + response = conn.get "vm/#{hostname}" res_body = JSON.parse(response.body) res_body @@ -117,7 +127,7 @@ class Pooler conn = Http.get_conn(verbose, url) conn.headers['X-AUTH-TOKEN'] = token - response = conn.post "/vm/#{hostname}/snapshot" + response = conn.post "vm/#{hostname}/snapshot" res_body = JSON.parse(response.body) res_body end @@ -126,7 +136,7 @@ class Pooler conn = Http.get_conn(verbose, url) conn.headers['X-AUTH-TOKEN'] = token - response = conn.post "/vm/#{hostname}/snapshot/#{snapshot_sha}" + response = conn.post "vm/#{hostname}/snapshot/#{snapshot_sha}" res_body = JSON.parse(response.body) res_body end diff --git a/lib/vmfloaty/version.rb b/lib/vmfloaty/version.rb index c269acc..a4c74af 100644 --- a/lib/vmfloaty/version.rb +++ b/lib/vmfloaty/version.rb @@ -1,6 +1,6 @@ class Version - @version = '0.2.19' + @version = '0.3.0' def self.get @version diff --git a/vmfloaty.gemspec b/vmfloaty.gemspec index b09083c..99b27ad 100644 --- a/vmfloaty.gemspec +++ b/vmfloaty.gemspec @@ -1,6 +1,6 @@ Gem::Specification.new do |s| s.name = 'vmfloaty' - s.version = '0.2.19' + s.version = '0.3.0' s.authors = ['Brian Cain'] s.email = ['brian.cain@puppetlabs.com'] s.license = 'Apache' diff --git a/vmfloaty.yml.example b/vmfloaty.yml.example index 7728537..1864257 100644 --- a/vmfloaty.yml.example +++ b/vmfloaty.yml.example @@ -1,2 +1,3 @@ -url: 'http://vmpooler.example.com' +url: 'http://vmpooler.example.com/api/v1' user: 'brian' +token: 'token here'