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

Inherits:
Object
  • Object
show all
Includes:
Collections
Defined in:
lib/fog/rackspace/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

Methods included from Collections

#flavors, #images, #servers

Constructor Details

#initialize(options = {}) ⇒ Mock

Returns a new instance of Mock.



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

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

Class Method Details

.dataObject



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

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



52
53
54
55
56
# File 'lib/fog/rackspace/servers.rb', line 52

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, 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/servers/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'        => 123456,
    'imageId'   => image_id,
    'hostId'    => "123456789ABCDEF01234567890ABCDEF",
    'metadata'  => options['metadata'] || {},
    'name'      => options['name'] || "server_#{rand(999)}",
    'progress'  => 0,
    'status'    => 'BUILD'
  }
  @data[:last_modified][:servers][data['id']] = Time.now
  @data[:servers][data['id']] = data
  response.body = { 'server' => data.merge({'adminPass' => 'password'}) }
  response
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/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
    response
  else
    response.status = 400
    raise(Excon::Errors.status_error({:expects => 202}, response))
  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/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
    response
  else
    raise Fog::Rackspace::Servers::NotFound
  end
end

#get_flavor_details(flavor_id) ⇒ Object



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

def get_flavor_details(flavor_id)
  Fog::Mock.not_implemented
end

#get_image_details(image_id) ⇒ Object



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

def get_image_details(image_id)
  Fog::Mock.not_implemented
end

#get_server_details(server_id) ⇒ Object



38
39
40
41
42
43
44
45
46
47
# 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 }
    response
  else
    raise Fog::Rackspace::Servers::NotFound
  end
end

#list_addresses(server_id) ⇒ Object



29
30
31
32
33
34
35
36
37
38
# 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'] }
    response
  else
    raise Fog::Rackspace::Servers::NotFound
  end
end

#list_flavorsObject



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

def list_flavors
  Fog::Mock.not_implemented
end

#list_flavors_detailObject



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

def list_flavors_detail
  Fog::Mock.not_implemented
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
# 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'] }
    response
  else
    raise Fog::Rackspace::Servers::NotFound
  end
end

#list_public_addresses(server_id) ⇒ Object



27
28
29
30
31
32
33
34
35
36
# 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'] }
    response
  else
    raise Fog::Rackspace::Servers::NotFound
  end
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 = 'SOFT') ⇒ Object



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

def reboot_server(server_id, type = 'SOFT')
  Fog::Mock.not_implemented
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/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
    response
  else
    raise Fog::Rackspace::Servers::NotFound
  end
end