Class: Fog::Vcloud::Mock

Inherits:
Real
  • Object
show all
Defined in:
lib/fog/vcloud.rb,
lib/fog/vcloud/models/vdcs.rb,
lib/fog/vcloud/requests/login.rb,
lib/fog/vcloud/requests/get_vdc.rb,
lib/fog/vcloud/requests/get_versions.rb,
lib/fog/vcloud/requests/get_organization.rb

Constant Summary collapse

DATA =
{
  :versions => [
    { :version => "v0.8", :login_url => "https://fakey.com/api/v0.8/login", :supported => true }
  ],
  :vdc_resources => [
    {
      :type => "application/vnd.vmware.vcloud.vApp+xml",
      :href => "https://fakey.com/api/v0.8/vapp/61",
      :name => "Foo App 1"
    },
    {
      :type => "application/vnd.vmware.vcloud.vApp+xml",
      :href => "https://fakey.com/api/v0.8/vapp/62",
      :name => "Bar App 1"
    },
    {
      :type => "application/vnd.vmware.vcloud.vApp+xml",
      :href => "https://fakey.com/api/v0.8/vapp/63",
      :name => "Bar App 2"
    }
  ],
  :organizations =>
  [
    {
      :info => {
        :href => "https://fakey.com/api/v0.8/org/1",
        :name => "Boom Inc.",
      },
      :vdcs => [
        { :href => "https://fakey.com/api/v0.8/vdc/21",
          :name => "Boomstick",
          :storage => { :used => 105, :allocated => 200 },
          :cpu => { :allocated => 10000 },
          :memory => { :allocated => 20480 },
          :networks => [
            { :href => "https://fakey.com/api/v0.8/network/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"
            },
            { :href => "https://fakey.com/api/v0.8/network/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 => [
            { :href => "https://fakey.com/api/v0.8/vap/41",
              :name => "Broom 1"
            },
            { :href => "https://fakey.com/api/v0.8/vap/42",
              :name => "Broom 2"
            },
            { :href => "https://fakey.com/api/v0.8/vap/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"
            }
          ]
        },
        { :href => "https://fakey.com/api/v0.8/vdc/22",
          :storage => { :used => 40, :allocated => 150 },
          :cpu => { :allocated => 1000 },
          :memory => { :allocated => 2048 },
          :name => "Rock-n-Roll",
          :networks => [
            { :href => "https://fakey.com/api/v0.8/network/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 => [
            { :href => "https://fakey.com/api/v0.8/vap/44",
              :name => "Master Blaster"
            }
          ],
          :public_ips => [
            { :id => 54,
              :name => "99.99.99.99"
            }
          ]
        }
      ]
    }
  ]
}

Instance Attribute Summary

Attributes inherited from Real

#login_uri, #supported_versions

Instance Method Summary collapse

Methods inherited from Real

#default_organization_uri

Constructor Details

#initialize(credentials = {}) ⇒ Mock

Returns a new instance of Mock.



234
235
236
237
238
# File 'lib/fog/vcloud.rb', line 234

def initialize(credentials = {})
  require 'builder'
  @versions_uri = URI.parse('https://vcloud.fakey.com/api/versions')
  @login_uri = 
end

Instance Method Details

#get_organization(organization_uri) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/fog/vcloud/requests/get_organization.rb', line 19

def get_organization(organization_uri)
  #
  # Based off of:
  # http://support.theenterprisecloud.com/kb/default.asp?id=540&Lang=1&SID=
  # 
  # vCloud API Guide v0.9 - Page 26
  #
  if org = mock_data[:organizations].detect { |org| URI.parse(org[:info][:href]) == organization_uri }
    xml = Builder::XmlMarkup.new

    mock_it Fog::Parsers::Vcloud::GetOrganization.new, 200,
      xml.Org(xmlns.merge(:href => org[:info][:href], :name => org[:info][:name])) {

        org[:vdcs].each do |vdc|
          xml.Link(:rel => "down",
                   :href => vdc[:href],
                   :type => "application/vnd.vmware.vcloud.vdc+xml",
                   :name => vdc[:name])
          xml.Link(:rel => "down",
                   :href => "#{vdc[:href]}/catalog",
                   :type => "application/vnd.vmware.vcloud.catalog+xml",
                   :name => "#{vdc[:name]} Catalog")
          xml.Link(:rel => "down",
                   :href => "#{vdc[:href]}/tasksList",
                   :type => "application/vnd.vmware.vcloud.tasksList+xml",
                   :name => "#{vdc[:name]} Tasks List")
        end
      },
      {'Content-Type' => "application/vnd.vmware.vcloud.org+xml" }
  else
    mock_error 200, "401 Unauthorized"
  end
end

#get_vdc(vdc_uri) ⇒ Object

WARNING: Incomplete Based off of: vCloud API Guide v0.9 - Page 27



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/fog/vcloud/requests/get_vdc.rb', line 22

def get_vdc(vdc_uri)
  if vdc = DATA[:organizations].map { |org| org[:vdcs] }.flatten.detect { |vdc| URI.parse(vdc[:href]) == vdc_uri }
    xml = Builder::XmlMarkup.new
    mock_it Fog::Parsers::Vcloud::GetVdc.new, 200,
      xml.Vdc(xmlns.merge(:href => vdc[:href], :name => vdc[:name])) {
        xml.Link(:rel => "up",
                 :href => DATA[:organizations].detect { |org| org[:vdcs].detect { |_vdc| vdc[:href] == _vdc[:href] }[:href] == vdc[:href] }[:info][:href],
                 :type => "application/vnd.vmware.vcloud.org+xml")
        xml.Link(:rel => "add",
                 :href => vdc[:href] + "/action/uploadVAppTemplate",
                 :type => "application/vnd.vmware.vcloud.uploadVAppTemplateParams+xml")
        xml.Link(:rel => "add",
                 :href => vdc[:href] + "/media",
                 :type => "application/vnd.vmware.vcloud.media+xml")
        xml.Link(:rel => "add",
                 :href => vdc[:href] + "/action/instantiateVAppTemplate",
                 :type => "application/vnd.vmware.vcloud.instantiateVAppTemplateParams+xml")
        xml.Link(:rel => "add",
                 :type => "application/vnd.vmware.vcloud.cloneVAppParams+xml",
                 :href => vdc[:href] + "/action/cloneVApp")
        xml.Link(:rel => "add",
                 :type => "application/vnd.vmware.vcloud.captureVAppParams+xml",
                 :href => vdc[:href] + "/action/captureVApp")
        xml.Link(:rel => "add",
                 :type => "application/vnd.vmware.vcloud.composeVAppParams+xml",
                 :href => vdc[:href] + "/action/composeVApp")
        xml.AllocationModel("AllocationPool")
        xml.Description(vdc[:name] + " VDC")
        xml.ResourceEntities {
          DATA[:vdc_resources].each do |resource|
            xml.ResourceEntity(resource)
          end
        }
        xml.AvailableNetworks {
          vdc[:networks].each do |network|
            xml.Network( :name => network[:name], :href => network[:href], :type => "application/vnd.vmware.vcloud.network+xml" )
          end
        }
        xml.ComputeCapacity{
          xml.Cpu {
            xml.Units("Mhz")
            xml.Allocated(vdc[:cpu][:allocated])
            xml.Limit(vdc[:cpu][:allocated])
          }
          xml.Memory {
            xml.Units("MB")
            xml.Allocated(vdc[:memory][:allocated])
            xml.Limit(vdc[:memory][:allocated])
          }
        }
        xml.StorageCapacity{
          xml.Units("MB")
          xml.Allocated(vdc[:storage][:allocated])
          xml.Limit(vdc[:storage][:allocated])
        }
        xml.VmQuota(0)
        xml.NicQuota(0)
        xml.IsEnabled('true')
        xml.NetworkQuota(0)
        #FIXME: Incomplete
      }, { 'Content-Type' => 'application/vnd.vmware.vcloud.vdc+xml' }
  else
    mock_error 200, "401 Unauthorized"
  end
end

#get_versionsObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/fog/vcloud/requests/get_versions.rb', line 19

def get_versions
  #
  # Based off of:
  # http://support.theenterprisecloud.com/kb/default.asp?id=535&Lang=1&SID=
  # https://community.vcloudexpress.terremark.com/en-us/product_docs/w/wiki/02-get-versions.aspx
  # vCloud API Guide v0.9 - Page 89
  #
  xml = Builder::XmlMarkup.new

  mock_it Fog::Parsers::Vcloud::GetVersions.new, 200,
    xml.SupportedVersions( xmlns.merge("xmlns" => "http://www.vmware.com/vcloud/versions")) {

      DATA[:versions].select {|version| version[:supported] }.each do |version|
        xml.VersionInfo {
          xml.Version(version[:version])
          xml.LoginUrl(version[:login_url])
        }
      end
    }

end

#loginObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/fog/vcloud/requests/login.rb', line 23

def 
  #
  # Based off of:
  # http://support.theenterprisecloud.com/kb/default.asp?id=536&Lang=1&SID=
  # https://community.vcloudexpress.terremark.com/en-us/product_docs/w/wiki/01-get-login-token.aspx
  # vCloud API Guide v0.9 - Page 17
  #
  xml = Builder::XmlMarkup.new

  mock_it Fog::Parsers::Vcloud::Login.new, 200,
    xml.OrgList(xmlns) {
        DATA[:organizations].each do |org|
          xml.Org( org[:info].merge( "type" => "application/vnd.vmware.vcloud.org+xml" ) ) {}
        end
      },
      { 'Set-Cookie' => 'vcloud-token=fc020a05-21d7-4f33-9b2a-25d8cd05a44e; path=/',
        'Content-Type' => 'application/vnd.vmware.vcloud.orgslist+xml' }

end

#mock_dataObject



261
262
263
# File 'lib/fog/vcloud.rb', line 261

def mock_data
  DATA
end

#mock_error(expected, status, body = '', headers = {}) ⇒ Object

Raises:

  • (Excon::Errors::Unauthorized)


257
258
259
# File 'lib/fog/vcloud.rb', line 257

def mock_error(expected, status, body='', headers={})
  raise Excon::Errors::Unauthorized.new("Expected(#{expected}) <=> Actual(#{status})")
end

#mock_it(parser, status, mock_data, mock_headers = {}) ⇒ Object



246
247
248
249
250
251
252
253
254
255
# File 'lib/fog/vcloud.rb', line 246

def mock_it(parser, status, mock_data, mock_headers = {})
  body = Nokogiri::XML::SAX::PushParser.new(parser)
  body << mock_data
  body.finish
  response = Excon::Response.new
  response.status = status
  response.body = parser.response
  response.headers = mock_headers
  response
end

#vdcs(options = {}) ⇒ Object



4
5
6
# File 'lib/fog/vcloud/models/vdcs.rb', line 4

def vdcs(options = {})
  @vdcs ||= Fog::Vcloud::Vdcs.new(options.merge(:connection => self))
end

#xmlnsObject



240
241
242
243
244
# File 'lib/fog/vcloud.rb', line 240

def xmlns
  { "xmlns" => "http://www.vmware.com/vcloud/v0.8",
    "xmlns:xsi" => "http://www.w3.org/2001/XMLSchema-instance",
    "xmlns:xsd" => "http://www.w3.org/2001/XMLSchema" }
end