Skip to content

Commit

Permalink
Merge pull request #500 from puppetlabs/DIO-3138
Browse files Browse the repository at this point in the history
(DIO-3138) vmpooler v2 api missing vm/hostname
  • Loading branch information
yachub authored Jul 6, 2022
2 parents c3a6fd2 + 6aa1015 commit 35102d5
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 8 deletions.
9 changes: 2 additions & 7 deletions lib/vmpooler/api/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -181,14 +181,9 @@ def validate_date_str(date_str)
/^\d{4}-\d{2}-\d{2}$/ === date_str
end

# note: domain is not needed here, so we should update the callers of this method
def hostname_shorten(hostname, domain=nil)
if domain && hostname =~ /^[\w-]+\.#{domain}$/
hostname = hostname[/[^.]+/]
elsif hostname =~ /^[\w-]+\..+$/
hostname = hostname[/[^.]+/]
end

hostname
hostname[/[^.]+/]
end

def get_task_times(backend, task, date_str)
Expand Down
2 changes: 1 addition & 1 deletion lib/vmpooler/api/v1.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1519,7 +1519,7 @@ def delete_ondemand_request(request_id)

post "#{api_prefix}/vm/:hostname/snapshot/:snapshot/?" do
content_type :json
metrics.increment('http_requests_vm_total.post.vm.disksize')
metrics.increment('http_requests_vm_total.post.vm.snapshot')

need_token! if Vmpooler::API.settings.config[:auth]

Expand Down
76 changes: 76 additions & 0 deletions lib/vmpooler/api/v2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,82 @@ def generate_ondemand_request(payload)
JSON.pretty_generate(result)
end

get "#{api_prefix}/vm/:hostname/?" do
content_type :json
metrics.increment('http_requests_vm_total.get.vm.hostname')

result = {}

status 404
result['ok'] = false

params[:hostname] = hostname_shorten(params[:hostname], nil)

rdata = backend.hgetall("vmpooler__vm__#{params[:hostname]}")
unless rdata.empty?
status 200
result['ok'] = true

result[params[:hostname]] = {}

result[params[:hostname]]['template'] = rdata['template']
result[params[:hostname]]['lifetime'] = (rdata['lifetime'] || config['vm_lifetime']).to_i

if rdata['destroy']
result[params[:hostname]]['running'] = ((Time.parse(rdata['destroy']) - Time.parse(rdata['checkout'])) / 60 / 60).round(2) if rdata['checkout']
result[params[:hostname]]['state'] = 'destroyed'
elsif rdata['checkout']
result[params[:hostname]]['running'] = ((Time.now - Time.parse(rdata['checkout'])) / 60 / 60).round(2)
result[params[:hostname]]['remaining'] = ((Time.parse(rdata['checkout']) + rdata['lifetime'].to_i*60*60 - Time.now) / 60 / 60).round(2)
result[params[:hostname]]['start_time'] = Time.parse(rdata['checkout']).to_datetime.rfc3339
result[params[:hostname]]['end_time'] = (Time.parse(rdata['checkout']) + rdata['lifetime'].to_i*60*60).to_datetime.rfc3339
result[params[:hostname]]['state'] = 'running'
elsif rdata['check']
result[params[:hostname]]['state'] = 'ready'
else
result[params[:hostname]]['state'] = 'pending'
end

rdata.keys.each do |key|
if key.match('^tag\:(.+?)$')
result[params[:hostname]]['tags'] ||= {}
result[params[:hostname]]['tags'][$1] = rdata[key]
end

if key.match('^snapshot\:(.+?)$')
result[params[:hostname]]['snapshots'] ||= []
result[params[:hostname]]['snapshots'].push($1)
end
end

if rdata['disk']
result[params[:hostname]]['disk'] = rdata['disk'].split(':')
end

# Look up IP address of the hostname
begin
ipAddress = TCPSocket.gethostbyname(params[:hostname])[3]
rescue StandardError
ipAddress = ""
end

result[params[:hostname]]['ip'] = ipAddress

if rdata['pool']
vmdomain = Parsing.get_domain_for_pool(full_config, rdata['pool'])
if vmdomain
result[params[:hostname]]['fqdn'] = "#{params[:hostname]}.#{vmdomain}"
end
end

result[params[:hostname]]['host'] = rdata['host'] if rdata['host']
result[params[:hostname]]['migrated'] = rdata['migrated'] if rdata['migrated']

end

JSON.pretty_generate(result)
end

post "#{api_prefix}/ondemandvm/?" do
content_type :json
metrics.increment('http_requests_vm_total.post.ondemand.requestid')
Expand Down

0 comments on commit 35102d5

Please sign in to comment.