class OneAndOne::Vpn
Attributes
id[RW]
specs[RW]
Public Class Methods
new(test: false)
click to toggle source
# File lib/1and1/vpn.rb, line 11 def initialize(test: false) @id = nil @specs = nil # Check if hitting mock api or live api if test @connection = Excon.new($base_url, :mock => true) else @connection = Excon.new($base_url) end end
Public Instance Methods
create(name: nil, description: nil, datacenter_id: nil)
click to toggle source
# File lib/1and1/vpn.rb, line 85 def create(name: nil, description: nil, datacenter_id: nil) # Build POST body new_vpn = { 'name' => name, 'description' => description, 'datacenter_id' => datacenter_id } # Clean out null keys in POST body body = OneAndOne.clean_hash(new_vpn) # Stringify the POST body string_body = body.to_json # Build URL path = OneAndOne.build_url('/vpns') # Perform request response = @connection.request(:method => :post, :path => path, :headers => $header, :body => string_body) # Check response status OneAndOne.check_response(response.body, response.status) #JSON-ify the response string json = JSON.parse(response.body) # Save new vpn ID to Vpn instance @id = json['id'] @specs = json # If all good, return JSON json end
delete(vpn_id: @id)
click to toggle source
# File lib/1and1/vpn.rb, line 160 def delete(vpn_id: @id) # If user passed in vpn ID, reassign @id = vpn_id # Build URL path = OneAndOne.build_url("/vpns/#{@id}") # Perform request response = @connection.request(:method => :delete, :path => path, :headers => $header) # Check response status OneAndOne.check_response(response.body, response.status) #JSON-ify the response string JSON.parse(response.body) end
download_config(vpn_id: @id)
click to toggle source
# File lib/1and1/vpn.rb, line 182 def download_config(vpn_id: @id) # If user passed in vpn ID, reassign @id = vpn_id # Build URL path = OneAndOne.build_url("/vpns/#{@id}/configuration_file") # Perform request response = @connection.request(:method => :get, :path => path, :headers => $header) # Check response status OneAndOne.check_response(response.body, response.status) #JSON-ify the response string JSON.parse(response.body) end
get(vpn_id: @id)
click to toggle source
# File lib/1and1/vpn.rb, line 57 def get(vpn_id: @id) # If user passed in vpn ID, reassign @id = vpn_id # Build URL path = OneAndOne.build_url("/vpns/#{@id}") # Perform request response = @connection.request(:method => :get, :path => path, :headers => $header) # Check response status OneAndOne.check_response(response.body, response.status) #JSON-ify the response string json = JSON.parse(response.body) # Reload specs attribute @specs = json # If all good, return JSON json end
list(page: nil, per_page: nil, sort: nil, q: nil, fields: nil)
click to toggle source
# File lib/1and1/vpn.rb, line 25 def list(page: nil, per_page: nil, sort: nil, q: nil, fields: nil) # Build hash for query parameters keyword_args = { :page => page, :per_page => per_page, :sort => sort, :q => q, :fields => fields } # Clean out null query parameters params = OneAndOne.clean_hash(keyword_args) # Build URL path = OneAndOne.build_url('/vpns') # Perform request response = @connection.request(:method => :get, :path => path, :headers => $header, :query => params) # Check response status OneAndOne.check_response(response.body, response.status) #JSON-ify the response string JSON.parse(response.body) end
modify(vpn_id: @id, name: nil, description: nil)
click to toggle source
# File lib/1and1/vpn.rb, line 125 def modify(vpn_id: @id, name: nil, description: nil) # If user passed in vpn ID, reassign @id = vpn_id # Build PUT body vpn_specs = { 'name' => name, 'description' => description } # Clean out null keys in PUT body body = OneAndOne.clean_hash(vpn_specs) # Stringify the PUT body string_body = body.to_json # Build URL path = OneAndOne.build_url("/vpns/#{@id}") # Perform Request response = @connection.request(:method => :put, :path => path, :headers => $header, :body => string_body) # Check response status OneAndOne.check_response(response.body, response.status) #JSON-ify the response string JSON.parse(response.body) end
reload()
click to toggle source
# File lib/1and1/vpn.rb, line 204 def reload # This reload fx is just a wrapper for the get fx get end
wait_for(timeout: 25, interval: 15)
click to toggle source
# File lib/1and1/vpn.rb, line 212 def wait_for(timeout: 25, interval: 15) # Capture start time start = Time.now # Poll VPN and check initial state initial_response = get vpn_state = initial_response['state'] # Keep polling the VPN's state until good until $good_states.include? vpn_state # Wait 15 seconds before polling again sleep interval # Check VPN state again current_response = get vpn_state = current_response['state'] # Calculate current duration and check for timeout duration = (Time.now - start) / 60 if duration > timeout puts "The operation timed out after #{timeout} minutes.\n" return end end # Return Duration {:duration => duration} end