Class: Fog::Compute::DigitalOceanV2::Real

Inherits:
Object
  • Object
show all
Defined in:
lib/fog/digitalocean/requests/compute_v2/create_server.rb,
lib/fog/digitalocean/compute_v2.rb,
lib/fog/digitalocean/requests/compute_v2/rename.rb,
lib/fog/digitalocean/requests/compute_v2/resize.rb,
lib/fog/digitalocean/requests/compute_v2/rebuild.rb,
lib/fog/digitalocean/requests/compute_v2/restore.rb,
lib/fog/digitalocean/requests/compute_v2/upgrade.rb,
lib/fog/digitalocean/requests/compute_v2/power_on.rb,
lib/fog/digitalocean/requests/compute_v2/shutdown.rb,
lib/fog/digitalocean/requests/compute_v2/snapshot.rb,
lib/fog/digitalocean/requests/compute_v2/power_off.rb,
lib/fog/digitalocean/requests/compute_v2/enable_ipv6.rb,
lib/fog/digitalocean/requests/compute_v2/get_ssh_key.rb,
lib/fog/digitalocean/requests/compute_v2/list_images.rb,
lib/fog/digitalocean/requests/compute_v2/power_cycle.rb,
lib/fog/digitalocean/requests/compute_v2/list_flavors.rb,
lib/fog/digitalocean/requests/compute_v2/list_regions.rb,
lib/fog/digitalocean/requests/compute_v2/list_servers.rb,
lib/fog/digitalocean/requests/compute_v2/change_kernel.rb,
lib/fog/digitalocean/requests/compute_v2/delete_server.rb,
lib/fog/digitalocean/requests/compute_v2/list_ssh_keys.rb,
lib/fog/digitalocean/requests/compute_v2/reboot_server.rb,
lib/fog/digitalocean/requests/compute_v2/create_ssh_key.rb,
lib/fog/digitalocean/requests/compute_v2/delete_ssh_key.rb,
lib/fog/digitalocean/requests/compute_v2/password_reset.rb,
lib/fog/digitalocean/requests/compute_v2/transfer_image.rb,
lib/fog/digitalocean/requests/compute_v2/update_ssh_key.rb,
lib/fog/digitalocean/requests/compute_v2/disable_backups.rb,
lib/fog/digitalocean/requests/compute_v2/get_droplet_action.rb,
lib/fog/digitalocean/requests/compute_v2/get_server_details.rb,
lib/fog/digitalocean/requests/compute_v2/convert_to_snapshot.rb,
lib/fog/digitalocean/requests/compute_v2/list_droplet_actions.rb,
lib/fog/digitalocean/requests/compute_v2/enable_private_networking.rb

Overview

noinspection RubyStringKeysInHashInspection

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Real

Returns a new instance of Real.



77
78
79
80
81
82
83
84
85
86
# File 'lib/fog/digitalocean/compute_v2.rb', line 77

def initialize(options={})
  digitalocean_token = options[:digitalocean_token]
  persistent         = false
  options            = {
    headers: {
      'Authorization' => "Bearer #{digitalocean_token}",
    }
  }
  @connection        = Fog::Core::Connection.new 'https://api.digitalocean.com', persistent, options
end

Instance Method Details

#change_kernel(id, kernel) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/fog/digitalocean/requests/compute_v2/change_kernel.rb', line 5

def change_kernel(id, kernel)
  body = { :type => "change_kernel", :kernel => kernel }

  encoded_body = Fog::JSON.encode(body)

  request(
    :expects => [201],
    :headers => {
      'Content-Type' => "application/json; charset=UTF-8",
    },
    :method  => 'POST',
    :path    => "v2/droplets/#{id}/actions",
    :body    => encoded_body,
  )
end

#convert_to_snapshot(id) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/fog/digitalocean/requests/compute_v2/convert_to_snapshot.rb', line 5

def convert_to_snapshot(id)
  body = { :type => 'convert',}

  encoded_body = Fog::JSON.encode(body)

  request(
    :expects => [201],
    :headers => {
      'Content-Type' => "application/json; charset=UTF-8",
    },
    :method  => 'POST',
    :path    => "v2/images/#{id}/actions",
    :body    => encoded_body,
  )
end

#create_server(name, size, image, region, options = {}) ⇒ Object



7
8
9
10
11
12
13
14
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
# File 'lib/fog/digitalocean/requests/compute_v2/create_server.rb', line 7

def create_server(name,
                  size,
                  image,
                  region,
                  options = {})

  create_options = {
    :name   => name,
    :region => region,
    :size   => size,
    :image  => image,
  }

  [:backups, :ipv6, :private_networking].each do |opt|
    create_options[opt] = !!options[opt] if options[opt]
  end

  [:user_data, :ssh_keys].each do |opt|
    create_options[opt] = options[opt] if options[opt]
  end

  encoded_body = Fog::JSON.encode(create_options)

  request(
    :expects => [202],
    :headers => {
      'Content-Type' => "application/json; charset=UTF-8",
    },
    :method  => 'POST',
    :path    => '/v2/droplets',
    :body    => encoded_body,
  )
end

#create_ssh_key(name, public_key) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/fog/digitalocean/requests/compute_v2/create_ssh_key.rb', line 7

def create_ssh_key(name, public_key)
  create_options = {
    :name       => name,
    :public_key => public_key,
  }

  encoded_body = Fog::JSON.encode(create_options)

  request(
    :expects => [202],
    :headers => {
      'Content-Type' => "application/json; charset=UTF-8",
    },
    :method  => 'POST',
    :path    => '/v2/account/keys',
    :body    => encoded_body,
  )
end

#delete_server(server_id) ⇒ Object



6
7
8
9
10
11
12
13
14
15
# File 'lib/fog/digitalocean/requests/compute_v2/delete_server.rb', line 6

def delete_server(server_id)
  request(
    :expects         => [204],
    :headers         => {
      'Content-Type' => "application/json; charset=UTF-8",
    },
    :method          => 'DELETE',
    :path            => "/v2/droplets/#{server_id}",
  )
end

#delete_ssh_key(id) ⇒ Object



6
7
8
9
10
11
12
13
14
15
# File 'lib/fog/digitalocean/requests/compute_v2/delete_ssh_key.rb', line 6

def delete_ssh_key(id)
  request(
    :expects => [204],
    :headers => {
      'Content-Type' => "application/json; charset=UTF-8",
    },
    :method  => 'DELETE',
    :path    => "/v2/account/keys#{id}",
  )
end

#disable_backups(id) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/fog/digitalocean/requests/compute_v2/disable_backups.rb', line 5

def disable_backups(id)
  body = { :type => "disable_backups" }

  encoded_body = Fog::JSON.encode(body)

  request(
    :expects => [201],
    :headers => {
      'Content-Type' => "application/json; charset=UTF-8",
    },
    :method  => 'POST',
    :path    => "v2/droplets/#{id}/actions",
    :body    => encoded_body,
  )
end

#enable_ipv6(id) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/fog/digitalocean/requests/compute_v2/enable_ipv6.rb', line 5

def enable_ipv6(id)
  body = { :type => "enable_ipv6" }

  encoded_body = Fog::JSON.encode(body)

  request(
    :expects => [201],
    :headers => {
      'Content-Type' => "application/json; charset=UTF-8",
    },
    :method  => 'POST',
    :path    => "v2/droplets/#{id}/actions",
    :body    => encoded_body,
  )
end

#enable_private_networking(id) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/fog/digitalocean/requests/compute_v2/enable_private_networking.rb', line 5

def enable_private_networking(id)
  body = { :type => "enable_private_networking" }

  encoded_body = Fog::JSON.encode(body)

  request(
    :expects => [201],
    :headers => {
      'Content-Type' => "application/json; charset=UTF-8",
    },
    :method  => 'POST',
    :path    => "v2/droplets/#{id}/actions",
    :body    => encoded_body,
  )
end

#get_droplet_action(droplet_id, action_id) ⇒ Object



5
6
7
8
9
10
11
# File 'lib/fog/digitalocean/requests/compute_v2/get_droplet_action.rb', line 5

def get_droplet_action(droplet_id, action_id)
  request(
    :expects => [200],
    :method  => 'GET',
    :path    => "v2/droplets/#{droplet_id}/actions/#{action_id}",
  )
end

#get_server_details(server_id) ⇒ Object



5
6
7
8
9
10
11
# File 'lib/fog/digitalocean/requests/compute_v2/get_server_details.rb', line 5

def get_server_details(server_id)
  request(
    :expects => [200],
    :method  => 'GET',
    :path    => "/v2/droplets/#{server_id}"
  )
end

#get_ssh_key(key_id) ⇒ Object



5
6
7
8
9
10
11
# File 'lib/fog/digitalocean/requests/compute_v2/get_ssh_key.rb', line 5

def get_ssh_key(key_id)
  request(
    :expects => [200],
    :method  => 'GET',
    :path    => "/v2/account/keys/#{key_id}"
  )
end

#list_droplet_actions(id) ⇒ Object



5
6
7
8
9
10
11
# File 'lib/fog/digitalocean/requests/compute_v2/list_droplet_actions.rb', line 5

def list_droplet_actions(id)
  request(
    :expects => [200],
    :method  => 'GET',
    :path    => "v2/droplets/#{id}/actions",
  )
end

#list_flavorsObject



5
6
7
8
9
10
11
# File 'lib/fog/digitalocean/requests/compute_v2/list_flavors.rb', line 5

def list_flavors
  request(
    :expects => [200],
    :method  => 'GET',
    :path    => '/v2/sizes'
  )
end

#list_imagesObject



5
6
7
8
9
10
11
# File 'lib/fog/digitalocean/requests/compute_v2/list_images.rb', line 5

def list_images
  request(
    :expects => [200],
    :method  => 'GET',
    :path    => '/v2/images'
  )
end

#list_regionsObject



5
6
7
8
9
10
11
# File 'lib/fog/digitalocean/requests/compute_v2/list_regions.rb', line 5

def list_regions
  request(
    :expects => [200],
    :method  => 'GET',
    :path    => '/v2/regions'
  )
end

#list_serversObject



5
6
7
8
9
10
11
# File 'lib/fog/digitalocean/requests/compute_v2/list_servers.rb', line 5

def list_servers
  request(
    :expects => [200],
    :method => 'GET',
    :path => '/v2/droplets'
  )
end

#list_ssh_keysObject



5
6
7
8
9
10
11
# File 'lib/fog/digitalocean/requests/compute_v2/list_ssh_keys.rb', line 5

def list_ssh_keys
  request(
    :expects => [200],
    :method => 'GET',
    :path => 'v2/account/keys',
  )
end

#password_reset(id) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/fog/digitalocean/requests/compute_v2/password_reset.rb', line 5

def password_reset(id)
  body = { :type => "password_reset" }

  encoded_body = Fog::JSON.encode(body)

  request(
    :expects => [201],
    :headers => {
      'Content-Type' => "application/json; charset=UTF-8",
    },
    :method  => 'POST',
    :path    => "v2/droplets/#{id}/actions",
    :body    => encoded_body,
  )
end

#power_cycle(id) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/fog/digitalocean/requests/compute_v2/power_cycle.rb', line 5

def power_cycle(id)
  body = { :type => "power_cycle" }

  encoded_body = Fog::JSON.encode(body)

  request(
    :expects => [201],
    :headers => {
      'Content-Type' => "application/json; charset=UTF-8",
    },
    :method  => 'POST',
    :path    => "v2/droplets/#{id}/actions",
    :body    => encoded_body,
  )
end

#power_off(id) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/fog/digitalocean/requests/compute_v2/power_off.rb', line 5

def power_off(id)
  body = { :type => "power_off" }

  encoded_body = Fog::JSON.encode(body)

  request(
    :expects => [201],
    :headers => {
      'Content-Type' => "application/json; charset=UTF-8",
    },
    :method  => 'POST',
    :path    => "v2/droplets/#{id}/actions",
    :body    => encoded_body,
  )
end

#power_on(id) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/fog/digitalocean/requests/compute_v2/power_on.rb', line 5

def power_on(id)
  body = { :type => "power_on" }

  encoded_body = Fog::JSON.encode(body)

  request(
    :expects => [201],
    :headers => {
      'Content-Type' => "application/json; charset=UTF-8",
    },
    :method  => 'POST',
    :path    => "v2/droplets/#{id}/actions",
    :body    => encoded_body,
  )
end

#reboot_server(id) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/fog/digitalocean/requests/compute_v2/reboot_server.rb', line 5

def reboot_server(id)
  body = { :type => "reboot" }

  encoded_body = Fog::JSON.encode(body)

  request(
    :expects => [201],
    :headers => {
      'Content-Type' => "application/json; charset=UTF-8",
    },
    :method  => 'POST',
    :path    => "v2/droplets/#{id}/actions",
    :body    => encoded_body,
  )
end

#rebuild(id, image) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/fog/digitalocean/requests/compute_v2/rebuild.rb', line 5

def rebuild(id, image)
  body = { :type => "rebuild", :image => image }

  encoded_body = Fog::JSON.encode(body)

  request(
    :expects => [201],
    :headers => {
      'Content-Type' => "application/json; charset=UTF-8",
    },
    :method  => 'POST',
    :path    => "v2/droplets/#{id}/actions",
    :body    => encoded_body,
  )
end

#rename(id, name) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/fog/digitalocean/requests/compute_v2/rename.rb', line 5

def rename(id, name)
  body = { :type => "rename", :name => name }

  encoded_body = Fog::JSON.encode(body)

  request(
    :expects => [201],
    :headers => {
      'Content-Type' => "application/json; charset=UTF-8",
    },
    :method  => 'POST',
    :path    => "v2/droplets/#{id}/actions",
    :body    => encoded_body,
  )
end

#request(params) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/fog/digitalocean/compute_v2.rb', line 88

def request(params)
  params[:headers] ||= {}
  begin
    response = @connection.request(params)
  rescue Excon::Errors::HTTPStatusError => error
    raise case error
            when Excon::Errors::NotFound
              Fog::Compute::Bluebox::NotFound.slurp(error)
            else
              error
          end
  end
  unless response.body.empty?
    response.body = Fog::JSON.decode(response.body)
  end
  response
end

#resize(id, resize_disk, size) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/fog/digitalocean/requests/compute_v2/resize.rb', line 5

def resize(id, resize_disk, size)
  body = {
    :type => "resize",
    :disk => resize_disk,
    :size => size
  }

  encoded_body = Fog::JSON.encode(body)

  request(
    :expects => [201],
    :headers => {
      'Content-Type' => "application/json; charset=UTF-8",
    },
    :method  => 'POST',
    :path    => "v2/droplets/#{id}/actions",
    :body    => encoded_body,
  )
end

#restore(id, image) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/fog/digitalocean/requests/compute_v2/restore.rb', line 5

def restore(id, image)
  body = { :type => "restore", :image => image }

  encoded_body = Fog::JSON.encode(body)

  request(
    :expects => [201],
    :headers => {
      'Content-Type' => "application/json; charset=UTF-8",
    },
    :method  => 'POST',
    :path    => "v2/droplets/#{id}/actions",
    :body    => encoded_body,
  )
end

#shutdown(id) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/fog/digitalocean/requests/compute_v2/shutdown.rb', line 5

def shutdown(id)
  body = { :type => "shutdown" }

  encoded_body = Fog::JSON.encode(body)

  request(
    :expects => [201],
    :headers => {
      'Content-Type' => "application/json; charset=UTF-8",
    },
    :method  => 'POST',
    :path    => "v2/droplets/#{id}/actions",
    :body    => encoded_body,
  )
end

#snapshot(id, name) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/fog/digitalocean/requests/compute_v2/snapshot.rb', line 5

def snapshot(id, name)
  body = { :type => "snapshot", :name => name }

  encoded_body = Fog::JSON.encode(body)

  request(
    :expects => [201],
    :headers => {
      'Content-Type' => "application/json; charset=UTF-8",
    },
    :method  => 'POST',
    :path    => "v2/droplets/#{id}/actions",
    :body    => encoded_body,
  )
end

#transfer_image(id, region) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/fog/digitalocean/requests/compute_v2/transfer_image.rb', line 5

def transfer_image(id, region)
  body = { :type => 'transfer', :region => region }

  encoded_body = Fog::JSON.encode(body)

  request(
    :expects => [201],
    :headers => {
      'Content-Type' => "application/json; charset=UTF-8",
    },
    :method  => 'POST',
    :path    => "v2/images/#{id}/actions",
    :body    => encoded_body,
  )
end

#update_ssh_key(key_id, name) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/fog/digitalocean/requests/compute_v2/update_ssh_key.rb', line 7

def update_ssh_key(key_id, name)
  update_options = {
    :name       => name,
  }

  encoded_body = Fog::JSON.encode(update_options)

  request(
    :expects => [200],
    :headers => {
      'Content-Type' => "application/json; charset=UTF-8",
    },
    :method  => 'PUT',
    :path    => "/v2/account/keys/#{key_id}",
    :body    => encoded_body,
  )
end

#upgrade(id) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/fog/digitalocean/requests/compute_v2/upgrade.rb', line 5

def upgrade(id)
  body = { :type => "upgrade" }

  encoded_body = Fog::JSON.encode(body)

  request(
    :expects => [201],
    :headers => {
      'Content-Type' => "application/json; charset=UTF-8",
    },
    :method  => 'POST',
    :path    => "v2/droplets/#{id}/actions",
    :body    => encoded_body,
  )
end