mirror of
https://github.com/puppetlabs/vmfloaty.git
synced 2026-01-26 05:28:40 -05:00
Adding command to list pools out of ABS
This commit is contained in:
parent
697cc878ef
commit
9b80b6bc78
5 changed files with 237 additions and 2 deletions
|
|
@ -131,6 +131,10 @@ services:
|
||||||
url: 'https://nspooler.example.net/api/v1'
|
url: 'https://nspooler.example.net/api/v1'
|
||||||
token: 'nspooler-tokenstring'
|
token: 'nspooler-tokenstring'
|
||||||
type: 'nonstandard' # <-- 'type' is necessary for any non-vmpooler service
|
type: 'nonstandard' # <-- 'type' is necessary for any non-vmpooler service
|
||||||
|
abs:
|
||||||
|
url: 'https://abs.example.net/api/v2'
|
||||||
|
token: 'abs-tokenstring'
|
||||||
|
type: 'abs' # <-- 'type' is necessary for any non-vmpooler service
|
||||||
```
|
```
|
||||||
|
|
||||||
With this configuration, you could list available OS types from nspooler like this:
|
With this configuration, you could list available OS types from nspooler like this:
|
||||||
|
|
|
||||||
139
lib/vmfloaty/abs.rb
Normal file
139
lib/vmfloaty/abs.rb
Normal file
|
|
@ -0,0 +1,139 @@
|
||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
require 'vmfloaty/errors'
|
||||||
|
require 'vmfloaty/http'
|
||||||
|
require 'faraday'
|
||||||
|
require 'json'
|
||||||
|
|
||||||
|
class ABS
|
||||||
|
# List available VMs in ABS
|
||||||
|
def self.list(verbose, url, os_filter = nil)
|
||||||
|
conn = Http.get_conn(verbose, url)
|
||||||
|
|
||||||
|
os_list = []
|
||||||
|
|
||||||
|
response = conn.get 'status/platforms/vmpooler'
|
||||||
|
response_body = JSON.parse(response.body)
|
||||||
|
os_list << "*** VMPOOLER Pools ***"
|
||||||
|
os_list = os_list + JSON.parse(response_body["vmpooler_platforms"])
|
||||||
|
|
||||||
|
response = conn.get 'status/platforms/nspooler'
|
||||||
|
response_body = JSON.parse(response.body)
|
||||||
|
os_list << ""
|
||||||
|
os_list << "*** NSPOOLER Pools ***"
|
||||||
|
os_list = os_list + JSON.parse(response_body["nspooler_platforms"])
|
||||||
|
|
||||||
|
response = conn.get 'status/platforms/aws'
|
||||||
|
response_body = JSON.parse(response.body)
|
||||||
|
os_list << ""
|
||||||
|
os_list << "*** AWS Pools ***"
|
||||||
|
os_list = os_list + JSON.parse(response_body["aws_platforms"])
|
||||||
|
|
||||||
|
os_list.delete 'ok'
|
||||||
|
|
||||||
|
puts os_list
|
||||||
|
|
||||||
|
os_filter ? os_list.select { |i| i[/#{os_filter}/] } : os_list
|
||||||
|
end
|
||||||
|
|
||||||
|
# List active VMs from ABS
|
||||||
|
def self.list_active(verbose, url, token)
|
||||||
|
status = Auth.token_status(verbose, url, token)
|
||||||
|
status['reserved_hosts'] || []
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.retrieve(verbose, os_type, token, url)
|
||||||
|
conn = Http.get_conn(verbose, url)
|
||||||
|
conn.headers['X-AUTH-TOKEN'] = token if token
|
||||||
|
|
||||||
|
os_string = os_type.map { |os, num| Array(os) * num }.flatten.join('+')
|
||||||
|
raise MissingParamError, 'No operating systems provided to obtain.' if os_string.empty?
|
||||||
|
|
||||||
|
response = conn.post "host/#{os_string}"
|
||||||
|
|
||||||
|
res_body = JSON.parse(response.body)
|
||||||
|
|
||||||
|
if res_body['ok']
|
||||||
|
res_body
|
||||||
|
elsif response.status == 401
|
||||||
|
raise AuthError, "HTTP #{response.status}: The token provided could not authenticate to the pooler.\n#{res_body}"
|
||||||
|
else
|
||||||
|
raise "HTTP #{response.status}: Failed to obtain VMs from the pooler at #{url}/host/#{os_string}. #{res_body}"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.modify(verbose, url, hostname, token, modify_hash)
|
||||||
|
raise TokenError, 'Token provided was nil; Request cannot be made to modify VM' if token.nil?
|
||||||
|
|
||||||
|
modify_hash.each do |key, _value|
|
||||||
|
raise ModifyError, "Configured service type does not support modification of #{key}" unless %i[reason reserved_for_reason].include? key
|
||||||
|
end
|
||||||
|
|
||||||
|
if modify_hash[:reason]
|
||||||
|
# "reason" is easier to type than "reserved_for_reason", but nspooler needs the latter
|
||||||
|
modify_hash[:reserved_for_reason] = modify_hash.delete :reason
|
||||||
|
end
|
||||||
|
|
||||||
|
conn = Http.get_conn(verbose, url)
|
||||||
|
conn.headers['X-AUTH-TOKEN'] = token
|
||||||
|
|
||||||
|
response = conn.put do |req|
|
||||||
|
req.url "host/#{hostname}"
|
||||||
|
req.body = modify_hash.to_json
|
||||||
|
end
|
||||||
|
|
||||||
|
response.body.empty? ? {} : JSON.parse(response.body)
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.disk(_verbose, _url, _hostname, _token, _disk)
|
||||||
|
raise ModifyError, 'Configured service type does not support modification of disk space'
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.snapshot(_verbose, _url, _hostname, _token)
|
||||||
|
raise ModifyError, 'Configured service type does not support snapshots'
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.revert(_verbose, _url, _hostname, _token, _snapshot_sha)
|
||||||
|
raise ModifyError, 'Configured service type does not support snapshots'
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.delete(verbose, url, hosts, token)
|
||||||
|
raise TokenError, 'Token provided was nil; Request cannot be made to delete VM' if token.nil?
|
||||||
|
|
||||||
|
conn = Http.get_conn(verbose, url)
|
||||||
|
|
||||||
|
conn.headers['X-AUTH-TOKEN'] = token if token
|
||||||
|
|
||||||
|
response_body = {}
|
||||||
|
|
||||||
|
hosts = hosts.split(',') unless hosts.is_a? Array
|
||||||
|
hosts.each do |host|
|
||||||
|
response = conn.delete "host/#{host}"
|
||||||
|
res_body = JSON.parse(response.body)
|
||||||
|
response_body[host] = res_body
|
||||||
|
end
|
||||||
|
|
||||||
|
response_body
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.status(verbose, url)
|
||||||
|
conn = Http.get_conn(verbose, url)
|
||||||
|
|
||||||
|
response = conn.get '/status'
|
||||||
|
JSON.parse(response.body)
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.summary(verbose, url)
|
||||||
|
conn = Http.get_conn(verbose, url)
|
||||||
|
|
||||||
|
response = conn.get '/summary'
|
||||||
|
JSON.parse(response.body)
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.query(verbose, url, hostname)
|
||||||
|
conn = Http.get_conn(verbose, url)
|
||||||
|
|
||||||
|
response = conn.get "host/#{hostname}"
|
||||||
|
JSON.parse(response.body)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
@ -52,7 +52,7 @@ class Service
|
||||||
|
|
||||||
def get_new_token(verbose)
|
def get_new_token(verbose)
|
||||||
username = user
|
username = user
|
||||||
pass = Commander::UI.password 'Enter your pooler service password:', '*'
|
pass = Commander::UI.password "Enter your #{@config["url"]} service password:", '*'
|
||||||
Auth.get_token(verbose, url, username, pass)
|
Auth.get_token(verbose, url, username, pass)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,8 @@
|
||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
require 'vmfloaty/pooler'
|
require 'vmfloaty/abs'
|
||||||
require 'vmfloaty/nonstandard_pooler'
|
require 'vmfloaty/nonstandard_pooler'
|
||||||
|
require 'vmfloaty/pooler'
|
||||||
|
|
||||||
class Utils
|
class Utils
|
||||||
# TODO: Takes the json response body from an HTTP GET
|
# TODO: Takes the json response body from an HTTP GET
|
||||||
|
|
@ -155,8 +156,11 @@ class Utils
|
||||||
|
|
||||||
def self.get_service_object(type = '')
|
def self.get_service_object(type = '')
|
||||||
nspooler_strings = %w[ns nspooler nonstandard nonstandard_pooler]
|
nspooler_strings = %w[ns nspooler nonstandard nonstandard_pooler]
|
||||||
|
abs_strings = %w[abs alwaysbescheduling always_be_scheduling]
|
||||||
if nspooler_strings.include? type.downcase
|
if nspooler_strings.include? type.downcase
|
||||||
NonstandardPooler
|
NonstandardPooler
|
||||||
|
elsif abs_strings.include? type.downcase
|
||||||
|
ABS
|
||||||
else
|
else
|
||||||
Pooler
|
Pooler
|
||||||
end
|
end
|
||||||
|
|
|
||||||
88
spec/vmfloaty/abs/auth_spec.rb
Normal file
88
spec/vmfloaty/abs/auth_spec.rb
Normal file
|
|
@ -0,0 +1,88 @@
|
||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
require 'spec_helper'
|
||||||
|
require_relative '../../../lib/vmfloaty/auth'
|
||||||
|
|
||||||
|
describe Pooler do
|
||||||
|
before :each do
|
||||||
|
@abs_url = 'https://abs.example.com'
|
||||||
|
end
|
||||||
|
|
||||||
|
describe '#get_token' do
|
||||||
|
before :each do
|
||||||
|
@get_token_response = '{"ok": true,"token":"utpg2i2xswor6h8ttjhu3d47z53yy47y"}'
|
||||||
|
@token = 'utpg2i2xswor6h8ttjhu3d47z53yy47y'
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'returns a token from vmpooler' do
|
||||||
|
stub_request(:post, 'https://first.last:password@abs.example.com/api/v2/token')
|
||||||
|
.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' })
|
||||||
|
.to_return(:status => 200, :body => @get_token_response, :headers => {})
|
||||||
|
|
||||||
|
token = Auth.get_token(false, @abs_url, 'first.last', 'password')
|
||||||
|
expect(token).to eq @token
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'raises a token error if something goes wrong' do
|
||||||
|
stub_request(:post, 'https://first.last:password@abs.example.com/api/v2/token')
|
||||||
|
.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' })
|
||||||
|
.to_return(:status => 500, :body => '{"ok":false}', :headers => {})
|
||||||
|
|
||||||
|
expect { Auth.get_token(false, @abs_url, 'first.last', 'password') }.to raise_error(TokenError)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe '#delete_token' do
|
||||||
|
before :each do
|
||||||
|
@delete_token_response = '{"ok":true}'
|
||||||
|
@token = 'utpg2i2xswor6h8ttjhu3d47z53yy47y'
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'deletes the specified token' do
|
||||||
|
stub_request(:delete, 'https://first.last:password@abs.example.com/api/v2/token/utpg2i2xswor6h8ttjhu3d47z53yy47y')
|
||||||
|
.with(:headers => { 'Accept' => '*/*', 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent' => 'Faraday v0.9.2' })
|
||||||
|
.to_return(:status => 200, :body => @delete_token_response, :headers => {})
|
||||||
|
|
||||||
|
expect(Auth.delete_token(false, @abs_url, 'first.last', 'password', @token)).to eq JSON.parse(@delete_token_response)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'raises a token error if something goes wrong' do
|
||||||
|
stub_request(:delete, 'https://first.last:password@abs.example.com/api/v2/token/utpg2i2xswor6h8ttjhu3d47z53yy47y')
|
||||||
|
.with(:headers => { 'Accept' => '*/*', 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent' => 'Faraday v0.9.2' })
|
||||||
|
.to_return(:status => 500, :body => '{"ok":false}', :headers => {})
|
||||||
|
|
||||||
|
expect { Auth.delete_token(false, @abs_url, 'first.last', 'password', @token) }.to raise_error(TokenError)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'raises a token error if no token provided' do
|
||||||
|
expect { Auth.delete_token(false, @abs_url, 'first.last', 'password', nil) }.to raise_error(TokenError)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe '#token_status' do
|
||||||
|
before :each do
|
||||||
|
@token_status_response = '{"ok":true,"utpg2i2xswor6h8ttjhu3d47z53yy47y":{"created":"2015-04-28 19:17:47 -0700"}}'
|
||||||
|
@token = 'utpg2i2xswor6h8ttjhu3d47z53yy47y'
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'checks the status of a token' do
|
||||||
|
stub_request(:get, "#{@abs_url}/token/utpg2i2xswor6h8ttjhu3d47z53yy47y")
|
||||||
|
.with(:headers => { 'Accept' => '*/*', 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent' => 'Faraday v0.9.2' })
|
||||||
|
.to_return(:status => 200, :body => @token_status_response, :headers => {})
|
||||||
|
|
||||||
|
expect(Auth.token_status(false, @abs_url, @token)).to eq JSON.parse(@token_status_response)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'raises a token error if something goes wrong' do
|
||||||
|
stub_request(:get, "#{@abs_url}/token/utpg2i2xswor6h8ttjhu3d47z53yy47y")
|
||||||
|
.with(:headers => { 'Accept' => '*/*', 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent' => 'Faraday v0.9.2' })
|
||||||
|
.to_return(:status => 500, :body => '{"ok":false}', :headers => {})
|
||||||
|
|
||||||
|
expect { Auth.token_status(false, @abs_url, @token) }.to raise_error(TokenError)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'raises a token error if no token provided' do
|
||||||
|
expect { Auth.token_status(false, @abs_url, nil) }.to raise_error(TokenError)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
Loading…
Add table
Add a link
Reference in a new issue