saklient 0.0.2.6 → 0.0.2.7

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4452225f5c19ef63758c70c0332415d3a0a9bc71
4
- data.tar.gz: 34c349f3d12de616bafe96305381a11e7de963a2
3
+ metadata.gz: 3fc3dcacb443ef9fb88e18864b25b6e983233659
4
+ data.tar.gz: 1cc5f3a9323e2634ca191c523bb0975c62b6ce4b
5
5
  SHA512:
6
- metadata.gz: 8948ac21bbcd87ad1b0a6330dc6acaadc48acb87b926286eba3710a3651451ab6809badd44c26140cebf6a34fd2f7215c7774d12c5463630beda7e17f0b9a001
7
- data.tar.gz: 1f57759c4078a35ed822a89b57967e2e785dae3eed4be9b16a1f0a345a476551dec75ef5dff44cdceecbf8381a7e05450cb3da454f9ec6f0f51fa2e41b0581e4
6
+ metadata.gz: 8ee4c6080b8ea97b6fc138d99b51d83aaef7473d47efec2ac696869cc18e336c5a4a47fefc16ffd8de49c19af1a27bcaaa70f7d079a51686d5ae97f26e8d2230
7
+ data.tar.gz: 13b55667084a18e62dc41441f751592da315998aba2975af3c9507589c51e5e7031fae5dfa4d5e3fd033673ecea8a32155fe6ac3889c76ac388f11267e390487
data/.gitignore CHANGED
@@ -19,3 +19,5 @@ config.sh
19
19
  testok
20
20
  .project
21
21
  .settings
22
+ vendor
23
+
@@ -73,7 +73,7 @@ module Saklient
73
73
 
74
74
  extra_headers = {
75
75
  'Content-Type' => 'application/x-www-form-urlencoded',
76
- 'User-Agent' => 'saklient.ruby ver-0.0.2.6 rev-ea935ca9d640e87509db882d9fd06ec2bf75fe23',
76
+ 'User-Agent' => 'saklient.ruby ver-0.0.2.7 rev-3f3b3b7ce4b10e7ebcd77c17497763ba558bf424',
77
77
  'X-Requested-With' => 'XMLHttpRequest',
78
78
  'X-Sakura-HTTP-Method' => method,
79
79
  'X-Sakura-Error-Level' => 'warning',
@@ -3,6 +3,7 @@
3
3
  require_relative '../client'
4
4
  require_relative 'resource'
5
5
  require_relative 'swytch'
6
+ require_relative 'ipv4_range'
6
7
 
7
8
  module Saklient
8
9
  module Cloud
@@ -38,6 +39,29 @@ module Saklient
38
39
  # @return [String]
39
40
  attr_accessor :m_next_hop
40
41
 
42
+ # @private
43
+ # @return [Ipv4Range]
44
+ attr_accessor :_range
45
+
46
+ public
47
+
48
+ # @private
49
+ # @return [Ipv4Range]
50
+ def get_range
51
+ return @_range
52
+ end
53
+
54
+ # 利用可能なIPアドレス範囲
55
+ #
56
+ # @return [Ipv4Range]
57
+ attr_reader :range
58
+
59
+ def range
60
+ get_range
61
+ end
62
+
63
+ protected
64
+
41
65
  # @private
42
66
  # @return [String]
43
67
  def _api_path
@@ -90,6 +114,16 @@ module Saklient
90
114
 
91
115
  protected
92
116
 
117
+ # @private
118
+ # @param [any] r
119
+ # @param [any] root
120
+ # @return [void]
121
+ def _on_after_api_deserialize(r, root)
122
+ @_range = nil
123
+ addresses = r[:IPAddresses]
124
+ @_range = Saklient::Cloud::Resources::Ipv4Range.new(addresses) if !(addresses).nil?
125
+ end
126
+
93
127
  # @return [bool]
94
128
  attr_accessor :n_id
95
129
 
@@ -0,0 +1,106 @@
1
+ # -*- encoding: UTF-8 -*-
2
+
3
+ require_relative '../../util'
4
+
5
+ module Saklient
6
+ module Cloud
7
+ module Resources
8
+
9
+ # IPv4ネットワークのIPアドレス範囲.
10
+ class Ipv4Range
11
+
12
+ protected
13
+
14
+ # @private
15
+ # @return [String]
16
+ attr_accessor :_first
17
+
18
+ public
19
+
20
+ # @private
21
+ # @return [String]
22
+ def get_first
23
+ return @_first
24
+ end
25
+
26
+ # 開始アドレス
27
+ #
28
+ # @return [String]
29
+ attr_reader :first
30
+
31
+ def first
32
+ get_first
33
+ end
34
+
35
+ protected
36
+
37
+ # @private
38
+ # @return [String]
39
+ attr_accessor :_last
40
+
41
+ public
42
+
43
+ # @private
44
+ # @return [String]
45
+ def get_last
46
+ return @_last
47
+ end
48
+
49
+ # 終了アドレス
50
+ #
51
+ # @return [String]
52
+ attr_reader :last
53
+
54
+ def last
55
+ get_last
56
+ end
57
+
58
+ protected
59
+
60
+ # @private
61
+ # @return [Array<String>]
62
+ attr_accessor :_as_array
63
+
64
+ public
65
+
66
+ # @private
67
+ # @return [Array<String>]
68
+ def get_as_array
69
+ ret = []
70
+ i = Saklient::Util::ip2long(@_first)
71
+ i1 = Saklient::Util::ip2long(@_last)
72
+ while i <= i1 do
73
+ ret << Saklient::Util::long2ip(i)
74
+ i += 1
75
+ end
76
+ return ret
77
+ end
78
+
79
+ # この範囲に属するIPv4アドレスの一覧を取得します.
80
+ #
81
+ # @return [Array<String>]
82
+ attr_reader :as_array
83
+
84
+ def as_array
85
+ get_as_array
86
+ end
87
+
88
+ # @private
89
+ # @param [any] obj
90
+ def initialize(obj = nil)
91
+ obj = {} if (obj).nil?
92
+ first = Saklient::Util::get_by_path_any([obj], ['Min', 'min'])
93
+ @_first = nil
94
+ @_first = first if !(first).nil?
95
+ @_first = nil if @_first == ''
96
+ last = Saklient::Util::get_by_path_any([obj], ['Max', 'max'])
97
+ @_last = nil
98
+ @_last = last if !(last).nil?
99
+ @_last = nil if @_last == ''
100
+ end
101
+
102
+ end
103
+
104
+ end
105
+ end
106
+ end
@@ -215,7 +215,7 @@ module Saklient
215
215
  r = api_serialize
216
216
  query = @_query
217
217
  @_query = {}
218
- keys = query.keys
218
+ keys = query.keys.map{|k| k.to_s}
219
219
  for k in keys
220
220
  v = query[k.to_sym]
221
221
  r[k.to_sym] = v
@@ -207,6 +207,57 @@ module Saklient
207
207
 
208
208
  protected
209
209
 
210
+ # @private
211
+ # @return [any]
212
+ def _used_ipv4_address_hash
213
+ filter = {}
214
+ filter[('Switch' + '.ID').to_sym] = _id
215
+ query = {}
216
+ Saklient::Util::set_by_path(query, 'Count', 0)
217
+ Saklient::Util::set_by_path(query, 'Filter', filter)
218
+ Saklient::Util::set_by_path(query, 'Include', ['IPAddress', 'UserIPAddress'])
219
+ result = @_client.request('GET', '/interface', query)
220
+ return nil if (result).nil?
221
+ result = result[:Interfaces]
222
+ return nil if (result).nil?
223
+ ifaces = result
224
+ return nil if (ifaces).nil?
225
+ found = {}
226
+ for iface in ifaces
227
+ ip = iface[:IPAddress]
228
+ userIp = iface[:UserIPAddress]
229
+ ip = userIp if (ip).nil?
230
+ found[ip.to_sym] = true if !(ip).nil?
231
+ end
232
+ return found
233
+ end
234
+
235
+ public
236
+
237
+ # このルータ+スイッチに接続中のインタフェースに割り当てられているIPアドレスを収集します.
238
+ #
239
+ # @return [Array<String>]
240
+ def collect_used_ipv4_addresses
241
+ found = _used_ipv4_address_hash
242
+ return found.keys.map{|k| k.to_s}.sort()
243
+ end
244
+
245
+ # このルータ+スイッチで利用できる未使用のIPアドレスを収集します.
246
+ #
247
+ # @return [Array<String>]
248
+ def collect_unused_ipv4_addresses
249
+ nets = get_ipv4_nets
250
+ return nil if nets.length < 1
251
+ used = _used_ipv4_address_hash
252
+ ret = []
253
+ for ip in nets[0].range.as_array
254
+ ret << ip if !(!used.nil? && used.key?(ip.to_sym))
255
+ end
256
+ return ret.sort()
257
+ end
258
+
259
+ protected
260
+
210
261
  # @return [bool]
211
262
  attr_accessor :n_id
212
263
 
data/lib/saklient/util.rb CHANGED
@@ -78,7 +78,7 @@ module Saklient
78
78
  raise Exception.new('Could not create class instance of ' + classPath) if (ret).nil?
79
79
  return ret
80
80
  end
81
-
81
+
82
82
  # @param [String] s
83
83
  # @return [NativeDate]
84
84
  def self.str2date(s)
@@ -93,6 +93,35 @@ module Saklient
93
93
  return d.to_s
94
94
  end
95
95
 
96
+ # @param [String] ip
97
+ # @return [Integer]
98
+ def self.ip2long(s)
99
+ return nil unless s.is_a? String
100
+ return nil unless /^\d+\.\d+\.\d+\.\d+$/.match(s)
101
+ ret = 0
102
+ s.split(/\./).each{|o|
103
+ v = o.to_i
104
+ ret = nil unless 0<=v && v<=255
105
+ ret = ret<<8 | v if !ret.nil?
106
+ }
107
+ return ret
108
+ end
109
+
110
+ # @param [Integer] long
111
+ # @return [String]
112
+ def self.long2ip(v)
113
+ v = v.to_i if v.is_a? String
114
+ return nil unless v.is_a? Integer
115
+ ret = []
116
+ v += (1<<32) if v<0
117
+ 4.times{||
118
+ ret.unshift(v & 255)
119
+ v >>= 8
120
+ }
121
+ return nil if v != 0
122
+ return ret.join(".")
123
+ end
124
+
96
125
  # @param [String] s
97
126
  # @return [String]
98
127
  def self.url_encode(s)
@@ -1,5 +1,5 @@
1
1
  # -*- encoding: UTF-8 -*-
2
2
 
3
3
  module Saklient
4
- VERSION = "0.0.2.6"
4
+ VERSION = "0.0.2.7"
5
5
  end
@@ -0,0 +1,61 @@
1
+ $: << File.dirname(__dir__) + '/lib'
2
+ require 'saklient/util'
3
+ require 'saklient/cloud/api'
4
+
5
+ describe 'Ipv4Net' do
6
+
7
+
8
+
9
+ before do
10
+
11
+ # load config file
12
+ root = File.dirname(__dir__)
13
+ test_ok_file = root + '/testok'
14
+ expect(File).to exist(test_ok_file)
15
+ config_file = root + '/config.sh'
16
+ expect(File).to exist(config_file)
17
+ @config = {}
18
+ fh = open(config_file)
19
+ fh.each {|line|
20
+ if /^\s*export\s+(\w+)\s*=\s*(.+?)\s*$/.match(line) then
21
+ key = $1
22
+ value = $2
23
+ @config[key.to_sym] = value.gsub(/'([^']*)'|"([^"]*)"|\\(.)|(.)/) {|m|
24
+ $1 || $2 || $3 || $4
25
+ }
26
+ end
27
+ }
28
+ fh.close
29
+ expect(@config[:SACLOUD_TOKEN]).not_to be_empty #'SACLOUD_TOKEN must be defined in config.sh'
30
+ expect(@config[:SACLOUD_SECRET]).not_to be_empty #'SACLOUD_SECRET must be defined in config.sh'
31
+ #expect(@config[:SACLOUD_ZONE]).not_to be_empty #'SACLOUD_ZONE must be defined in config.sh'
32
+
33
+ # authorize
34
+ @api = Saklient::Cloud::API::authorize(@config[:SACLOUD_TOKEN], @config[:SACLOUD_SECRET])
35
+ @api = @api.in_zone(@config[:SACLOUD_ZONE]) if @config[:SACLOUD_ZONE]
36
+ expect(@api).to be_an_instance_of Saklient::Cloud::API
37
+
38
+ end
39
+
40
+
41
+
42
+ it 'should access objects by path' do
43
+
44
+ @api.router.find.each{|router|
45
+ router.get_swytch.ipv4_nets.each{|net|
46
+ if !net.range.nil?
47
+ p net.range.first
48
+ p net.range.last
49
+ # p net.range.as_array
50
+ end
51
+ }
52
+ p router.get_swytch.collect_used_ipv4_addresses
53
+ p router.get_swytch.collect_unused_ipv4_addresses
54
+ p
55
+ }
56
+
57
+ end
58
+
59
+
60
+
61
+ end
data/spec/router_spec.rb CHANGED
@@ -45,6 +45,9 @@ describe 'Router' do
45
45
  name = '!ruby_rspec-' + DateTime.now.strftime('%Y%m%d_%H%M%S') + '-' + SecureRandom.uuid[0, 8]
46
46
  description = 'This instance was created by saklient.ruby rspec'
47
47
  mask_len = 28
48
+ mask_len_cnt = 1<<32-mask_len
49
+ sroute_mask_len = 28
50
+ sroute_mask_len_cnt = 1<<32-sroute_mask_len
48
51
 
49
52
  #
50
53
  swytch = nil
@@ -77,8 +80,38 @@ describe 'Router' do
77
80
  end
78
81
 
79
82
  expect(swytch).to be_an_instance_of Saklient::Cloud::Resources::Swytch
80
- expect(swytch.ipv4_nets.length).to be > 0
83
+ expect(swytch.ipv4_nets.length).to eq 1
81
84
  expect(swytch.ipv4_nets[0]).to be_an_instance_of Saklient::Cloud::Resources::Ipv4Net
85
+ expect(swytch.ipv4_nets[0].range.as_array.length).to eq mask_len_cnt-5
86
+ expect(swytch.collect_used_ipv4_addresses.length).to eq 0
87
+ expect(swytch.collect_unused_ipv4_addresses.length).to eq mask_len_cnt-5
88
+
89
+ #
90
+ puts 'サーバを作成しています...'
91
+ server = @api.server.create
92
+ expect(server).to be_an_instance_of Saklient::Cloud::Resources::Server
93
+ server.name = name
94
+ server.description = description
95
+ server.plan = @api.product.server.get_by_spec(1, 1)
96
+ server.save
97
+ expect(server.id.to_i).to be > 0
98
+
99
+ #
100
+ puts 'インタフェースを増設しています...'
101
+ iface = server.add_iface()
102
+ expect(iface).to be_an_instance_of Saklient::Cloud::Resources::Iface
103
+ expect(iface.id.to_i).to be > 0
104
+
105
+ #
106
+ puts 'インタフェースをルータ+スイッチに接続しています...'
107
+ iface.connect_to_swytch(swytch)
108
+
109
+ #
110
+ puts 'インタフェースにIPアドレスを設定しています...'
111
+ iface.user_ip_address = swytch.ipv4_nets[0].range.as_array[1]
112
+ iface.save
113
+ expect(swytch.collect_used_ipv4_addresses.length).to eq 1
114
+ expect(swytch.collect_unused_ipv4_addresses.length).to eq mask_len_cnt-6
82
115
 
83
116
  #
84
117
  puts 'ルータ+スイッチの帯域プランを変更しています...'
@@ -87,15 +120,20 @@ describe 'Router' do
87
120
  expect(swytch.router.id).not_to eq router_id_before
88
121
 
89
122
  #
90
- if 0 < swytch.ipv6_nets.length then
91
- puts 'ルータ+スイッチからIPv6ネットワークの割当を解除しています...'
92
- swytch.remove_ipv6_net
93
- end
94
123
  puts 'ルータ+スイッチにIPv6ネットワークを割り当てています...'
95
124
  v6net = swytch.add_ipv6_net
96
125
  expect(v6net).to be_an_instance_of Saklient::Cloud::Resources::Ipv6Net
97
126
  expect(swytch.ipv6_nets.length).to eq 1
98
127
 
128
+ #
129
+ puts 'ルータ+スイッチにスタティックルートを割り当てています...'
130
+ net0 = swytch.ipv4_nets[0]
131
+ next_hop = IPAddr.new(IPAddr.new(net0.address).to_i + 4, Socket::AF_INET).to_s
132
+ sroute = swytch.add_static_route(sroute_mask_len, next_hop)
133
+ expect(sroute).to be_an_instance_of Saklient::Cloud::Resources::Ipv4Net
134
+ expect(swytch.ipv4_nets.length).to eq 2
135
+ expect(swytch.ipv4_nets[1].range.as_array.length).to eq sroute_mask_len_cnt
136
+
99
137
  #
100
138
  (swytch.ipv4_nets.length - 1).downto(1) do |i|
101
139
  puts 'ルータ+スイッチからスタティックルートの割当を解除しています...'
@@ -103,12 +141,17 @@ describe 'Router' do
103
141
  swytch.remove_static_route(net)
104
142
  end
105
143
 
106
- puts 'ルータ+スイッチにスタティックルートを割り当てています...'
107
- net0 = swytch.ipv4_nets[0]
108
- next_hop = IPAddr.new(IPAddr.new(net0.address).to_i + 4, Socket::AF_INET).to_s
109
- sroute = swytch.add_static_route(28, next_hop)
110
- expect(sroute).to be_an_instance_of Saklient::Cloud::Resources::Ipv4Net
111
- expect(swytch.ipv4_nets.length).to eq 2
144
+ #
145
+ if 0 < swytch.ipv6_nets.length then
146
+ puts 'ルータ+スイッチからIPv6ネットワークの割当を解除しています...'
147
+ swytch.remove_ipv6_net
148
+ end
149
+
150
+ #
151
+ puts 'サーバを削除しています...'
152
+ server.destroy
153
+
154
+ #
112
155
 
113
156
  end
114
157
 
data/spec/swytch_spec.rb CHANGED
@@ -46,7 +46,7 @@ describe 'Swytch' do
46
46
  description = 'This instance was created by saklient.ruby rspec'
47
47
 
48
48
  #
49
- puts 'ルータ+スイッチを作成しています...'
49
+ puts 'スイッチを作成しています...'
50
50
  swytch = @api.swytch.create
51
51
  swytch.name = name
52
52
  swytch.description = description
data/spec/util_spec.rb CHANGED
@@ -67,6 +67,34 @@ describe 'Util' do
67
67
  end
68
68
  fail '未定義または読み取り専用フィールドへのset時は NoMethodError がスローされなければなりません' unless ok
69
69
 
70
+ #
71
+ expect(Util::ip2long('0.0.0.0')).to eq 0
72
+ expect(Util::ip2long('127.255.255.255')).to eq 0x7FFFFFFF
73
+ expect(Util::ip2long('128.0.0.0')).to eq 0x80000000
74
+ expect(Util::ip2long('255.255.255.255')).to eq 0xFFFFFFFF
75
+ expect(Util::ip2long('222.173.190.239')).to eq 0xDEADBEEF
76
+ #
77
+ expect(Util::long2ip(0)).to eq '0.0.0.0'
78
+ expect(Util::long2ip(0x7FFFFFFF)).to eq '127.255.255.255'
79
+ expect(Util::long2ip(0x80000000)).to eq '128.0.0.0'
80
+ expect(Util::long2ip(0xFFFFFFFF)).to eq '255.255.255.255'
81
+ expect(Util::long2ip(0xDEADBEEF)).to eq '222.173.190.239'
82
+ expect(Util::long2ip(Util::ip2long('127.255.255.255') + 1)).to eq '128.0.0.0'
83
+ #
84
+ expect(Util::ip2long(nil)).to be_nil
85
+ expect(Util::ip2long(0)).to be_nil
86
+ expect(Util::ip2long('')).to be_nil
87
+ expect(Util::ip2long('x')).to be_nil
88
+ expect(Util::ip2long('0.0.0')).to be_nil
89
+ expect(Util::ip2long('0.0.0.x')).to be_nil
90
+ expect(Util::ip2long('0.0.0.0.0')).to be_nil
91
+ expect(Util::ip2long('255.255.255.256')).to be_nil
92
+ expect(Util::ip2long('256.255.255.255')).to be_nil
93
+ expect(Util::long2ip(nil)).to be_nil
94
+ expect(Util::long2ip('0')).to eq '0.0.0.0'
95
+ expect(Util::long2ip(Util::ip2long('0.0.0.0') - 1)).to eq '255.255.255.255'
96
+ expect(Util::long2ip(Util::ip2long('255.255.255.255') + 1)).to be_nil
97
+
70
98
  end
71
99
 
72
100
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: saklient
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2.6
4
+ version: 0.0.2.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - townewgokgok
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-02-10 00:00:00.000000000 Z
11
+ date: 2015-04-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -198,6 +198,7 @@ files:
198
198
  - lib/saklient/cloud/resources/icon.rb
199
199
  - lib/saklient/cloud/resources/iface.rb
200
200
  - lib/saklient/cloud/resources/ipv4_net.rb
201
+ - lib/saklient/cloud/resources/ipv4_range.rb
201
202
  - lib/saklient/cloud/resources/ipv6_net.rb
202
203
  - lib/saklient/cloud/resources/iso_image.rb
203
204
  - lib/saklient/cloud/resources/lb_server.rb
@@ -256,6 +257,7 @@ files:
256
257
  - spec/bridge_spec.rb
257
258
  - spec/enum_spec.rb
258
259
  - spec/exception_spec.rb
260
+ - spec/ipv4net_spec.rb
259
261
  - spec/iso_image_spec.rb
260
262
  - spec/license_spec.rb
261
263
  - spec/loadbalancer_spec.rb
@@ -293,6 +295,7 @@ test_files:
293
295
  - spec/bridge_spec.rb
294
296
  - spec/enum_spec.rb
295
297
  - spec/exception_spec.rb
298
+ - spec/ipv4net_spec.rb
296
299
  - spec/iso_image_spec.rb
297
300
  - spec/license_spec.rb
298
301
  - spec/loadbalancer_spec.rb
@@ -300,4 +303,3 @@ test_files:
300
303
  - spec/server_spec.rb
301
304
  - spec/swytch_spec.rb
302
305
  - spec/util_spec.rb
303
- has_rdoc: