From 22e12e514ae3f23c2d2d307b26b55643b944e398 Mon Sep 17 00:00:00 2001
From: Rubem Nakamura <rubem.nakamura@gmail.com>
Date: Wed, 28 Aug 2013 13:53:32 -0300
Subject: [PATCH] add optional parameters to server#console

---
 lib/solusvm/base.rb                   |  2 ++
 lib/solusvm/server.rb                 | 12 +++++++++---
 test/solusvm/test_server.rb           |  7 +++++++
 test/vcr_cassettes/server/console.yml | 13 +++++++++++++
 test/vcr_cassettes/server/create.yml  |  2 +-
 5 files changed, 32 insertions(+), 4 deletions(-)

diff --git a/lib/solusvm/base.rb b/lib/solusvm/base.rb
index 9029a5a..b2379f0 100644
--- a/lib/solusvm/base.rb
+++ b/lib/solusvm/base.rb
@@ -26,6 +26,8 @@ def perform_request(options = {}, force_array = false)
       ca_path  = File.join(File.dirname(__FILE__), "..", "cacert.pem")
       ssl      = {verify: true, ca_file: File.expand_path(ca_path)}
 
+      options.reject! {|_,v| v.nil? }
+
       response = Faraday.new(url: api_endpoint, ssl: ssl) do |c|
         c.params = options.merge(api_login)
         c.adapter :net_http
diff --git a/lib/solusvm/server.rb b/lib/solusvm/server.rb
index 67f1795..93f527a 100644
--- a/lib/solusvm/server.rb
+++ b/lib/solusvm/server.rb
@@ -165,11 +165,17 @@ def vnc(vid)
     end
 
     # Retrieves server console information.
-    def console(vid)
-      perform_request(action: 'vserver-console', vserverid: vid)
+    #
+    # params - A Hash to pass optional parameters to vserver-console call:
+    #          :access - A String that can be 'enable' or 'disable'
+    #          :time   - A Integer that can be 1|2|3|4|5|6|7|8
+    #
+    # returns a Hash
+    def console(vid, params = {})
+      perform_request(action: 'vserver-console', vserverid: vid, access: params[:access], time: params[:time])
       returned_parameters
     end
-    
+
     # Retrieves all available server information.
     def info_all(vid)
       perform_request(action: 'vserver-infoall', vserverid: vid)
diff --git a/test/solusvm/test_server.rb b/test/solusvm/test_server.rb
index 6c4c750..e0c8d72 100644
--- a/test/solusvm/test_server.rb
+++ b/test/solusvm/test_server.rb
@@ -228,6 +228,13 @@ def test_console
     assert_equal 'theconsoleport', info['consoleport']
     assert_equal 'theconsolepassword', info['consolepassword']
     assert_equal 'theconsoleusername', info['consoleusername']
+
+    # with the optional parameters
+    VCR.use_cassette "server/console" do
+      @server.console(1, access: "admin", time: 1)
+    end
+    # a check to see if it returned a expected parameter even with the optional parameter
+    assert_equal 'thetype', @server.returned_parameters['type']
   end
 
   def test_info_all
diff --git a/test/vcr_cassettes/server/console.yml b/test/vcr_cassettes/server/console.yml
index 6f09685..82c5d07 100644
--- a/test/vcr_cassettes/server/console.yml
+++ b/test/vcr_cassettes/server/console.yml
@@ -12,4 +12,17 @@ http_interactions:
     body: <status>success</status><statusmsg></statusmsg> <type>thetype</type><consoleip>theconsoleip</consoleip><consoleport>theconsoleport</consoleport><consolepassword>theconsolepassword</consolepassword><consoleusername>theconsoleusername</consoleusername>
     http_version: '1.1'
   recorded_at: Tue, 01 Nov 2011 04:58:44 GMT
+- request:
+    method: get
+    uri: http://www.example.com/api?action=vserver-console&vserverid=1&access=admin&time=1&id=api_id&key=api_key
+    body: ''
+    headers: {}
+  response:
+    status:
+      code: 200
+      message: OK
+    body: <status>success</status><statusmsg></statusmsg> <type>thetype</type><consoleip>theconsoleip</consoleip><consoleport>theconsoleport</consoleport><consolepassword>theconsolepassword</consolepassword><consoleusername>theconsoleusername</consoleusername>
+    http_version: '1.1'
+  recorded_at: Tue, 01 Nov 2011 04:58:44 GMT
+
 recorded_with: VCR 2.0.0
diff --git a/test/vcr_cassettes/server/create.yml b/test/vcr_cassettes/server/create.yml
index 495f27f..9fda88b 100644
--- a/test/vcr_cassettes/server/create.yml
+++ b/test/vcr_cassettes/server/create.yml
@@ -14,7 +14,7 @@ http_interactions:
   recorded_at: Tue, 01 Nov 2011 04:58:44 GMT
 - request: 
     method: get
-    uri: http://www.example.com/api?type=xen&username&ips=1&node&plan&template&password=&hostname=&action=vserver-create&id=api_id&key=api_key
+    uri: http://www.example.com/api?type=xen&ips=1&password=&hostname=&action=vserver-create&id=api_id&key=api_key
     body: ''
     headers: {}
   response: