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
79 changes: 42 additions & 37 deletions lib/ruby_vcloud_sdk/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,55 @@ module VCloudSdk
class Client
attr_reader :ovdc

def initialize(url, username, password, entities, control,
connection = nil)
@logger = Config.logger
@url = url
@organization = entities["organization"]
@ovdc_name = entities["virtual_datacenter"]
@vapp_catalog_name = entities["vapp_catalog"]
@media_catalog_name = entities["media_catalog"]
@control = control
@retries = @control["retries"]
@time_limit = @control["time_limit_sec"]
construct_rest_logger
Config.configure({ "rest_logger" => @rest_logger,
"rest_throttle" => control["rest_throttle"] })

if connection
@connection = connection
else
@connection = Connection::Connection.new(@url, @organization,
@time_limit["http_request"])
end
RETRIES =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just move the methods into the private block for now.

{
default: 5,
upload_vapp_files: 7,
cpi: 1
}

TIME_LIMIT_SEC =
{
default: 120,
delete_vapp_template: 120,
delete_vapp: 120,
delete_media: 120,
instantiate_vapp_template: 300,
power_on: 600,
power_off: 600,
undeploy: 720,
process_descriptor_vapp_template: 300,
http_request: 240
}

REST_THROTTLE =
{
min: 0,
max: 1
}

private_constant :RETRIES, :TIME_LIMIT_SEC, :REST_THROTTLE

def initialize(url, username, password, options = {}, logger = nil)
@logger = logger || Logger.new(STDOUT)
@retries = options[:retries] || RETRIES
@time_limit = options[:time_limit_sec] || TIME_LIMIT_SEC
Config.configure(rest_throttle: options[:rest_throttle] || REST_THROTTLE)

@connection = Connection::Connection.new(
@url,
@time_limit[:http_request])
@root = @connection.connect(username, password)
@admin_root = @connection.get(@root.admin_root)
@entity_resolver_link = @root.entity_resolver.href
# We assume the organization does not change often so we can get it at
# login and cache it
@admin_org = @connection.get(@admin_root.organization(@organization))
@logger.info("Successfully connected.")
@admin_org = @connection.get(@admin_root.organization)
@logger.info('Successfully connected.')
end

private

def get_catalog_vapp(id)
resolve_entity(id)
end
Expand Down Expand Up @@ -499,8 +518,6 @@ def get_catalog(name)
catalog = @connection.get(@admin_org.catalog(name))
end

private

ERROR_STATUSES = [Xml::TASK_STATUS[:ABORTED], Xml::TASK_STATUS[:ERROR],
Xml::TASK_STATUS[:CANCELED]]
SUCCESS_STATUS = [Xml::TASK_STATUS[:SUCCESS]]
Expand Down Expand Up @@ -882,18 +899,6 @@ def eject_media_task(vm, params, media)
end
end
end

def construct_rest_logger
@logger.debug("constructing rest_logger")
rest_log_filename = File.join(File.dirname(@logger.instance_eval {
@logdev }.dev.path), "rest")
log_file = File.open(rest_log_filename, "w")
log_file.sync = true

@rest_logger = Logger.new(log_file || STDOUT)
@rest_logger.level = @logger.level
@rest_logger.formatter = @logger.formatter
end
end

end
6 changes: 3 additions & 3 deletions lib/ruby_vcloud_sdk/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ class << self
attr_accessor :rest_throttle

def configure(config)
@logger = config["logger"] || @logger || Logger.new(STDOUT)
@rest_logger = config["rest_logger"] || @logger
@rest_throttle = config["rest_throttle"]
@logger = config[:logger] || @logger || Logger.new(STDOUT)
@rest_logger = config[:rest_logger] || @logger
@rest_throttle = config[:rest_throttle]
end
end
end
Expand Down
59 changes: 38 additions & 21 deletions lib/ruby_vcloud_sdk/connection/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,33 @@ module VCloudSdk
module Connection

class Connection
SECURITY_CHECK = "/cloud/security_check"
ACCEPT = "application/*+xml;version=5.1"

def initialize(hostname_port, organization, request_timeout = nil,
private_constant :ACCEPT

def initialize(url, request_timeout = nil,
rest_client = nil, site = nil, file_uploader = nil)
@organization = organization
@logger = Config.logger
@rest_logger = Config.rest_logger
@rest_throttle = Config.rest_throttle

construct_rest_logger
Config.configure(rest_logger: @rest_logger)

rest_client = RestClient unless rest_client
rest_client.log = @rest_logger
request_timeout = 60 unless request_timeout
@site = site.nil? ? rest_client::Resource.new(hostname_port,
:timeout => request_timeout) : site
@file_uploader = file_uploader.nil? ? FileUploader : file_uploader
@site = site || rest_client::Resource.new(
url,
timeout: request_timeout)
@file_uploader = file_uploader || FileUploader
end

def connect(username, password)
login = "#{username}@#{@organization}"
login_password = "#{login}:#{password}"
login_password = "#{username}:#{password}"
auth_header_value = "Basic #{Base64.encode64(login_password)}"
# TODO: call "api/versions" first
response = @site["/api/sessions"].post(
{:Authorization=>auth_header_value, :Accept=>ACCEPT})
Authorization: auth_header_value, Accept: ACCEPT)
@logger.debug(response)
@cookies = response.cookies
unless @cookies["vcloud-token"].gsub!("+", "%2B").nil?
Expand All @@ -41,15 +45,14 @@ def get(destination)
@rest_logger.info "#{__method__.to_s.upcase} #{delay}\t " +
"#{self.class.get_href(destination)}"
sleep(delay)
response = @site[get_nested_resource(destination)].get({
:Accept=>ACCEPT,
:cookies=>@cookies
})
response = @site[get_nested_resource(destination)].get(
Accept: ACCEPT,
cookies: @cookies)
@rest_logger.debug(response)
Xml::WrapperFactory.wrap_document(response)
end

def post(destination, data, content_type = '*/*')
def post(destination, data, content_type = "*/*")
@rest_logger.info "#{__method__.to_s.upcase} #{delay}\t " +
"#{self.class.get_href(destination)}"
sleep(delay)
Expand All @@ -59,11 +62,11 @@ def post(destination, data, content_type = '*/*')
end
@rest_logger.info("#{__method__.to_s.upcase} data:#{data.to_s}")
response = @site[get_nested_resource(destination)].post(data.to_s, {
:Accept=>ACCEPT,
:cookies=>@cookies,
:content_type=>content_type
Accept: ACCEPT,
cookies: @cookies,
content_type: content_type
})
raise ApiRequestError if http_error?(response)
fail ApiRequestError if http_error?(response)
@rest_logger.debug(response)
Xml::WrapperFactory.wrap_document(response)
end
Expand Down Expand Up @@ -120,6 +123,20 @@ def put_file(destination, file)
end

private

def construct_rest_logger
@logger.debug("constructing rest_logger")
rest_log_filename = File.join(
File.dirname(@logger.instance_eval { @logdev }.dev.path),
"rest")
log_file = File.open(rest_log_filename, "w")
log_file.sync = true

@rest_logger = Logger.new(log_file || STDOUT)
@rest_logger.level = @logger.level
@rest_logger.formatter = @logger.formatter
end

def log_exceptions(e)
if e.is_a? RestClient::Exception
@logger.error("HTTP Code: #{e.http_code}")
Expand All @@ -130,8 +147,8 @@ def log_exceptions(e)
end

def delay()
@rest_throttle["min"] + rand(@rest_throttle["max"] -
@rest_throttle["min"])
@rest_throttle[:min] + rand(@rest_throttle[:max] -
@rest_throttle[:min])
end

def get_nested_resource(destination)
Expand Down
8 changes: 5 additions & 3 deletions lib/ruby_vcloud_sdk/xml/wrapper_classes/vcloud.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ module Xml

class VCloud < Wrapper
def organizations
get_nodes("OrganizationReference")
get_nodes('OrganizationReference')
end

def organization(name)
get_nodes("OrganizationReference", {"name" => name}).first
def organization
# TODO: check and make sure that
# there is only one "OrganizationReference" node in response
get_nodes('OrganizationReference').first
end
end

Expand Down
8 changes: 7 additions & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,14 @@ def rest_logger.<<(str)
end
rest_logger
end
end

def verify_settings(obj, settings)
settings.each do |instance_variable_name, target_value|
instance_variable = obj.instance_variable_get(instance_variable_name)
instance_variable.should == target_value
end
end
end
end

module Xml
Expand Down
Loading