From 35dc7cb26f3f7d0bc48f1aecb4b8912a926b9e9e Mon Sep 17 00:00:00 2001 From: Jake Spain Date: Thu, 16 Feb 2023 08:55:33 -0500 Subject: [PATCH] Update domain in V2 api --- lib/vmpooler.rb | 1 - lib/vmpooler/api.rb | 3 +-- lib/vmpooler/api/v2.rb | 26 ++++++++++---------------- lib/vmpooler/public/lib/dashboard.js | 6 +++--- lib/vmpooler/util/parsing.rb | 20 -------------------- 5 files changed, 14 insertions(+), 42 deletions(-) diff --git a/lib/vmpooler.rb b/lib/vmpooler.rb index 516843f..cfb2e2c 100644 --- a/lib/vmpooler.rb +++ b/lib/vmpooler.rb @@ -76,7 +76,6 @@ module Vmpooler parsed_config[:config]['prefix'] = ENV['PREFIX'] || parsed_config[:config]['prefix'] || '' parsed_config[:config]['logfile'] = ENV['LOGFILE'] if ENV['LOGFILE'] parsed_config[:config]['site_name'] = ENV['SITE_NAME'] if ENV['SITE_NAME'] - parsed_config[:config]['domain'] = ENV['DOMAIN'] if ENV['DOMAIN'] parsed_config[:config]['clone_target'] = ENV['CLONE_TARGET'] if ENV['CLONE_TARGET'] parsed_config[:config]['timeout'] = string_to_int(ENV['TIMEOUT']) if ENV['TIMEOUT'] parsed_config[:config]['vm_lifetime_auth'] = string_to_int(ENV['VM_LIFETIME_AUTH']) if ENV['VM_LIFETIME_AUTH'] diff --git a/lib/vmpooler/api.rb b/lib/vmpooler/api.rb index a7eea30..5397637 100644 --- a/lib/vmpooler/api.rb +++ b/lib/vmpooler/api.rb @@ -3,7 +3,7 @@ module Vmpooler class API < Sinatra::Base # Load API components - %w[helpers dashboard reroute v1 v2 request_logger healthcheck].each do |lib| + %w[helpers dashboard reroute v2 request_logger healthcheck].each do |lib| require "vmpooler/api/#{lib}" end # Load dashboard components @@ -53,7 +53,6 @@ module Vmpooler use Vmpooler::Dashboard use Vmpooler::API::Dashboard use Vmpooler::API::Reroute - use Vmpooler::API::V1 use Vmpooler::API::V2 end diff --git a/lib/vmpooler/api/v2.rb b/lib/vmpooler/api/v2.rb index 44dad6e..d7feb78 100644 --- a/lib/vmpooler/api/v2.rb +++ b/lib/vmpooler/api/v2.rb @@ -1,6 +1,7 @@ # frozen_string_literal: true -require 'vmpooler/api/v1' +require 'vmpooler/util/parsing' +require 'vmpooler/dns' module Vmpooler class API @@ -87,9 +88,7 @@ module Vmpooler # last vm added. The part of the response is only being retained for # backwards compatibility as the hostnames are now fqdn's instead of bare # hostnames. This change is a result of now being able to specify a domain - # per pool. If no vm's in the result had a domain sepcified then the - # domain key will be omitted similar to how it was previously omitted if - # the global option domain wasn't specified. + # per pool. def atomically_allocate_vms(payload) tracer.in_span("Vmpooler::API::V2.#{__method__}") do |span| result = { 'ok' => false } @@ -126,15 +125,10 @@ module Vmpooler else vm_names = [] vms.each do |(vmpool, vmname, vmtemplate)| - vmdomain = Parsing.get_domain_for_pool(full_config, vmpool) - if vmdomain - vmfqdn = "#{vmname}.#{vmdomain}" - update_result_hosts(result, vmtemplate, vmfqdn) - vm_names.append(vmfqdn) - else - update_result_hosts(result, vmtemplate, vmname) - vm_names.append(vmname) - end + vmdomain = Dns.get_domain_for_pool(full_config, vmpool) + vmfqdn = "#{vmname}.#{vmdomain}" + update_result_hosts(result, vmtemplate, vmfqdn) + vm_names.append(vmfqdn) end span.set_attribute('vmpooler.vm_names', vm_names.join(',')) unless vm_names.empty? @@ -320,7 +314,7 @@ module Vmpooler result[params[:hostname]]['ip'] = ipAddress if rdata['pool'] - vmdomain = Parsing.get_domain_for_pool(full_config, rdata['pool']) + vmdomain = Dns.get_domain_for_pool(full_config, rdata['pool']) if vmdomain result[params[:hostname]]['fqdn'] = "#{params[:hostname]}.#{vmdomain}" end @@ -436,8 +430,8 @@ module Vmpooler result['ready'] = true Parsing.get_platform_pool_count(request_hash['requested']) do |platform_alias, pool, _count| instances = backend.smembers("vmpooler__#{request_id}__#{platform_alias}__#{pool}") - domain = Parsing.get_domain_for_pool(full_config, pool) - instances.map! { |instance| instance.concat(".#{domain}") } if domain + domain = Dns.get_domain_for_pool(full_config, pool) + instances.map! { |instance| instance.concat(".#{domain}") } if result.key?(platform_alias) result[platform_alias][:hostname] = result[platform_alias][:hostname] + instances diff --git a/lib/vmpooler/public/lib/dashboard.js b/lib/vmpooler/public/lib/dashboard.js index e726b01..11bc868 100644 --- a/lib/vmpooler/public/lib/dashboard.js +++ b/lib/vmpooler/public/lib/dashboard.js @@ -29,10 +29,10 @@ Date.prototype.yyyymmdd = function() { var data_url = { 'capacity': '/dashboard/stats/vmpooler/pool', - 'pools' : '/api/v1/vm', + 'pools' : '/api/v2/vm', 'running' : '/dashboard/stats/vmpooler/running', - 'status' : '/api/v1/status', - 'summary' : '/api/v1/summary' + 'status' : '/api/v2/status', + 'summary' : '/api/v2/summary' }; diff --git a/lib/vmpooler/util/parsing.rb b/lib/vmpooler/util/parsing.rb index 3949a55..1ca6b13 100644 --- a/lib/vmpooler/util/parsing.rb +++ b/lib/vmpooler/util/parsing.rb @@ -12,25 +12,5 @@ module Vmpooler yield platform_alias, pool, count end end - - # @param config [String] - the full config structure - # @param pool_name [String] - the name of the pool - # @return [String] - domain name for pool, if set in the provider for the pool or in the config block - def self.get_domain_for_pool(config, pool_name) - pool = config[:pools].find { |p| p['name'] == pool_name } - return nil unless pool - - provider_name = pool.fetch('provider', 'vsphere') # see vmpooler.yaml.example where it states defaulting to vsphere - - if config[:providers] && config[:providers][provider_name.to_sym] && config[:providers][provider_name.to_sym]['domain'] - domain = config[:providers][provider_name.to_sym]['domain'] - elsif config[:config] && config[:config]['domain'] - domain = config[:config]['domain'] - else - domain = nil - end - - domain - end end end