Permalink
Browse files

Authentication + get_organizations working, need to be cleaned up

  • Loading branch information...
1 parent 47216cc commit f9e28e4ef7ff1497c2bac6c50ef474d31adc405e Rodrigo Estebanez committed Jun 11, 2013
View
@@ -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'
View
@@ -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
View
@@ -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
+
+
View
@@ -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
+
+
@@ -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
@@ -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
Oops, something went wrong.

0 comments on commit f9e28e4

Please sign in to comment.