Skip to content

Commit

Permalink
Authentication + get_organizations working, need to be cleaned up
Browse files Browse the repository at this point in the history
  • Loading branch information
Rodrigo Estebanez committed Jun 11, 2013
1 parent 47216cc commit f9e28e4
Show file tree
Hide file tree
Showing 7 changed files with 540 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/fog.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
require 'fog/storm_on_demand'
require 'fog/terremark'
require 'fog/vcloud'
require 'fog/vcloudng'
require 'fog/vmfusion'
require 'fog/vsphere'
require 'fog/voxel'
Expand Down
10 changes: 10 additions & 0 deletions lib/fog/vcloudng.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
require 'fog/core'

require 'fog/vcloudng/parser'
require 'fog/vcloudng/compute'

module Fog
module Vcloudng
VCLOUDNG_OPTIONS = [:vcloudng_username, :vcloudng_password, :vcloudng_host]
end
end
136 changes: 136 additions & 0 deletions lib/fog/vcloudng/compute.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
require_relative './shared'


module Fog
module Vcloudng
module Compute

module Bin
end

module Defaults
PATH = '/api'
PORT = 443
SCHEME = 'https'
end

extend Fog::Vcloudng::Shared

def self.new(options={})
# Fog::Logger.deprecation("Fog::Vcloudng::Compute is deprecated, to be replaced with Compute 1.0 someday/maybe [light_black](#{caller.first})[/]")

unless @required
shared_requires
@required = true
end

check_shared_options(options)

if Fog.mocking?
Fog::Vcloudng::Compute::Mock.new(options)
else
Fog::Vcloudng::Compute::Real.new(options)
end
end

class Real

include Fog::Vcloudng::Shared::Real
include Fog::Vcloudng::Shared::Parser

def initialize(options={})
@vcloudng_password = options[:vcloudng_password]
@vcloudng_username = options[:vcloudng_username]
@connection_options = options[:connection_options] || {}
@host = options[:vcloudng_host]
@path = options[:path] || Fog::Vcloudng::Compute::Defaults::PATH
@persistent = options[:persistent] || false
@port = options[:port] || Fog::Vcloudng::Compute::Defaults::PORT
@scheme = options[:scheme] || Fog::Vcloudng::Compute::Defaults::SCHEME
@connection = Fog::Connection.new("#{@scheme}://#{@host}:#{@port}", @persistent, @connection_options)
end

def default_vdc_id
if default_organization_id
@default_vdc_id ||= begin
vdcs = get_organization(default_organization_id).body['Links'].select {|link|
link['type'] == 'application/vnd.vmware.vcloud.vdc+xml'
}
if vdcs.length == 1
vdcs.first['href'].split('/').last.to_i
else
nil
end
end
else
nil
end
end

def default_network_id
if default_vdc_id
@default_network_id ||= begin
networks = get_vdc(default_vdc_id).body['AvailableNetworks']
if networks.length == 1
networks.first['href'].split('/').last.to_i
else
nil
end
end
else
nil
end
end

def default_public_ip_id
if default_vdc_id
@default_public_ip_id ||= begin
ips = get_public_ips(default_vdc_id).body['PublicIpAddresses']
if ips.length == 1
ips.first['href'].split('/').last.to_i
else
nil
end
end
else
nil
end
end
end

def default_ssh_key
if default_ssh_key
@default_ssh_key ||= begin
keys = get_keys_list(default_organization_id).body["Keys"]
keys.find { |item| item["IsDefault"] == "true" }
end
end
end
class Mock
include Fog::Vcloudng::Shared::Mock
include Fog::Vcloudng::Shared::Parser

def initialize(option = {})
super
@base_url = Fog::Vcloudng::Compute::Defaults::SCHEME + "://" +
options[:vcloudng_host] +
Fog::Vcloudng::Compute::Defaults::PATH

@vcloudng_username = options[:vcloudng_username]
end

def data
self.class.data[@vcloudng_username]
end

def reset_data
self.class.data.delete(@vcloudng_username)
end

end

end
end
end


20 changes: 20 additions & 0 deletions lib/fog/vcloudng/parser.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class VcloudngParser < Fog::Parsers::Base

def extract_attributes(attributes_xml)
attributes = {}
until attributes_xml.empty?
if attributes_xml.first.is_a?(Array)
until attributes_xml.first.empty?
attribute = attributes_xml.first.shift
attributes[attribute.localname] = attribute.value
end
else
attribute = attributes_xml.shift
attributes[attribute.localname] = attribute.value
end
end
attributes
end
end


32 changes: 32 additions & 0 deletions lib/fog/vcloudng/parsers/compute/get_organizations.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
module Fog
module Parsers
module Vcloudng
module Compute

class GetOrganizations < VcloudngParser

def reset
@response = { 'OrgList' => [] }
end

def start_element(name, attributes)
super
if name == 'Org'
organization = extract_attributes(attributes)
until attributes.empty?
if attributes.first.is_a?(Array)
attribute = attributes.shift
organization[attribute.first] = attribute.last
else
organization[attributes.shift] = attributes.shift
end
end
@response['OrgList'] << organization
end
end

end
end
end
end
end
33 changes: 33 additions & 0 deletions lib/fog/vcloudng/requests/compute/get_organizations.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
module Fog
module Vcloudng
module Compute
class Real

# Get list of organizations
#
# ==== Returns
# * response<~Excon::Response>:
# * body<~Array>:
# * 'description'<~String> - Description of organization
# * 'links'<~Array> - An array of links to entities in the organization
# * 'name'<~String> - Name of organization
def get_organizations
request({
:expects => 200,
:headers => {
#'Content-Type' => "application/vnd.vmware.vcloud.orgList+xml"
'Accept' => 'application/*+xml;version=1.5'
},
:method => 'GET',
:parser => Fog::Parsers::Vcloudng::Compute::GetOrganizations.new,
:override_path => true,
:path => '/api/org'
})
end

end


end
end
end
Loading

0 comments on commit f9e28e4

Please sign in to comment.