Class: Fog::Compute::SakuraCloud::Real

Inherits:
Object
  • Object
show all
Defined in:
lib/fog/sakuracloud/compute.rb,
lib/fog/sakuracloud/requests/compute/list_plans.rb,
lib/fog/sakuracloud/requests/compute/list_zones.rb,
lib/fog/sakuracloud/requests/compute/boot_server.rb,
lib/fog/sakuracloud/requests/compute/stop_server.rb,
lib/fog/sakuracloud/requests/compute/list_servers.rb,
lib/fog/sakuracloud/requests/compute/create_server.rb,
lib/fog/sakuracloud/requests/compute/delete_server.rb,
lib/fog/sakuracloud/requests/compute/list_ssh_keys.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Real

Returns a new instance of Real.



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

def initialize(options = {})
  @auth_encord = Base64.strict_encode64([
    options[:sakuracloud_api_token],
    options[:sakuracloud_api_token_secret]
  ].join(':'))
  Fog.credentials[:sakuracloud_api_token]        = options[:sakuracloud_api_token]
  Fog.credentials[:sakuracloud_api_token_secret] = options[:sakuracloud_api_token_secret]

  @sakuracloud_api_url = options[:sakuracloud_api_url] || 'https://secure.sakura.ad.jp'

  @connection = Fog::Core::Connection.new(@sakuracloud_api_url)
end

Instance Method Details

#boot_server(id) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/fog/sakuracloud/requests/compute/boot_server.rb', line 7

def boot_server( id )
  request(
    :headers => {
      'Authorization' => "Basic #{@auth_encord}"
    },
    :expects  => [200],
    :method => 'PUT',
    :path => "#{Fog::SakuraCloud::SAKURACLOUD_API_ENDPOINT}/server/#{id}/power"
  )
  true
end

#create_server(name, serverplan) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/fog/sakuracloud/requests/compute/create_server.rb', line 7

def create_server( name, serverplan )
  body = {
    "Server" => {
      "Name" => name,
      "ServerPlan" => {
        "ID" => serverplan.to_i
      },
      "ConnectedSwitches"=>[{"Scope"=>"shared", "BandWidthMbps"=>100}]
    }
  }

  request(
    :headers => {
      'Authorization' => "Basic #{@auth_encord}"
    },
    :expects  => [201],
    :method => 'POST',
    :path => "#{Fog::SakuraCloud::SAKURACLOUD_API_ENDPOINT}/server",
    :body => Fog::JSON.encode(body)
  )
end

#delete_server(id, force = false, disks = []) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/fog/sakuracloud/requests/compute/delete_server.rb', line 7

def delete_server( id, force = false, disks = [] )
  body = { "Force" => force, 'WithDisk' => disks }

  request(
    :headers => {
      'Authorization' => "Basic #{@auth_encord}"
    },
    :expects  => [200],
    :method => 'DELETE',
    :path => "#{Fog::SakuraCloud::SAKURACLOUD_API_ENDPOINT}/server/#{id}",
    :body => Fog::JSON.encode(body)
  )
end

#list_plans(options = {}) ⇒ Object



7
8
9
10
11
12
13
14
15
# File 'lib/fog/sakuracloud/requests/compute/list_plans.rb', line 7

def list_plans(options = {})
  request(
    :headers => {
      'Authorization' => "Basic #{@auth_encord}"
    },
    :method => 'GET',
    :path => "#{Fog::SakuraCloud::SAKURACLOUD_API_ENDPOINT}/product/server"
  )
end

#list_servers(options = {}) ⇒ Object



7
8
9
10
11
12
13
14
15
# File 'lib/fog/sakuracloud/requests/compute/list_servers.rb', line 7

def list_servers(options = {})
  request(
    :headers => {
      'Authorization' => "Basic #{@auth_encord}"
    },
    :method => 'GET',
    :path => "#{Fog::SakuraCloud::SAKURACLOUD_API_ENDPOINT}/server"
  )
end

#list_ssh_keys(options = {}) ⇒ Object



7
8
9
10
11
12
13
14
15
# File 'lib/fog/sakuracloud/requests/compute/list_ssh_keys.rb', line 7

def list_ssh_keys(options = {})
  request(
    :headers => {
      'Authorization' => "Basic #{@auth_encord}"
    },
    :method => 'GET',
    :path => "#{Fog::SakuraCloud::SAKURACLOUD_API_ENDPOINT}/sshkey"
  )
end

#list_zones(options = {}) ⇒ Object



7
8
9
10
11
12
13
14
15
# File 'lib/fog/sakuracloud/requests/compute/list_zones.rb', line 7

def list_zones(options = {})
  request(
    :headers => {
      'Authorization' => "Basic #{@auth_encord}"
    },
    :method => 'GET',
    :path => "#{Fog::SakuraCloud::SAKURACLOUD_API_ENDPOINT}/zone"
  )
end

#request(params) ⇒ Object



46
47
48
49
# File 'lib/fog/sakuracloud/compute.rb', line 46

def request(params)
  response = parse @connection.request(params)
  response
end

#stop_server(id, force = false) ⇒ Object



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

def stop_server( id, force = false )
  if force
    body = { "Force" => true }
  else
    body = {}
  end
  request(
    :headers => {
      'Authorization' => "Basic #{@auth_encord}"
    },
    :expects  => [200,202],
    :method => 'DELETE',
    :path => "#{Fog::SakuraCloud::SAKURACLOUD_API_ENDPOINT}/server/#{id}/power",
    :body => Fog::JSON.encode(body)
  )
  true
end