From d3b1af4a06cfd3e2ff00e45e66ec97df12dd99e2 Mon Sep 17 00:00:00 2001 From: Brian Cain Date: Mon, 7 Sep 2015 14:11:05 -0700 Subject: [PATCH] (#1) Add token status and delete methods --- lib/vmfloaty.rb | 30 +++++++++++++++++++++++++++++- lib/vmfloaty/auth.rb | 29 +++++++++++++++++++++++++---- 2 files changed, 54 insertions(+), 5 deletions(-) diff --git a/lib/vmfloaty.rb b/lib/vmfloaty.rb index 47d0c7c..ad311f6 100644 --- a/lib/vmfloaty.rb +++ b/lib/vmfloaty.rb @@ -33,7 +33,7 @@ class Vmfloaty pass = password "Enter your password please:", '*' unless options.token - token = Auth.get_token(user, url, pass) + token = Auth.get_token(url, user, pass) end unless os_types.nil? @@ -172,6 +172,34 @@ class Vmfloaty end end + command :token do |c| + c.syntax = 'floaty token [get | delete | status]' + c.summary = 'Retrieves or deletes a token' + c.description = '' + c.example '', '' + c.option '--url STRING', String, 'URL of vmpooler' + c.option '--user STRING', String, 'User to authenticate with' + c.option '--token STRING', String, 'Token for vmpooler' + c.action do |args, options| + action = args.first + url = options.url ||= config['url'] + token = options.token + user = options.user ||= config['user'] + pass = password "Enter your password please:", '*' + + case action + when "get" + puts Auth.get_token(url, user, pass) + when "delete" + Auth.delete_token(url, user, pass, token) + when "status" + Auth.token_status(url, user, pass, token) + else + puts "Unknown action: #{action}" + end + end + end + run! end diff --git a/lib/vmfloaty/auth.rb b/lib/vmfloaty/auth.rb index ce1c78d..d799153 100644 --- a/lib/vmfloaty/auth.rb +++ b/lib/vmfloaty/auth.rb @@ -3,20 +3,41 @@ require 'json' require 'vmfloaty/http' class Auth - def self.get_token(user, url, password) + def self.get_token(url, user, password) conn = Http.get_conn(url) resp = conn.post do |req| req.url '/v1/token' req.headers['Content-Type'] = 'application/json' - req.user = user end - # if ok: true, return token + resp_body = JSON.parse(resp.body) resp_body end - def self.delete_token(user, token) + def self.delete_token(url, user, pass, token) + if token.nil? + puts 'You did not provide a token' + return + end + conn = Http.get_conn(url) + + response = conn.delete "/v1/token/#{token}" + res_body = JSON.parse(response) + puts res_body + end + + def self.token_status(url, user, pass, token) + if token.nil? + puts 'You did not provide a token' + return + end + + conn = Http.get_conn(url) + + response = conn.get "/v1/token/#{token}" + res_body = JSON.parse(response.body) + puts res_body end end