From 4103fdecccdde1a022fd0d6e4006bfbd8306fe14 Mon Sep 17 00:00:00 2001 From: Jake Spain Date: Fri, 1 Apr 2022 11:32:22 -0400 Subject: [PATCH] Add VMPooler api v2 support for `floaty get` --- lib/vmfloaty/utils.rb | 19 +++++++++++++++++-- spec/vmfloaty/utils_spec.rb | 22 +++++++++++++++++++--- 2 files changed, 36 insertions(+), 5 deletions(-) diff --git a/lib/vmfloaty/utils.rb b/lib/vmfloaty/utils.rb index 8470921..39c97a3 100644 --- a/lib/vmfloaty/utils.rb +++ b/lib/vmfloaty/utils.rb @@ -9,7 +9,7 @@ class Utils # TODO: Takes the json response body from an HTTP GET # request and "pretty prints" it def self.standardize_hostnames(response_body) - # vmpooler response body example when `floaty get` arguments are `ubuntu-1610-x86_64=2 centos-7-x86_64`: + # vmpooler api v1 response body example when `floaty get` arguments are `ubuntu-1610-x86_64=2 centos-7-x86_64`: # { # "ok": true, # "domain": "delivery.mycompany.net", @@ -21,6 +21,17 @@ class Utils # } # } + # vmpooler api v2 response body example when `floaty get` arguments are `ubuntu-1610-x86_64=2 centos-7-x86_64`: + # { + # "ok": true, + # "ubuntu-1610-x86_64": { + # "hostname": ["gdoy8q3nckuob0i.pooler.example.com", "ctnktsd0u11p9tm.pooler.example.com"] + # }, + # "centos-7-x86_64": { + # "hostname": "dlgietfmgeegry2.pooler.example.com" + # } + # } + # nonstandard pooler response body example when `floaty get` arguments are `solaris-11-sparc=2 ubuntu-16.04-power8`: # { # "ok": true, @@ -98,7 +109,11 @@ class Utils puts abs_hostnames.join("\n") when 'Pooler' - puts "#{hostname}.#{host_data['domain']}" + if host_data['domain'].nil? + puts hostname + else + puts "#{hostname}.#{host_data['domain']}" + end when 'NonstandardPooler' puts host_data['fqdn'] else diff --git a/spec/vmfloaty/utils_spec.rb b/spec/vmfloaty/utils_spec.rb index a567b0c..0994107 100644 --- a/spec/vmfloaty/utils_spec.rb +++ b/spec/vmfloaty/utils_spec.rb @@ -13,7 +13,7 @@ end describe Utils do describe '#standardize_hostnames' do before :each do - @vmpooler_response_body = '{ + @vmpooler_api_v1_response_body = '{ "ok": true, "domain": "delivery.mycompany.net", "ubuntu-1610-x86_64": { @@ -23,6 +23,15 @@ describe Utils do "hostname": "dlgietfmgeegry2" } }' + @vmpooler_api_v2_response_body = '{ + "ok": true, + "ubuntu-1610-x86_64": { + "hostname": ["gdoy8q3nckuob0i.delivery.mycompany.net", "ctnktsd0u11p9tm.delivery.mycompany.net"] + }, + "centos-7-x86_64": { + "hostname": "dlgietfmgeegry2.delivery.mycompany.net" + } + }' @nonstandard_response_body = '{ "ok": true, "solaris-10-sparc": { @@ -34,8 +43,15 @@ describe Utils do }' end - it 'formats a result from vmpooler into a hash of os to hostnames' do - result = Utils.standardize_hostnames(JSON.parse(@vmpooler_response_body)) + it 'formats a result from vmpooler v1 api into a hash of os to hostnames' do + result = Utils.standardize_hostnames(JSON.parse(@vmpooler_api_v1_response_body)) + expect(result).to eq('centos-7-x86_64' => ['dlgietfmgeegry2.delivery.mycompany.net'], + 'ubuntu-1610-x86_64' => ['gdoy8q3nckuob0i.delivery.mycompany.net', + 'ctnktsd0u11p9tm.delivery.mycompany.net']) + end + + it 'formats a result from vmpooler v2 api into a hash of os to hostnames' do + result = Utils.standardize_hostnames(JSON.parse(@vmpooler_api_v2_response_body)) expect(result).to eq('centos-7-x86_64' => ['dlgietfmgeegry2.delivery.mycompany.net'], 'ubuntu-1610-x86_64' => ['gdoy8q3nckuob0i.delivery.mycompany.net', 'ctnktsd0u11p9tm.delivery.mycompany.net'])