Class: Fog::Compute::Clodo::Mock

Inherits:
Object
  • Object
show all
Defined in:
lib/fog/clodo/compute.rb,
lib/fog/clodo/requests/compute/list_images.rb,
lib/fog/clodo/requests/compute/stop_server.rb,
lib/fog/clodo/requests/compute/list_servers.rb,
lib/fog/clodo/requests/compute/start_server.rb,
lib/fog/clodo/requests/compute/create_server.rb,
lib/fog/clodo/requests/compute/delete_server.rb,
lib/fog/clodo/requests/compute/reboot_server.rb,
lib/fog/clodo/requests/compute/server_action.rb,
lib/fog/clodo/requests/compute/add_ip_address.rb,
lib/fog/clodo/requests/compute/rebuild_server.rb,
lib/fog/clodo/requests/compute/move_ip_address.rb,
lib/fog/clodo/requests/compute/delete_ip_address.rb,
lib/fog/clodo/requests/compute/get_image_details.rb,
lib/fog/clodo/requests/compute/get_server_details.rb,
lib/fog/clodo/requests/compute/list_images_detail.rb,
lib/fog/clodo/requests/compute/list_servers_detail.rb

Overview

request :list_addresses request :list_private_addresses request :list_public_addresses request :confirm_resized_server request :revert_resized_server request :resize_server request :update_server

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Mock

Returns a new instance of Mock.



61
62
63
# File 'lib/fog/clodo/compute.rb', line 61

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

Class Method Details

.dataObject



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

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

.resetObject



57
58
59
# File 'lib/fog/clodo/compute.rb', line 57

def self.reset
  @data = nil
end

Instance Method Details

#add_ip_address(server_id) ⇒ Object

Raises:

  • (Excon::Errors::BadRequest)


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/fog/clodo/requests/compute/add_ip_address.rb', line 24

def add_ip_address(server_id)

  raise Excon::Errors::BadRequest.new(
                                      "Invalid image ID"
                                      ) unless server_id > 0

  data = {
    'primary_ip' => false,
    'isp' => false,
    'ip' => "66.6.#{rand(255)}.#{rand(255)}"
  }

  raise Excon::Errors::BadRequest unless self.data[:servers][server_id]

  raise Excon::Errors::BadRequest.new "No addresses" unless self.data[:servers][server_id]['addresses']

  self.data[:servers][server_id]['addresses']['public'] << data

  response = Excon::Response.new
  response.status = 204
  response
end

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

Raises:

  • (Excon::Errors::BadRequest)


43
44
45
46
47
48
49
50
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
# File 'lib/fog/clodo/requests/compute/create_server.rb', line 43

def create_server(image_id, options = {})

  raise Excon::Errors::BadRequest.new("Invalid image ID") unless image_id > 0

  response = Excon::Response.new
  response.status = 202

  id = Fog::Mock.random_numbers(6).to_i

  data = {
    'id'        => id,
    'imageId'   => "#{image_id}",
    'name'      => options['name'] || "VPS #{rand(999)}",
    'adminPass'    => '23ryh8udbcbyt'
  }

  self.data[:last_modified][:servers][id] = Time.now
  self.data[:servers][id] = {
    'id' => "#{id}",
    'imageId' => data['imageId'],
    'name' => data['name'],
    'vps_os_title' => "OSTitle",
    'vps_root_pass' => data['adminPass'],
    'status' => "is_running",
    'addresses' => {'public' =>[{
                                  'primary_ip' => true,
                                  'isp' => false,
                                  'ip' => '66.6.6.66'
                                },
                                {
                                  'primary_ip' => false,
                                  'isp' => false,
                                  'ip' => '13.13.13.13'
                                }]},
    'vps_createdate' => "#{Time.now}",
    'vps_hdd_max' => "5",
    'vps_traff' => nil,
    'vps_mem_1h_max' => "0",
    'vps_mem_load' => "0",
    'vps_user_pass' => "wer45345ht",
    'vps_vnc_pass' => "bi65tdfyb",
    'vps_adddate' => "#{Time.now}",
    'vps_update' => "#{Time.now}",
    'vps_mem_1h_min' => "0",
    'vps_mem_1h_avg' => nil,
    'vps_memory_max' => options['vps_memory_max'] || "512",
    'vps_os_version' => "6.6.6",
    'vps_cpu_1h_max' => "0",
    'vps_hdd_load' => "0",
    'vps_disk_load' => "0",
    'vps_os_type' => options['vps_os_type'] || "VirtualServer",
    'type' => options['vps_os_type'] || "VirtualServer",
    'vps_memory' => options['vps_memory'] || "512",
    'vps_cpu_load' => "0",
    'vps_update_days' => "0",
    'vps_os_bits' => "64",
    'vps_vnc' => "6.6.6.6:5900",
    'vps_cpu_max' => "0",
    'vps_cpu_1h_min' => "0",
    'vps_cpu_1h_avg' => nil
  }

  response.body = { 'server' => data }
  response
end

#dataObject



65
66
67
# File 'lib/fog/clodo/compute.rb', line 65

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

#delete_ip_address(server_id, ip) ⇒ Object



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

def delete_ip_address(server_id, ip)

  server = self.data[:servers][server_id]

  raise Excon::Errors::BadRequest.new "Server not found" unless server

  pa = server['addresses']['public']

  raise Excon::Errors::BadRequest.new "Address not found" unless pa && pa.reject! {|addr|
    addr['ip'] == ip
  }
  
  response = Excon::Response.new
  response.status = 204
  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/clodo/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'].to_i == server_id }
    if server['status'] == 'is_install'
      response.status = 405
      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::Clodo::NotFound
  end
end

#get_image_details(image_id) ⇒ Object



12
13
14
15
16
# File 'lib/fog/clodo/requests/compute/get_image_details.rb', line 12

def get_image_details(image_id)
  response = Excon::Response.new
  response.status = 404
  response
end

#get_server_details(server_id) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/fog/clodo/requests/compute/get_server_details.rb', line 34

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.body['server']['id'] = server['id'].to_i
    response
  else
    raise Fog::Compute::Clodo::NotFound
  end
end

#list_imagesObject



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

def list_images
  response = Excon::Response.new
  response.status = 200
  response.body = {
    'images' => [
                 { 'name' => 'Debian 6 64 bits',
                   'id' => "541",
                   'vps_type' => 'ScaleServer',
                   'status' => 'ACTIVE' },
                 { 'name' => 'CentOS 5.5 32 bits',
                   'id' => "31",
                   'vps_type' => 'VirtualServer',
                   'status' => 'ACTIVE' }
                ]
  }
  response
end

#list_images_detailObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/fog/clodo/requests/compute/list_images_detail.rb', line 32

def list_images_detail
  response = Excon::Response.new
  response.status = 200
  response.body = {
    'images' => [{ 'os_type' => 'debian',
                   'os_bits' => "64",
                   'os_hvm' => "0",
                   '_attr' => {
                     'id' => "541",
                     'name' => 'Debian 6 64 bits',
                     'status' => 'ACTIVE',
                     'vps_type' => 'ScaleServer'
                   }},
                 { 'os_type' => 'centos',
                   'os_bits' => "32",
                   'os_hvm' => "0",
                   '_attr' => {
                     'name' => 'CentOS 5.5 32 bits',
                     'id' => "31",
                     'vps_type' => 'VirtualServer',
                     'status' => 'ACTIVE',
                   }}]
  }
  response
end

#list_serversObject



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/fog/clodo/requests/compute/list_servers.rb', line 35

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

#list_servers_detailObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/fog/clodo/requests/compute/list_servers_detail.rb', line 30

def list_servers_detail
  response = Excon::Response.new

  servers = self.data[:servers].values
  for server in servers
    case server['status']
    when 'is_install'
      if Time.now - self.data[:last_modified][:servers][server['id']] > Fog::Mock.delay * 2
        server['status'] = 'is_running'
      end
    end
  end

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

#move_ip_address(server_id, ip) ⇒ Object



26
27
28
29
30
# File 'lib/fog/clodo/requests/compute/move_ip_address.rb', line 26

def move_ip_address(server_id, ip)
  response = Excon::Response.new
  response.status = [204]
  response
end

#reboot_server(id, type) ⇒ Object



12
13
14
15
# File 'lib/fog/clodo/requests/compute/reboot_server.rb', line 12

def reboot_server(id, type)
  body = {'reboot' => {}}
  server_action(id, body)
end

#rebuild_server(id, image_id, vps_isp = nil) ⇒ Object



13
14
15
16
17
# File 'lib/fog/clodo/requests/compute/rebuild_server.rb', line 13

def rebuild_server(id, image_id, vps_isp = nil)
  body = {'rebuild' => {'imageId' => image_id}}
  body['rebuild']['vps_isp'] = vps_isp if vps_isp
  server_action(id, body)
end

#reset_dataObject



69
70
71
# File 'lib/fog/clodo/compute.rb', line 69

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

#server_action(id, action) ⇒ Object

Raises:

  • (Excon::Errors::BadRequest)


14
15
16
17
18
19
20
21
# File 'lib/fog/clodo/requests/compute/server_action.rb', line 14

def server_action(id, action)

  raise Excon::Errors::BadRequest.new("Invalid server id #{id}.") unless id > 0

  response = Excon::Response.new
  response.status = 204
  response
end

#start_server(id) ⇒ Object



12
13
14
15
# File 'lib/fog/clodo/requests/compute/start_server.rb', line 12

def start_server(id)
  body = {'start' => {}}
  server_action(id, body)
end

#stop_server(id) ⇒ Object



12
13
14
15
# File 'lib/fog/clodo/requests/compute/stop_server.rb', line 12

def stop_server(id)
  body = {'stop' => {}}
  server_action(id, body)
end