class Fog::Network::SakuraCloud::Real

Public Class Methods

new(options = {}) click to toggle source
# File lib/fog/sakuracloud/network.rb, line 30
def initialize(options = {})
  @auth_encode = 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

Public Instance Methods

connect_interface_to_switch( id, switch_id ) click to toggle source
# File lib/fog/sakuracloud/requests/network/connect_interface_to_switch.rb, line 6
def connect_interface_to_switch( id, switch_id )
  response = request(
    :headers => {
      'Authorization' => "Basic #{@auth_encode}"
    },
    :expects  => [200],
    :method => 'PUT',
    :path => "#{Fog::SakuraCloud::SAKURACLOUD_API_ENDPOINT}/interface/#{id}/to/switch/#{switch_id}"
  )
  response.body['Interface']['ID']
end
create_router(options) click to toggle source
# File lib/fog/sakuracloud/requests/network/create_router.rb, line 6
def create_router(options)
  bandwidthmbps = options[:bandwidthmbps] ? options[:bandwidthmbps].to_i : 100

  body = {
    "Internet" => {
      "Name" => options[:name],
      "NetworkMaskLen"=> options[:networkmasklen].to_i,
      "BandWidthMbps"=> bandwidthmbps
    }
  }

  request(
    :headers => {
      'Authorization' => "Basic #{@auth_encode}"
    },
    :expects  => 202,
    :method => 'POST',
    :path => "#{Fog::SakuraCloud::SAKURACLOUD_API_ENDPOINT}/internet",
    :body => Fog::JSON.encode(body)
  )
end
create_switch(options) click to toggle source
# File lib/fog/sakuracloud/requests/network/create_switch.rb, line 6
def create_switch(options)
  body = {
    "Switch" => {
      "Name" => options[:name]
    }
  }

  request(
    :headers => {
      'Authorization' => "Basic #{@auth_encode}"
    },
    :expects  => 201,
    :method => 'POST',
    :path => "#{Fog::SakuraCloud::SAKURACLOUD_API_ENDPOINT}/switch",
    :body => Fog::JSON.encode(body)
  )
end
delete_interface( id ) click to toggle source
# File lib/fog/sakuracloud/requests/network/delete_interface.rb, line 6
def delete_interface( id )
  request(
    :headers => {
      'Authorization' => "Basic #{@auth_encode}"
    },
    :expects  => [200],
    :method => 'DELETE',
    :path => "#{Fog::SakuraCloud::SAKURACLOUD_API_ENDPOINT}/interface/#{id}"
  )
end
delete_router( id ) click to toggle source
# File lib/fog/sakuracloud/requests/network/delete_router.rb, line 6
def delete_router( id )
  request(
    :headers => {
      'Authorization' => "Basic #{@auth_encode}"
    },
    :expects  => [200],
    :method => 'DELETE',
    :path => "#{Fog::SakuraCloud::SAKURACLOUD_API_ENDPOINT}/internet/#{id}"
  )
end
delete_switch( id ) click to toggle source
# File lib/fog/sakuracloud/requests/network/delete_switch.rb, line 6
def delete_switch( id )
  request(
    :headers => {
      'Authorization' => "Basic #{@auth_encode}"
    },
    :expects  => [200],
    :method => 'DELETE',
    :path => "#{Fog::SakuraCloud::SAKURACLOUD_API_ENDPOINT}/switch/#{id}"
  )
end
list_interfaces(options = {}) click to toggle source
# File lib/fog/sakuracloud/requests/network/list_interfaces.rb, line 6
def list_interfaces(options = {})
  filter = {
    "Include" => [
      "ID",
      "MACAddress",
      "IPAddress",
      "UserIPAddress",
      "Switch.ID",
      "Server.ID"]
  }
  request(
    :headers => {
      'Authorization' => "Basic #{@auth_encode}"
    },
    :method => 'GET',
    :path => "#{Fog::SakuraCloud::SAKURACLOUD_API_ENDPOINT}/interface",
    :query => URI.encode(Fog::JSON.encode(filter))
  )
end
list_routers(options = {}) click to toggle source
# File lib/fog/sakuracloud/requests/network/list_routers.rb, line 6
def list_routers(options = {})
  request(
    :headers => {
      'Authorization' => "Basic #{@auth_encode}"
    },
    :method => 'GET',
    :path => "#{Fog::SakuraCloud::SAKURACLOUD_API_ENDPOINT}/internet"
  )
end
list_switches(options = {}) click to toggle source
# File lib/fog/sakuracloud/requests/network/list_switches.rb, line 6
def list_switches(options = {})
  request(
    :headers => {
      'Authorization' => "Basic #{@auth_encode}"
    },
    :method => 'GET',
    :path => "#{Fog::SakuraCloud::SAKURACLOUD_API_ENDPOINT}/switch"
  )
end
regist_interface_to_server( server_id ) click to toggle source
# File lib/fog/sakuracloud/requests/network/regist_interface_to_server.rb, line 6
def regist_interface_to_server( server_id )
  body = {
    "Interface" => {
      "Server" => {
        "ID" => server_id
      }
    }
  }

  response = request(
    :headers => {
      'Authorization' => "Basic #{@auth_encode}"
    },
    :expects  => [201],
    :method => 'POST',
    :path => "#{Fog::SakuraCloud::SAKURACLOUD_API_ENDPOINT}/interface",
    :body => Fog::JSON.encode(body)
  )
  response.body['Interface']['ID']
end
request(params) click to toggle source
# File lib/fog/sakuracloud/network.rb, line 43
def request(params)
  response = parse @connection.request(params)
  response
end

Private Instance Methods

parse(response) click to toggle source
# File lib/fog/sakuracloud/network.rb, line 49
def parse(response)
  return response if response.body.empty?
  response.body = Fog::JSON.decode(response.body)
  response
end