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_flavors.rb,
lib/fog/rackspace/requests/compute/list_servers.rb,
lib/fog/rackspace/requests/compute/create_server.rb,
lib/fog/rackspace/requests/compute/delete_server.rb,
lib/fog/rackspace/requests/compute/reboot_server.rb,
lib/fog/rackspace/requests/compute/resize_server.rb,
lib/fog/rackspace/requests/compute/update_server.rb,
lib/fog/rackspace/requests/compute/list_addresses.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_images_detail.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.



174
175
176
# File 'lib/fog/rackspace/compute.rb', line 174

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

Class Method Details

.dataObject



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

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



170
171
172
# File 'lib/fog/rackspace/compute.rb', line 170

def self.reset
  @data = nil
end

Instance Method Details

#confirm_resized_server(server_id) ⇒ Object



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

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



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

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



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

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



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

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

#delete_image(image_id) ⇒ Object



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

def delete_image(image_id)
  response = Excon::Response.new
  if image = list_images_detail.body['images'].detect {|_| _['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



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

def delete_server(server_id)
  response = Excon::Response.new
  if server = list_servers_detail.body['servers'].detect {|_| _['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



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

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



38
39
40
41
42
43
44
45
46
47
# File 'lib/fog/rackspace/requests/compute/get_server_details.rb', line 38

def get_server_details(server_id)
  response = Excon::Response.new
  if server = list_servers_detail.body['servers'].detect {|_| _['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



29
30
31
32
33
34
35
36
37
38
# File 'lib/fog/rackspace/requests/compute/list_addresses.rb', line 29

def list_addresses(server_id)
  response = Excon::Response.new
  if server = list_servers_detail.body['servers'].detect {|_| _['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



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

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



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

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



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

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



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

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



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

def list_private_addresses(server_id)
  response = Excon::Response.new
  if server = list_servers_detail.body['servers'].detect {|_| _['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



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

def list_public_addresses(server_id)
  response = Excon::Response.new
  if server = list_servers_detail.body['servers'].detect {|_| _['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



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

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



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

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



21
22
23
24
25
# File 'lib/fog/rackspace/requests/compute/reboot_server.rb', line 21

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

#reset_dataObject



182
183
184
# File 'lib/fog/rackspace/compute.rb', line 182

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



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

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



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

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



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

def update_server(server_id, options)
  response = Excon::Response.new
  if server = list_servers_detail.body['servers'].detect {|_| _['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