Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Authentication + get_organizations working, need to be cleaned up

  • Loading branch information...
commit f9e28e4ef7ff1497c2bac6c50ef474d31adc405e 1 parent 47216cc
@restebanez restebanez authored
View
1  lib/fog.rb
@@ -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
10 lib/fog/vcloudng.rb
@@ -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
136 lib/fog/vcloudng/compute.rb
@@ -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
20 lib/fog/vcloudng/parser.rb
@@ -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
+
+
View
32 lib/fog/vcloudng/parsers/compute/get_organizations.rb
@@ -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
View
33 lib/fog/vcloudng/requests/compute/get_organizations.rb
@@ -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
View
308 lib/fog/vcloudng/shared.rb
@@ -0,0 +1,308 @@
+module Fog
+ module Vcloudng
+ module Shared
+
+ # Commond methods shared by Real and Mock
+ module Common
+
+ def default_organization_id
+ @default_organization_id ||= begin
+ org_list = get_organizations.body['OrgList']
+ if org_list.length == 1
+ org_list.first['href'].split('/').last.to_i
+ else
+ nil
+ end
+ end
+ end
+
+ end
+
+ module Parser
+
+ def parse(data)
+ case data['type']
+ when 'application/vnd.vmware.vcloud.vApp+xml'
+ servers.new(data.merge!(:service => self))
+ else
+ data
+ end
+ end
+
+ end
+
+ module Real
+ include Common
+
+# private
+
+ def auth_token
+ response = @connection.request({
+ :expects => 200,
+ :headers => { 'Authorization' => "Basic #{Base64.encode64("#{@vcloudng_username}:#{@vcloudng_password}").delete("\r\n")}",
+ 'Accept' => 'application/*+xml;version=1.5'
+ },
+ :host => @host,
+ :method => 'POST',
+ :parser => Fog::Parsers::Vcloudng::Compute::GetOrganizations.new,
+ :path => "/api/sessions" # curl http://devlab.mdsol.com/api/versions | grep LoginUrl
+ })
+ response.headers['Set-Cookie']
+ end
+
+ def reload
+ @connection.reset
+ end
+
+ def request(params)
+ unless @cookie
+ @cookie = auth_token
+ end
+ begin
+ do_request(params)
+ rescue Excon::Errors::Unauthorized
+ @cookie = auth_token
+ do_request(params)
+ end
+ end
+
+ def do_request(params)
+ headers = {}
+ if @cookie
+ headers.merge!('Cookie' => @cookie)
+ end
+ if params[:path]
+ if params[:override_path] == true
+ path = params[:path]
+ else
+ path = "#{@path}/#{params[:path]}"
+ end
+ else
+ path = "#{@path}"
+ end
+ @connection.request({
+ :body => params[:body],
+ :expects => params[:expects],
+ :headers => headers.merge!(params[:headers] || {}),
+ :host => @host,
+ :method => params[:method],
+ :parser => params[:parser],
+ :path => path
+ })
+ end
+
+ end
+
+ module Mock
+ include Common
+
+ def self.mock_data
+ {
+ :organizations =>
+ [
+ {
+ :info => {
+ :name => "Boom Inc.",
+ :id => 1
+ },
+ :vdcs => [
+ { :id => 21,
+ :name => "Boomstick",
+ :storage => { :used => 105, :allocated => 200 },
+ :cpu => { :allocated => 10000 },
+ :memory => { :allocated => 20480 },
+ :networks => [
+ { :id => 31,
+ :name => "1.2.3.0/24",
+ :subnet => "1.2.3.0/24",
+ :gateway => "1.2.3.1",
+ :netmask => "255.255.255.0",
+ :fencemode => "isolated"
+ },
+ { :id => 32,
+ :name => "4.5.6.0/24",
+ :subnet => "4.5.6.0/24",
+ :gateway => "4.5.6.1",
+ :netmask => "255.255.255.0",
+ :fencemode => "isolated"
+ },
+ ],
+ :vms => [
+ { :id => 41,
+ :name => "Broom 1"
+ },
+ { :id => 42,
+ :name => "Broom 2"
+ },
+ { :id => 43,
+ :name => "Email!"
+ }
+ ],
+ :public_ips => [
+ { :id => 51,
+ :name => "99.1.2.3"
+ },
+ { :id => 52,
+ :name => "99.1.2.4"
+ },
+ { :id => 53,
+ :name => "99.1.9.7"
+ }
+ ]
+ },
+ { :id => 22,
+ :storage => { :used => 40, :allocated => 150 },
+ :cpu => { :allocated => 1000 },
+ :memory => { :allocated => 2048 },
+ :name => "Rock-n-Roll",
+ :networks => [
+ { :id => 33,
+ :name => "7.8.9.0/24",
+ :subnet => "7.8.9.0/24",
+ :gateway => "7.8.9.1",
+ :netmask => "255.255.255.0",
+ :fencemode => "isolated"
+ }
+ ],
+ :vms => [
+ { :id => 44,
+ :name => "Master Blaster"
+ }
+ ],
+ :public_ips => [
+ { :id => 54,
+ :name => "99.99.99.99"
+ }
+ ]
+ }
+ ]
+ }
+ ]
+ }
+ end
+
+ def self.error_headers
+ {"X-Powered-By"=>"ASP.NET",
+ "Date"=> Time.now.to_s,
+ "Content-Type"=>"text/html",
+ "Content-Length"=>"0",
+ "Server"=>"Microsoft-IIS/7.0",
+ "Cache-Control"=>"private"}
+ end
+
+ def self.unathorized_status
+ 401
+ end
+
+ def self.headers(body, content_type)
+ {"X-Powered-By"=>"ASP.NET",
+ "Date"=> Time.now.to_s,
+ "Content-Type"=> content_type,
+ "Content-Length"=> body.to_s.length,
+ "Server"=>"Microsoft-IIS/7.0",
+ "Set-Cookie"=>"vcloud-token=ecb37bfc-56f0-421d-97e5-bf2gdf789457; path=/",
+ "Cache-Control"=>"private"}
+ end
+
+ def self.status
+ 200
+ end
+
+ def initialize(options={})
+ self.class.instance_eval '
+ def self.data
+ @data ||= Hash.new do |hash, key|
+ hash[key] = Fog::Vcloudng::Compute::Mock.mock_data
+ end
+ end'
+ self.class.instance_eval '
+ def self.reset
+ @data = nil
+ end
+
+ def self.reset_data(keys=data.keys)
+ for key in [*keys]
+ data.delete(key)
+ end
+ end'
+ end
+ end
+
+ def check_shared_options(options)
+ cloud_option_keys = options.keys.select { |key| key.to_s =~ /^vcloudng_.*/ }
+ unless cloud_option_keys.length == 0 || cloud_option_keys.length == 3
+ raise ArgumentError.new("vcloudng_username, vcloudng_password and vcloudng_host required to access vcloud")
+ end
+ end
+
+ def shared_requires
+ #require 'fog/vcloudng/models/compute/address'
+ #require 'fog/vcloudng/models/compute/addresses'
+ #require 'fog/vcloudng/models/compute/network'
+ #require 'fog/vcloudng/models/compute/networks'
+ #require 'fog/vcloudng/models/compute/server'
+ #require 'fog/vcloudng/models/compute/servers'
+ #require 'fog/vcloudng/models/compute/image'
+ #require 'fog/vcloudng/models/compute/images'
+ #require 'fog/vcloudng/models/compute/task'
+ #require 'fog/vcloudng/models/compute/tasks'
+ #require 'fog/vcloudng/models/compute/vdc'
+ #require 'fog/vcloudng/models/compute/vdcs'
+ #require 'fog/vcloudng/models/compute/internetservice'
+ #require 'fog/vcloudng/models/compute/internetservices'
+ #require 'fog/vcloudng/models/compute/nodeservice'
+ #require 'fog/vcloudng/models/compute/nodeservices'
+ #require 'fog/vcloudng/parsers/compute/get_catalog'
+ #require 'fog/vcloudng/parsers/compute/get_catalog_item'
+ #require 'fog/vcloudng/parsers/compute/get_internet_services'
+ #require 'fog/vcloudng/parsers/compute/get_network_ips'
+ #require 'fog/vcloudng/parsers/compute/get_node_services'
+ #require 'fog/vcloudng/parsers/compute/get_organization'
+ require 'fog/vcloudng/parsers/compute/get_organizations'
+ #require 'fog/vcloudng/parsers/compute/get_public_ips'
+ #require 'fog/vcloudng/parsers/compute/get_tasks_list'
+ #require 'fog/vcloudng/parsers/compute/get_keys_list'
+ #require 'fog/vcloudng/parsers/compute/get_vapp_template'
+ #require 'fog/vcloudng/parsers/compute/get_vdc'
+ #require 'fog/vcloudng/parsers/compute/instantiate_vapp_template'
+ #require 'fog/vcloudng/parsers/compute/internet_service'
+ #require 'fog/vcloudng/parsers/compute/network'
+ #require 'fog/vcloudng/parsers/compute/node_service'
+ #require 'fog/vcloudng/parsers/compute/public_ip'
+ #require 'fog/vcloudng/parsers/compute/task'
+ #require 'fog/vcloudng/parsers/compute/vapp'
+ #require 'fog/vcloudng/requests/compute/add_internet_service'
+ #require 'fog/vcloudng/requests/compute/add_node_service'
+ #require 'fog/vcloudng/requests/compute/create_internet_service'
+ #require 'fog/vcloudng/requests/compute/delete_internet_service'
+ #require 'fog/vcloudng/requests/compute/delete_public_ip'
+ #require 'fog/vcloudng/requests/compute/delete_node_service'
+ #require 'fog/vcloudng/requests/compute/delete_vapp'
+ #require 'fog/vcloudng/requests/compute/deploy_vapp'
+ #require 'fog/vcloudng/requests/compute/get_catalog'
+ #require 'fog/vcloudng/requests/compute/get_catalog_item'
+ #require 'fog/vcloudng/requests/compute/get_internet_services'
+ #require 'fog/vcloudng/requests/compute/get_network'
+ #require 'fog/vcloudng/requests/compute/get_network_ips'
+ #require 'fog/vcloudng/requests/compute/get_node_services'
+ #require 'fog/vcloudng/requests/compute/get_organization'
+ require 'fog/vcloudng/requests/compute/get_organizations'
+ #require 'fog/vcloudng/requests/compute/get_public_ip'
+ #require 'fog/vcloudng/requests/compute/get_public_ips'
+ #require 'fog/vcloudng/requests/compute/get_task'
+ #require 'fog/vcloudng/requests/compute/get_tasks_list'
+ #require 'fog/vcloudng/requests/compute/get_keys_list'
+ #require 'fog/vcloudng/requests/compute/get_vapp'
+ #require 'fog/vcloudng/requests/compute/get_vapp_template'
+ #require 'fog/vcloudng/requests/compute/get_vdc'
+ #require 'fog/vcloudng/requests/compute/instantiate_vapp_template'
+ #require 'fog/vcloudng/requests/compute/configure_vapp'
+ #require 'fog/vcloudng/requests/compute/power_off'
+ #require 'fog/vcloudng/requests/compute/power_on'
+ #require 'fog/vcloudng/requests/compute/power_reset'
+ #require 'fog/vcloudng/requests/compute/power_shutdown'
+ end
+
+ end
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.