From f95b878c8a636deb4be99fe64d2fec6d6579e85f Mon Sep 17 00:00:00 2001 From: Spencer McElmurry Date: Thu, 5 Jul 2018 15:46:55 -0700 Subject: [PATCH] (POOLER-81) Add time_remaining information Before, the only time calculation displayed for a given VM was the lifetime parameter. Added the remaining parameter which will display time until the VM is destroyed as a float. Additionally, start_time and end_time were added to api to return as UTC based times (e.g. 2018-07-10 11:01:03 -0700). --- lib/vmpooler/api/v1.rb | 3 +++ spec/integration/api/v1/vm_spec.rb | 22 ++++++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/lib/vmpooler/api/v1.rb b/lib/vmpooler/api/v1.rb index 29d52a6..54f3780 100644 --- a/lib/vmpooler/api/v1.rb +++ b/lib/vmpooler/api/v1.rb @@ -645,6 +645,9 @@ module Vmpooler 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).abs + result[params[:hostname]]['start_time'] = Time.parse(rdata['checkout']) + result[params[:hostname]]['end_time'] = Time.parse(rdata['checkout']) + rdata['lifetime'].to_i*60*60 result[params[:hostname]]['state'] = 'running' elsif rdata['check'] result[params[:hostname]]['state'] = 'ready' diff --git a/spec/integration/api/v1/vm_spec.rb b/spec/integration/api/v1/vm_spec.rb index 387ecd0..059e96e 100644 --- a/spec/integration/api/v1/vm_spec.rb +++ b/spec/integration/api/v1/vm_spec.rb @@ -39,6 +39,28 @@ describe Vmpooler::API::V1 do create_token('abcdefghijklmnopqrstuvwxyz012345', 'jdoe', current_time) end + describe 'GET /vm/:hostname' do + it 'returns correct information on a running vm' do + create_running_vm 'pool1', 'abcdefghijklmnop' + get "#{prefix}/vm/abcdefghijklmnop" + expect_json(ok = true, http = 200) + expected = { + ok: true, + abcdefghijklmnop: { + template: "pool1", + lifetime: 0, + running: 0.0, + remaining: 0.0, + start_time: "#{current_time}", + end_time: "#{current_time}", + state: "running", + ip: "" + } + } + expect(last_response.body).to match(JSON.pretty_generate(expected)) + end + end + describe 'POST /vm' do it 'returns a single VM' do create_ready_vm 'pool1', 'abcdefghijklmnop'