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

Instance Attribute Summary

Attributes inherited from Real

#login_uri, #supported_versions

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Real

#default_organization_uri

Constructor Details

#initialize(credentials = {}) ⇒ Mock

Returns a new instance of Mock.



270
271
272
273
# File 'lib/fog/vcloud.rb', line 270

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

Class Method Details

.base_urlObject



130
131
132
# File 'lib/fog/vcloud.rb', line 130

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

.dataObject



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
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
# File 'lib/fog/vcloud.rb', line 138

def self.data
  @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 => [
              { :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",
                :fencemode => "isolated"
              },
              { :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",
                :fencemode => "isolated"
              },
            ],
            :vms => [
              { :href => "#{base_url}/vap/41",
                :name => "Broom 1"
              },
              { :href => "#{base_url}/vap/42",
                :name => "Broom 2"
              },
              { :href => "#{base_url}/vap/43",
                :name => "Email!"
              }
            ],
            :public_ips => [
              { :id => 51,
                :name => "99.1.2.3",
                :services => [
                  { :id => 71, :port => 80, :protocol => 'HTTP', :enabled => true, :timeout => 2, :name => 'Web Site', :description => 'Web Servers' },
                  { :id => 72, :port => 7000, :protocol => 'HTTP', :enabled => true, :timeout => 2, :name => 'An SSH Map', :description => 'SSH 1' }
                ]
              },
              { :id => 52,
                :name => "99.1.2.4",
                :services => [
                  { :id => 73, :port => 80, :protocol => 'HTTP', :enabled => true, :timeout => 2, :name => 'Web Site', :description => 'Web Servers' },
                  { :id => 74, :port => 7000, :protocol => 'HTTP', :enabled => true, :timeout => 2, :name => 'An SSH Map', :description => 'SSH 2' }
                ]
              },
              { :id => 53,
                :name => "99.1.9.7",
                :services => []
              }
            ]
          },
          { :href => "#{base_url}/vdc/22",
            :id => 22,
            :storage => { :used => 40, :allocated => 150 },
            :cpu => { :allocated => 1000 },
            :memory => { :allocated => 2048 },
            :name => "Rock-n-Roll",
            :networks => [
              { :href => "#{base_url}/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 => "#{base_url}/vap/44",
                :name => "Master Blaster"
              }
            ],
            :public_ips => [
              { :id => 54,
                :name => "99.99.99.99",
                :services => []
              }
            ]
          }
        ]
      }
    ]
  }
end

.data_resetObject



134
135
136
# File 'lib/fog/vcloud.rb', line 134

def self.data_reset
  @mock_data = nil
end

.ip_from_uri(uri) ⇒ Object



263
264
265
266
267
268
# File 'lib/fog/vcloud.rb', line 263

def self.ip_from_uri(uri)
  match = Regexp.new(%r:.*/publicIp/(\d+):).match(uri.to_s)
  if match
    Fog::Vcloud::Mock.data[:organizations].map { |org| org[:vdcs] }.flatten.map { |vdc| vdc[:public_ips] }.flatten.compact.detect { |public_ip| public_ip[:id] == match[1].to_i }
  end
end

.vdc_from_uri(uri) ⇒ Object



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

def self.vdc_from_uri(uri)
  match = Regexp.new(%r:.*/vdc/(\d+):).match(uri.to_s)
  if match
    Fog::Vcloud::Mock.data[:organizations].map { |org| org[:vdcs] }.flatten.detect { |vdc| vdc[:id] == match[1].to_i }
  end
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 = Fog::Vcloud::Mock.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 => 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_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")) {

      Fog::Vcloud::Mock.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) {
        Fog::Vcloud::Mock.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



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

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

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

Raises:

  • (Excon::Errors::Unauthorized)


296
297
298
# File 'lib/fog/vcloud.rb', line 296

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



281
282
283
284
285
286
287
288
289
290
291
292
293
294
# File 'lib/fog/vcloud.rb', line 281

def mock_it(parser, status, mock_data, mock_headers = {})
  response = Excon::Response.new
  if parser
    body = Nokogiri::XML::SAX::PushParser.new(parser)
    body << mock_data
    body.finish
    response.body = parser.response
  else
    response.body = mock_data
  end
  response.status = status
  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



275
276
277
278
279
# File 'lib/fog/vcloud.rb', line 275

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