Class: Fog::Rackspace::Servers::Mock

Inherits:
Object
  • Object
show all
Defined in:
lib/fog/rackspace/servers.rb,
lib/fog/rackspace/models/servers/images.rb,
lib/fog/rackspace/models/servers/flavors.rb,
lib/fog/rackspace/models/servers/servers.rb,
lib/fog/rackspace/requests/servers/list_images.rb,
lib/fog/rackspace/requests/servers/create_image.rb,
lib/fog/rackspace/requests/servers/delete_image.rb,
lib/fog/rackspace/requests/servers/list_flavors.rb,
lib/fog/rackspace/requests/servers/list_servers.rb,
lib/fog/rackspace/requests/servers/create_server.rb,
lib/fog/rackspace/requests/servers/delete_server.rb,
lib/fog/rackspace/requests/servers/reboot_server.rb,
lib/fog/rackspace/requests/servers/update_server.rb,
lib/fog/rackspace/requests/servers/list_addresses.rb,
lib/fog/rackspace/requests/servers/get_image_details.rb,
lib/fog/rackspace/requests/servers/get_flavor_details.rb,
lib/fog/rackspace/requests/servers/get_server_details.rb,
lib/fog/rackspace/requests/servers/list_images_detail.rb,
lib/fog/rackspace/requests/servers/list_flavors_detail.rb,
lib/fog/rackspace/requests/servers/list_servers_detail.rb,
lib/fog/rackspace/requests/servers/list_public_addresses.rb,
lib/fog/rackspace/requests/servers/list_private_addresses.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Mock

Returns a new instance of Mock.


64
65
66
67
# File 'lib/fog/rackspace/servers.rb', line 64

def initialize(options={})
  @rackspace_username = options[:rackspace_username]
  @data = self.class.data[@rackspace_username]
end

Class Method Details

.dataObject


45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/fog/rackspace/servers.rb', line 45

def self.data
  @data ||= Hash.new do |hash, key|
    hash[key] = {
      :last_modified => {
        :images  => {},
        :servers => {}
      },
      :images  => {},
      :servers => {}
    }
  end
end

.reset_data(keys = data.keys) ⇒ Object


58
59
60
61
62
# File 'lib/fog/rackspace/servers.rb', line 58

def self.reset_data(keys=data.keys)
  for key in [*keys]
    data.delete(key)
  end
end

Instance Method Details

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


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

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

  now = Time.now
  data = {
    'created'   => now,
    'id'        => 123456,
    'name'      => options['name'] || '',
    'serverId'  => server_id,
    'status'    => 'SAVING',
    'updated'   => now,
  }

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

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


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

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

  data = {
    'addresses' => { 'private' => ['0.0.0.0'], 'public' => ['0.0.0.0'] },
    'flavorId'  => flavor_id,
    'id'        => 123456,
    'imageId'   => image_id,
    'hostId'    => "123456789ABCDEF01234567890ABCDEF",
    'metadata'  => options[:metadata] || {},
    'name'      => name,
    'progress'  => 0,
    'status'    => 'BUILD'
  }
  data['adminPass'] = "#{data['name']}password"
  @data[:last_modified][:servers][data['id']] = Time.now
  @data[:servers][data['id']] = data
  response.body = { 'server' => data }
  response
end

#delete_image(image_id) ⇒ Object


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

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

#delete_server(server_id) ⇒ Object


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

def delete_server(server_id)
  response = Excon::Response.new
  if server = list_servers_detail.body['servers'].detect { |server| server['id'] == server_id }
    if server['status'] == 'BUILD'
      response.status = 409
      raise(Excon::Errors.status_error({:expects => 202}, response))
    else
      @data[:last_modified][:servers].delete(server_id)
      @data[:servers].delete(server_id)
      response.status = 202
    end
  else
    response.status = 404
    raise(Excon::Errors.status_error({:expects => 202}, response))
  end
  response
end

#flavorsObject


15
16
17
# File 'lib/fog/rackspace/models/servers/flavors.rb', line 15

def flavors
  Fog::Rackspace::Servers::Flavors.new(:connection => self)
end

#get_flavor_details(flavor_id) ⇒ Object

Raises:


27
28
29
# File 'lib/fog/rackspace/requests/servers/get_flavor_details.rb', line 27

def get_flavor_details(flavor_id)
  raise MockNotImplemented.new("Contributions welcome!")
end

#get_image_details(image_id) ⇒ Object

Raises:


28
29
30
# File 'lib/fog/rackspace/requests/servers/get_image_details.rb', line 28

def get_image_details(image_id)
  raise MockNotImplemented.new("Contributions welcome!")
end

#get_server_details(server_id) ⇒ Object


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

def get_server_details(server_id)
  response = Excon::Response.new
  if server = list_servers_detail.body['servers'].detect { |server| server['id'] == server_id }
    response.status = [200, 203][rand(1)]
    response.body = { 'server' => server }
  else
    response.status = 404
    raise(Excon::Errors.status_error({:expects => 202}, response))
  end
  response
end

#images(attributes = {}) ⇒ Object


17
18
19
20
21
# File 'lib/fog/rackspace/models/servers/images.rb', line 17

def images(attributes = {})
  Fog::Rackspace::Servers::Images.new({
    :connection => self
  }.merge!(attributes))
end

#list_addresses(server_id) ⇒ Object


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

def list_addresses(server_id)
  response = Excon::Response.new
  if server = list_servers_detail.body['servers'].detect { |server| server['id'] == server_id }
    response.status = [200, 203][rand(1)]
    response.body = { 'addresses' => server['addresses'] }
  else
    response.status = 404
    raise(Excon::Errors.status_error({:expects => 202}, response))
  end
  response
end

#list_flavorsObject

Raises:


25
26
27
# File 'lib/fog/rackspace/requests/servers/list_flavors.rb', line 25

def list_flavors
  raise MockNotImplemented.new("Contributions welcome!")
end

#list_flavors_detailObject

Raises:


27
28
29
# File 'lib/fog/rackspace/requests/servers/list_flavors_detail.rb', line 27

def list_flavors_detail
  raise MockNotImplemented.new("Contributions welcome!")
end

#list_imagesObject


25
26
27
28
29
30
31
32
33
34
35
# File 'lib/fog/rackspace/requests/servers/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/servers/list_images_detail.rb', line 28

def list_images_detail
  response = Excon::Response.new

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

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

#list_private_addresses(server_id) ⇒ Object


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

def list_private_addresses(server_id)
  response = Excon::Response.new
  if server = list_servers_detail.body['servers'].detect { |server| server['id'] == server_id }
    response.status = [200, 203][rand(1)]
    response.body = { 'private' => server['addresses']['private'] }
  else
    response.status = 404
    raise(Excon::Errors.status_error({:expects => 202}, response))
  end
  response
end

#list_public_addresses(server_id) ⇒ Object


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

def list_public_addresses(server_id)
  response = Excon::Response.new
  if server = list_servers_detail.body['servers'].detect { |server| server['id'] == server_id }
    response.status = [200, 203][rand(1)]
    response.body = { 'public' => server['addresses']['public'] }
  else
    response.status = 404
    raise(Excon::Errors.status_error({:expects => 202}, response))
  end
  response
end

#list_serversObject


26
27
28
29
30
31
32
33
34
35
36
# File 'lib/fog/rackspace/requests/servers/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/servers/list_servers_detail.rb', line 35

def list_servers_detail
  response = Excon::Response.new

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

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

#reboot_server(server_id, type) ⇒ Object

Raises:


25
26
27
# File 'lib/fog/rackspace/requests/servers/reboot_server.rb', line 25

def reboot_server(server_id, type)
  raise MockNotImplemented.new("Contributions welcome!")
end

#serversObject


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

def servers
  Fog::Rackspace::Servers::Servers.new(:connection => self)
end

#update_server(server_id, options) ⇒ Object


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

def update_server(server_id, options)
  response = Excon::Response.new
  if server = list_servers_detail.body['servers'].detect { |server| server['id'] == server_id }
    if options['adminPass']
      server['adminPass'] = options['adminPass']
    end
    if options['name']
      server['name'] = options['name']
    end
    response.status = 204
  else
    response.status = 404
    raise(Excon::Errors.status_error({:expects => 202}, response))
  end
  response
end