Class: Fog::Vcloud::Mock

Inherits:
Real
  • Object
show all
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 inherited from Real

#login_uri, #supported_versions, #versions_uri

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Real

#default_organization_uri, #reload, #request, #vdcs, #xmlns

Methods included from Generators

#basic_request, #unauthenticated_basic_request

Constructor Details

#initialize(credentials = {}) ⇒ Mock

Returns a new instance of Mock.



347
348
349
350
# File 'lib/fog/vcloud.rb', line 347

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

Class Method Details

.base_urlObject



217
218
219
# File 'lib/fog/vcloud.rb', line 217

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

.data(base_url = self.base_url) ⇒ Object



225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
# File 'lib/fog/vcloud.rb', line 225

def self.data( base_url = self.base_url )
  @mock_data ||=
  {
    :versions => [
      { :version => "v0.8", :login_url => "#{base_url}/login", :supported => true }
    ],
    :vdc_resources => [
      {
        :type => "application/vnd.vmware.vcloud.vApp+xml",
        :href => "#{base_url}/vapp/61",
        :name => "Foo App 1"
      },
      {
        :type => "application/vnd.vmware.vcloud.vApp+xml",
        :href => "#{base_url}/vapp/62",
        :name => "Bar App 1"
      },
      {
        :type => "application/vnd.vmware.vcloud.vApp+xml",
        :href => "#{base_url}/vapp/63",
        :name => "Bar App 2"
      }
    ],
    :organizations =>
    [
      {
        :info => {
          :href => "#{base_url}/org/1",
          :name => "Boom Inc.",
        },
        :vdcs => [

          { :href => "#{base_url}/vdc/21",
            :id => "21",
            :name => "Boomstick",
            :storage => { :used => "105", :allocated => "200" },
            :cpu => { :allocated => "10000" },
            :memory => { :allocated => "20480" },
            :networks => [
              { :id => "31",
                :href => "#{base_url}/network/31",
                :name => "1.2.3.0/24",
                :subnet => "1.2.3.0/24",
                :gateway => "1.2.3.1",
                :netmask => "255.255.255.0",
                :dns => "8.8.8.8",
                :features => [
                  { :type => :FenceMode, :value => "isolated" }
                ],
                :ips => { "1.2.3.3" => "Broom 1", "1.2.3.4" => "Broom 2", "1.2.3.10" => "Email" }
              },
              { :id => "32",
                :href => "#{base_url}/network/32",
                :name => "4.5.6.0/24",
                :subnet => "4.5.6.0/24",
                :gateway => "4.5.6.1",
                :netmask => "255.255.255.0",
                :dns => "8.8.8.8",
                :features => [
                  { :type => :FenceMode, :value => "isolated" }
                ],
                :ips => { }
              },
            ],
            :vms => [
              { :href => "#{base_url}/vap/41",
                :name => "Broom 1"
              },
              { :href => "#{base_url}/vap/42",
                :name => "Broom 2"
              },
              { :href => "#{base_url}/vap/43",
                :name => "Email!"
              }
            ]
          },
          { :href => "#{base_url}/vdc/22",
            :id => "22",
            :storage => { :used => "40", :allocated => "150" },
            :cpu => { :allocated => "1000" },
            :memory => { :allocated => "2048" },
            :name => "Rock-n-Roll",
            :networks => [
              { :id => "33",
                :href => "#{base_url}/network/33",
                :name => "7.8.9.0/24",
                :subnet => "7.8.9.0/24",
                :gateway => "7.8.9.1",
                :dns => "8.8.8.8",
                :netmask => "255.255.255.0",
                :features => [
                  { :type => :FenceMode, :value => "isolated" }
                ],
                :ips => { "7.8.9.10" => "Master Blaster" }
              }
            ],
            :vms => [
              { :href => "#{base_url}/vap/44",
                :name => "Master Blaster"
              }
            ]
          }
        ]
      }
    ]
  }
end

.data_resetObject



221
222
223
# File 'lib/fog/vcloud.rb', line 221

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 org = mock_data[:organizations].detect { |org| org[:info][:href] == organization_uri }
    xml = Builder::XmlMarkup.new

    mock_it 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



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 => Fog::Vcloud::Mock.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 {
          Fog::Vcloud::Mock.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_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[:login_url])
        }
      end
    }

end

#ip_from_uri(uri) ⇒ Object



340
341
342
343
344
345
# File 'lib/fog/vcloud.rb', line 340

def ip_from_uri(uri)
  match = Regexp.new(%r:.*/publicIp/(\d+):).match(uri.to_s)
  if match
    mock_data[:organizations].map { |org| org[:vdcs] }.flatten.map { |vdc| vdc[:public_ips] }.flatten.compact.detect { |public_ip| public_ip[:id] == match[1] }
  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 |org|
          xml.Org( :type => "application/vnd.vmware.vcloud.org+xml", :href => org[:info][:href], :name => org[:info][:name] )
        end
      },
      { 'Set-Cookie' => 'vcloud-token=fc020a05-21d7-4f33-9b2a-25d8cd05a44e; path=/',
        'Content-Type' => 'application/vnd.vmware.vcloud.orgslist+xml' }

end

#mock_dataObject



375
376
377
# File 'lib/fog/vcloud.rb', line 375

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

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

Raises:

  • (Excon::Errors::Unauthorized)


371
372
373
# File 'lib/fog/vcloud.rb', line 371

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



352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
# File 'lib/fog/vcloud.rb', line 352

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

#vdc_from_uri(uri) ⇒ Object



333
334
335
336
337
338
# File 'lib/fog/vcloud.rb', line 333

def vdc_from_uri(uri)
  match = Regexp.new(%r:.*/vdc/(\d+):).match(uri.to_s)
  if match
    mock_data[:organizations].map { |org| org[:vdcs] }.flatten.detect { |vdc| vdc[:id] == match[1] }
  end
end