Class: Fog::Compute::DigitalOcean::Mock

Inherits:
Object
  • Object
show all
Defined in:
lib/fog/digitalocean/compute.rb,
lib/fog/digitalocean/requests/compute/get_ssh_key.rb,
lib/fog/digitalocean/requests/compute/list_images.rb,
lib/fog/digitalocean/requests/compute/list_flavors.rb,
lib/fog/digitalocean/requests/compute/list_regions.rb,
lib/fog/digitalocean/requests/compute/list_servers.rb,
lib/fog/digitalocean/requests/compute/create_server.rb,
lib/fog/digitalocean/requests/compute/list_ssh_keys.rb,
lib/fog/digitalocean/requests/compute/reboot_server.rb,
lib/fog/digitalocean/requests/compute/create_ssh_key.rb,
lib/fog/digitalocean/requests/compute/destroy_server.rb,
lib/fog/digitalocean/requests/compute/destroy_ssh_key.rb,
lib/fog/digitalocean/requests/compute/power_on_server.rb,
lib/fog/digitalocean/requests/compute/shutdown_server.rb,
lib/fog/digitalocean/requests/compute/power_off_server.rb,
lib/fog/digitalocean/requests/compute/get_server_details.rb,
lib/fog/digitalocean/requests/compute/power_cycle_server.rb

Overview

request :digitalocean_resize

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/digitalocean/compute.rb', line 60

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

Class Method Details

.dataObject



47
48
49
50
51
52
53
54
# File 'lib/fog/digitalocean/compute.rb', line 47

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

.resetObject



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

def self.reset
  @data = nil
end

Instance Method Details

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



41
42
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
# File 'lib/fog/digitalocean/requests/compute/create_server.rb', line 41

def create_server( name,
                   size_id,
                   image_id,
                   region_id,
                   options = {} )
  response = Excon::Response.new
  response.status = 200

  mock_data = {
    "id" => Fog::Mock.random_numbers(1).to_i,
    "event_id" => Fog::Mock.random_numbers(2).to_i,
    "name" => name,
    "size_id" => size_id,
    "image_id" => image_id,
    "region_id" => region_id,
    "ip_address" => "127.0.0.1",
    "status" => 'active',
    "created_at" => Time.now.strftime("%FT%TZ")
  }

  response.body = {
    "status" => "OK",
    "droplet"  => mock_data
  }

  self.data[:servers] << mock_data
  response
end

#create_ssh_key(name, pub_key) ⇒ Object



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

def create_ssh_key( name, pub_key )
  response = Excon::Response.new
  response.status = 200
  mock_data = {
    "id" => Fog::Mock.random_numbers(1).to_i,
    "name" => name,
    "ssh_pub_key" => pub_key
  }
  response.body = {
    "status" => "OK",
    "ssh_key"  => mock_data
  }
  self.data[:ssh_keys] << mock_data
  response
end

#dataObject



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

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

#destroy_server(id) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/fog/digitalocean/requests/compute/destroy_server.rb', line 23

def destroy_server( id )
  response = Excon::Response.new
  response.status = 200
  response.body = {
    "event_id" => Fog::Mock.random_numbers(1).to_i,
    "status" => "OK"
  }

  server = self.data[:servers].reject! { |s| s['id'] == id }

  response
end

#destroy_ssh_key(id) ⇒ Object



23
24
25
26
27
28
29
30
31
32
# File 'lib/fog/digitalocean/requests/compute/destroy_ssh_key.rb', line 23

def destroy_ssh_key(id)
  response = Excon::Response.new
  response.status = 200
  if self.data[:ssh_keys].reject! { |k| k['id'] == id }
    response.body = { "status" => "OK" }
  else
    response.body = { "status" => "ERROR" }
  end
  response
end

#get_server_details(server_id) ⇒ Object



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

def get_server_details(server_id)
  response = Excon::Response.new
  response.status = 200

  server = self.data[:servers].find { |s| s['id'] == server_id }

  response.body = {
    "status" => "OK",
    "droplet"  => self.data[:servers].find { |s| s['id'] == server_id }
  }

  response
end

#get_ssh_key(id) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/fog/digitalocean/requests/compute/get_ssh_key.rb', line 24

def get_ssh_key(id)
  response = Excon::Response.new
  response.status = 200
  response.body = {
    "status" => "OK",
    # key listing does not return ssh_pub_key
    # https://www.digitalocean.com/api#ssh_keys
    "ssh_key"  => self.data[:ssh_keys].find { |k| k['id'] == id }
  }
  response
end

#list_flavorsObject



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

def list_flavors
  response = Excon::Response.new
  response.status = 200
  response.body = {
    "status" => "OK",
    "sizes"  => [
      {"id" => 33,"name" => "512MB"},
      {"id" => 34,"name" => "1GB"},
      {"id" => 35,"name" => "2GB"},
      {"id" => 36,"name" => "4GB"},
      {"id" => 37,"name" => "8GB"},
      {"id" => 38,"name" => "16GB"}
    ]
  }
  response
end

#list_imagesObject



18
19
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/digitalocean/requests/compute/list_images.rb', line 18

def list_images
  response = Excon::Response.new
  response.status = 200
  response.body = {
    "status" => "OK",
    "images" => [
      # Sample image
      {
        "id" => 1601,
        "name" => "CentOS 5.8 x64",
        "distribution" => "CentOS"
      },
      {
        "id" => 1602,
        "name" => "CentOS 5.8 x32",
        "distribution" => "CentOS"
      },
      {
        "id" => 2676,
        "name" => "Ubuntu 12.04 x64",
        "distribution" => "Ubuntu"
      },

    ]
  }
  response
end

#list_regionsObject



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

def list_regions
  response = Excon::Response.new
  response.status = 200
  response.body = {
    "status" => "OK",
    "regions"  => [
      {"id" => 1,"name" => "New York 1"},
      {"id" => 2,"name" => "Amsterdam 1"}
    ]
  }
  response
end

#list_serversObject



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

def list_servers
  response = Excon::Response.new
  response.status = 200
  response.body = {
    "status" => "OK",
    "droplets"  => self.data[:servers]
  }
  response
end

#list_ssh_keysObject



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

def list_ssh_keys
  response = Excon::Response.new
  response.status = 200
  response.body = {
    "status" => "OK",
    "ssh_keys"  => self.data[:ssh_keys]
  }
  response
end

#power_cycle_server(id) ⇒ Object



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

def power_cycle_server( id )
  response = Excon::Response.new
  response.status = 200
  server = self.data[:servers].find { |s| s['id'] == id }
  server['status'] = 'off' if server
  response.body = {
    "event_id" => Fog::Mock.random_numbers(1).to_i,
    "status" => "OK"
  }
  response
end

#power_off_server(id) ⇒ Object



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

def power_off_server( id )
  response = Excon::Response.new
  response.status = 200
  server = self.data[:servers].find { |s| s['id'] }
  server['status'] = 'off' if server
  response.body = {
    "event_id" => Fog::Mock.random_numbers(1).to_i,
    "status" => "OK"
  }
  response
end

#power_on_server(id) ⇒ Object



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

def power_on_server( id )
  response = Excon::Response.new
  response.status = 200
  server = self.data[:servers].find { |s| s['id'] }
  server['status'] = 'active' if server
  response.body = {
    "event_id" => Fog::Mock.random_numbers(1).to_i,
    "status" => "OK"
  }
  response
end

#reboot_server(id) ⇒ Object



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

def reboot_server( id )
  response = Excon::Response.new
  response.status = 200
  server = self.data[:servers].find { |s| s['id'] == id }
  server['status'] = 'off' if server
  response.body = {
    "event_id" => Fog::Mock.random_numbers(1).to_i,
    "status" => "OK"
  }
  response
end

#reset_dataObject



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

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

#shutdown_server(id) ⇒ Object



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

def shutdown_server( id )
  response = Excon::Response.new
  response.status = 200
  server = self.data[:servers].find { |s| s['id'] == id }

  # Simulate reboot
  server['status'] = 'off' if server

  response.body = {
    "event_id" => Fog::Mock.random_numbers(1).to_i,
    "status" => "OK"
  }
  response
end