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

Inherits:
Object
  • Object
show all
Defined in:
lib/fog/digitalocean/compute.rb,
lib/fog/digitalocean/requests/compute/list_images.rb,
lib/fog/digitalocean/requests/compute/get_ssh_key.rb,
lib/fog/digitalocean/requests/compute/list_servers.rb,
lib/fog/digitalocean/requests/compute/list_flavors.rb,
lib/fog/digitalocean/requests/compute/list_regions.rb,
lib/fog/digitalocean/requests/compute/list_ssh_keys.rb,
lib/fog/digitalocean/requests/compute/create_server.rb,
lib/fog/digitalocean/requests/compute/reboot_server.rb,
lib/fog/digitalocean/requests/compute/destroy_server.rb,
lib/fog/digitalocean/requests/compute/create_ssh_key.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/power_cycle_server.rb,
lib/fog/digitalocean/requests/compute/get_server_details.rb

Overview

request :digitalocean_resize

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Mock

Returns a new instance of Mock



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

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

Class Method Details

.dataObject



44
45
46
47
48
49
50
51
# File 'lib/fog/digitalocean/compute.rb', line 44

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

.resetObject



53
54
55
# File 'lib/fog/digitalocean/compute.rb', line 53

def self.reset
  @data = nil
end

Instance Method Details

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



40
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
69
70
71
72
73
# File 'lib/fog/digitalocean/requests/compute/create_server.rb', line 40

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

  # New York 2 (region id 4) is currently the only region that supports
  # private networking.  The Digital Ocean IP will return a null
  # private_ip_address for any other region
  has_private_ip = !!options[:private_networking] && (region_id == 4)

  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",
    "private_ip_address" => has_private_ip ? "10.0.0.1" : nil,
    "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



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

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



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

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

#destroy_server(id) ⇒ Object



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

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



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

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



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

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



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

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://developers.digitalocean.com/ssh-keys
    "ssh_key"  => self.data[:ssh_keys].find { |k| k['id'] == id }
  }
  response
end

#list_flavorsObject



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

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



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/fog/digitalocean/requests/compute/list_images.rb', line 15

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



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

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" },
      { "id" => 3, "name" => "San Francisco 1" },
      { "id" => 4, "name" => "New York 2" },
      { "id" => 5, "name" => "Amsterdam 2" },
      { "id" => 6, "name" => "Singapore 1" }
    ]
  }
  response
end

#list_serversObject



15
16
17
18
19
20
21
22
23
# File 'lib/fog/digitalocean/requests/compute/list_servers.rb', line 15

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

#list_ssh_keysObject



15
16
17
18
19
20
21
22
23
# File 'lib/fog/digitalocean/requests/compute/list_ssh_keys.rb', line 15

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



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

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



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

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



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

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



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

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



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

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

#shutdown_server(id) ⇒ Object



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

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