Class: Fog::Compute::Rackspace::Mock

Inherits:
Rackspace::Service show all
Defined in:
lib/fog/rackspace/compute.rb,
lib/fog/rackspace/requests/compute/list_images.rb,
lib/fog/rackspace/requests/compute/create_image.rb,
lib/fog/rackspace/requests/compute/delete_image.rb,
lib/fog/rackspace/requests/compute/list_servers.rb,
lib/fog/rackspace/requests/compute/list_flavors.rb,
lib/fog/rackspace/requests/compute/create_server.rb,
lib/fog/rackspace/requests/compute/resize_server.rb,
lib/fog/rackspace/requests/compute/update_server.rb,
lib/fog/rackspace/requests/compute/delete_server.rb,
lib/fog/rackspace/requests/compute/reboot_server.rb,
lib/fog/rackspace/requests/compute/list_addresses.rb,
lib/fog/rackspace/requests/compute/list_images_detail.rb,
lib/fog/rackspace/requests/compute/get_flavor_details.rb,
lib/fog/rackspace/requests/compute/get_server_details.rb,
lib/fog/rackspace/requests/compute/list_flavors_detail.rb,
lib/fog/rackspace/requests/compute/list_servers_detail.rb,
lib/fog/rackspace/requests/compute/list_public_addresses.rb,
lib/fog/rackspace/requests/compute/revert_resized_server.rb,
lib/fog/rackspace/requests/compute/confirm_resized_server.rb,
lib/fog/rackspace/requests/compute/list_private_addresses.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Rackspace::Service

#authenticate, #endpoint_uri, #region, #request, #request_without_retry, #service_name, #service_net?

Constructor Details

#initialize(options = {}) ⇒ Mock

Returns a new instance of Mock



172
173
174
# File 'lib/fog/rackspace/compute.rb', line 172

def initialize(options={})
  @rackspace_username = options[:rackspace_username]
end

Class Method Details

.dataObject



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

def self.data
  @data ||= Hash.new do |hash, key|
    hash[key] = {
      :last_modified => {
        :images  => {},
        :servers => {}
      },
        :servers => {},
        :images  => {
          112 =>
            {'id' => 112,
              'status' => "ACTIVE",
              'updated' => "2011-04-21T10:24:01-05:00",
              'name' => "Ubuntu 10.04 LTS"},
          100 =>
            {'id' => 100,
              'status' => "ACTIVE",
              'updated' => "2011-09-12T09:09:23-05:00",
              'name' => "Arch 2011.10"},
          31 =>
            {'id' => 31,
              'status' => "ACTIVE",
              'updated' => "2010-01-26T12:07:44-06:00",
              'name' => "Windows Server 2008 SP2 x86"},
          108 =>
            {'id' => 108,
              'status' => "ACTIVE",
              'updated' => "2011-11-01T08:32:30-05:00",
              'name' => "Gentoo 11.0"},
          109 =>
            {'id' => 109,
              'status' => "ACTIVE",
              'updated' => "2011-11-03T06:28:56-05:00",
              'name' => "openSUSE 12"},
          89 =>
            {'id' => 89,
              'status' => "ACTIVE",
              'updated' => "2011-10-04T08:39:34-05:00",
              'name' => "Windows Server 2008 R2 x64 - SQL2K8R2 Web"},
          24 =>
            {'id' => 24,
              'status' => "ACTIVE",
              'updated' => "2010-01-26T12:07:04-06:00",
              'name' => "Windows Server 2008 SP2 x64"},
          110 =>
            {'id' => 110,
              'status' => "ACTIVE",
              'updated' => "2011-08-17T05:11:30-05:00",
              'name' => "Red Hat Enterprise Linux 5.5"},
          57 =>
            {'id' => 57,
              'status' => "ACTIVE",
              'updated' => "2010-09-17T07:16:25-05:00",
              'name' => "Windows Server 2008 SP2 x64 - MSSQL2K8R2"},
          85 =>
            {'id' => 85,
              'status' => "ACTIVE",
              'updated' => "2010-01-26T12:07:17-06:00",
              'name' => "Windows Server 2008 R2 x64"},
          111 =>
            {'id' => 111,
              'status' => "ACTIVE",
              'updated' => "2011-09-12T10:53:12-05:00",
              'name' => "Red Hat Enterprise Linux 6"},
          120 =>
            {'id' => 120,
              'status' => "ACTIVE",
              'updated' => "2012-01-03T04:39:05-06:00",
              'name' => "Fedora 16"},
          119 =>
            {'id' => 119,
              'status' => "ACTIVE",
              'updated' => "2011-11-03T08:55:15-05:00",
              'name' => "Ubuntu 11.10"},
          116 =>
            {'id' => 116,
              'status' => "ACTIVE",
              'updated' => "2011-08-17T05:11:30-05:00",
              'name' => "Fedora 15"},
          56 =>
            {'id' => 56,
              'status' => "ACTIVE",
              'updated' => "2010-09-17T07:12:56-05:00",
              'name' => "Windows Server 2008 SP2 x86 - MSSQL2K8R2"},
          114 =>
            {'id' => 114,
              'status' => "ACTIVE",
              'updated' => "2011-08-17T05:11:30-05:00",
              'name' => "CentOS 5.6"},
          86 =>
            {'id' => 86,
              'status' => "ACTIVE",
              'updated' => "2010-09-17T07:19:20-05:00",
              'name' => "Windows Server 2008 R2 x64 - MSSQL2K8R2"},
          115 =>
            {'id' => 115,
              'status' => "ACTIVE",
              'updated' => "2011-08-17T05:11:30-05:00",
              'name' => "Ubuntu 11.04"},
          103 =>
            {'id' => 103,
              'status' => "ACTIVE",
              'updated' => "2011-08-17T05:11:30-05:00",
              'name' => "Debian 5 (Lenny)"},
          104 =>
            {'id' => 104,
              'status' => "ACTIVE",
              'updated' => "2011-08-17T05:11:30-05:00",
              'name' => "Debian 6 (Squeeze)"},
          118 =>
            {'id' => 118,
              'status' => "ACTIVE",
              'updated' => "2011-08-17T05:11:30-05:00",
              'name' => "CentOS 6.0"}
            }
      }
  end
end

.resetObject



168
169
170
# File 'lib/fog/rackspace/compute.rb', line 168

def self.reset
  @data = nil
end

Instance Method Details

#confirm_resized_server(server_id) ⇒ Object



17
18
19
20
21
22
23
24
25
26
# File 'lib/fog/rackspace/requests/compute/confirm_resized_server.rb', line 17

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

  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

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



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/fog/rackspace/requests/compute/create_image.rb', line 34

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

  now = Time.now
  data = {
    'created'   => now,
    'id'        => Fog::Mock.random_numbers(6).to_i,
    'name'      => options['name'] || '',
    'serverId'  => server_id,
    'status'    => 'SAVING',
    'updated'   => now.to_s,
  }

  self.data[:last_modified][:images][data['id']] = now
  self.data[:images][data['id']] = data
  response.body = { 'image' => data.reject {|key, value| !['id', 'name', 'serverId', 'status', 'updated'].include?(key)} }
  response
end

#create_server(flavor_id, image_id, options = {}) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/fog/rackspace/requests/compute/create_server.rb', line 67

def create_server(flavor_id, image_id, options = {})
  response = Excon::Response.new
  response.status = 202

  data = {
    'addresses' => { 'private' => ['0.0.0.0'], 'public' => ['0.0.0.0'] },
    'flavorId'  => flavor_id,
    'id'        => Fog::Mock.random_numbers(6).to_i,
    'imageId'   => image_id,
    'hostId'    => "123456789ABCDEF01234567890ABCDEF",
    'metadata'  => options['metadata'] || {},
    'name'      => options['name'] || "server_#{rand(999)}",
    'progress'  => 0,
    'status'    => 'BUILD'
  }
  self.data[:last_modified][:servers][data['id']] = Time.now
  self.data[:servers][data['id']] = data
  response.body = { 'server' => data.merge({'adminPass' => 'password'}) }
  response
end

#dataObject



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

def data
  self.class.data[@rackspace_username]
end

#delete_image(image_id) ⇒ Object



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

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 Fog::Compute::Rackspace::NotFound.new
  end
end

#delete_server(server_id) ⇒ Object



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

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 => 202}, response))
    else
      self.data[:last_modified][:servers].delete(server_id)
      self.data[:servers].delete(server_id)
      response.status = 202
    end
    response
  else
    raise Fog::Compute::Rackspace::NotFound
  end
end

#get_flavor_details(flavor_id) ⇒ Object



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

def get_flavor_details(flavor_id)
  response = Excon::Response.new
  flavor = {
    1 => { 'name' => '256 server',    'ram' => 256,    'disk' => 10   },
    2 => { 'name' => '512 server',    'ram' => 512,    'disk' => 20   },
    3 => { 'name' => '1GB server',    'ram' => 1024,   'disk' => 40   },
    4 => { 'name' => '2GB server',    'ram' => 2048,   'disk' => 80   },
    5 => { 'name' => '4GB server',    'ram' => 4096,   'disk' => 160  },
    6 => { 'name' => '8GB server',    'ram' => 8192,   'disk' => 320  },
    7 => { 'name' => '15.5GB server', 'ram' => 15872,  'disk' => 620  }
  }[flavor_id]
  if flavor
    response.status = 200
    response.body = {
      'flavor' => flavor
    }
    response
  else
    raise Fog::Compute::Rackspace::NotFound
  end
end

#get_server_details(server_id) ⇒ Object



35
36
37
38
39
40
41
42
43
44
# File 'lib/fog/rackspace/requests/compute/get_server_details.rb', line 35

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

#list_addresses(server_id) ⇒ Object



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

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::Rackspace::NotFound
  end
end

#list_flavorsObject



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

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

#list_flavors_detailObject



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

def list_flavors_detail
  response = Excon::Response.new
  response.status = 200
  response.body = {
    'flavors' => [
      { 'name' => '256 server',    'id' => 1, 'ram' => 256,    'disk' => 10   },
      { 'name' => '512 server',    'id' => 2, 'ram' => 512,    'disk' => 20   },
      { 'name' => '1GB server',    'id' => 3, 'ram' => 1024,   'disk' => 40   },
      { 'name' => '2GB server',    'id' => 4, 'ram' => 2048,   'disk' => 80   },
      { 'name' => '4GB server',    'id' => 5, 'ram' => 4096,   'disk' => 160  },
      { 'name' => '8GB server',    'id' => 6, 'ram' => 8192,   'disk' => 320  },
      { 'name' => '15.5GB server', 'id' => 7, 'ram' => 15872,  'disk' => 620  }
    ]
  }
  response
end

#list_imagesObject



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/fog/rackspace/requests/compute/list_images.rb', line 22

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'].include?(key) }
  end
  response.status = [200, 203][rand(1)]
  response.body = { 'images' => images }
  response
end

#list_images_detailObject



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

def list_images_detail
  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', 'status', 'updated'].include?(key)}} }
  response
end

#list_private_addresses(server_id) ⇒ Object



24
25
26
27
28
29
30
31
32
33
# File 'lib/fog/rackspace/requests/compute/list_private_addresses.rb', line 24

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::Rackspace::NotFound
  end
end

#list_public_addresses(server_id) ⇒ Object



24
25
26
27
28
29
30
31
32
33
# File 'lib/fog/rackspace/requests/compute/list_public_addresses.rb', line 24

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::Rackspace::NotFound
  end
end

#list_serversObject



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/fog/rackspace/requests/compute/list_servers.rb', line 23

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

#list_servers_detailObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/fog/rackspace/requests/compute/list_servers_detail.rb', line 32

def list_servers_detail
  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

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



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

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

#reset_dataObject



180
181
182
# File 'lib/fog/rackspace/compute.rb', line 180

def reset_data
  self.class.data.delete(@rackspace_username)
end

#resize_server(server_id, flavor_id) ⇒ Object

FIXME: should probably transition instead of skipping to VERIFY_RESIZE



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/fog/rackspace/requests/compute/resize_server.rb', line 19

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

  # keep track of this for reverts
  self.data[:servers][server_id]['old_flavorId'] = self.data[:servers][server_id]['flavorId']

  self.data[:servers][server_id]['flavorId'] = flavor_id
  self.data[:last_modified][:servers][server_id] = Time.now
  self.data[:servers][server_id]['status'] = 'VERIFY_RESIZE'

  response
end

#revert_resized_server(server_id) ⇒ Object



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

def revert_resized_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

#update_server(server_id, options) ⇒ Object



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

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