Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 8 additions & 106 deletions lib/jager.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
require 'base64'
require 'faraday'
require 'json'

require 'jager/base'
require 'jager/datacenter'
require 'jager/server'

module Jager

# API_ENDPOINT = "https://api.cloud.net" its used for live data

API_ENDPOINT = "https://api.staging.cloud.net/" #now testing with staging data.

class CloudNet


attr_accessor :authentication_string
attr_accessor :connection
def initialize auth_string
@authentication_string = auth_string
@connection = Faraday.new(API_ENDPOINT,{ssl: {verify: false}}) #its only for staging account
Expand All @@ -20,110 +27,5 @@ def self.setup mail_id, api_secret
auth_string = Base64.encode64("#{mail_id}:#{api_secret}")
return self.new(auth_string) #creates new object of CloudNet for accessing all instance methods availabele
end

#datacenter requests

def get_all_datacenters
return collection_request "datacenters"
end

def get_datacenter id
return member_request id, "datacenters"
end

#server requests

def get_all_servers
return collection_request "servers"
end

def get_server id
return member_request id, "servers"
end

# server CRUD actions

def create_server template_id, options = {}
options = {name: nil, hostname: nil, memory: 1024, disk_size: 20, cpus: 2}.merge(options)
resp = @connection.post("#{API_ENDPOINT}/servers") do |req|
req.headers["Authorization"] = "Basic #{@authentication_string}"
req.params["template_id"] = template_id
req.params["name"] = options[:name]
req.params["hostname"] = options[:hostname]
req.params["memory"] = options[:memory]
req.params["disk_size"] = options[:disk_size]
req.params["cpus"] = options[:cpus]
end
return JSON.parse(resp.body)
end

def edit_server server_id, options = {}
options = {template_id: nil, memory: nil, cpus: nil, disk_size: nil}.merge(options)
resp = @connection.put("#{API_ENDPOINT}/servers/#{server_id}") do |req|
req.headers["Authorization"] = "Basic #{@authentication_string}"
req.params["template_id"] = options[:template_id]
req.params["memory"] = options[:memory]
req.params["disk_size"] = options[:disk_size]
req.params["cpus"] = options[:cpus]
req.params["id"] = server_id
end
return JSON.parse(resp.body)
end

def destroy_server id
resp = @connection.delete("#{API_ENDPOINT}/servers/#{id}") do |req|
req.headers["Authorization"] = "Basic #{@authentication_string}"
end
end

#server power options

def reboot_server id
return power_options id,"reboot"
end

def shutdown_server id
return power_options id,"shutdown"
end

def startup_server id
return power_options id,"startup"
end

private

def collection_request type
#send intial request for getting full response header and use total number of results to get all data in one request.

resp = @connection.get("#{API_ENDPOINT}/#{type}") do |req|
req.headers["Authorization"] = "Basic #{@authentication_string}"
end
total_results = resp.headers["x-total"]

#get all in one request

full_data = @connection.get("#{API_ENDPOINT}/#{type}") do |req|
req.params["per_page"] = total_results.to_i
req.params["pege"] = 1
req.headers["Authorization"] = "Basic #{@authentication_string}"
end

return JSON.parse(full_data.body)

end

def member_request id, type
resp = @connection.get("#{API_ENDPOINT}/#{type}/#{id}") do |req|
req.headers["Authorization"] = "Basic #{@authentication_string}"
end
return JSON.parse(resp.body)
end

def power_options server_id, option
resp = @connection.put("#{API_ENDPOINT}/servers/#{server_id}/#{option}") do |req|
req.headers["Authorization"] = "Basic #{@authentication_string}"
end
return JSON.parse(resp.body)
end
end
end
50 changes: 50 additions & 0 deletions lib/jager/base.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
module Jager

class Base

def request method, path, options = {}
connection = Faraday.new(url: API_ENDPOINT ,{ssl: {verify: false}}) do |c|
c.headers["Authorization"] = "Basic #{Jager.authentication_string}"
c.params = options
end
response = connection.send(method,path)
end

protected

def collection_request type
#send intial request for getting full response header and use total
#number of results to get all data in one request.

resp = @connection.get("#{API_ENDPOINT}/#{type}") do |req|
req.headers["Authorization"] = "Basic #{@authentication_string}"
end
total_results = resp.headers["x-total"]

#get all in one request

full_data = @connection.get("#{API_ENDPOINT}/#{type}") do |req|
req.params["per_page"] = total_results.to_i
req.params["pege"] = 1
req.headers["Authorization"] = "Basic #{@authentication_string}"
end

return JSON.parse(full_data.body)

end

def member_request id, type
resp = @connection.get("#{API_ENDPOINT}/#{type}/#{id}") do |req|
req.headers["Authorization"] = "Basic #{@authentication_string}"
end
return JSON.parse(resp.body)
end

def power_options server_id, option
resp = @connection.put("#{API_ENDPOINT}/servers/#{server_id}/#{option}") do |req|
req.headers["Authorization"] = "Basic #{@authentication_string}"
end
return JSON.parse(resp.body)
end
end
end
15 changes: 15 additions & 0 deletions lib/jager/datacenter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module Jager

class Datacenter < Base
def initialize obj
super obj
end
def list
return collection_request "datacenters"
end

def show id
return member_request id, "datacenters"
end
end
end
66 changes: 66 additions & 0 deletions lib/jager/server.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
module Jager
class Server < Base

def initialize obj
super obj
end

def list
# return collection_request "servers"
request(:get, "/servers")
end

def show id
return member_request id, "servers"
end

# server CRUD actions

def create template_id, options = {}
options = {name: nil, hostname: nil, memory: 1024, disk_size: 20, cpus: 2}.merge(options)
resp = @connection.post("#{API_ENDPOINT}/servers") do |req|
req.headers["Authorization"] = "Basic #{@authentication_string}"
req.params["template_id"] = template_id
req.params["name"] = options[:name]
req.params["hostname"] = options[:hostname]
req.params["memory"] = options[:memory]
req.params["disk_size"] = options[:disk_size]
req.params["cpus"] = options[:cpus]
end
return JSON.parse(resp.body)
end

def edit server_id, options = {}
options = {template_id: nil, memory: nil, cpus: nil, disk_size: nil}.merge(options)
resp = @connection.put("#{API_ENDPOINT}/servers/#{server_id}") do |req|
req.headers["Authorization"] = "Basic #{@authentication_string}"
req.params["template_id"] = options[:template_id]
req.params["memory"] = options[:memory]
req.params["disk_size"] = options[:disk_size]
req.params["cpus"] = options[:cpus]
req.params["id"] = server_id
end
return JSON.parse(resp.body)
end

def destroy id
resp = @connection.delete("#{API_ENDPOINT}/servers/#{id}") do |req|
req.headers["Authorization"] = "Basic #{@authentication_string}"
end
end

#server power options

def reboot id
return power_options id,"reboot"
end

def shutdown id
return power_options id,"shutdown"
end

def startup id
return power_options id,"startup"
end
end
end