Module: Fog::Terremark::Shared::Mock

Includes:
Common
Included in:
Vcloud::Mock
Defined in:
lib/fog/terremark/shared.rb,
lib/fog/terremark/models/shared/vdcs.rb,
lib/fog/terremark/models/shared/tasks.rb,
lib/fog/terremark/models/shared/images.rb,
lib/fog/terremark/models/shared/servers.rb,
lib/fog/terremark/models/shared/networks.rb,
lib/fog/terremark/models/shared/addresses.rb,
lib/fog/terremark/requests/shared/get_vdc.rb,
lib/fog/terremark/models/shared/nodeservices.rb,
lib/fog/terremark/requests/shared/get_network.rb,
lib/fog/terremark/models/shared/internetservices.rb,
lib/fog/terremark/requests/shared/get_public_ips.rb,
lib/fog/terremark/requests/shared/get_organization.rb,
lib/fog/terremark/requests/shared/get_organizations.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Common

#default_organization_id

Class Method Details

.error_headersObject



185
186
187
188
189
190
191
192
# File 'lib/fog/terremark/shared.rb', line 185

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

.headers(body, content_type) ⇒ Object



198
199
200
201
202
203
204
205
206
# File 'lib/fog/terremark/shared.rb', line 198

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

.mock_dataObject



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
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
# File 'lib/fog/terremark/shared.rb', line 100

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

.statusObject



208
209
210
# File 'lib/fog/terremark/shared.rb', line 208

def self.status
  200
end

.unathorized_statusObject



194
195
196
# File 'lib/fog/terremark/shared.rb', line 194

def self.unathorized_status
  401
end

Instance Method Details

#addresses(options = {}) ⇒ Object



6
7
8
# File 'lib/fog/terremark/models/shared/addresses.rb', line 6

def addresses(options = {})
  Fog::Terremark::Shared::Addresses.new(options.merge(:service => self))
end

#get_network(network_id) ⇒ Object



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
# File 'lib/fog/terremark/requests/shared/get_network.rb', line 28

def get_network(network_id)
  network_id = network_id.to_i
  response = Excon::Response.new
  if network = self.data[:organizations].map { |org| org[:vdcs].map { |vdc| vdc[:networks] } }.flatten.detect { |network| network[:id] == network_id }

    body = { "links" => [],
             "type" => "application/vnd.vmware.vcloud.network+xml",
             "href" => "#{@base_url}/network/#{network_id}" }

    network.each_key do |key|
      body[key.to_s] = network[key]
    end

    link = { "name" => "IP Addresses",
             "rel"  => "down",
             "type" => "application/xml" }
    link["href"] = case self
    when Fog::Terremark::Ecloud::Mock
      "#{@base_url}/extensions/network/#{network_id}/ips"
    when Fog::Terremark::Vcloud::Mock
      "#{@base_url}/network/#{network_id}/ipAddresses"
    end
    body["links"] << link

    response.status = 200
    response.body = body
    response.headers = Fog::Terremark::Shared::Mock.headers(response.body, 
      case self
      when Fog::Terremark::Ecloud::Mock
        "application/vnd.vmware.vcloud.network+xml"
      when Fog::Terremark::Vcloud::Mock
        "application/xml; charset=utf-8"
      end
    )
  else
    response.status = Fog::Terremark::Shared::Mock.unathorized_status
    response.headers = Fog::Terremark::Shared::Mock.error_headers
  end

  response
end

#get_organization(organization_id) ⇒ Object



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
# File 'lib/fog/terremark/requests/shared/get_organization.rb', line 35

def get_organization(organization_id)
  organization_id = organization_id.to_i
  response = Excon::Response.new

  if org = self.data[:organizations].detect { |org| org[:info][:id] == organization_id }

    body = { "name" => org[:info][:name],
             "href" => "#{@base_url}/org/#{org[:info][:id]}",
             "Links" => [] }

    body["Links"] = case self
    when Fog::Terremark::Vcloud::Mock
      _vdc_links(org[:vdcs][0])
    when Fog::Terremark::Ecloud::Mock
      org[:vdcs].map do |vdc|
        _vdc_links(vdc)
      end.flatten
    end

    response.status = 200
    response.body = body
    response.headers = Fog::Terremark::Shared::Mock.headers(response.body, "application/vnd.vmware.vcloud.org+xml")
  else
    response.status = Fog::Terremark::Shared::Mock.unathorized_status
    response.headers = Fog::Terremark::Shared::Mock.error_headers
  end

  response
end

#get_organizationsObject



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/fog/terremark/requests/shared/get_organizations.rb', line 32

def get_organizations
  response = Excon::Response.new
  org_list = self.data[:organizations].map do |organization|
    { "name" => organization[:info][:name],
      "href" => "#{@base_url}/org/#{organization[:info][:id]}",
      "type" => "application/vnd.vmware.vcloud.org+xml"
    }
  end
  response.body = { "OrgList" => org_list }
  response.status = 200
  response.headers = Fog::Terremark::Shared::Mock.headers(response.body, "application/vnd.vmware.vcloud.orgList+xml")
  response
end

#get_public_ips(vdc_id) ⇒ Object



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
# File 'lib/fog/terremark/requests/shared/get_public_ips.rb', line 34

def get_public_ips(vdc_id)
  vdc_id = vdc_id.to_i
  response = Excon::Response.new

  if vdc = self.data[:organizations].map { |org| org[:vdcs] }.flatten.detect { |vdc| vdc[:id] == vdc_id }
    body = { "PublicIpAddresses" => [] }
    vdc[:public_ips].each do |ip|
      ip = { "name" => ip[:name],
             "href" => case self
                        when Fog::Terremark::Ecloud::Mock
                          "#{@base_url}/extensions/publicIp/#{ip[:id]}"
                        when Fog::Terremark::Vcloud::Mock
                          "#{@base_url}/PublicIps/#{ip[:id]}"
                        end,
             "id"   => ip[:id].to_s }
      body["PublicIpAddresses"] << ip
    end
    response.status = 200
    response.body = body
    response.headers = Fog::Terremark::Shared::Mock.headers(response.body,
                      case self
                      when Fog::Terremark::Ecloud::Mock
                        "application/vnd.tmrk.ecloud.publicIpsList+xml"
                      when Fog::Terremark::Vcloud::Mock
                        "application/xml; charset=utf-8"
                      end
    )
  else
    response.status = Fog::Terremark::Shared::Mock.unathorized_status
    response.headers = Fog::Terremark::Shared::Mock.error_headers
  end

  response
end

#get_vdc(vdc_id) ⇒ Object



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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/fog/terremark/requests/shared/get_vdc.rb', line 36

def get_vdc(vdc_id)
  vdc_id = vdc_id.to_i
  response = Excon::Response.new

  if vdc = self.data[:organizations].map { |org| org[:vdcs] }.flatten.detect { |vdc| vdc[:id] == vdc_id }

    body = { "name" => vdc[:name],
             "href" => "#{@base_url}/vdc/#{vdc[:id]}",
             "StorageCapacity" => {},
             "ComputeCapacity" => { "InstantiatedVmsQuota" => {},
                                    "DeployedVmsQuota" => {},
                                    "Cpu" => {},
                                    "Memory" => {} },
             "ResourceEntities" => [],
             "AvailableNetworks" => [],
             "links" => [] }

    case self
    when Fog::Terremark::Ecloud::Mock
      body["StorageCapacity"] = { "Units" => "bytes * 10^9" }
      vdc[:storage].each { |k,v| body["StorageCapacity"][k.to_s.capitalize] = v.to_s }

      body["ComputeCapacity"] = { "InstantiatedVmsQuota" => {"Limit" => "-1", "Used" => "-1"},
                                   "DeployedVmsQuota" => {"Limit" => "-1", "Used" => "-1"},
                                   "Cpu" => { "Units" => "hz * 10^6" },
                                   "Memory" => { "Units" => "bytes * 2^20" } }

      [:cpu, :memory].each do |key|
        vdc[key].each { |k,v| body["ComputeCapacity"][key.to_s.capitalize][k.to_s.capitalize] = v.to_s }
      end

      body["links"] << { "name" => "Public IPs",
                         "href" => "#{@base_url}/extensions/vdc/#{vdc[:id]}/publicIps",
                         "rel"  => "down",
                         "type" => "application/vnd.tmrk.ecloud.publicIpsList+xml" }

      body["links"] << { "name" => "Internet Services",
                         "href" => "#{@base_url}/extensions/vdc/#{vdc[:id]}/internetServices",
                         "rel"  => "down",
                         "type" => "application/vnd.tmrk.ecloud.internetServicesList+xml" }

      body["links"] << { "name" => "Firewall Access List",
                         "href" => "#{@base_url}/extensions/vdc/#{vdc[:id]}/firewallAcls",
                         "rel"  => "down",
                         "type" => "application/vnd.tmrk.ecloud.firewallAclsList+xml" }

    when Fog::Terremark::Vcloud::Mock
      body["links"] << { "name" => "Public IPs",
                         "href" => "#{@base_url}/vdc/#{vdc[:id]}/publicIps",
                         "rel"  => "down",
                         "type" => "application/xml" }

      body["links"] << { "name" => "Internet Services",
                         "href" => "#{@base_url}/vdc/#{vdc[:id]}/internetServices",
                         "rel"  => "down",
                         "type" => "application/xml" }
    end

    vdc[:vms].each do |vm|
      body["ResourceEntities"] << { "name" => vm[:name],
                                    "href" => "#{@base_url}/vapp/#{vm[:id]}",
                                    "type" => "application/vnd.vmware.vcloud.vApp+xml" }
    end

    vdc[:networks].each do |network|
      body["AvailableNetworks"] << { "name" => network[:name],
                                     "href" => "#{@base_url}/network/#{network[:id]}",
                                     "type" => "application/vnd.vmware.vcloud.network+xml" }
    end

    body["links"] << { "name" => vdc[:name],
                       "href" => "#{@base_url}/vdc/#{vdc[:id]}/catalog",
                       "rel"  => "down",
                       "type" => "application/vnd.vmware.vcloud.catalog+xml" }

    response.status = 200
    response.body = body
    response.headers = Fog::Terremark::Shared::Mock.headers(response.body, "application/vnd.vmware.vcloud.org+xml")
  else
    response.status = Fog::Terremark::Shared::Mock.unathorized_status
    response.headers = Fog::Terremark::Shared::Mock.error_headers
  end

  response
end

#images(options = {}) ⇒ Object



10
11
12
# File 'lib/fog/terremark/models/shared/images.rb', line 10

def images(options = {})
  Fog::Terremark::Shared::Images.new(options.merge(:service => self))
end

#initialize(options = {}) ⇒ Object



212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/fog/terremark/shared.rb', line 212

def initialize(options={})
  self.class.instance_eval '
    def self.data
      @data ||= Hash.new do |hash, key|
        hash[key] = Fog::Terremark::Shared::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

#internetservices(options = {}) ⇒ Object



9
10
11
# File 'lib/fog/terremark/models/shared/internetservices.rb', line 9

def internetservices(options = {})
  Fog::Terremark::Shared::Servers.new(options.merge(:service => self))
end

#networks(options = {}) ⇒ Object



6
7
8
# File 'lib/fog/terremark/models/shared/networks.rb', line 6

def networks(options = {})
  Fog::Terremark::Shared::Networks.new(options.merge(:service => self))
end

#nodeservices(options = {}) ⇒ Object



9
10
11
# File 'lib/fog/terremark/models/shared/nodeservices.rb', line 9

def nodeservices(options = {})
  Fog::Terremark::Shared::Servers.new(options.merge(:service => self))
end

#servers(options = {}) ⇒ Object



9
10
11
# File 'lib/fog/terremark/models/shared/servers.rb', line 9

def servers(options = {})
  Fog::Terremark::Shared::Servers.new(options.merge(:service => self))
end

#tasksObject



9
10
11
# File 'lib/fog/terremark/models/shared/tasks.rb', line 9

def tasks
  Fog::Terremark::Shared::Tasks.new(:service => self)
end

#vdcs(options = {}) ⇒ Object



6
7
8
# File 'lib/fog/terremark/models/shared/vdcs.rb', line 6

def vdcs(options = {})
  Fog::Terremark::Shared::Vdcs.new(options.merge(:service => self))
end