Class: Fog::Vcloud::Mock

Inherits:
Object
  • Object
show all
Includes:
MockDataClasses, Shared
Defined in:
lib/fog/vcloud.rb,
lib/fog/vcloud/requests/login.rb,
lib/fog/vcloud/requests/get_vdc.rb,
lib/fog/vcloud/requests/get_network.rb,
lib/fog/vcloud/requests/get_versions.rb,
lib/fog/vcloud/requests/get_organization.rb

Instance Attribute Summary

Attributes included from Shared

#versions_uri

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Shared

#default_organization_uri, #do_login, #ensure_unparsed, #supported_versions, #xmlns

Constructor Details

#initialize(options = {}) ⇒ Mock

Returns a new instance of Mock.



301
302
303
# File 'lib/fog/vcloud.rb', line 301

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

Class Method Details

.base_urlObject



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

def self.base_url
  "https://fakey.com/api/v0.8"
end

.data(base_url = self.base_url) ⇒ Object



266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
# File 'lib/fog/vcloud.rb', line 266

def self.data( base_url = self.base_url )
  MockDataClasses::Base.base_url = base_url

  @mock_data ||= MockData.new.tap do |mock_data|
    mock_data.versions << MockVersion.new(:version => "v0.8", :supported => true)

    mock_data.organizations << MockOrganization.new(:name => "Boom Inc.").tap do |mock_organization|
      mock_organization.vdcs << MockVdc.new(:name => "Boomstick").tap do |mock_vdc|
        mock_vdc.catalog.items << MockCatalogItem.new(:name => "Item 0").tap do |mock_catalog_item|
          mock_catalog_item.disks << MockVirtualMachineDisk.new(:size => 25 * 1024)
        end
        mock_vdc.catalog.items << MockCatalogItem.new(:name => "Item 1").tap do |mock_catalog_item|
          mock_catalog_item.disks << MockVirtualMachineDisk.new(:size => 25 * 1024)
        end
        mock_vdc.catalog.items << MockCatalogItem.new(:name => "Item 2").tap do |mock_catalog_item|
          mock_catalog_item.disks << MockVirtualMachineDisk.new(:size => 25 * 1024)
        end

        mock_vdc.networks << MockNetwork.new({ :subnet => "1.2.3.0/24" }, mock_vdc)
        mock_vdc.networks << MockNetwork.new({ :subnet => "4.5.6.0/24" }, mock_vdc)

        mock_vdc.virtual_machines << MockVirtualMachine.new({ :name => "Broom 1", :ip => "1.2.3.3" }, mock_vdc)
        mock_vdc.virtual_machines << MockVirtualMachine.new({ :name => "Broom 2", :ip => "1.2.3.4" }, mock_vdc)
        mock_vdc.virtual_machines << MockVirtualMachine.new({ :name => "Email!", :ip => "1.2.3.10" }, mock_vdc)
      end

      mock_organization.vdcs << MockVdc.new(:name => "Rock-n-Roll", :storage_allocated => 150, :storage_used => 40, :cpu_allocated => 1000, :memory_allocated => 2048).tap do |mock_vdc|
        mock_vdc.networks << MockNetwork.new({ :subnet => "7.8.9.0/24" }, mock_vdc)

        mock_vdc.virtual_machines << MockVirtualMachine.new({ :name => "Master Blaster", :ip => "7.8.9.10" }, mock_vdc)
      end
    end
  end
end

.data_resetObject



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

def self.data_reset
  @mock_data = nil
end

Instance Method Details

#get_network(network_uri) ⇒ Object



10
11
12
13
14
15
16
17
18
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
# File 'lib/fog/vcloud/requests/get_network.rb', line 10

def get_network(network_uri)
  #
  # Based off of:
  # vCloud API Guide v0.8 - Page 50
  #
  # Did not implement AssociatedNetwork, seems redundant, haven't seen it in use yet
  # Did not implement the following features: Dhcp, Nat & Firewall
  #
  network_uri = ensure_unparsed(network_uri)
  type = "application/vnd.vmware.vcloud.network+xml"
  response = Excon::Response.new
  if network = mock_data.organizations.map { |org| org.vdcs.map { |vdc| vdc.networks } }.flatten.detect { |network| network.href == network_uri.to_s }
    xml = Builder::XmlMarkup.new
    mock_it 200,
      xml.Network(xmlns.merge(:href => network.href, :name => network.name, :type => type)) {
        xml.Description(network.name)
        xml.Configuration {
          xml.Gateway(network.gateway)
          xml.Netmask(network.netmask)
          xml.Dns(network.dns)
        }
        if network.features
          xml.Features {
            if feature = network.features.detect { |feature| feature[:type] == :FenceMode }
              xml.FenceMode(feature[:value])
            end
          }
        end
      },
      { 'Content-Type' => type }
  else
    mock_error 200, "401 Unauthorized"
  end
end

#get_organization(organization_uri) ⇒ Object



10
11
12
13
14
15
16
17
18
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
# File 'lib/fog/vcloud/requests/get_organization.rb', line 10

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
  #
  organization_uri = ensure_unparsed(organization_uri)
  if organization = mock_data.organization_from_href(organization_uri)
    xml = Builder::XmlMarkup.new

    mock_it 200,
      xml.Org(xmlns.merge(:href => organization.href, :name => organization.name)) {

        organization.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.catalog.href,
                   :type => "application/vnd.vmware.vcloud.catalog+xml",
                   :name => vdc.catalog.name)
          xml.Link(:rel => "down",
                   :href => vdc.task_list.href,
                   :type => "application/vnd.vmware.vcloud.tasksList+xml",
                   :name => vdc.task_list.name)
        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



13
14
15
16
17
18
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
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
# File 'lib/fog/vcloud/requests/get_vdc.rb', line 13

def get_vdc(vdc_uri)
  vdc_uri = ensure_unparsed(vdc_uri)
  if vdc = mock_data.organizations.map { |org| org.vdcs }.flatten.detect { |vdc| vdc.href == vdc_uri }
    xml = Builder::XmlMarkup.new
    mock_it 200,
      xml.Vdc(xmlns.merge(:href => vdc.href, :name => vdc.name)) {
        xml.Link(:rel => "up",
                 :href => mock_data.organizations.detect { |org| org.vdcs.detect { |_vdc| vdc.href == _vdc.href }.href == vdc.href }.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 {
          mock_data.organizations.first.vdcs.first.virtual_machines.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_versions(versions_uri) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/fog/vcloud/requests/get_versions.rb', line 10

def get_versions(versions_uri)
  #
  # 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 200,
    xml.SupportedVersions( xmlns.merge("xmlns" => "http://www.vmware.com/vcloud/versions")) {

      mock_data.versions.select {|version| version.supported }.each do |version|
        xml.VersionInfo {
          xml.Version(version.version)
          xml.LoginUrl(version.)
        }
      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 200,
    xml.OrgList(xmlns) {
        mock_data.organizations.each do |organization|
          xml.Org( :type => "application/vnd.vmware.vcloud.org+xml", :href => organization.href, :name => organization.name )
        end
      },
      { 'Set-Cookie' => 'vcloud-token=fc020a05-21d7-4f33-9b2a-25d8cd05a44e; path=/',
        'Content-Type' => 'application/vnd.vmware.vcloud.orgslist+xml' }

end

#mock_dataObject



328
329
330
# File 'lib/fog/vcloud.rb', line 328

def mock_data
  Fog::Vcloud::Mock.data
end

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

Raises:

  • (Excon::Errors::Unauthorized)


324
325
326
# File 'lib/fog/vcloud.rb', line 324

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

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



305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
# File 'lib/fog/vcloud.rb', line 305

def mock_it(status, mock_data, mock_headers = {})
  response = Excon::Response.new

  #Parse the response body into a hash
  if mock_data.empty?
    response.body = mock_data
  else
    document = Fog::ToHashDocument.new
    parser = Nokogiri::XML::SAX::PushParser.new(document)
    parser << mock_data
    parser.finish
    response.body = document.body
  end

  response.status = status
  response.headers = mock_headers
  response
end