Class: Fog::Compute::OpenStack::Mock

Inherits:
Object
  • Object
show all
Defined in:
lib/fog/openstack/compute.rb,
lib/fog/openstack/requests/compute/get_meta.rb,
lib/fog/openstack/requests/compute/delete_meta.rb,
lib/fog/openstack/requests/compute/list_images.rb,
lib/fog/openstack/requests/compute/update_meta.rb,
lib/fog/openstack/requests/compute/create_image.rb,
lib/fog/openstack/requests/compute/delete_image.rb,
lib/fog/openstack/requests/compute/list_flavors.rb,
lib/fog/openstack/requests/compute/list_servers.rb,
lib/fog/openstack/requests/compute/set_metadata.rb,
lib/fog/openstack/requests/compute/create_server.rb,
lib/fog/openstack/requests/compute/delete_server.rb,
lib/fog/openstack/requests/compute/list_metadata.rb,
lib/fog/openstack/requests/compute/reboot_server.rb,
lib/fog/openstack/requests/compute/resize_server.rb,
lib/fog/openstack/requests/compute/update_server.rb,
lib/fog/openstack/requests/compute/list_addresses.rb,
lib/fog/openstack/requests/compute/rebuild_server.rb,
lib/fog/openstack/requests/compute/update_metadata.rb,
lib/fog/openstack/requests/compute/get_image_details.rb,
lib/fog/openstack/requests/compute/get_flavor_details.rb,
lib/fog/openstack/requests/compute/get_server_details.rb,
lib/fog/openstack/requests/compute/list_images_detail.rb,
lib/fog/openstack/requests/compute/list_flavors_detail.rb,
lib/fog/openstack/requests/compute/list_servers_detail.rb,
lib/fog/openstack/requests/compute/list_public_addresses.rb,
lib/fog/openstack/requests/compute/revert_resized_server.rb,
lib/fog/openstack/requests/compute/change_password_server.rb,
lib/fog/openstack/requests/compute/confirm_resized_server.rb,
lib/fog/openstack/requests/compute/list_private_addresses.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Mock

Returns a new instance of Mock.



89
90
91
92
# File 'lib/fog/openstack/compute.rb', line 89

def initialize(options={})
  require 'multi_json'
  @openstack_username = options[:openstack_username]
end

Class Method Details

.dataObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/fog/openstack/compute.rb', line 60

def self.data
  @data ||= Hash.new do |hash, key|
    hash[key] = {
      :last_modified => {
        :images  => {},
        :servers => {}
      },
      :images  => {
        "1" => {
          'id'        => "1",
          'name'      => "img1",
          'progress'  => 100,
          'status'    => "ACTIVE",
          'updated'   => "",
          'minRam'    => 0,
          'minDisk'   => 0,
          'metadata'  => {},
          'links'     => []
        }
      },
      :servers => {}
    }
  end
end

.resetObject



85
86
87
# File 'lib/fog/openstack/compute.rb', line 85

def self.reset
  @data = nil
end

Instance Method Details

#change_password_server(server_id, admin_password) ⇒ Object



15
16
17
18
19
# File 'lib/fog/openstack/requests/compute/change_password_server.rb', line 15

def change_password_server(server_id, admin_password)
  response = Excon::Response.new
  response.status = 202
  response
end

#confirm_resized_server(server_id) ⇒ Object



15
16
17
18
19
# File 'lib/fog/openstack/requests/compute/confirm_resized_server.rb', line 15

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

#create_image(server_id, name, metadata = {}) ⇒ Object



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

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

  img_id=Fog::Mock.random_numbers(6).to_s

  data = {
    'id'        => img_id,
    'server'     => {"id"=>"3", "links"=>[{"href"=>"http://nova1:8774/admin/servers/#{server_id}", "rel"=>"bookmark"}]},
    'links'     => [{"href"=>"http://nova1:8774/v1.1/admin/images/#{img_id}", "rel"=>"self"}, {"href"=>"http://nova1:8774/admin/images/#{img_id}", "rel"=>"bookmark"}],
    'metadata'  =>  || {},
    'name'      => name || "server_#{rand(999)}",
    'progress'  => 0,
    'status'    => 'SAVING',
    'minDisk'   => 0,
    'minRam'    => 0,
    'updated'   => "",
    'created'   => ""
  }
  self.data[:last_modified][:images][data['id']] = Time.now
  self.data[:images][data['id']] = data
  response.body = { 'image' => data }
  response

end

#create_server(name, image_ref, flavor_ref, options = {}) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/fog/openstack/requests/compute/create_server.rb', line 43

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

  data = {
    'addresses' => {},
    'flavor'  => {"id"=>"1", "links"=>[{"href"=>"http://nova1:8774/admin/flavors/1", "rel"=>"bookmark"}]},
    'id'        => Fog::Mock.random_numbers(6).to_s,
    'image'     => {"id"=>"3", "links"=>[{"href"=>"http://nova1:8774/admin/images/3", "rel"=>"bookmark"}]},
    'links'     => [{"href"=>"http://nova1:8774/v1.1/admin/servers/5", "rel"=>"self"}, {"href"=>"http://nova1:8774/admin/servers/5", "rel"=>"bookmark"}],
    'hostId'    => "123456789ABCDEF01234567890ABCDEF",
    'metadata'  => options['metadata'] || {},
    'name'      => options['name'] || "server_#{rand(999)}",
    'accessIPv4'  => options['accessIPv4'] || "",
    'accessIPv6'  => options['accessIPv6'] || "",
    '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



94
95
96
# File 'lib/fog/openstack/compute.rb', line 94

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

#delete_image(image_id) ⇒ Object



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

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_meta(collection_name, parent_id, key) ⇒ Object



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

def delete_meta(collection_name, parent_id, key)
  response = Excon::Response.new
  response.status = 204
  response
end

#delete_server(server_id) ⇒ Object



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

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

#get_flavor_details(flavor_ref) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/fog/openstack/requests/compute/get_flavor_details.rb', line 18

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

#get_image_details(image_id) ⇒ Object



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

def get_image_details(image_id)
  response = Excon::Response.new
  if image = list_images_detail.body['images'].detect {|_| _['id'] == image_id}
    response.status = [200, 203][rand(1)]
    response.body = { 'image' => image }
    response
  else
    raise Fog::Compute::OpenStack::NotFound
  end
end

#get_meta(collection_name, parent_id, key) ⇒ Object



18
19
20
21
22
23
# File 'lib/fog/openstack/requests/compute/get_meta.rb', line 18

def get_meta(collection_name, parent_id, key)
  response = Excon::Response.new
  response.status = 200
  response.body = { 'meta' => {} }
  response
end

#get_server_details(server_id) ⇒ Object



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

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

#list_addresses(server_id) ⇒ Object



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

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

#list_flavorsObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/fog/openstack/requests/compute/list_flavors.rb', line 18

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

#list_flavors_detailObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/fog/openstack/requests/compute/list_flavors_detail.rb', line 18

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

#list_imagesObject



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/fog/openstack/requests/compute/list_images.rb', line 18

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

#list_images_detailObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/fog/openstack/requests/compute/list_images_detail.rb', line 18

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', 'links', 'minRam', 'minDisk', 'metadata', 'status', 'updated'].include?(key)}} }
  response
end

#list_metadata(collection_name, parent_id) ⇒ Object



18
19
20
21
22
23
# File 'lib/fog/openstack/requests/compute/list_metadata.rb', line 18

def (collection_name, parent_id)
  response = Excon::Response.new
  response.status = 200
  response.body = {}
  response
end

#list_private_addresses(server_id) ⇒ Object



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

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



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

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



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/fog/openstack/requests/compute/list_servers.rb', line 18

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

#list_servers_detailObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/fog/openstack/requests/compute/list_servers_detail.rb', line 18

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



15
16
17
18
19
# File 'lib/fog/openstack/requests/compute/reboot_server.rb', line 15

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

#rebuild_server(server_id, image_ref, name, admin_pass = nil, metadata = nil, personality = nil) ⇒ Object



22
23
24
25
26
# File 'lib/fog/openstack/requests/compute/rebuild_server.rb', line 22

def rebuild_server(server_id, image_ref, name, admin_pass=nil, =nil, personality=nil)
  response = get_server_details(server_id)
  response.body['server']['status'] = "REBUILD"
  response
end

#reset_dataObject



98
99
100
# File 'lib/fog/openstack/compute.rb', line 98

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

#resize_server(server_id, flavor_ref) ⇒ Object



15
16
17
18
19
# File 'lib/fog/openstack/requests/compute/resize_server.rb', line 15

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

#revert_resized_server(server_id) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/fog/openstack/requests/compute/revert_resized_server.rb', line 15

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

#set_metadata(collection_name, parent_id, metadata = {}) ⇒ Object



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

def (collection_name, parent_id,  = {})

  if collection_name == "images" then
    if not list_images_detail.body['images'].detect {|_| _['id'] == parent_id}
      raise Fog::Compute::OpenStack::NotFound
    end 
  end

  if collection_name == "servers" then
    if not list_servers_detail.body['servers'].detect {|_| _['id'] == parent_id}
      raise Fog::Compute::OpenStack::NotFound
    end 
  end

  response = Excon::Response.new
  response.body = { "metadata" =>  }
  response.status = 200
  response

end

#update_meta(collection_name, parent_id, key, value) ⇒ Object



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

def update_meta(collection_name, parent_id, key, value)

  if collection_name == "images" then
    if not list_images_detail.body['images'].detect {|_| _['id'] == parent_id}
      raise Fog::Compute::OpenStack::NotFound
    end 
  end

  if collection_name == "servers" then
    if not list_servers_detail.body['servers'].detect {|_| _['id'] == parent_id}
      raise Fog::Compute::OpenStack::NotFound
    end 
  end

  response = Excon::Response.new
  response.body = { "meta" => { key => value } }
  response.status = 200
  response

end

#update_metadata(collection_name, parent_id, metadata = {}) ⇒ Object



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

def (collection_name, parent_id,  = {})

  if collection_name == "images" then
    if not list_images_detail.body['images'].detect {|_| _['id'] == parent_id}
      raise Fog::Compute::OpenStack::NotFound
    end 
  end

  if collection_name == "servers" then
    if not list_servers_detail.body['servers'].detect {|_| _['id'] == parent_id}
      raise Fog::Compute::OpenStack::NotFound
    end 
  end

  #FIXME join w/ existing metadata here
  response = Excon::Response.new
  response.body = { "metadata" =>  }
  response.status = 200
  response

end

#update_server(server_id, options) ⇒ Object



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

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