Class: Fog::Compute::OpenStack::Mock

Inherits:
Object
  • Object
show all
Defined in:
lib/fog/openstack/requests/compute/stop_server.rb,
lib/fog/openstack/compute.rb,
lib/fog/openstack/requests/compute/get_quota.rb,
lib/fog/openstack/requests/compute/get_usage.rb,
lib/fog/openstack/requests/compute/get_limits.rb,
lib/fog/openstack/requests/compute/list_hosts.rb,
lib/fog/openstack/requests/compute/set_tenant.rb,
lib/fog/openstack/requests/compute/delete_meta.rb,
lib/fog/openstack/requests/compute/get_address.rb,
lib/fog/openstack/requests/compute/list_images.rb,
lib/fog/openstack/requests/compute/list_usages.rb,
lib/fog/openstack/requests/compute/update_meta.rb,
lib/fog/openstack/requests/compute/add_fixed_ip.rb,
lib/fog/openstack/requests/compute/create_image.rb,
lib/fog/openstack/requests/compute/delete_image.rb,
lib/fog/openstack/requests/compute/get_metadata.rb,
lib/fog/openstack/requests/compute/list_flavors.rb,
lib/fog/openstack/requests/compute/list_servers.rb,
lib/fog/openstack/requests/compute/list_tenants.rb,
lib/fog/openstack/requests/compute/list_volumes.rb,
lib/fog/openstack/requests/compute/pause_server.rb,
lib/fog/openstack/requests/compute/set_metadata.rb,
lib/fog/openstack/requests/compute/start_server.rb,
lib/fog/openstack/requests/compute/update_quota.rb,
lib/fog/openstack/requests/compute/attach_volume.rb,
lib/fog/openstack/requests/compute/create_flavor.rb,
lib/fog/openstack/requests/compute/create_server.rb,
lib/fog/openstack/requests/compute/create_volume.rb,
lib/fog/openstack/requests/compute/delete_flavor.rb,
lib/fog/openstack/requests/compute/delete_server.rb,
lib/fog/openstack/requests/compute/delete_volume.rb,
lib/fog/openstack/requests/compute/detach_volume.rb,
lib/fog/openstack/requests/compute/list_metadata.rb,
lib/fog/openstack/requests/compute/reboot_server.rb,
lib/fog/openstack/requests/compute/rescue_server.rb,
lib/fog/openstack/requests/compute/resize_server.rb,
lib/fog/openstack/requests/compute/resume_server.rb,
lib/fog/openstack/requests/compute/update_server.rb,
lib/fog/openstack/requests/compute/list_addresses.rb,
lib/fog/openstack/requests/compute/list_key_pairs.rb,
lib/fog/openstack/requests/compute/list_snapshots.rb,
lib/fog/openstack/requests/compute/migrate_server.rb,
lib/fog/openstack/requests/compute/rebuild_server.rb,
lib/fog/openstack/requests/compute/server_actions.rb,
lib/fog/openstack/requests/compute/suspend_server.rb,
lib/fog/openstack/requests/compute/unpause_server.rb,
lib/fog/openstack/requests/compute/create_key_pair.rb,
lib/fog/openstack/requests/compute/delete_key_pair.rb,
lib/fog/openstack/requests/compute/delete_metadata.rb,
lib/fog/openstack/requests/compute/delete_snapshot.rb,
lib/fog/openstack/requests/compute/get_vnc_console.rb,
lib/fog/openstack/requests/compute/release_address.rb,
lib/fog/openstack/requests/compute/remove_fixed_ip.rb,
lib/fog/openstack/requests/compute/update_metadata.rb,
lib/fog/openstack/requests/compute/allocate_address.rb,
lib/fog/openstack/requests/compute/get_host_details.rb,
lib/fog/openstack/requests/compute/add_flavor_access.rb,
lib/fog/openstack/requests/compute/associate_address.rb,
lib/fog/openstack/requests/compute/get_image_details.rb,
lib/fog/openstack/requests/compute/add_security_group.rb,
lib/fog/openstack/requests/compute/get_console_output.rb,
lib/fog/openstack/requests/compute/get_flavor_details.rb,
lib/fog/openstack/requests/compute/get_quota_defaults.rb,
lib/fog/openstack/requests/compute/get_security_group.rb,
lib/fog/openstack/requests/compute/get_server_details.rb,
lib/fog/openstack/requests/compute/get_server_volumes.rb,
lib/fog/openstack/requests/compute/get_volume_details.rb,
lib/fog/openstack/requests/compute/list_address_pools.rb,
lib/fog/openstack/requests/compute/list_all_addresses.rb,
lib/fog/openstack/requests/compute/list_images_detail.rb,
lib/fog/openstack/requests/compute/reset_server_state.rb,
lib/fog/openstack/requests/compute/server_diagnostics.rb,
lib/fog/openstack/requests/compute/list_flavors_detail.rb,
lib/fog/openstack/requests/compute/list_servers_detail.rb,
lib/fog/openstack/requests/compute/live_migrate_server.rb,
lib/fog/openstack/requests/compute/disassociate_address.rb,
lib/fog/openstack/requests/compute/get_snapshot_details.rb,
lib/fog/openstack/requests/compute/list_security_groups.rb,
lib/fog/openstack/requests/compute/remove_flavor_access.rb,
lib/fog/openstack/requests/compute/revert_resize_server.rb,
lib/fog/openstack/requests/compute/confirm_resize_server.rb,
lib/fog/openstack/requests/compute/create_security_group.rb,
lib/fog/openstack/requests/compute/delete_security_group.rb,
lib/fog/openstack/requests/compute/list_public_addresses.rb,
lib/fog/openstack/requests/compute/remove_security_group.rb,
lib/fog/openstack/requests/compute/change_server_password.rb,
lib/fog/openstack/requests/compute/create_volume_snapshot.rb,
lib/fog/openstack/requests/compute/list_private_addresses.rb,
lib/fog/openstack/requests/compute/get_security_group_rule.rb,
lib/fog/openstack/requests/compute/create_security_group_rule.rb,
lib/fog/openstack/requests/compute/delete_security_group_rule.rb,
lib/fog/openstack/requests/compute/list_tenants_with_flavor_access.rb

Overview

class Real

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Mock

Returns a new instance of Mock.



248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# File 'lib/fog/openstack/compute.rb', line 248

def initialize(options={})
  @openstack_username = options[:openstack_username]
  @openstack_auth_uri = URI.parse(options[:openstack_auth_url])

  @current_tenant = options[:openstack_tenant]

  @auth_token = Fog::Mock.random_base64(64)
  @auth_token_expiration = (Time.now.utc + 86400).iso8601

  management_url = URI.parse(options[:openstack_auth_url])
  management_url.port = 8774
  management_url.path = '/v1.1/1'
  @openstack_management_url = management_url.to_s

  identity_public_endpoint = URI.parse(options[:openstack_auth_url])
  identity_public_endpoint.port = 5000
  @openstack_identity_public_endpoint = identity_public_endpoint.to_s
end

Instance Attribute Details

#auth_tokenObject (readonly)

Returns the value of attribute auth_token.



175
176
177
# File 'lib/fog/openstack/compute.rb', line 175

def auth_token
  @auth_token
end

#auth_token_expirationObject (readonly)

Returns the value of attribute auth_token_expiration.



176
177
178
# File 'lib/fog/openstack/compute.rb', line 176

def auth_token_expiration
  @auth_token_expiration
end

#current_tenantObject (readonly)

Returns the value of attribute current_tenant.



178
179
180
# File 'lib/fog/openstack/compute.rb', line 178

def current_tenant
  @current_tenant
end

#current_userObject (readonly)

Returns the value of attribute current_user.



177
178
179
# File 'lib/fog/openstack/compute.rb', line 177

def current_user
  @current_user
end

Class Method Details

.dataObject



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
# File 'lib/fog/openstack/compute.rb', line 180

def self.data
  @data ||= Hash.new do |hash, key|
    hash[key] = {
      :last_modified => {
        :images  => {},
        :servers => {},
        :key_pairs => {},
        :security_groups => {},
        :addresses => {}
      },
      :images  => {
        "0e09fbd6-43c5-448a-83e9-0d3d05f9747e" => {
          "id"=>"0e09fbd6-43c5-448a-83e9-0d3d05f9747e",
          "name"=>"cirros-0.3.0-x86_64-blank",
          'progress'  => 100,
          'status'    => "ACTIVE",
          'updated'   => "",
          'minRam'    => 0,
          'minDisk'   => 0,
          'metadata'  => {},
          'links'     => [{"href"=>"http://nova1:8774/v1.1/admin/images/1", "rel"=>"self"}, {"href"=>"http://nova1:8774/admin/images/2", "rel"=>"bookmark"}]
        }
      },
      :servers => {},
      :key_pairs => {},
      :security_groups => {
        '0' => {
          "id"          => 0,
          "tenant_id"   => Fog::Mock.random_hex(8),
          "name"        => "default",
          "description" => "default",
          "rules"       => [
            { "id"              => 0,
              "parent_group_id" => 0,
              "from_port"       => 68,
              "to_port"         => 68,
              "ip_protocol"     => "udp",
              "ip_range"        => { "cidr" => "0.0.0.0/0" },
              "group"           => {}, },
          ],
        },
      },
      :server_security_group_map => {},
      :addresses => {},
      :quota => {
        'security_group_rules' => 20,
        'security_groups' => 10,
        'injected_file_content_bytes' => 10240,
        'injected_file_path_bytes' => 256,
        'injected_files' => 5,
        'metadata_items' => 128,
        'floating_ips'   => 10,
        'instances'      => 10,
        'key_pairs'      => 10,
        'gigabytes'      => 5000,
        'volumes'        => 10,
        'cores'          => 20,
        'ram'            => 51200
      },
      :volumes => {}
    }
  end
end

.resetObject



244
245
246
# File 'lib/fog/openstack/compute.rb', line 244

def self.reset
  @data = nil
end

Instance Method Details

#add_fixed_ip(server_id, network_id) ⇒ Object



23
24
25
# File 'lib/fog/openstack/requests/compute/add_fixed_ip.rb', line 23

def add_fixed_ip(server_id, network_id)
  true
end

#add_flavor_access(flavor_ref, tenant_id) ⇒ Object



20
21
22
23
24
25
26
27
# File 'lib/fog/openstack/requests/compute/add_flavor_access.rb', line 20

def add_flavor_access(flavor_ref, tenant_id)
  response = Excon::Response.new
  response.status = 200
  response.body = {
    "flavor_access" => [{ "tenant_id" => tenant_id.to_s, "flavor_id" => flavor_ref.to_s }]
  }
  response
end

#add_security_group(server_id, group_name) ⇒ Object



12
13
14
15
16
# File 'lib/fog/openstack/requests/compute/add_security_group.rb', line 12

def add_security_group(server_id, group_name)
  response = Excon::Response.new
  response.status = 200
  response
end

#allocate_address(pool = nil) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/fog/openstack/requests/compute/allocate_address.rb', line 16

def allocate_address(pool = nil)
  response = Excon::Response.new
  response.status = 200
  response.headers = {
    "X-Compute-Request-Id" => "req-d4a21158-a86c-44a6-983a-e25645907f26",
    "Content-Type" => "application/json",
    "Content-Length" => "105",
    "Date"=> Date.new
  }
  response.body = {
    "floating_ip" => {
      "instance_id" => nil,
      "ip" => "192.168.27.132",
      "fixed_ip" => nil,
      "id" => 4,
      "pool"=>"nova"
    }
  }
  response
end

#associate_address(server_id, ip_address) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/fog/openstack/requests/compute/associate_address.rb', line 12

def associate_address(server_id, ip_address)
  server = data[:servers][server_id]
  server["addresses"]['mocknet'] ||= []
  ip_hash = {"OS-EXT-IPS-MAC:mac_addr"=>"fa:16:3e:85:47:40", "version"=>4, "addr"=>ip_address, "OS-EXT-IPS:type"=>"floating"}
  server["addresses"]['mocknet'] << ip_hash

  response = Excon::Response.new
  response.status = 202
  response.headers = {
    "Content-Type" => "text/html, charset=UTF-8",
    "Content-Length" => "0",
    "Date"=> Date.new
  }
  response
end

#attach_volume(volume_id, server_id, device) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/fog/openstack/requests/compute/attach_volume.rb', line 22

def attach_volume(volume_id, server_id, device)
  response = Excon::Response.new
  response.status = 200
  data = {
     'id'       => volume_id,
     'volumeId' => volume_id,
     'serverId' => server_id,
     'device'   => device
  }
  self.data[:volumes][volume_id]['attachments'] << data
  response.body = { 'volumeAttachment' => data }
  response
end

#change_server_password(server_id, admin_password) ⇒ Object



12
13
14
15
16
# File 'lib/fog/openstack/requests/compute/change_server_password.rb', line 12

def change_server_password(server_id, admin_password)
  response = Excon::Response.new
  response.status = 202
  response
end

#confirm_resize_server(server_id) ⇒ Object



12
13
14
15
16
# File 'lib/fog/openstack/requests/compute/confirm_resize_server.rb', line 12

def confirm_resize_server(server_id)
  response = Excon::Response.new
  response.status = 204
  response
end

#create_flavor(attributes) ⇒ Object



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
# File 'lib/fog/openstack/requests/compute/create_flavor.rb', line 47

def create_flavor(attributes)
  response = Excon::Response.new
  response.status = 200
  response.headers = {
    "X-Compute-Request-Id" => "req-fdc6f99e-55a2-4ab1-8904-0892753828cf",
    "Content-Type" => "application/json",
    "Content-Length" => "356",
    "Date" => Date.new
  }
  response.body = {
    "flavor" => {
      "vcpus" => attributes[:vcpus],
      "disk" => attributes[:disk],
      "name" => attributes[:name],
      "links" => [
        {
          "href" => "http://192.168.27.100:8774/v1.1/6733e93c5f5c4eb1bcabc6902ba208d6/flavors/11",
          "rel" => "self"
        },
        {
          "href" => "http://192.168.27.100:8774/6733e93c5f5c4eb1bcabc6902ba208d6/flavors/11",
          "rel" => "bookmark"
        }
      ],
      "rxtx_factor" => attributes[:rxtx_factor] || 1.0,
      "OS-FLV-EXT-DATA:ephemeral" => attributes[:ephemeral] || 0,
      "os-flavor-access:is_public" => attributes[:is_public] || false,
      "OS-FLV-DISABLED:disabled" => attributes[:disabled] || false,
      "ram" => attributes[:ram],
      "id" => "11",
      "swap" => attributes[:swap] || ""
    }
  }
  response
end

#create_image(server_id, name, metadata = {}) ⇒ Object



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

def create_image(server_id, name, ={})
  response = Excon::Response.new
  response.status = 202

  img_id=Fog::Mock.random_numbers(6).to_s

  data = {
    'id'        => img_id,
    'server'     => {"id"=>"3", "links"=>[{"href"=>"http://nova1:8774/admin/servers/#{server_id}", "rel"=>"bookmark"}]},
    'links'     => [{"href"=>"http://nova1:8774/v1.1/admin/images/#{img_id}", "rel"=>"self"}, {"href"=>"http://nova1:8774/admin/images/#{img_id}", "rel"=>"bookmark"}],
    'metadata'  =>  || {},
    'name'      => name || "server_#{rand(999)}",
    'progress'  => 0,
    'status'    => 'SAVING',
    'minDisk'   => 0,
    'minRam'    => 0,
    'updated'   => "",
    'created'   => ""
  }
  self.data[:last_modified][:images][data['id']] = Time.now
  self.data[:images][data['id']] = data
  response.body = { 'image' => data }
  response
end

#create_key_pair(key_name, public_key = nil) ⇒ Object



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

def create_key_pair(key_name, public_key = nil)
  response = Excon::Response.new
  response.status = 200
  response.headers = {
    "X-Compute-Request-Id" => "req-c373a42c-2825-4e60-8d34-99416ea850be",
    "Content-Type" => "application/json",
    "Content-Length" => "1289",
    "Date" => Date.new}
  response.body = {
    "keypair" => {
      "public_key" => "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAgQDCdAZLjljntJbLVVkNHjWFSoKen2nZbk39ZfqhZJOMdeFdz02GWBS45rcuboeGg/gozKRwsLu4N6NLPlYtbK/NapJIvgO/djBp+FQG1QZNtLPsx7j4hVJac3yISGms+Xtu4cEv6j5sFDzAgTQbWez0Z1+9qOq9ngdaoW+YClfQ== vagrant@nova\n",
      "private_key" => "-----BEGIN RSA PRIVATE KEY-----\nMIICXgIBAAKBgQDCdAZLjljn1tJbLVVkNHjWFSoKen2nZbk39ZfqhZJOMdeFdz02\nGWBS45rcuHboeGg/gozKRwsLu4N6NLPlYtbK/NapJIvgO/djBp+FQG1QZNtLPsx7\nj4hVJac3yISGms+Xtu4cEv6j5sFDzAgTQbWez0Z1+9qOq9ngdaoW+YClfQIDAQAB\nAoGBALBoT9m1vuQ82EONQf2RONqHAsfUzi/SMhEZRgOlv9AemXZkcWyl4uPvxmtd\nEcreiTystAtCHjw7lhCExXthipevUjtIAAt+b3pMn6Oyjad3IRvde6atMdjrje43\n/nftYtuXYyJTsvwEvLYqSioLQ0Nn/XDKhOpcM5tejDHOH35lAkEA+H4r7y9X521u\nIABVAezBWaT/wvdMjx5cwfyYEQjnI1bxfRIqkgoY5gDDBdVbT75UTsHHbHLORQcw\nRjRvS2zgewJBAMhT6eyMonJvHHvC5RcchcY+dWkscIKoOzeyUKMb+7tERQa9/UN2\njYb+jdM0VyL0ruLFwYtl2m34gfmhcXgIvGcCQGzKMEnjHEUBr7jq7EyPbobkqeSd\niDMQQ+PZxmmO0EK0ib0L+v881HG926PuKK/cz+Q7Cif8iznFT+ksg50t6YkCQQC9\nwfcAskqieSuS9A9LcCIrojhXctf0e+T0Ij2N89DlF4sHEuqXf/IZ4IB5gsfTfdE3\nUDnAkK9yogaEbu/r0uKbAkEAy5kl71bIqvKTKsY2mES9ziVxfftl/9UIi5LI+QHb\nmC/c6cTrGVCM71fi2GMxGgBeEea4+7xwoWTL4CxA00kmTg==\n-----END RSA PRIVATE KEY-----\n",
      "user_id" => "admin",
      "name" => key_name,
      "fingerprint" => "97:86:f4:15:68:0c:7b:a7:e5:8f:f0:bd:1f:27:65:ad"
    }
  }
  response
end

#create_security_group(name, description) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/fog/openstack/requests/compute/create_security_group.rb', line 23

def create_security_group(name, description)
  Fog::Identity::OpenStack.new(:openstack_auth_url => credentials[:openstack_auth_url])
  tenant_id = Fog::Identity::OpenStack::Mock.data[current_tenant][:tenants].keys.first
  security_group_id = Fog::Mock.random_numbers(2).to_i + 1
  self.data[:security_groups][security_group_id.to_s] = {
    'tenant_id' => tenant_id,
    'rules'     => [],
    'id'        => security_group_id,
    'name'      => name,
    'description' => description
  }

  response = Excon::Response.new
  response.status = 200
  response.headers = {
    'X-Compute-Request-Id' => "req-#{Fog::Mock.random_hex(32)}",
    'Content-Type'   => 'application/json',
    'Content-Length' => Fog::Mock.random_numbers(3).to_s,
    'Date'           => Date.new}
  response.body = {
    'security_group' => self.data[:security_groups][security_group_id.to_s]
  }
  response
end

#create_security_group_rule(parent_group_id, ip_protocol, from_port, to_port, cidr, group_id = nil) ⇒ Object



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
# File 'lib/fog/openstack/requests/compute/create_security_group_rule.rb', line 27

def create_security_group_rule(parent_group_id, ip_protocol, from_port, to_port, cidr, group_id=nil)
  parent_group_id = parent_group_id.to_i
  response = Excon::Response.new
  response.status = 200
  response.headers = {
    'X-Compute-Request-Id' => "req-#{Fog::Mock.random_hex(32)}",
    'Content-Type'   => 'application/json',
    'Content-Length' => Fog::Mock.random_numbers(3).to_s,
    'Date' => Date.new
  }
  rule = {
    'id' => Fog::Mock.random_numbers(2).to_i,
    'from_port'   => from_port,
    'group'       => group_id || {},
    'ip_protocol' => ip_protocol,
    'to_port'     => to_port,
    'parent_group_id' => parent_group_id,
    'ip_range' => {
      'cidr'   => cidr
    }
  }
  self.data[:security_groups][parent_group_id.to_s]['rules'].push(rule)
  response.body = {
    'security_group_rule' => rule
  }
  response
end

#create_server(name, image_ref, flavor_ref, options = {}) ⇒ Object



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
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
# File 'lib/fog/openstack/requests/compute/create_server.rb', line 88

def create_server(name, image_ref, flavor_ref, options = {})
  response = Excon::Response.new
  response.status = 202

  server_id = Fog::Mock.random_numbers(6).to_s
  identity = Fog::Identity::OpenStack.new :openstack_auth_url => credentials[:openstack_auth_url]
  user = identity.users.find { |u|
    u.name == @openstack_username
  }

  user_id = if user then
              user.id
            else
               response = identity.create_user(@openstack_username,
                 'password',
                 "#{@openstack_username}@example.com")
               response.body["user"]["id"]
            end

  mock_data = {
    'addresses'    => {"Private" => [{"addr" => Fog::Mock.random_ip }]},
    'flavor'       => {"id" => flavor_ref, "links"=>[{"href"=>"http://nova1:8774/admin/flavors/1", "rel"=>"bookmark"}]},
    'id'           => server_id,
    'image'        => {"id" => image_ref, "links"=>[{"href"=>"http://nova1:8774/admin/images/#{image_ref}", "rel"=>"bookmark"}]},
    'links'        => [{"href"=>"http://nova1:8774/v1.1/admin/servers/5", "rel"=>"self"}, {"href"=>"http://nova1:8774/admin/servers/5", "rel"=>"bookmark"}],
    'hostId'       => "123456789ABCDEF01234567890ABCDEF",
    'metadata'     => options['metadata'] || {},
    'name'         => name || "server_#{rand(999)}",
    'accessIPv4'   => options['accessIPv4'] || "",
    'accessIPv6'   => options['accessIPv6'] || "",
    'progress'     => 0,
    'status'       => 'BUILD',
    'created'      => '2012-09-27T00:04:18Z',
    'updated'      => '2012-09-27T00:04:27Z',
    'user_id'      => @openstack_username,
    'config_drive' => options['config_drive'] || '',
  }

  if nics = options['nics']
    nics.each do |nic|
      mock_data["addresses"].merge!(
        "Public" => [{ 'addr' => Fog::Mock.random_ip }]
      )
    end
  end

  response_data = {}
  if options['return_reservation_id'] == 'True' then
    response_data = { 'reservation_id' => "r-#{Fog::Mock.random_numbers(6).to_s}" }
  else
    response_data = {
      'adminPass'       => 'password',
      'id'              => server_id,
      'links'           => mock_data['links'],
    }
  end

  self.data[:last_modified][:servers][server_id] = Time.now
  self.data[:servers][server_id] = mock_data
  if security_groups = options['security_groups'] then
    groups = Array(options['security_groups']).map do |sg|
      if sg.is_a?(Fog::Compute::OpenStack::SecurityGroup) then
        sg.name
      else
        sg
      end
    end

    self.data[:server_security_group_map][server_id] = groups
    response_data['security_groups'] = groups
  end

  self.data[:last_modified][:servers][server_id] = Time.now
  self.data[:servers][server_id] = mock_data
  if options['return_reservation_id'] == 'True' then
    response.body = response_data
  else
    response.body = { 'server' => response_data }
  end
  response
end

#create_volume(name, description, size, options = {}) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/fog/openstack/requests/compute/create_volume.rb', line 28

def create_volume(name, description, size, options={})
  response = Excon::Response.new
  response.status = 202
  data = {
    'id'                  => Fog::Mock.random_numbers(2),
    'displayName'         => name,
    'displayDescription'  => description,
    'size'                => size,
    'status'              => 'creating',
    'snapshotId'          => nil,
    'volumeType'          => 'None',
    'availabilityZone'    => 'nova',
    'createdAt'           => Time.now.strftime('%FT%T.%6N'),
    'attachments'         => [],
    'metadata'            => {}
  }
  self.data[:volumes][data['id']] = data
  response.body = { 'volume' => data }
  response
end

#create_volume_snapshot(volume_id, name, description, force = false) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/fog/openstack/requests/compute/create_volume_snapshot.rb', line 25

def create_volume_snapshot(volume_id, name, description, force=false)
  response = Excon::Response.new
  response.status = 202
  response.body = {
    "snapshot"=> {
       "status"=>"creating",
       "display_name"=>name,
       "created_at"=>Time.now,
       "display_description"=>description,
       "volume_id"=>volume_id,
       "id"=>"5",
       "size"=>1
    }
  }
  response
end

#credentialsObject



275
276
277
278
279
280
281
# File 'lib/fog/openstack/compute.rb', line 275

def credentials
  { :provider                 => 'openstack',
    :openstack_auth_url       => @openstack_auth_uri.to_s,
    :openstack_auth_token     => @auth_token,
    :openstack_management_url => @openstack_management_url,
    :openstack_identity_endpoint => @openstack_identity_public_endpoint }
end

#dataObject



267
268
269
# File 'lib/fog/openstack/compute.rb', line 267

def data
  self.class.data["#{@openstack_username}-#{@current_tenant}"]
end

#delete_flavor(flavor_id) ⇒ Object



15
16
17
18
19
20
21
22
23
24
# File 'lib/fog/openstack/requests/compute/delete_flavor.rb', line 15

def delete_flavor(flavor_id)
  response = Excon::Response.new
  response.status = 202
  response.headers = {
    "Content-Type" => "text/html; charset=UTF-8",
    "Content-Length" => "0",
    "Date" => Date.new
  }
  response
end

#delete_image(image_id) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/fog/openstack/requests/compute/delete_image.rb', line 15

def delete_image(image_id)
  response = Excon::Response.new
  if image = list_images_detail.body['images'].find {|_| _['id'] == image_id}
    if image['status'] == 'SAVING'
      response.status = 409
      raise(Excon::Errors.status_error({:expects => 202}, response))
    else
      self.data[:last_modified][:images].delete(image_id)
      self.data[:images].delete(image_id)
      response.status = 202
    end
    response
  else
    response.status = 400
    raise(Excon::Errors.status_error({:expects => 202}, response))
  end
end

#delete_key_pair(key_name) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/fog/openstack/requests/compute/delete_key_pair.rb', line 15

def delete_key_pair(key_name)
  response = Excon::Response.new
  response.status = 202
  response.headers = {
    "Content-Type" => "text/html; charset=UTF-8",
    "Content-Length" => "0",
    "Date" => Date.new
  }
  response.body = {}
  response
end

#delete_meta(collection_name, parent_id, key) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/fog/openstack/requests/compute/delete_meta.rb', line 15

def delete_meta(collection_name, parent_id, key)
  if collection_name == "images" then
    if not list_images_detail.body['images'].find {|_| _['id'] == parent_id}
      raise Fog::Compute::OpenStack::NotFound
    end
  end

  if collection_name == "servers" then
    if not list_servers_detail.body['servers'].find {|_| _['id'] == parent_id}
      raise Fog::Compute::OpenStack::NotFound
    end
  end

  response = Excon::Response.new
  response.status = 204
  response
end

#delete_metadata(collection_name, parent_id, key) ⇒ Object



15
16
17
18
19
# File 'lib/fog/openstack/requests/compute/delete_metadata.rb', line 15

def (collection_name, parent_id, key)
  response = Excon::Response.new
  response.status = 204
  response
end

#delete_security_group(security_group_id) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/fog/openstack/requests/compute/delete_security_group.rb', line 15

def delete_security_group(security_group_id)
  self.data[:security_groups].delete security_group_id.to_s

  response = Excon::Response.new
  response.status = 202
  response.headers = {
    "Content-Type" => "text/html; charset=UTF-8",
    "Content-Length" => "0",
    "Date" => Date.new
  }
  response.body = {}
  response
end

#delete_security_group_rule(security_group_rule_id) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/fog/openstack/requests/compute/delete_security_group_rule.rb', line 15

def delete_security_group_rule(security_group_rule_id)
  security_group = self.data[:security_groups].values.find{|sg| sg["rules"].find{ |sgr| sgr["id"].to_s == security_group_rule_id.to_s }}
  security_group["rules"].reject! { |sgr| sgr["id"] == security_group_rule_id }
  response = Excon::Response.new
  response.status = 202
  response.headers = {
    "Content-Type" => "text/html; charset=UTF-8",
    "Content-Length" => "0",
    "Date" => Date.new
  }
  response.body = {}
  response
end

#delete_server(server_id) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/fog/openstack/requests/compute/delete_server.rb', line 15

def delete_server(server_id)
  response = Excon::Response.new
  if server = list_servers_detail.body['servers'].find {|_| _['id'] == server_id}
    if server['status'] == 'BUILD'
      response.status = 409
      raise(Excon::Errors.status_error({:expects => 204}, response))
    else
      self.data[:last_modified][:servers].delete(server_id)
      self.data[:servers].delete(server_id)
      response.status = 204
    end
    response
  else
    raise Fog::Compute::OpenStack::NotFound
  end
end

#delete_snapshot(snapshot_id) ⇒ Object



15
16
17
18
19
# File 'lib/fog/openstack/requests/compute/delete_snapshot.rb', line 15

def delete_snapshot(snapshot_id)
  response = Excon::Response.new
  response.status = 204
  response
end

#delete_volume(volume_id) ⇒ Object



15
16
17
18
19
20
21
22
23
24
# File 'lib/fog/openstack/requests/compute/delete_volume.rb', line 15

def delete_volume(volume_id)
  response = Excon::Response.new
  if list_volumes.body['volumes'].map { |v| v['id'] }.include? volume_id
    self.data[:volumes].delete(volume_id)
    response.status = 204
    response
  else
    raise Fog::Compute::OpenStack::NotFound
  end
end

#detach_volume(server_id, attachment_id) ⇒ Object



15
16
17
18
19
20
21
22
23
24
# File 'lib/fog/openstack/requests/compute/detach_volume.rb', line 15

def detach_volume(server_id, attachment_id)
  response = Excon::Response.new
  if self.data[:volumes][attachment_id] &&
     self.data[:volumes][attachment_id]['attachments'].reject! { |attachment| attachment['serverId'] == server_id }
    response.status = 202
    response
  else
    raise Fog::Compute::OpenStack::NotFound
  end
end

#disassociate_address(server_id, ip_address) ⇒ Object



12
13
14
15
16
17
18
19
20
21
# File 'lib/fog/openstack/requests/compute/disassociate_address.rb', line 12

def disassociate_address(server_id, ip_address)
  response = Excon::Response.new
  response.status = 202
  response.headers = {
    "Content-Type" => "text/html, charset=UTF-8",
    "Content-Length" => "0",
    "Date"=> Date.new
  }
  response
end

#get_address(address_id) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/fog/openstack/requests/compute/get_address.rb', line 15

def get_address(address_id)
  response = Excon::Response.new
  response.status = 200
  response.headers = {
    "X-Compute-Request-Id" => "req-d4a21158-a86c-44a6-983a-e25645907f26",
    "Content-Type" => "application/json",
    "Content-Length" => "105",
    "Date"=> Date.new
  }
  response.body = {
    "floating_ip" => {
      "instance_id" => nil,
      "ip" => "192.168.27.129",
      "fixed_ip" => nil,
      "id" => 1,
      "pool" => "nova"
    }
  }
  response
end

#get_console_output(server_id, log_length) ⇒ Object



16
17
18
19
20
# File 'lib/fog/openstack/requests/compute/get_console_output.rb', line 16

def get_console_output(server_id, log_length)
  response = Excon::Response.new
  response.status = 200
  response
end

#get_flavor_details(flavor_ref) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/fog/openstack/requests/compute/get_flavor_details.rb', line 15

def get_flavor_details(flavor_ref)
  response = Excon::Response.new
  flavor = {
    '1' => { 'id' => '1', 'name' => '256 server', 'ram' => 256, 'disk' => 10, 'swap' => '1', 'vcpus' => 1, 'OS-FLV-EXT-DATA:ephemeral' => 1, 'links' => [] },
    '2' => { 'id' => '2', 'name' => '512 server', 'ram' => 512, 'disk' => 20, 'swap' => '1', 'vcpus' => 2, 'OS-FLV-EXT-DATA:ephemeral' => 1, 'links' => [] },
    '3' => { 'id' => '3', 'name' => '1GB server', 'ram' => 1024, 'disk' => 40, 'swap' => '2', 'vcpus' => 2, 'OS-FLV-EXT-DATA:ephemeral' => 1, 'links' => [] },
    '4' => { 'id' => '4', 'name' => '2GB server', 'ram' => 2048, 'disk' => 80, 'swap' => '4', 'vcpus' => 4, 'OS-FLV-EXT-DATA:ephemeral' => 1, 'links' => [] },
    '5' => { 'id' => '5', 'name' => '4GB server', 'ram' => 4096, 'disk' => 160, 'swap' => '8', 'vcpus' => 8, 'OS-FLV-EXT-DATA:ephemeral' => 1, 'links' => [] },
    '6' => { 'id' => '6', 'name' => '8GB server', 'ram' => 8192, 'disk' => 320, 'swap' => '16', 'vcpus' => 16, 'OS-FLV-EXT-DATA:ephemeral' => 1, 'links' => [] },
    '7' => { 'id' => '7', 'name' => '15.5GB server', 'ram' => 15872, 'disk' => 620, 'swap' => '32', 'vcpus' => 32, 'OS-FLV-EXT-DATA:ephemeral' => 1, 'links' => [] }
  }[flavor_ref]
  if flavor
    response.status = 200
    response.body = {
      'flavor' => flavor
    }
    response
  else
    raise Fog::Compute::OpenStack::NotFound
  end
end

#get_host_details(host) ⇒ Object



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
# File 'lib/fog/openstack/requests/compute/get_host_details.rb', line 15

def get_host_details(host)
  response = Excon::Response.new
  response.status = 200
  response.body = { "host" => [
      { "resource" => {
          "project" => "(total)",
          "memory_mb" => 64427,
          "host" => "cn28.la-1-3.morphcloud.net",
          "cpu" => 12,
          "disk_gb" => 1608
          }
      },
      { "resource" => {
          "project" => "(used_now)",
          "memory_mb" => 1753,
          "host" => "cn28.la-1-3.morphcloud.net",
          "cpu" => 3,
          "disk_gb" => 33
        }
      },
      { "resource" => {
          "project" => "(used_max)",
          "memory_mb" => 7168,
          "host" => "cn28.la-1-3.morphcloud.net",
          "cpu" => 3,
          "disk_gb" => 45
        }
      },
      { "resource" => {
          "project" => "bf8301f5164f4790889a1bc2bfb16d99",
          "memory_mb" => 5120,
          "host" => "cn28.la-1-3.morphcloud.net",
          "cpu" => 2,
          "disk_gb" => 35
        }
      },
      { "resource" => {
          "project" => "3bb4d0301c5f47d5b4d96a361fcf96f4",
          "memory_mb" => 2048,
          "host" => "cn28.la-1-3.morphcloud.net",
          "cpu" => 1,
          "disk_gb" => 10
        }
      }
    ]
  }
  response
end

#get_image_details(image_id) ⇒ Object



15
16
17
18
19
20
21
22
23
24
# File 'lib/fog/openstack/requests/compute/get_image_details.rb', line 15

def get_image_details(image_id)
  response = Excon::Response.new
  if image = list_images_detail.body['images'].find {|_| _['id'] == image_id}
    response.status = [200, 203][rand(1)]
    response.body = { 'image' => image }
    response
  else
    raise Fog::Compute::OpenStack::NotFound
  end
end

#get_limitsObject



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
79
80
81
82
83
84
85
# File 'lib/fog/openstack/requests/compute/get_limits.rb', line 17

def get_limits
  rate_limits = [
    { 'regex' => '.*',
      'limit' => [
        { 'next-available' => '2012-11-22T16:13:44Z',
          'unit'           => 'MINUTE',
          'verb'           => 'POST',
          'remaining'      => 9,
          'value'          => 10 },
        { 'next-available' => '2012-11-23T00:46:14Z',
          'unit'           => 'MINUTE',
          'verb'           => 'PUT',
          'remaining'      => 10,
          'value' => 10 },
        { 'next-available' => '2012-11-22T16:14:30Z',
          'unit'           => 'MINUTE',
          'verb'           => 'DELETE',
          'remaining'      => 99,
          'value'          => 100 } ],
        'uri' => '*' },
    { 'regex' => '^/servers',
      'limit' => [
        { 'next-available' => '2012-11-23T00:46:14Z',
          'unit'           => 'DAY',
          'verb'           => 'POST',
          'remaining'      => 50,
          'value'          => 50} ],
      'uri'=>'*/servers' },
    { 'regex' => '.*changes-since.*',
      'limit' => [
        { 'next-available' => '2012-11-23T00:46:14Z',
          'unit'           => 'MINUTE',
          'verb'           => 'GET',
          'remaining'      => 3,
          'value'          => 3 } ],
      'uri' => '*changes-since*' }
  ]

  absolute_limits = {
    # Max
    'maxServerMeta'            => 128,
    'maxTotalInstances'        => 10,
    'maxPersonality'           => 5,
    'maxImageMeta'             => 128,
    'maxPersonalitySize'       => 10240,
    'maxSecurityGroupRules'    => 20,
    'maxTotalKeypairs'         => 100,
    'maxSecurityGroups'        => 10,
    'maxTotalCores'            => 20,
    'maxTotalFloatingIps'      => 10,
    'maxTotalRAMSize'          => 51200,

    # Used
    'totalCoresUsed'           => -1,
    'totalRAMUsed'             => -2048,
    'totalInstancesUsed'       => -1,
    'totalSecurityGroupsUsed'  => 0,
    'totalFloatingIpsUsed'     => 0
  }

  Excon::Response.new(
    :status => 200,
    :body => {
      'limits' => {
        'rate'     => rate_limits,
        'absolute' => absolute_limits }
    }
  )
end

#get_metadata(collection_name, parent_id, key) ⇒ Object



15
16
17
18
19
20
# File 'lib/fog/openstack/requests/compute/get_metadata.rb', line 15

def (collection_name, parent_id, key)
  response = Excon::Response.new
  response.status = 200
  response.body = { 'meta' => {} }
  response
end

#get_quota(tenant_id) ⇒ Object



15
16
17
18
19
20
21
22
# File 'lib/fog/openstack/requests/compute/get_quota.rb', line 15

def get_quota(tenant_id)
  response = Excon::Response.new
  response.status = 200
  response.body = {
    'quota_set' => (self.data[:quota_updated] or self.data[:quota]).merge({'id' => tenant_id})
  }
  response
end

#get_quota_defaults(tenant_id) ⇒ Object



15
16
17
18
19
20
21
22
# File 'lib/fog/openstack/requests/compute/get_quota_defaults.rb', line 15

def get_quota_defaults(tenant_id)
  response = Excon::Response.new
  response.status = 200
  response.body = {
    'quota_set' => self.data[:quota].merge({'id' => tenant_id})
  }
  response
end

#get_security_group(security_group_id) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/fog/openstack/requests/compute/get_security_group.rb', line 15

def get_security_group(security_group_id)
  security_group = self.data[:security_groups][security_group_id.to_s]
  response = Excon::Response.new
  if security_group
    response.status = 200
    response.headers = {
      "X-Compute-Request-Id" => "req-63a90344-7c4d-42e2-936c-fd748bced1b3",
      "Content-Type" => "application/json",
      "Content-Length" => "167",
      "Date" => Date.new
    }
    response.body = {
      "security_group" => security_group
    }
  else
    raise Fog::Compute::OpenStack::NotFound, "Security group #{security_group_id} does not exist"
  end
  response
end

#get_security_group_rule(security_group_rule_id) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/fog/openstack/requests/compute/get_security_group_rule.rb', line 15

def get_security_group_rule(security_group_rule_id)
  security_group_rule = nil
  self.data[:security_groups].find{|id, sg| security_group_rule = sg["rules"].find{ |sgr| sgr["id"].to_s == security_group_rule_id.to_s }}
  response = Excon::Response.new
  if security_group_rule
    response.status = 200
    response.headers = {
      "X-Compute-Request-Id" => "req-63a90344-7c4d-42e2-936c-fd748bced1b3",
      "Content-Type" => "application/json",
      "Content-Length" => "167",
      "Date" => Date.new
    }
    response.body = {
      "security_group_rule" => security_group_rule
    }
  else
    raise Fog::Compute::OpenStack::NotFound, "Security group rule #{security_group_rule_id} does not exist"
  end
  response
end

#get_server_details(server_id) ⇒ Object



15
16
17
18
19
20
21
22
23
24
# File 'lib/fog/openstack/requests/compute/get_server_details.rb', line 15

def get_server_details(server_id)
  response = Excon::Response.new
  if server = list_servers_detail.body['servers'].find {|_| _['id'] == server_id}
    response.status = [200, 203][rand(1)]
    response.body = { 'server' => server }
    response
  else
    raise Fog::Compute::OpenStack::NotFound
  end
end

#get_server_volumes(server_id) ⇒ Object



15
16
17
18
19
20
21
22
23
# File 'lib/fog/openstack/requests/compute/get_server_volumes.rb', line 15

def get_server_volumes(server_id)
  response = Excon::Response.new
  response.status = 200
  data = self.data[:volumes].values.select do |vol|
    vol['attachments'].find { |attachment| attachment["serverId"] == server_id }
  end
  response.body = { 'volumeAttachments' => data.map! { |vol| vol['attachments'] }.flatten(1) }
  response
end

#get_snapshot_details(snapshot_id) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/fog/openstack/requests/compute/get_snapshot_details.rb', line 15

def get_snapshot_details(snapshot_id)
  response = Excon::Response.new
  response.status = 200
  response.body = {
    'snapshot' => {
      'id'                 => '1',
      'display_name'        => Fog::Mock.random_letters(rand(8) + 5),
      'display_description' => Fog::Mock.random_letters(rand(12) + 10),
      'size'               => 3,
      'volume_id'           => '4',
      'status'             => 'online',
      'created_at'          => Time.now
    }
  }
  response
end

#get_usage(tenant_id, date_start, date_end) ⇒ 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
# File 'lib/fog/openstack/requests/compute/get_usage.rb', line 19

def get_usage(tenant_id, date_start, date_end)
    response        = Excon::Response.new
    response.status = 200
    response.body   = {"tenant_usage"=>
                        {"total_memory_mb_usage" => 0.0,
                         "total_vcpus_usage"     => 0.0,
                         "total_hours"           => 0.0,
                         "tenant_id"             => tenant_id,
                         "stop"                  => date_start,
                         "start"                 => date_end,
                         "total_local_gb_usage"  => 0.0,
                         "server_usages" =>[{
                           "hours"       => 0.0,
                           "uptime"      => 69180,
                           "local_gb"    => 0,
                           "ended_at"    => nil,
                           "name"        => "test server",
                           "tenant_id"   => tenant_id,
                           "vcpus"       => 1,
                           "memory_mb"   => 512,
                           "state"       => "active",
                           "flavor"      => "m1.tiny",
                           "started_at"  => "2012-03-05 09:11:44"
                            }
                          ]
                        }
                      }
  response
end

#get_vnc_console(server_id, console_type) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/fog/openstack/requests/compute/get_vnc_console.rb', line 26

def get_vnc_console(server_id, console_type)
  response = Excon::Response.new
  response.status = 200
  response.body = {
    "console" => {
      "url"  => "http://192.168.27.100:6080/vnc_auto.html?token=c3606020-d1b7-445d-a88f-f7af48dd6a20",
      "type" => "novnc"
    }
  }
  response
end

#get_volume_details(volume_id) ⇒ Object



15
16
17
18
19
20
21
22
23
24
# File 'lib/fog/openstack/requests/compute/get_volume_details.rb', line 15

def get_volume_details(volume_id)
  response = Excon::Response.new
  if data = self.data[:volumes][volume_id]
    response.status = 200
    response.body = { 'volume' => data }
    response
  else
    raise Fog::Compute::OpenStack::NotFound
  end
end

#list_address_poolsObject



15
16
17
18
19
20
21
22
23
24
# File 'lib/fog/openstack/requests/compute/list_address_pools.rb', line 15

def list_address_pools
  response = Excon::Response.new
  response.status = 200
  response.body = {
    'floating_ip_pools' => [
      { 'name' => 'nova' }
    ]
  }
  response
end

#list_addresses(server_id) ⇒ Object



15
16
17
18
19
20
21
22
23
24
# File 'lib/fog/openstack/requests/compute/list_addresses.rb', line 15

def list_addresses(server_id)
  response = Excon::Response.new
  if server = list_servers_detail.body['servers'].find {|_| _['id'] == server_id}
    response.status = [200, 203][rand(1)]
    response.body = { 'addresses' => server['addresses'] }
    response
  else
    raise Fog::Compute::OpenStack::NotFound
  end
end

#list_all_addressesObject



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
# File 'lib/fog/openstack/requests/compute/list_all_addresses.rb', line 15

def list_all_addresses
  response = Excon::Response.new
  response.status = 200
  response.headers = {
    "X-Compute-Request-Id" => "req-d4a21158-a86c-44a6-983a-e25645907f26",
    "Content-Type" => "application/json",
    "Content-Length" => "378",
    "Date"=> Date.new
  }
  response.body = {
    "floating_ips" => [
      {
        "instance_id" => nil,
        "ip" => "192.168.27.129",
        "fixed_ip" => nil,
        "id" => 1,
        "pool" => "nova"
      },
      {
        "instance_id" => nil,
        "ip" => "192.168.27.130",
        "fixed_ip" => nil,
        "id" => 2,
        "pool" => "nova"
      },
      {
        "instance_id" => nil,
        "ip" => "192.168.27.131",
        "fixed_ip" => nil,
        "id" => 3,
        "pool" => "nova"
      },
      {
        "instance_id" => nil,
        "ip" => "192.168.27.132",
        "fixed_ip" => nil,
        "id" => 4,
        "pool" => "nova"
      }
    ]
  }
  response
end

#list_flavorsObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/fog/openstack/requests/compute/list_flavors.rb', line 15

def list_flavors
  response = Excon::Response.new
  response.status = 200
  response.body = {
    'flavors' => [
      { 'name' => '256 server', 'id' => '1', 'links' => ['https://itdoesntmatterwhatshere.heh'] },
      { 'name' => '512 server', 'id' => '2', 'links' => ['https://itdoesntmatterwhatshere.heh'] },
      { 'name' => '1GB server', 'id' => '3', 'links' => ['https://itdoesntmatterwhatshere.heh'] },
      { 'name' => '2GB server', 'id' => '4', 'links' => ['https://itdoesntmatterwhatshere.heh'] },
      { 'name' => '4GB server', 'id' => '5', 'links' => ['https://itdoesntmatterwhatshere.heh'] },
      { 'name' => '8GB server', 'id' => '6', 'links' => ['https://itdoesntmatterwhatshere.heh'] },
      { 'name' => '15.5GB server', 'id' => '7', 'links' => ['https://itdoesntmatterwhatshere.heh'] }
    ]
  }
  response
end

#list_flavors_detail(options = {}) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/fog/openstack/requests/compute/list_flavors_detail.rb', line 16

def list_flavors_detail(options = {})
  response = Excon::Response.new
  response.status = 200
  response.body = {
    'flavors' => [
      { 'id' => '1', 'name' => '256 server', 'ram' => 256, 'disk' => 10, 'swap' => '1', 'vcpus' => 1, 'OS-FLV-EXT-DATA:ephemeral' => 1, 'links' => [] },
      { 'id' => '2', 'name' => '512 server', 'ram' => 512, 'disk' => 20, 'swap' => '1', 'vcpus' => 2, 'OS-FLV-EXT-DATA:ephemeral' => 1, 'links' => [] },
      { 'id' => '3', 'name' => '1GB server', 'ram' => 1024, 'disk' => 40, 'swap' => '2', 'vcpus' => 2, 'OS-FLV-EXT-DATA:ephemeral' => 1, 'links' => [] },
      { 'id' => '4', 'name' => '2GB server', 'ram' => 2048, 'disk' => 80, 'swap' => '4', 'vcpus' => 4, 'OS-FLV-EXT-DATA:ephemeral' => 1, 'links' => [] },
      { 'id' => '5', 'name' => '4GB server', 'ram' => 4096, 'disk' => 160, 'swap' => '8', 'vcpus' => 8, 'OS-FLV-EXT-DATA:ephemeral' => 1, 'links' => [] },
      { 'id' => '6', 'name' => '8GB server', 'ram' => 8192, 'disk' => 320, 'swap' => '16', 'vcpus' => 16, 'OS-FLV-EXT-DATA:ephemeral' => 1, 'links' => [] },
      { 'id' => '7', 'name' => '15.5GB server', 'ram' => 15872, 'disk' => 620, 'swap' => '32', 'vcpus' => 32, 'OS-FLV-EXT-DATA:ephemeral' => 1, 'links' => [] }
    ]
  }
  response
end

#list_hostsObject



15
16
17
18
19
20
21
22
23
# File 'lib/fog/openstack/requests/compute/list_hosts.rb', line 15

def list_hosts
  response = Excon::Response.new
  response.status = 200
  response.body = { "hosts" => [
      {"host_name" => "host.test.net", "service"=>"compute", "zone" => "az1"}
    ]
  }
  response
end

#list_imagesObject



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/fog/openstack/requests/compute/list_images.rb', line 15

def list_images
  response = Excon::Response.new
  data = list_images_detail.body['images']
  images = []
  for image in data
    images << image.reject { |key, value| !['id', 'name', 'links'].include?(key) }
  end
  response.status = [200, 203][rand(1)]
  response.body = { 'images' => images }
  response
end

#list_images_detail(filters = {}) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/fog/openstack/requests/compute/list_images_detail.rb', line 16

def list_images_detail(filters = {})
  response = Excon::Response.new

  images = self.data[:images].values
  for image in images
    case image['status']
    when 'SAVING'
      if Time.now - self.data[:last_modified][:images][image['id']] >= Fog::Mock.delay
        image['status'] = 'ACTIVE'
      end
    end
  end

  response.status = [200, 203][rand(1)]
  response.body = { 'images' => images.map {|image| image.reject {|key, value| !['id', 'name', 'links', 'minRam', 'minDisk', 'metadata', 'status', 'updated'].include?(key)}} }
  response
end

#list_key_pairsObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/fog/openstack/requests/compute/list_key_pairs.rb', line 15

def list_key_pairs
  response = Excon::Response.new
  response.status = 200
  response.headers = {
    "X-Compute-Request-Id" => "req-c373a42c-2825-4e60-8d34-99416ea850be",
    "Content-Type" => "application/json",
    "Content-Length" => "360",
    "Date" => Date.new}
  response.body = {
    "keypairs" => [{
      "keypair" => {
        "public_key" => "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAgQDCdAZLjln1tJbLVVkNHjWFSoKen2nZbk39ZfqhZJOMdeFdz02GWBS4rcuHboeGg/gozKRwsLu4N6NLPlYtbK/NapJIvgO/djBp+FG1QZNtLPsx7j4hVJac3yISGms+Xtu4cEv6j5sFDzAgTQbWz0Z1+9qOq9ngdaoW+YClfQ== vagrant@nova\n",
        "name" => "test_key",
        "fingerprint" => "97:86:f4:15:68:0c:7b:a7:e5:8f:f0:bd:1f:27:65:ad"
      }
    }]
  }
  response
end

#list_metadata(collection_name, parent_id) ⇒ Object



15
16
17
18
19
20
# File 'lib/fog/openstack/requests/compute/list_metadata.rb', line 15

def (collection_name, parent_id)
  response = Excon::Response.new
  response.status = 200
  response.body = {}
  response
end

#list_private_addresses(server_id) ⇒ Object



15
16
17
18
19
20
21
22
23
24
# File 'lib/fog/openstack/requests/compute/list_private_addresses.rb', line 15

def list_private_addresses(server_id)
  response = Excon::Response.new
  if server = list_servers_detail.body['servers'].find {|_| _['id'] == server_id}
    response.status = [200, 203][rand(1)]
    response.body = { 'private' => server['addresses']['private'] }
    response
  else
    raise Fog::Compute::OpenStack::NotFound
  end
end

#list_public_addresses(server_id) ⇒ Object



15
16
17
18
19
20
21
22
23
24
# File 'lib/fog/openstack/requests/compute/list_public_addresses.rb', line 15

def list_public_addresses(server_id)
  response = Excon::Response.new
  if server = list_servers_detail.body['servers'].find {|_| _['id'] == server_id}
    response.status = [200, 203][rand(1)]
    response.body = { 'public' => server['addresses']['public'] }
    response
  else
    raise Fog::Compute::OpenStack::NotFound
  end
end

#list_security_groups(server_id = nil) ⇒ 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
# File 'lib/fog/openstack/requests/compute/list_security_groups.rb', line 19

def list_security_groups(server_id = nil)
  security_groups = self.data[:security_groups].values

  groups = if server_id then
             server_group_names =
               Array(self.data[:server_security_group_map][server_id])

             server_group_names.map do |name|
               security_groups.find do |sg|
                 sg['name'] == name
               end
             end.compact
           else
             security_groups
           end

  Excon::Response.new(
    :body     => { 'security_groups' => groups },
    :headers  => {
      "X-Compute-Request-Id" => "req-#{Fog::Mock.random_base64(36)}",
      "Content-Type" => "application/json",
      "Date" => Date.new
    },
    :status   => 200
  )
end

#list_servers(options = {}) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/fog/openstack/requests/compute/list_servers.rb', line 19

def list_servers(options = {})
  response = Excon::Response.new
  data = list_servers_detail.body['servers']
  servers = []
  for server in data
    servers << server.reject { |key, value| !['id', 'name', 'links'].include?(key) }
  end
  response.status = [200, 203][rand(1)]
  response.body = { 'servers' => servers }
  response
end

#list_servers_detail(filters = {}) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/fog/openstack/requests/compute/list_servers_detail.rb', line 20

def list_servers_detail(filters = {})
  response = Excon::Response.new

  servers = self.data[:servers].values
  for server in servers
    case server['status']
    when 'BUILD'
      if Time.now - self.data[:last_modified][:servers][server['id']] > Fog::Mock.delay * 2
        server['status'] = 'ACTIVE'
      end
    end
  end

  response.status = [200, 203][rand(1)]
  response.body = { 'servers' => servers }
  response
end

#list_snapshots(detailed = true) ⇒ Object



16
17
18
19
20
21
22
23
# File 'lib/fog/openstack/requests/compute/list_snapshots.rb', line 16

def list_snapshots(detailed=true)
  response = Excon::Response.new
  response.status = 200
  response.body = {
    'snapshots' => [get_snapshot_details.body["snapshot"]]
  }
  response
end

#list_tenantsObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/fog/openstack/requests/compute/list_tenants.rb', line 20

def list_tenants
  response = Excon::Response.new
  response.status = [200, 204][rand(1)]
  response.body = {
    'tenants_links' => [],
    'tenants' => [
      {'id' => '1',
       'description' => 'Has access to everything',
       'enabled' => true,
       'name' => 'admin'},
      {'id' => '2',
       'description' => 'Normal tenant',
       'enabled' => true,
       'name' => 'default'},
      {'id' => '3',
       'description' => 'Disabled tenant',
       'enabled' => false,
       'name' => 'disabled'}
    ]
  }
  response
end

#list_tenants_with_flavor_access(flavor_ref) ⇒ Object



15
16
17
18
19
20
21
22
# File 'lib/fog/openstack/requests/compute/list_tenants_with_flavor_access.rb', line 15

def list_tenants_with_flavor_access(flavor_ref)
  response = Excon::Response.new
  response.status = 200
  response.body = {
    "flavor_access" => [{ "tenant_id" => @tenant_id.to_s, "flavor_id" => flavor_ref.to_s }]
  }
  response
end

#list_usages(date_start = nil, date_end = nil, detailed = false) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/fog/openstack/requests/compute/list_usages.rb', line 21

def list_usages(date_start = nil, date_end = nil, detailed=false)
  params = Hash.new
  response        = Excon::Response.new
  response.status = 200
  response.body   = {"tenant_usages"=>[{
                       "total_memory_mb_usage" => 0.00036124444444444445,
                       "total_vcpus_usage"     => 7.055555555555556e-07,
                       "start"                 => "2012-03-06 05:05:56.349001",
                       "tenant_id"             => "b97c8abba0c44a0987c63b858a4823e5",
                       "stop"                  => "2012-03-06 05:05:56.349255",
                       "total_hours"           => 7.055555555555556e-07,
                       "total_local_gb_usage"  => 0.0
                        }
                      ]
                    }
  response
end

#list_volumes(detailed = true) ⇒ Object



16
17
18
19
20
21
# File 'lib/fog/openstack/requests/compute/list_volumes.rb', line 16

def list_volumes(detailed=true)
  Excon::Response.new(
    :body   => { 'volumes' => self.data[:volumes].values },
    :status => 200
  )
end

#live_migrate_server(server_id, host, block_migration, disk_over_commit) ⇒ Object



18
19
20
21
22
# File 'lib/fog/openstack/requests/compute/live_migrate_server.rb', line 18

def live_migrate_server(server_id, host, block_migration, disk_over_commit)
  response = Excon::Response.new
  response.status = 202
  response
end

#migrate_server(server_id) ⇒ Object



12
13
14
15
16
# File 'lib/fog/openstack/requests/compute/migrate_server.rb', line 12

def migrate_server(server_id)
  response = Excon::Response.new
  response.status = 202
  response
end

#pause_server(server_id) ⇒ Object



18
19
20
# File 'lib/fog/openstack/requests/compute/pause_server.rb', line 18

def pause_server(server_id)
  true
end

#reboot_server(server_id, type = 'SOFT') ⇒ Object



12
13
14
15
16
# File 'lib/fog/openstack/requests/compute/reboot_server.rb', line 12

def reboot_server(server_id, type = 'SOFT')
  response = Excon::Response.new
  response.status = 202
  response
end

#rebuild_server(server_id, image_ref, name, admin_pass = nil, metadata = nil, personality = nil) ⇒ Object



26
27
28
29
30
# File 'lib/fog/openstack/requests/compute/rebuild_server.rb', line 26

def rebuild_server(server_id, image_ref, name, admin_pass=nil, =nil, personality=nil)
  response = get_server_details(server_id)
  response.body['server']['status'] = "REBUILD"
  response
end

#release_address(address_id) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/fog/openstack/requests/compute/release_address.rb', line 15

def release_address(address_id)
  response = Excon::Response.new
  response.status = 202
  response.headers = {
    "Content-Type" => "text/html; charset=UTF-8",
    "Content-Length" => "0",
    "Date" => Date.new
  }
  response.body = {}
  response
end

#remove_fixed_ip(server_id, address) ⇒ Object



23
24
25
# File 'lib/fog/openstack/requests/compute/remove_fixed_ip.rb', line 23

def remove_fixed_ip(server_id, address)
  true
end

#remove_flavor_access(flavor_ref, tenant_id) ⇒ Object



20
21
22
23
24
25
26
27
# File 'lib/fog/openstack/requests/compute/remove_flavor_access.rb', line 20

def remove_flavor_access(flavor_ref, tenant_id)
  response = Excon::Response.new
  response.status = 200
  response.body = {
    "flavor_access" => []
  }
  response
end

#remove_security_group(server_id, group_name) ⇒ Object



12
13
14
15
16
# File 'lib/fog/openstack/requests/compute/remove_security_group.rb', line 12

def remove_security_group(server_id, group_name)
  response = Excon::Response.new
  response.status = 200
  response
end

#rescue_server(server_id) ⇒ Object



18
19
20
# File 'lib/fog/openstack/requests/compute/rescue_server.rb', line 18

def rescue_server(server_id)
  true
end

#reset_dataObject



271
272
273
# File 'lib/fog/openstack/compute.rb', line 271

def reset_data
  self.class.data.delete("#{@openstack_username}-#{@current_tenant}")
end

#reset_server_state(server_id, status) ⇒ Object



12
13
14
15
16
# File 'lib/fog/openstack/requests/compute/reset_server_state.rb', line 12

def reset_server_state(server_id, status)
  response = get_server_details(server_id)
  response.body['server']['status'] = status.upcase
  response
end

#resize_server(server_id, flavor_ref) ⇒ Object



12
13
14
15
16
# File 'lib/fog/openstack/requests/compute/resize_server.rb', line 12

def resize_server(server_id, flavor_ref)
  response = Excon::Response.new
  response.status = 202
  response
end

#resume_server(server_id) ⇒ Object



18
19
20
# File 'lib/fog/openstack/requests/compute/resume_server.rb', line 18

def resume_server(server_id)
  true
end

#revert_resize_server(server_id) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/fog/openstack/requests/compute/revert_resize_server.rb', line 12

def revert_resize_server(server_id)
  response = Excon::Response.new
  response.status = 202

  self.data[:servers][server_id]['flavorId'] = self.data[:servers][server_id]['old_flavorId']
  self.data[:servers][server_id].delete('old_flavorId')
  self.data[:last_modified][:servers][server_id] = Time.now
  self.data[:servers][server_id]['status'] = 'ACTIVE'

  response
end

#server_actions(server_id) ⇒ Object



21
22
23
# File 'lib/fog/openstack/requests/compute/server_actions.rb', line 21

def server_actions(server_id)
  Array.new
end

#server_diagnostics(server_id) ⇒ Object



20
21
# File 'lib/fog/openstack/requests/compute/server_diagnostics.rb', line 20

def server_diagnostics(server_id)
end

#set_metadata(collection_name, parent_id, metadata = {}) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/fog/openstack/requests/compute/set_metadata.rb', line 16

def (collection_name, parent_id,  = {})
  if collection_name == "images" then
    if not list_images_detail.body['images'].find {|_| _['id'] == parent_id}
      raise Fog::Compute::OpenStack::NotFound
    end
  end

  if collection_name == "servers" then
    if not list_servers_detail.body['servers'].find {|_| _['id'] == parent_id}
      raise Fog::Compute::OpenStack::NotFound
    end
  end

  response = Excon::Response.new
  response.body = { "metadata" =>  }
  response.status = 200
  response
end

#set_tenant(tenant) ⇒ Object



13
14
15
# File 'lib/fog/openstack/requests/compute/set_tenant.rb', line 13

def set_tenant(tenant)
  true
end

#start_server(server_id) ⇒ Object



18
19
20
# File 'lib/fog/openstack/requests/compute/start_server.rb', line 18

def start_server(server_id)
  true
end

#stop_server(server_id) ⇒ Object



18
19
20
# File 'lib/fog/openstack/requests/compute/stop_server.rb', line 18

def stop_server(server_id)
  true
end

#suspend_server(server_id) ⇒ Object



18
19
20
# File 'lib/fog/openstack/requests/compute/suspend_server.rb', line 18

def suspend_server(server_id)
  true
end

#unpause_server(server_id) ⇒ Object



18
19
20
# File 'lib/fog/openstack/requests/compute/unpause_server.rb', line 18

def unpause_server(server_id)
  true
end

#update_meta(collection_name, parent_id, key, value) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/fog/openstack/requests/compute/update_meta.rb', line 16

def update_meta(collection_name, parent_id, key, value)
  if collection_name == "images" then
    if not list_images_detail.body['images'].find {|_| _['id'] == parent_id}
      raise Fog::Compute::OpenStack::NotFound
    end
  end

  if collection_name == "servers" then
    if not list_servers_detail.body['servers'].find {|_| _['id'] == parent_id}
      raise Fog::Compute::OpenStack::NotFound
    end
  end

  #FIXME join w/ existing metadata here
  response = Excon::Response.new
  response.body = { "metadata" => {key => value} }
  response.status = 200
  response
end

#update_metadata(collection_name, parent_id, metadata = {}) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/fog/openstack/requests/compute/update_metadata.rb', line 16

def (collection_name, parent_id,  = {})
  if collection_name == "images" then
    if not list_images_detail.body['images'].find {|_| _['id'] == parent_id}
      raise Fog::Compute::OpenStack::NotFound
    end
  end

  if collection_name == "servers" then
    if not list_servers_detail.body['servers'].find {|_| _['id'] == parent_id}
      raise Fog::Compute::OpenStack::NotFound
    end
  end

  #FIXME join w/ existing metadata here
  response = Excon::Response.new
  response.body = { "metadata" =>  }
  response.status = 200
  response
end

#update_quota(tenant_id, options = {}) ⇒ Object



17
18
19
20
21
22
23
24
# File 'lib/fog/openstack/requests/compute/update_quota.rb', line 17

def update_quota(tenant_id, options = {})
  self.data[:quota_updated] = self.data[:quota].merge options

  response = Excon::Response.new
  response.status = 200
  response.body = { 'quota_set' => self.data[:quota_updated] }
  response
end

#update_server(server_id, options = {}) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/fog/openstack/requests/compute/update_server.rb', line 16

def update_server(server_id, options = {})
  response = Excon::Response.new
  if server = list_servers_detail.body['servers'].find {|_| _['id'] == server_id}
    if options['name']
      server['name'] = options['name']
    end
    response.status = 200
    response
  else
    raise Fog::Compute::OpenStack::NotFound
  end
end