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

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Mock

Returns a new instance of Mock.



60
61
62
# File 'lib/fog/compute/rackspace.rb', line 60

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

Class Method Details

.dataObject



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/fog/compute/rackspace.rb', line 43

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

.resetObject



56
57
58
# File 'lib/fog/compute/rackspace.rb', line 56

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/compute/requests/rackspace/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



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/fog/compute/requests/rackspace/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'        => 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/compute/requests/rackspace/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



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

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/compute/requests/rackspace/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(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/compute/requests/rackspace/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/compute/requests/rackspace/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/compute/requests/rackspace/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'] == server_id}
    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/compute/requests/rackspace/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/compute/requests/rackspace/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/compute/requests/rackspace/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/compute/requests/rackspace/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/compute/requests/rackspace/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/compute/requests/rackspace/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/compute/requests/rackspace/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/compute/requests/rackspace/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/compute/requests/rackspace/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/compute/requests/rackspace/reboot_server.rb', line 21

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

#reset_dataObject



68
69
70
# File 'lib/fog/compute/rackspace.rb', line 68

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/compute/requests/rackspace/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/compute/requests/rackspace/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/compute/requests/rackspace/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