Last active
April 10, 2020 13:24
-
-
Save Heavyblade/c31c27f82d2458c21f330eab678091a3 to your computer and use it in GitHub Desktop.
vcloud.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' | |
require 'httparty' | |
require 'net/https' | |
require 'json' | |
module Velneo | |
class Vcloud | |
include HTTParty | |
base_uri "https://cloudapi.velneo.com/v1" | |
format :json | |
debug_output $stdout | |
attr_accessor :email, :api_key, :admin_user, :admin_password, :session | |
# xxxxxxxxxxxx Usage xxxxxxxxxxxxxx | |
# | |
# session = Velneo::Vcloud.new(:email => "[email protected]", :api_key => "") | |
# session = Velneo::Vcloud.get_session("[email protected]", "") | |
def initialize(params) | |
@velneo_vserver = params[:velneo_vserver] || ::VelneoVserver.where(:api_key => params[:api_key]).first | |
@email = params[:email] || @velneo_vserver.email | |
@api_key = params[:api_key] || @velneo_vserver.api_key | |
@session = @velneo_vserver.get_session if @velneo_vserver | |
end | |
def signed(timestamp) | |
Digest::SHA1.hexdigest("#{session}#{timestamp}#{api_key}") | |
end | |
def http_request(options, end_point, method) | |
timestamp = Time.now.utc.to_i | |
basic = {:session => session, :timestamp => timestamp, :signed => signed(timestamp)} | |
basic.merge!(options) | |
basic_txt = "method=#{method}¶ms=" + basic.to_json.gsub("\"", "'") | |
headers = { "Content-Type" => "application/x-www-form-urlencoded", | |
"Accept" => "application/json "} | |
self.class.post("/#{end_point}", :body => basic_txt, :verify => false, :headers => headers) | |
end | |
def call(options, end_point, method="PUT") | |
times = 3 | |
begin | |
json = http_request(options, end_point, method) | |
if json["status_code"] == 401 # in case session gets closed | |
@session = @velneo_vserver.get_session(true) | |
json = http_request(options, end_point, method) | |
raise Velneo::AuthError if json["status_code"] == 401 | |
else | |
json | |
end | |
rescue Errno::ECONNREFUSED, SocketError, Timeout::Error | |
times -= 1 | |
retry if times != 0 | |
end | |
end | |
def status | |
self.call({}, "vserver", "GET") | |
end | |
def start_vserver | |
self.call({:action => 'start'}, "vserver") | |
end | |
def stop_vserver | |
self.call({:action => 'stop'}, "vserver") | |
end | |
def set_admin_user(admin_user, admin_password) | |
self.call({:username => admin_user, :password => admin_password}, "vserver") | |
end | |
def create_instance(name, solution, project, folder) | |
self.call({:name => name, | |
:project => project, | |
:solution => solution, | |
:folderShared => folder}, "instance") | |
end | |
def create_folder(name, path, group) | |
self.call({:name => name, :path => path, :group => group}, "folder") | |
end | |
def create_group(name, manageSolutions, addSolution, addAppInstance) | |
self.call({:name => name, :manageSolutions=> manageSolutions, :addSolution => addSolution, :addAppInstance => addAppInstance}, "group") | |
end | |
def create_user(username, password, fullname, group, isSupervisor, accountDisabled, mustChangePassword, passwordNeverExpires) | |
self.call({:username => username, | |
:password => password, | |
:fullname => fullname, | |
:group => group, | |
:isSupervisor => isSupervisor, | |
:accountDisabled => accountDisabled, | |
:mustChangePassword => mustChangePassword, | |
:passwordNeverExpires => passwordNeverExpires | |
}, "user") | |
end | |
def create_installation(base_name, client, solution, project) | |
base_name = base_name.upcase | |
self.create_group(base_name, "no", "", "") | |
self.create_folder(base_name, "#{base_name}", base_name) | |
self.create_user(client.email, client.email, client.full_name.parameterize, base_name, "no", "no", "yes", "no") | |
# create instance | |
self.create_instance(base_name, solution, project, base_name) | |
# assing instance to group | |
self.create_group(base_name, "no", solution, base_name) | |
end | |
end | |
class Authentication | |
include HTTParty | |
base_uri "https://cloudapi.velneo.com/v1" | |
format :json | |
# debug_output $stdout | |
def self.get_session(email, api_key) | |
timestamp = Time.now.utc.to_i | |
signed = Digest::SHA1.hexdigest("#{email}#{timestamp}#{api_key}") | |
options = "method=POST¶ms={'email':'#{email}','timestamp':'#{timestamp}','signed':'#{signed}'}" | |
headers = { "Content-Type" => "application/x-www-form-urlencoded", | |
"Accept" => "application/json "} | |
json = self.post("/session", :body => options, :query => {"_method" => "POST"}, :verify => false, :headers => headers) | |
json["session"] | |
end | |
end | |
class AuthError < StandardError | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment