Class: Fog::Network::SakuraCloud::Real

Inherits:
Object
  • Object
show all
Includes:
SakuraCloud::Utils::Request
Defined in:
lib/fog/sakuracloud/network.rb,
lib/fog/sakuracloud/requests/network/list_routers.rb,
lib/fog/sakuracloud/requests/network/create_router.rb,
lib/fog/sakuracloud/requests/network/create_switch.rb,
lib/fog/sakuracloud/requests/network/delete_router.rb,
lib/fog/sakuracloud/requests/network/delete_switch.rb,
lib/fog/sakuracloud/requests/network/list_switches.rb,
lib/fog/sakuracloud/requests/network/list_interfaces.rb,
lib/fog/sakuracloud/requests/network/delete_interface.rb,
lib/fog/sakuracloud/requests/network/collect_monitor_router.rb,
lib/fog/sakuracloud/requests/network/change_router_bandwidth.rb,
lib/fog/sakuracloud/requests/network/regist_interface_to_server.rb,
lib/fog/sakuracloud/requests/network/connect_interface_to_switch.rb

Instance Method Summary collapse

Methods included from SakuraCloud::Utils::Request

#request

Constructor Details

#initialize(options = {}) ⇒ Real

Returns a new instance of Real.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/fog/sakuracloud/network.rb', line 34

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'
  @api_zone            = options[:api_zone] || Fog.credentials[:sakuracloud_api_zone] || 'is1b'
  Fog::SakuraCloud.validate_api_zone!(@api_zone)

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

Instance Method Details

#change_router_bandwidth(id, bandwidthmbps) ⇒ Object



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

def change_router_bandwidth( id, bandwidthmbps )
  body = {
    "Internet" => {
      "BandWidthMbps" => bandwidthmbps
    }
  }

  request(
    :headers => {
      'Authorization' => "Basic #{@auth_encode}"
    },
    :expects  => [200],
    :method => 'PUT',
    :path => "#{Fog::SakuraCloud.build_endpoint(@api_zone)}/internet/#{id}/bandwidth",
    :body => Fog::JSON.encode(body)
  )
end

#collect_monitor_router(id, start_time = nil, end_time = nil) ⇒ Object



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

def collect_monitor_router( id ,start_time = nil, end_time = nil)
  filter = {}
  filter['Start'] = start_time if start_time
  filter['End']   = end_time if end_time
  request(
    :headers => {
      'Authorization' => "Basic #{@auth_encode}"
    },
    :expects  => [200],
    :method => 'GET',
    :path => "#{Fog::SakuraCloud.build_endpoint(@api_zone)}/internet/#{id}/monitor",
    :query => URI.encode(Fog::JSON.encode(filter))
  )
end

#connect_interface_to_switch(id, switch_id) ⇒ Object



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

def connect_interface_to_switch( id, switch_id )
  response = request(
    :headers => {
      'Authorization' => "Basic #{@auth_encode}"
    },
    :expects  => [200],
    :method => 'PUT',
    :path => "#{Fog::SakuraCloud.build_endpoint(@api_zone)}/interface/#{id}/to/switch/#{switch_id}"
  )
  response.body['Interface']['ID']
end

#create_router(options) ⇒ 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/network/create_router.rb', line 7

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.build_endpoint(@api_zone)}/internet",
    :body => Fog::JSON.encode(body)
  )
end

#create_switch(options) ⇒ Object



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

def create_switch(options)
  body = {
    "Switch" => {
      "Name" => options[:name]
    }
  }

  request(
    :headers => {
      'Authorization' => "Basic #{@auth_encode}"
    },
    :expects  => 201,
    :method => 'POST',
    :path => "#{Fog::SakuraCloud.build_endpoint(@api_zone)}/switch",
    :body => Fog::JSON.encode(body)
  )
end

#delete_interface(id) ⇒ Object



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

def delete_interface( id )
  request(
    :headers => {
      'Authorization' => "Basic #{@auth_encode}"
    },
    :expects  => [200],
    :method => 'DELETE',
    :path => "#{Fog::SakuraCloud.build_endpoint(@api_zone)}/interface/#{id}"
  )
end

#delete_router(id) ⇒ Object



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

def delete_router( id )
  request(
    :headers => {
      'Authorization' => "Basic #{@auth_encode}"
    },
    :expects  => [200],
    :method => 'DELETE',
    :path => "#{Fog::SakuraCloud.build_endpoint(@api_zone)}/internet/#{id}"
  )
end

#delete_switch(id) ⇒ Object



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

def delete_switch( id )
  request(
    :headers => {
      'Authorization' => "Basic #{@auth_encode}"
    },
    :expects  => [200],
    :method => 'DELETE',
    :path => "#{Fog::SakuraCloud.build_endpoint(@api_zone)}/switch/#{id}"
  )
end

#list_interfaces(options = {}) ⇒ Object



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

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.build_endpoint(@api_zone)}/interface",
    :query => URI.encode(Fog::JSON.encode(filter))
  )
end

#list_routers(options = {}) ⇒ Object



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

def list_routers(options = {})
  request(
    :headers => {
      'Authorization' => "Basic #{@auth_encode}"
    },
    :method => 'GET',
    :path => "#{Fog::SakuraCloud.build_endpoint(@api_zone)}/internet"
  )
end

#list_switches(options = {}) ⇒ Object



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

def list_switches(options = {})
  request(
    :headers => {
      'Authorization' => "Basic #{@auth_encode}"
    },
    :method => 'GET',
    :path => "#{Fog::SakuraCloud.build_endpoint(@api_zone)}/switch"
  )
end

#regist_interface_to_server(server_id) ⇒ Object



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

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.build_endpoint(@api_zone)}/interface",
    :body => Fog::JSON.encode(body)
  )
  response.body['Interface']['ID']
end