soracom 1.0.9 → 1.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/soracom/cli.rb +363 -42
- data/lib/soracom/client.rb +202 -14
- data/lib/soracom/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 76e5bdebebad971ecab7c61854f14379ed898737
|
4
|
+
data.tar.gz: 1b4187c8005078047866acaec5e3bd5771bfd47e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b32df7ef3e41262310fc24f97e52f61a527173758961b770fcfbc4d78e11f7f4dd5ec1a22331f39c9cee234ec2ae3a2d0aec44fb2792ccd6ae13303d1392a76e
|
7
|
+
data.tar.gz: 02ff32ffa7a5e7e70ba311503e8fec1ac8e1441acb5e98103182c486f5d5c793fb411144d6ab715634cb1e360f94e3ee120ec6701ad13732260f6743462b14c9
|
data/lib/soracom/cli.rb
CHANGED
@@ -10,7 +10,8 @@ module SoracomCli
|
|
10
10
|
option :limit, type: :numeric, required: false, desc: 'max number of result(default:1024)'
|
11
11
|
option :filter, type: :hash, required: false, desc: 'ex) --filter key:imsi value:001010000000001 / --filter key:tagname value:tagvalue'
|
12
12
|
def list
|
13
|
-
client = Soracom::Client.new
|
13
|
+
client = Soracom::Client.new(profile:options.profile)
|
14
|
+
options.delete('profile')
|
14
15
|
sims = client.list_subscribers(Hash[options.map { |k, v| [k.to_sym, v] }])
|
15
16
|
puts JSON.pretty_generate(sims)
|
16
17
|
end
|
@@ -21,21 +22,22 @@ module SoracomCli
|
|
21
22
|
option :type, type: :string, desc: 'default plan type'
|
22
23
|
option :tags, type: :hash, desc: 'tag as hash i.e. --tags name:"foo" group:"bar"'
|
23
24
|
def register
|
24
|
-
client = Soracom::Client.new
|
25
|
+
client = Soracom::Client.new(profile:options.profile)
|
26
|
+
options.delete('profile')
|
25
27
|
puts JSON.pretty_generate(client.register_subscriber(Hash[options.map { |k, v| [k.to_sym, v] }]))
|
26
28
|
end
|
27
29
|
|
28
30
|
desc 'activate', 'activate Subscriber(SIM)s'
|
29
31
|
option :imsi, type: :array, required: true
|
30
32
|
def activate
|
31
|
-
client = Soracom::Client.new
|
33
|
+
client = Soracom::Client.new(profile:options.profile)
|
32
34
|
puts JSON.pretty_generate(client.activate_subscriber(options[:imsi]))
|
33
35
|
end
|
34
36
|
|
35
37
|
desc 'deactivate', 'deactivate Subscriber(SIM)s'
|
36
38
|
option :imsi, type: :array, required: true
|
37
39
|
def deactivate
|
38
|
-
client = Soracom::Client.new
|
40
|
+
client = Soracom::Client.new(profile:options.profile)
|
39
41
|
puts JSON.pretty_generate(client.deactivate_subscriber(options[:imsi]))
|
40
42
|
end
|
41
43
|
|
@@ -44,21 +46,21 @@ module SoracomCli
|
|
44
46
|
option :confirm, type: :string
|
45
47
|
def terminate
|
46
48
|
abort 'You may not revert terminate opereation. Please add "--confirm YES" if you are sure.' if options[:confirm] != 'YES'
|
47
|
-
client = Soracom::Client.new
|
49
|
+
client = Soracom::Client.new(profile:options.profile)
|
48
50
|
puts JSON.pretty_generate(client.terminate_subscriber(options[:imsi]))
|
49
51
|
end
|
50
52
|
|
51
53
|
desc 'enable_termination', 'enable termination of Subscriber(SIM)s'
|
52
54
|
option :imsi, type: :array, required: true
|
53
55
|
def enable_termination
|
54
|
-
client = Soracom::Client.new
|
56
|
+
client = Soracom::Client.new(profile:options.profile)
|
55
57
|
puts JSON.pretty_generate(client.enable_termination(options[:imsi]))
|
56
58
|
end
|
57
59
|
|
58
60
|
desc 'disable_termination', 'disable termination of Subscriber(SIM)s'
|
59
61
|
option :imsi, type: :array, required: true
|
60
62
|
def disable_termination
|
61
|
-
client = Soracom::Client.new
|
63
|
+
client = Soracom::Client.new(profile:options.profile)
|
62
64
|
puts JSON.pretty_generate(client.disable_termination(options[:imsi]))
|
63
65
|
end
|
64
66
|
|
@@ -66,7 +68,7 @@ module SoracomCli
|
|
66
68
|
option :imsi, type: :array, required: true
|
67
69
|
option :tags, type: :hash, required: true
|
68
70
|
def update_tags
|
69
|
-
client = Soracom::Client.new
|
71
|
+
client = Soracom::Client.new(profile:options.profile)
|
70
72
|
puts JSON.pretty_generate(client.update_subscriber_tags(options[:imsi], options[:tags]))
|
71
73
|
end
|
72
74
|
|
@@ -74,7 +76,7 @@ module SoracomCli
|
|
74
76
|
option :imsi, type: :array, required: true
|
75
77
|
option :tag_name, type: :string, required: true
|
76
78
|
def delete_tag
|
77
|
-
client = Soracom::Client.new
|
79
|
+
client = Soracom::Client.new(profile:options.profile)
|
78
80
|
puts JSON.pretty_generate(client.delete_subscriber_tag(options[:imsi], options[:tag_name]))
|
79
81
|
end
|
80
82
|
|
@@ -82,7 +84,7 @@ module SoracomCli
|
|
82
84
|
option :imsi, type: :array, required: true
|
83
85
|
option :speed_class, type: :string, required: true
|
84
86
|
def update_speed_class
|
85
|
-
client = Soracom::Client.new
|
87
|
+
client = Soracom::Client.new(profile:options.profile)
|
86
88
|
puts JSON.pretty_generate(client.update_subscriber_speed_class(options[:imsi], options[:speed_class]))
|
87
89
|
end
|
88
90
|
|
@@ -90,14 +92,14 @@ module SoracomCli
|
|
90
92
|
option :imsi, type: :array, required: true
|
91
93
|
option :expiry_time, type: :numeric, required: true
|
92
94
|
def set_expiry_time
|
93
|
-
client = Soracom::Client.new
|
95
|
+
client = Soracom::Client.new(profile:options.profile)
|
94
96
|
puts JSON.pretty_generate(client.set_expiry_time(options[:imsi], options[:expiry_time]))
|
95
97
|
end
|
96
98
|
|
97
99
|
desc 'unset_expiry_time', 'delete expiry time of Subscriber(SIM)s'
|
98
100
|
option :imsi, type: :array, required: true
|
99
101
|
def unset_expiry_time
|
100
|
-
client = Soracom::Client.new
|
102
|
+
client = Soracom::Client.new(profile:options.profile)
|
101
103
|
puts JSON.pretty_generate(client.unset_expiry_time(options[:imsi]))
|
102
104
|
end
|
103
105
|
|
@@ -105,14 +107,14 @@ module SoracomCli
|
|
105
107
|
option :imsi, type: :string, required: true
|
106
108
|
option :group_id, type: :string, required: true
|
107
109
|
def set_group
|
108
|
-
client = Soracom::Client.new
|
110
|
+
client = Soracom::Client.new(profile:options.profile)
|
109
111
|
puts JSON.pretty_generate(client.set_group(options[:imsi], options[:group_id]))
|
110
112
|
end
|
111
113
|
|
112
114
|
desc 'unset_group', 'unset group of Subscriber(SIM)'
|
113
115
|
option :imsi, type: :string, required: true
|
114
116
|
def unset_group
|
115
|
-
client = Soracom::Client.new
|
117
|
+
client = Soracom::Client.new(profile:options.profile)
|
116
118
|
puts JSON.pretty_generate(client.unset_group(options[:imsi]))
|
117
119
|
end
|
118
120
|
end
|
@@ -122,28 +124,28 @@ module SoracomCli
|
|
122
124
|
desc 'list', 'list groups'
|
123
125
|
option :group_id, type: :string, desc: 'group ID'
|
124
126
|
def list
|
125
|
-
client = Soracom::Client.new
|
127
|
+
client = Soracom::Client.new(profile:options.profile)
|
126
128
|
puts JSON.pretty_generate(client.list_groups(options.group_id))
|
127
129
|
end
|
128
130
|
|
129
131
|
desc 'list_subscribers', 'list subscriber in a group'
|
130
132
|
option :group_id, type: :string, required:true, desc: 'group ID'
|
131
133
|
def list_subscribers
|
132
|
-
client = Soracom::Client.new
|
134
|
+
client = Soracom::Client.new(profile:options.profile)
|
133
135
|
puts JSON.pretty_generate(client.list_subscribers_in_group(options.group_id))
|
134
136
|
end
|
135
137
|
|
136
138
|
desc 'create', 'create group'
|
137
139
|
option :tags, type: :hash, desc: 'group tags'
|
138
140
|
def create
|
139
|
-
client = Soracom::Client.new
|
141
|
+
client = Soracom::Client.new(profile:options.profile)
|
140
142
|
puts JSON.pretty_generate(client.create_group(options.tags))
|
141
143
|
end
|
142
144
|
|
143
145
|
desc 'delete_group', 'delete a group'
|
144
146
|
option :group_id, type: :string, required:true, desc: 'group ID'
|
145
147
|
def delete
|
146
|
-
client = Soracom::Client.new
|
148
|
+
client = Soracom::Client.new(profile:options.profile)
|
147
149
|
puts JSON.pretty_generate(client.delete_group(options.group_id))
|
148
150
|
end
|
149
151
|
|
@@ -152,7 +154,16 @@ module SoracomCli
|
|
152
154
|
option :namespace, type: :string, required:true, desc: 'namespace of the parameter'
|
153
155
|
option :params, type: :string, required:true, desc: 'JSON string of the configuration parameter'
|
154
156
|
def update_configuration
|
155
|
-
|
157
|
+
if options.params=~/^[a-z]+:\/\/(.+)/
|
158
|
+
begin
|
159
|
+
content = open(options.params.sub(/^file:\/\//,'')).read
|
160
|
+
options['params'] = content
|
161
|
+
rescue Errno::ENOENT
|
162
|
+
rescue SocketError
|
163
|
+
abort "ERROR: Cannot access #{options.params}."
|
164
|
+
end
|
165
|
+
end
|
166
|
+
client = Soracom::Client.new(profile:options.profile)
|
156
167
|
puts JSON.pretty_generate(client.update_group_configuration(options.group_id, options.namespace, options.params))
|
157
168
|
end
|
158
169
|
|
@@ -161,7 +172,7 @@ module SoracomCli
|
|
161
172
|
option :namespace, type: :string, required:true, desc: 'namespace of the parameter'
|
162
173
|
option :name, type: :string, required:true, desc: 'name of configuration parameter to delete'
|
163
174
|
def delete_configuration
|
164
|
-
client = Soracom::Client.new
|
175
|
+
client = Soracom::Client.new(profile:options.profile)
|
165
176
|
puts JSON.pretty_generate(client.delete_group_configuration(options.group_id, options.namespace, options.name))
|
166
177
|
end
|
167
178
|
|
@@ -169,7 +180,7 @@ module SoracomCli
|
|
169
180
|
option :group_id, type: :string, required:true, desc: 'group ID'
|
170
181
|
option :tags, type: :string, required:true, desc: 'JSON string of tags i.e. [{"tagName":"name","tagValue":"new group name"}]'
|
171
182
|
def update_tags
|
172
|
-
client = Soracom::Client.new
|
183
|
+
client = Soracom::Client.new(profile:options.profile)
|
173
184
|
puts JSON.pretty_generate(client.update_group_tags(options.group_id, options.tags))
|
174
185
|
end
|
175
186
|
|
@@ -177,7 +188,7 @@ module SoracomCli
|
|
177
188
|
option :group_id, type: :string, required:true, desc: 'group ID'
|
178
189
|
option :name, type: :string, required:true, desc: 'tag name to delete'
|
179
190
|
def delete_tag
|
180
|
-
client = Soracom::Client.new
|
191
|
+
client = Soracom::Client.new(profile:options.profile)
|
181
192
|
puts JSON.pretty_generate(client.delete_group_tags(options.group_id, options.name))
|
182
193
|
end
|
183
194
|
end
|
@@ -189,21 +200,31 @@ module SoracomCli
|
|
189
200
|
option :imsi, type: :string, desc: 'SIM unique ID, 15 digits'
|
190
201
|
option :target, type: :string, desc: 'target can be operator/imsi/tag'
|
191
202
|
def list
|
192
|
-
client = Soracom::Client.new
|
203
|
+
client = Soracom::Client.new(profile:options.profile)
|
204
|
+
options.delete('profile')
|
193
205
|
puts JSON.pretty_generate(client.list_event_handlers(Hash[options.map { |k, v| [k.to_sym, v] }]))
|
194
206
|
end
|
195
207
|
|
196
208
|
desc 'create', 'create event handler'
|
197
209
|
option :req, type: :string, desc: 'JSON string of event handler configuration'
|
198
210
|
def create
|
199
|
-
|
211
|
+
if options.req=~/^[a-z]+:\/\/(.+)/
|
212
|
+
begin
|
213
|
+
content = open(options.req.sub(/^file:\/\//,'')).read
|
214
|
+
options['req'] = content
|
215
|
+
rescue Errno::ENOENT
|
216
|
+
rescue SocketError
|
217
|
+
abort "ERROR: Cannot access #{options.req}."
|
218
|
+
end
|
219
|
+
end
|
220
|
+
client = Soracom::Client.new(profile:options.profile)
|
200
221
|
puts JSON.pretty_generate(client.create_event_handler(options.req))
|
201
222
|
end
|
202
223
|
|
203
224
|
desc 'delete', 'delete event handler'
|
204
225
|
option :handler_id, type: :string, required:true, desc: 'Event Handler ID'
|
205
226
|
def delete
|
206
|
-
client = Soracom::Client.new
|
227
|
+
client = Soracom::Client.new(profile:options.profile)
|
207
228
|
puts JSON.pretty_generate(client.delete_event_handler(options.handler_id))
|
208
229
|
end
|
209
230
|
|
@@ -211,7 +232,16 @@ module SoracomCli
|
|
211
232
|
option :handler_id, type: :string, required:true, desc: 'Event Handler ID'
|
212
233
|
option :req, type: :string, desc: 'JSON string of event handler configuration'
|
213
234
|
def update
|
214
|
-
|
235
|
+
if options.req=~/^[a-z]+:\/\/(.+)/
|
236
|
+
begin
|
237
|
+
content = open(options.req.sub(/^file:\/\//,'')).read
|
238
|
+
options['req'] = content
|
239
|
+
rescue Errno::ENOENT
|
240
|
+
rescue SocketError
|
241
|
+
abort "ERROR: Cannot access #{options.req}."
|
242
|
+
end
|
243
|
+
end
|
244
|
+
client = Soracom::Client.new(profile:options.profile)
|
215
245
|
puts JSON.pretty_generate(client.update_event_handler(options.handler_id, options.req))
|
216
246
|
end
|
217
247
|
end
|
@@ -224,7 +254,8 @@ module SoracomCli
|
|
224
254
|
option :to, type: :numeric, required: false, desc: 'UNIX time in seconds when stat window ends'
|
225
255
|
option :period, type: :string, required: false, desc: 'miniutes or day or month'
|
226
256
|
def get_air_usage
|
227
|
-
client = Soracom::Client.new
|
257
|
+
client = Soracom::Client.new(profile:options.profile)
|
258
|
+
options.delete('profile')
|
228
259
|
data = client.get_air_usage(Hash[options.map { |k, v| [k.to_sym, v] }])
|
229
260
|
puts JSON.pretty_generate(data)
|
230
261
|
end
|
@@ -235,7 +266,8 @@ module SoracomCli
|
|
235
266
|
option :to, type: :numeric, required: false, desc: 'UNIX time in seconds when stat window ends'
|
236
267
|
option :period, type: :string, required: false, desc: 'miniutes or day or month'
|
237
268
|
def get_beam_usage
|
238
|
-
client = Soracom::Client.new
|
269
|
+
client = Soracom::Client.new(profile:options.profile)
|
270
|
+
options.delete('profile')
|
239
271
|
data = client.get_beam_usage(Hash[options.map { |k, v| [k.to_sym, v] }])
|
240
272
|
puts JSON.pretty_generate(data)
|
241
273
|
end
|
@@ -245,7 +277,7 @@ module SoracomCli
|
|
245
277
|
option :to, type: :numeric, required: false, desc: 'UNIX time in seconds when stat window ends'
|
246
278
|
option :period, type: :string, required: false, desc: 'miniutes or day or month'
|
247
279
|
def export_air_usage
|
248
|
-
client = Soracom::Client.new
|
280
|
+
client = Soracom::Client.new(profile:options.profile)
|
249
281
|
csv = client.export_air_usage(Hash[options.map { |k, v| [k.to_sym, v] }])
|
250
282
|
puts csv
|
251
283
|
end
|
@@ -255,54 +287,299 @@ module SoracomCli
|
|
255
287
|
option :to, type: :numeric, required: false, desc: 'UNIX time in seconds when stat window ends'
|
256
288
|
option :period, type: :string, required: false, desc: 'miniutes or day or month'
|
257
289
|
def export_beam_usage
|
258
|
-
client = Soracom::Client.new
|
290
|
+
client = Soracom::Client.new(profile:options.profile)
|
291
|
+
options.delete('profile')
|
259
292
|
csv = client.export_beam_usage(Hash[options.map { |k, v| [k.to_sym, v] }])
|
260
293
|
puts csv
|
261
294
|
end
|
262
295
|
end
|
263
296
|
|
297
|
+
# Operator related commands
|
264
298
|
class Operator < Thor
|
265
|
-
desc 'list_auth_keys', 'list auth keys'
|
299
|
+
desc 'list_auth_keys', 'list auth keys for Operator'
|
266
300
|
def list_auth_keys
|
267
|
-
client = Soracom::Client.new
|
268
|
-
data = client.
|
301
|
+
client = Soracom::Client.new(profile:options.profile)
|
302
|
+
data = client.list_operator_auth_keys()
|
269
303
|
puts JSON.pretty_generate(data)
|
270
304
|
end
|
271
305
|
|
272
|
-
desc '
|
273
|
-
def
|
274
|
-
client = Soracom::Client.new
|
275
|
-
data = client.
|
306
|
+
desc 'generate_auth_key', 'generate new auth key for Operator'
|
307
|
+
def generate_auth_key
|
308
|
+
client = Soracom::Client.new(profile:options.profile)
|
309
|
+
data = client.generate_operator_auth_key()
|
276
310
|
puts JSON.pretty_generate(data)
|
277
311
|
end
|
278
312
|
|
279
|
-
desc 'delete_auth_key', 'delete existing auth key'
|
313
|
+
desc 'delete_auth_key', 'delete existing auth key for Operator'
|
280
314
|
option :auth_key_id, type: :string, required: true, desc: 'auth key id starting "keyId-"'
|
281
315
|
def delete_auth_key
|
316
|
+
client = Soracom::Client.new(profile:options.profile)
|
317
|
+
data = client.delete_operator_auth_key(options.auth_key_id)
|
318
|
+
puts JSON.pretty_generate(data)
|
319
|
+
end
|
320
|
+
end
|
321
|
+
|
322
|
+
# User related commands
|
323
|
+
class User < Thor
|
324
|
+
desc 'list_users', 'list users under Operator'
|
325
|
+
def list_users
|
326
|
+
client = Soracom::Client.new(profile:options.profile)
|
327
|
+
data = client.list_users()
|
328
|
+
puts JSON.pretty_generate(data)
|
329
|
+
end
|
330
|
+
|
331
|
+
desc 'delete_user', 'delete user under Operator'
|
332
|
+
option :user_name, required: true, desc: 'User name'
|
333
|
+
def delete_user
|
334
|
+
client = Soracom::Client.new(profile:options.profile)
|
335
|
+
data = client.delete_user(options.user_name)
|
336
|
+
puts JSON.pretty_generate(data)
|
337
|
+
end
|
338
|
+
|
339
|
+
desc 'get_user', 'get user info under Operator'
|
340
|
+
option :user_name, desc: 'User name'
|
341
|
+
def get_user
|
342
|
+
client = Soracom::Client.new(profile:options.profile)
|
343
|
+
data = client.get_user(options.user_name)
|
344
|
+
puts JSON.pretty_generate(data)
|
345
|
+
end
|
346
|
+
|
347
|
+
desc 'create_user', 'create user under Operator'
|
348
|
+
option :user_name, required: true, desc: 'User name'
|
349
|
+
option :description, desc: 'description for the user'
|
350
|
+
def create_user
|
351
|
+
client = Soracom::Client.new(profile:options.profile)
|
352
|
+
data = client.create_user(options.user_name, options.description)
|
353
|
+
puts JSON.pretty_generate(data)
|
354
|
+
end
|
355
|
+
|
356
|
+
desc 'update_user', 'update user under Operator'
|
357
|
+
option :user_name, required: true, desc: 'User name'
|
358
|
+
option :description, desc: 'description for the user'
|
359
|
+
def update_user
|
360
|
+
client = Soracom::Client.new(profile:options.profile)
|
361
|
+
data = client.update_user(options.user_name, options.description)
|
362
|
+
puts JSON.pretty_generate(data)
|
363
|
+
end
|
364
|
+
|
365
|
+
desc 'list_auth_keys', 'list auth keys for user'
|
366
|
+
option :user_name, desc: 'User name'
|
367
|
+
def list_auth_keys
|
368
|
+
client = Soracom::Client.new(profile:options.profile)
|
369
|
+
data = client.list_users_auth_key(options.user_name)
|
370
|
+
puts JSON.pretty_generate(data)
|
371
|
+
end
|
372
|
+
|
373
|
+
desc 'generate_auth_key', 'generate new auth key for user'
|
374
|
+
option :user_name, desc: 'User name'
|
375
|
+
def generate_auth_key
|
376
|
+
client = Soracom::Client.new(profile:options.profile)
|
377
|
+
data = client.generate_users_auth_key(options.user_name)
|
378
|
+
puts JSON.pretty_generate(data)
|
379
|
+
end
|
380
|
+
|
381
|
+
desc 'delete_auth_key', 'delete existing auth key for user'
|
382
|
+
option :user_name, desc: 'User name'
|
383
|
+
option :auth_key_id, type: :string, required: true, desc: 'auth key id starting "keyId-"'
|
384
|
+
def delete_auth_key
|
385
|
+
client = Soracom::Client.new(profile:options.profile)
|
386
|
+
data = client.delete_users_auth_key(options.user_name, options.auth_key_id)
|
387
|
+
puts JSON.pretty_generate(data)
|
388
|
+
end
|
389
|
+
|
390
|
+
desc 'get_auth_key', 'get auth key info for user'
|
391
|
+
option :user_name, desc: 'User name'
|
392
|
+
option :auth_key_id, type: :string, required: true, desc: 'auth key id starting "keyId-"'
|
393
|
+
def get_auth_key
|
394
|
+
client = Soracom::Client.new(profile:options.profile)
|
395
|
+
data = client.get_users_auth_key(options.user_name, options.auth_key_id)
|
396
|
+
puts JSON.pretty_generate(data)
|
397
|
+
end
|
398
|
+
|
399
|
+
desc 'delete_password', 'delete existing password for user'
|
400
|
+
option :user_name, desc: 'User name'
|
401
|
+
def delete_password
|
402
|
+
client = Soracom::Client.new(profile:options.profile)
|
403
|
+
data = client.delete_user_password(options.user_name)
|
404
|
+
puts JSON.pretty_generate(data)
|
405
|
+
end
|
406
|
+
|
407
|
+
desc 'has_password', 'check if the user has password'
|
408
|
+
option :user_name, desc: 'User name'
|
409
|
+
def has_password
|
410
|
+
client = Soracom::Client.new(profile:options.profile)
|
411
|
+
data = client.has_user_password(options.user_name)
|
412
|
+
puts JSON.pretty_generate(data)
|
413
|
+
end
|
414
|
+
|
415
|
+
desc 'create_password', 'create passwod for the user'
|
416
|
+
option :user_name, desc: 'User name'
|
417
|
+
option :password, desc: 'Password for the user'
|
418
|
+
def create_password
|
419
|
+
client = Soracom::Client.new(profile:options.profile)
|
420
|
+
data = client.create_user_password(options.user_name, options.password)
|
421
|
+
puts JSON.pretty_generate(data)
|
422
|
+
end
|
423
|
+
|
424
|
+
desc 'get_permission', 'get permission for the user'
|
425
|
+
option :user_name, desc: 'User name'
|
426
|
+
def get_permission
|
427
|
+
client = Soracom::Client.new(profile:options.profile)
|
428
|
+
data = client.get_user_permission(options.user_name)
|
429
|
+
puts JSON.pretty_generate(data)
|
430
|
+
end
|
431
|
+
|
432
|
+
desc 'upadte_permission', 'update permission for the user'
|
433
|
+
option :user_name, desc: 'User name'
|
434
|
+
option :permission, required:true, desc: 'Permission string for the user'
|
435
|
+
option :description, desc: 'Description for the permission'
|
436
|
+
def update_permission
|
437
|
+
client = Soracom::Client.new(profile:options.profile)
|
438
|
+
data = client.update_user_permission(options.user_name, options.permission, options.description)
|
439
|
+
puts JSON.pretty_generate(data)
|
440
|
+
end
|
441
|
+
|
442
|
+
end
|
443
|
+
|
444
|
+
# Role related commands
|
445
|
+
class Role < Thor
|
446
|
+
desc 'list_roles', 'list roles'
|
447
|
+
def list_roles
|
448
|
+
client = Soracom::Client.new(profile:options.profile)
|
449
|
+
data = client.list_roles()
|
450
|
+
puts JSON.pretty_generate(data)
|
451
|
+
end
|
452
|
+
|
453
|
+
desc 'delete_roles', 'delete role'
|
454
|
+
option :role_id, required: true, desc: 'ID of the role'
|
455
|
+
def delete_role
|
456
|
+
client = Soracom::Client.new(profile:options.profile)
|
457
|
+
data = client.delete_role()
|
458
|
+
puts JSON.pretty_generate(data)
|
459
|
+
end
|
460
|
+
|
461
|
+
desc 'get_role', 'get information of role'
|
462
|
+
option :role_id, required: true, desc: 'ID of the role'
|
463
|
+
def get_role
|
464
|
+
client = Soracom::Client.new(profile:options.profile)
|
465
|
+
data = client.get_role(options.role_id)
|
466
|
+
puts JSON.pretty_generate(data)
|
467
|
+
end
|
468
|
+
|
469
|
+
desc 'create_role', 'create a role'
|
470
|
+
option :role_id, required: true, desc: 'ID of the role'
|
471
|
+
option :permission, required: true, desc: 'Permission for the role'
|
472
|
+
option :description, desc: 'Descroption for the role'
|
473
|
+
def create_role
|
474
|
+
client = Soracom::Client.new(profile:options.profile)
|
475
|
+
data = client.create_role(options.role_id, options.permission, options.description)
|
476
|
+
puts JSON.pretty_generate(data)
|
477
|
+
end
|
478
|
+
|
479
|
+
desc 'update_role', 'update existing role'
|
480
|
+
option :role_id, required: true, desc: 'ID of the role'
|
481
|
+
option :permission, required: true, desc: 'Permission for the role'
|
482
|
+
option :description, desc: 'Descroption for the role'
|
483
|
+
def update_role
|
484
|
+
client = Soracom::Client.new(profile:options.profile)
|
485
|
+
data = client.update_role(options.role_id, options.permission, options.description)
|
486
|
+
puts JSON.pretty_generate(data)
|
487
|
+
end
|
488
|
+
|
489
|
+
desc 'list_role_attached_users', 'list users having a role attached'
|
490
|
+
def list_role_attached_users
|
491
|
+
client = Soracom::Client.new(profile:options.profile)
|
492
|
+
data = client.list_role_attached_users
|
493
|
+
puts JSON.pretty_generate(data)
|
494
|
+
end
|
495
|
+
|
496
|
+
desc 'list_user_roles', 'list roles attached to a user'
|
497
|
+
option :user_name, required: true, desc: 'user name'
|
498
|
+
def list_user_roles
|
499
|
+
client = Soracom::Client.new(profile:options.profile)
|
500
|
+
data = client.list_user_roles(options.user_name)
|
501
|
+
puts JSON.pretty_generate(data)
|
502
|
+
end
|
503
|
+
|
504
|
+
desc 'attach_role_to_user', 'attach role to user'
|
505
|
+
option :user_name, required: true, desc: 'user name'
|
506
|
+
option :role_id, required: true, desc: 'role_id'
|
507
|
+
def attach_role_to_user
|
508
|
+
client = Soracom::Client.new(profile:options.profile)
|
509
|
+
data = client.attach_role_to_user(options.user_name, options.role_id)
|
510
|
+
puts JSON.pretty_generate(data)
|
511
|
+
end
|
512
|
+
|
513
|
+
desc 'delete_role_from_user', 'delete role from user'
|
514
|
+
option :user_name, required: true, desc: 'user name'
|
515
|
+
option :role_id, required: true, desc: 'role_id'
|
516
|
+
def delete_role_from_user
|
517
|
+
client = Soracom::Client.new(profile:options.profile)
|
518
|
+
data = client.delete_role_from_user(options.user_name, options.role_id)
|
519
|
+
puts JSON.pretty_generate(data)
|
520
|
+
end
|
521
|
+
|
522
|
+
end
|
523
|
+
|
524
|
+
class Credentials < Thor
|
525
|
+
desc 'list_credentials', 'list stored credentials'
|
526
|
+
def list_credentials
|
527
|
+
client = Soracom::Client.new
|
528
|
+
data = client.list_credentials
|
529
|
+
puts JSON.pretty_generate(data)
|
530
|
+
end
|
531
|
+
|
532
|
+
desc 'create_credentials', 'create new credentials'
|
533
|
+
option :credentials_id, type: :string, required: true, desc: 'Credentials Id'
|
534
|
+
option :credentials_body, type: :string, required: true, desc: 'Credentials Body as JSON string'
|
535
|
+
def create_credentials
|
536
|
+
if options.credentials_body=~/^[a-z]+:\/\/(.+)/
|
537
|
+
begin
|
538
|
+
content = open(options.credentials_body.sub(/^file:\/\//,'')).read
|
539
|
+
options['credentials_body'] = content
|
540
|
+
rescue Errno::ENOENT
|
541
|
+
rescue SocketError
|
542
|
+
abort "ERROR: Cannot access #{options.credentials_body}."
|
543
|
+
end
|
544
|
+
end
|
545
|
+
client = Soracom::Client.new
|
546
|
+
data = client.create_credentials(options.credentials_id, options.credentials_body)
|
547
|
+
puts JSON.pretty_generate(data)
|
548
|
+
end
|
549
|
+
|
550
|
+
desc 'delete_credentials', 'delete credentials'
|
551
|
+
option :credentials_id, type: :string, required: true, desc: 'credentials Id'
|
552
|
+
def delete_credentials
|
282
553
|
client = Soracom::Client.new
|
283
|
-
data = client.
|
554
|
+
data = client.delete_credentials(options.credentials_id)
|
284
555
|
puts JSON.pretty_generate(data)
|
285
556
|
end
|
286
557
|
end
|
287
558
|
|
288
559
|
# Using Thor for CLI Implementation
|
289
560
|
class CLI < Thor
|
561
|
+
class_option :profile, default: 'default', desc: 'profile to use, stored in $HOME/.soracom/PROFILE.json'
|
562
|
+
|
290
563
|
register(Subscriber, 'subscriber', 'subscriber <command>', 'Subscriber related operations')
|
291
564
|
register(Subscriber, 'sim', 'sim <command>', 'Subscriber related operations(alias)')
|
292
565
|
register(Group, 'group', 'group <command>', 'Group related operations')
|
293
566
|
register(EventHandler, 'event_handler', 'event_handler <command>', 'Event Handler related operations')
|
294
567
|
register(Stats, 'stats', 'stats <command>', 'Stats related operations')
|
295
568
|
register(Operator, 'operator', 'operator <command>', 'Operator related operations')
|
569
|
+
register(User, 'user', 'user <command>', 'SAM User related operations')
|
570
|
+
register(Role, 'role', 'role <command>', 'Role related operations')
|
571
|
+
register(Credentials, 'credentials', 'credentials <command>', 'Credentials related operations')
|
296
572
|
|
297
573
|
desc 'auth', 'test authentication'
|
298
574
|
def auth
|
299
|
-
puts
|
575
|
+
puts "testing authentication... #{options.profile}"
|
300
576
|
begin
|
301
|
-
client = Soracom::Client.new
|
577
|
+
client = Soracom::Client.new(profile:options.profile)
|
302
578
|
puts <<EOS
|
303
579
|
authentication succeeded.
|
304
580
|
apiKey: #{client.api_key}
|
305
581
|
operatorId: #{client.operator_id}
|
582
|
+
userName: #{client.user_name}
|
306
583
|
token: #{client.token}
|
307
584
|
EOS
|
308
585
|
rescue => evar
|
@@ -310,9 +587,53 @@ EOS
|
|
310
587
|
end
|
311
588
|
end
|
312
589
|
|
590
|
+
desc 'configure', 'setup soracom environment'
|
591
|
+
def configure
|
592
|
+
print <<EOS
|
593
|
+
--- SORACOM CLI setup ---
|
594
|
+
This will create .soracom directory under #{ENV['HOME']} and place '#{options.profile}.json' in it.
|
595
|
+
|
596
|
+
Please select which authentication method to use.
|
597
|
+
|
598
|
+
1. Input AuthKeyId and AuthKey *Recommended*
|
599
|
+
2. Input Operator credentials (Operator Email and Password)
|
600
|
+
3. Input SAM credentials (OperatorId and UserName and Password)
|
601
|
+
|
602
|
+
EOS
|
603
|
+
mode = STDIN.tap{print "select(1-3)> "}.gets.chomp
|
604
|
+
begin
|
605
|
+
Dir.mkdir("#{ENV['HOME']}/.soracom/",0700)
|
606
|
+
rescue Errno::EEXIST
|
607
|
+
end
|
608
|
+
case mode
|
609
|
+
when '1'
|
610
|
+
authKeyId = STDIN.tap{print "authKeyId: "}.gets.chomp
|
611
|
+
authKey = STDIN.tap{print "authKey: "}.noecho(&:gets).tap{print "\n"}.chomp
|
612
|
+
File.open("#{ENV['HOME']}/.soracom/#{options.profile}.json", "w") do |f|
|
613
|
+
f.print JSON.pretty_generate ({authKeyId: authKeyId, authKey: authKey})
|
614
|
+
end
|
615
|
+
when '2'
|
616
|
+
email = STDIN.tap{print "Email: "}.gets.chomp
|
617
|
+
password = STDIN.tap{print "Password: "}.noecho(&:gets).tap{print "\n"}.chomp
|
618
|
+
File.open("#{ENV['HOME']}/.soracom/#{options.profile}.json", "w") do |f|
|
619
|
+
f.print JSON.pretty_generate ({email: email, password: password})
|
620
|
+
end
|
621
|
+
when '3'
|
622
|
+
operatorId = STDIN.tap{print "operatorId: "}.gets.chomp
|
623
|
+
userName = STDIN.tap{print "userName: "}.gets.chomp
|
624
|
+
password = STDIN.tap{print "password: "}.noecho(&:gets).tap{print "\n"}.chomp
|
625
|
+
File.open("#{ENV['HOME']}/.soracom/#{options.profile}.json", "w") do |f|
|
626
|
+
f.print JSON.pretty_generate ({operatorId: operatorId, userName: userName, password: password})
|
627
|
+
end
|
628
|
+
else
|
629
|
+
abort "invalid number."
|
630
|
+
end
|
631
|
+
print "wrote to #{ENV['HOME']}/.soracom/#{options.profile}.json\n"
|
632
|
+
end
|
633
|
+
|
313
634
|
desc 'support', 'open support site'
|
314
635
|
def support
|
315
|
-
client = Soracom::Client.new
|
636
|
+
client = Soracom::Client.new(profile:options.profile)
|
316
637
|
url = client.get_support_url
|
317
638
|
system "open '#{url}' &> /dev/null || ( echo open following URL in your browser. ; echo '#{url}' )"
|
318
639
|
end
|
data/lib/soracom/client.rb
CHANGED
@@ -13,24 +13,35 @@ module Soracom
|
|
13
13
|
class Client
|
14
14
|
# 設定されなかった場合には、環境変数から認証情報を取得
|
15
15
|
def initialize(
|
16
|
-
|
17
|
-
|
18
|
-
|
16
|
+
profile: nil,
|
17
|
+
endpoint: ENV['SORACOM_ENDPOINT'] || API_BASE_URL,
|
18
|
+
email:ENV['SORACOM_EMAIL'], password:ENV['SORACOM_PASSOWRD'],
|
19
|
+
auth_key_id:ENV['SORACOM_AUTH_KEY_ID'], auth_key:ENV['SORACOM_AUTH_KEY'],
|
20
|
+
operator_id:ENV['SORACOM_OPERATOR_ID'], user_name:ENV['SORACOM_USER_NAME']
|
19
21
|
)
|
20
22
|
@log = Logger.new(STDERR)
|
21
23
|
@log.level = ENV['SORACOM_DEBUG'] ? Logger::DEBUG : Logger::WARN
|
24
|
+
@endpoint = endpoint
|
22
25
|
begin
|
23
|
-
if
|
24
|
-
@auth =
|
26
|
+
if profile
|
27
|
+
@auth = auth_by_profile(profile)
|
28
|
+
elsif auth_key_id && auth_key
|
29
|
+
@auth = auth_by_key(auth_key_id, auth_key, @endpoint)
|
25
30
|
elsif email && password
|
26
|
-
@auth =
|
31
|
+
@auth = auth_by_email(email, password, @endpoint)
|
32
|
+
elsif operator_id && user_name && password
|
33
|
+
@auth = auth_by_user(operator_id, user_name, password, @endpoint)
|
27
34
|
else
|
28
|
-
|
35
|
+
@auth = auth_by_profile('default')
|
29
36
|
end
|
30
37
|
rescue => evar
|
31
38
|
abort 'ERROR: ' + evar.to_s
|
32
39
|
end
|
33
|
-
|
40
|
+
if @auth
|
41
|
+
@api = Soracom::ApiClient.new(@auth, @endpoint)
|
42
|
+
else
|
43
|
+
fail 'Could not find any credentials(authKeyId & authKey or email & password or operatorId & userName and password)'
|
44
|
+
end
|
34
45
|
end
|
35
46
|
|
36
47
|
# 特定Operator下のSubscriber一覧を取
|
@@ -233,6 +244,7 @@ module Soracom
|
|
233
244
|
|
234
245
|
# コンフィグパラメータの更新
|
235
246
|
def update_group_configuration(group_id, namespace, params)
|
247
|
+
params = transform_json(params)
|
236
248
|
@api.put(path: "/groups/#{group_id}/configuration/#{namespace}", payload: params)
|
237
249
|
end
|
238
250
|
|
@@ -266,6 +278,7 @@ module Soracom
|
|
266
278
|
|
267
279
|
# イベントハンドラーを新規作成する
|
268
280
|
def create_event_handler(req)
|
281
|
+
req = transform_json req
|
269
282
|
@api.post(path: '/event_handlers', payload: req)
|
270
283
|
end
|
271
284
|
|
@@ -280,8 +293,9 @@ module Soracom
|
|
280
293
|
end
|
281
294
|
|
282
295
|
# イベントハンドラーを更新する
|
283
|
-
def update_event_handler(handler_id,
|
284
|
-
|
296
|
+
def update_event_handler(handler_id, req)
|
297
|
+
req = transform_json req
|
298
|
+
@api.put(path: "/event_handlers/#{handler_id}", payload: req)
|
285
299
|
end
|
286
300
|
|
287
301
|
# Subscriber毎のAir使用状況を得る(デフォルトでは直近1日)
|
@@ -312,15 +326,145 @@ module Soracom
|
|
312
326
|
"https://soracom.zendesk.com/access/jwt?jwt=#{res['token']}&return_to=#{return_to}"
|
313
327
|
end
|
314
328
|
|
315
|
-
def
|
329
|
+
def list_credentials()
|
330
|
+
@api.get(path: '/credentials')
|
331
|
+
end
|
332
|
+
|
333
|
+
def create_credentials(credentials_id, credentials)
|
334
|
+
@api.post(path: "/credentials/#{credentials_id}", payload: credentials)
|
335
|
+
end
|
336
|
+
|
337
|
+
def delete_credentials(credentials_id)
|
338
|
+
@api.delete(path: "/credentials/#{credentials_id}")
|
339
|
+
end
|
340
|
+
|
341
|
+
# SAMユーザー一覧取得
|
342
|
+
def list_users()
|
343
|
+
@api.get(path: "/operators/#{@auth[:operatorId]}/users")
|
344
|
+
end
|
345
|
+
|
346
|
+
# SAMユーザーを削除する
|
347
|
+
def delete_user(username)
|
348
|
+
@api.delete(path: "/operators/#{@auth[:operatorId]}/users/#{username}")
|
349
|
+
end
|
350
|
+
|
351
|
+
# SAMユーザー取得
|
352
|
+
def get_user(username=@auth[:userName])
|
353
|
+
@api.get(path: "/operators/#{@auth[:operatorId]}/users/#{username}")
|
354
|
+
end
|
355
|
+
|
356
|
+
# SAMユーザーを新しく追加する
|
357
|
+
def create_user(username, description='')
|
358
|
+
@api.post(path: "/operators/#{@auth[:operatorId]}/users/#{username}", payload: { description: description })
|
359
|
+
end
|
360
|
+
|
361
|
+
# SAMユーザーを更新する
|
362
|
+
def update_user(username, description='')
|
363
|
+
@api.put(path: "/operators/#{@auth[:operatorId]}/users/#{username}", payload: { description: description })
|
364
|
+
end
|
365
|
+
|
366
|
+
# SAMユーザーのAuthKey一覧取得
|
367
|
+
def list_users_auth_key(username=@auth[:userName])
|
368
|
+
@api.get(path: "/operators/#{@auth[:operatorId]}/users/#{username}/auth_keys")
|
369
|
+
end
|
370
|
+
|
371
|
+
# SAMユーザーのAuthKey生成
|
372
|
+
def generate_users_auth_key(username=@auth[:userName])
|
373
|
+
@api.post(path: "/operators/#{@auth[:operatorId]}/users/#{username}/auth_keys")
|
374
|
+
end
|
375
|
+
|
376
|
+
# SAMユーザーのAuthKey削除
|
377
|
+
def delete_users_auth_key(username, auth_key_id)
|
378
|
+
@api.delete(path: "/operators/#{@auth[:operatorId]}/users/#{username}/auth_keys/#{auth_key_id}")
|
379
|
+
end
|
380
|
+
|
381
|
+
# SAMユーザーのAuthKey取得
|
382
|
+
def get_users_auth_key(username, auth_key_id)
|
383
|
+
@api.get(path: "/operators/#{@auth[:operatorId]}/users/#{username}/auth_keys/#{auth_key_id}")
|
384
|
+
end
|
385
|
+
|
386
|
+
# SAMユーザーのパスワードを削除する
|
387
|
+
def delete_user_password(username=@auth[:userName])
|
388
|
+
@api.delete(path: "/operators/#{@auth[:operatorId]}/users/#{username}/password")
|
389
|
+
end
|
390
|
+
|
391
|
+
# SAMユーザーのパスワードがセットされているかを取得する
|
392
|
+
def has_user_password(username=@auth[:userName])
|
393
|
+
@api.get(path: "/operators/#{@auth[:operatorId]}/users/#{username}/password")
|
394
|
+
end
|
395
|
+
|
396
|
+
# SAMユーザーのパスワードを作成する
|
397
|
+
def create_user_password(username=@auth[:userName], password)
|
398
|
+
@api.post(path: "/operators/#{@auth[:operatorId]}/users/#{username}/password", payload:{password: password})
|
399
|
+
end
|
400
|
+
|
401
|
+
# SAMユーザーの権限設定を取得する
|
402
|
+
def get_user_permission(username=@auth[:userName])
|
403
|
+
@api.get(path: "/operators/#{@auth[:operatorId]}/users/#{username}/permission")
|
404
|
+
end
|
405
|
+
|
406
|
+
# SAMユーザーの権限を更新する
|
407
|
+
def update_user_permission(username=@auth[:userName], permission="", description="")
|
408
|
+
@api.put(path: "/operators/#{@auth[:operatorId]}/users/#{username}/permission", payload:{description: description, permission: permission})
|
409
|
+
end
|
410
|
+
|
411
|
+
# Role一覧取得
|
412
|
+
def list_roles()
|
413
|
+
@api.get(path: "/operators/#{@auth[:operatorId]}/roles")
|
414
|
+
end
|
415
|
+
|
416
|
+
# Role を削除する
|
417
|
+
def delete_role(role_id)
|
418
|
+
@api.delete(path: "/operators/#{@auth[:operatorId]}/roles/#{role_id}")
|
419
|
+
end
|
420
|
+
|
421
|
+
# Role を取得する
|
422
|
+
def get_role(role_id)
|
423
|
+
@api.get(path: "/operators/#{@auth[:operatorId]}/roles/#{role_id}")
|
424
|
+
end
|
425
|
+
|
426
|
+
# Role を新しく追加する
|
427
|
+
def create_role(role_id, permission, description='')
|
428
|
+
@api.post(path: "/operators/#{@auth[:operatorId]}/roles/#{role_id}", payload:{description: description, permission: permission})
|
429
|
+
end
|
430
|
+
|
431
|
+
# Role を編集する
|
432
|
+
def update_role(role_id, permission, description='')
|
433
|
+
@api.put(path: "/operators/#{@auth[:operatorId]}/roles/#{role_id}", payload:{description: description, permission: permission})
|
434
|
+
end
|
435
|
+
|
436
|
+
# Role に紐づくユーザーの一覧を取得する
|
437
|
+
def list_role_attached_users(role_id)
|
438
|
+
@api.get(path: "/operators/#{@auth[:operatorId]}/roles/#{role_id}/users")
|
439
|
+
end
|
440
|
+
|
441
|
+
# SAMユーザーのロール一覧取得
|
442
|
+
def list_user_roles(user_name)
|
443
|
+
@api.get(path: "/operators/#{@auth[:operatorId]}/users/#{user_name}/roles")
|
444
|
+
end
|
445
|
+
|
446
|
+
# SAMユーザーにロールをアタッチ
|
447
|
+
def attach_role_to_user(user_name, role_id)
|
448
|
+
@api.post(path: "/operators/#{@auth[:operatorId]}/users/#{user_name}/roles", payload: {roleId: role_id})
|
449
|
+
end
|
450
|
+
|
451
|
+
# SAMユーザーからロールをデタッチ
|
452
|
+
def delete_role_from_user(user_name, role_id)
|
453
|
+
@api.delete(path: "/operators/#{@auth[:operatorId]}/users/#{user_name}/roles/#{role_id}")
|
454
|
+
end
|
455
|
+
|
456
|
+
# OperatorのAuthKey一覧取得
|
457
|
+
def list_operator_auth_keys()
|
316
458
|
@api.get(path: "/operators/#{@auth[:operatorId]}/auth_keys")
|
317
459
|
end
|
318
460
|
|
319
|
-
|
461
|
+
# OperatorのAuthKey生成
|
462
|
+
def generate_operator_auth_key()
|
320
463
|
@api.post(path: "/operators/#{@auth[:operatorId]}/auth_keys")
|
321
464
|
end
|
322
465
|
|
323
|
-
|
466
|
+
# OperatorのAuthKey削除
|
467
|
+
def delete_operator_auth_key(auth_key_id)
|
324
468
|
@api.delete(path: "/operators/#{@auth[:operatorId]}/auth_keys/#{auth_key_id}")
|
325
469
|
end
|
326
470
|
|
@@ -334,6 +478,11 @@ module Soracom
|
|
334
478
|
@auth[:operatorId]
|
335
479
|
end
|
336
480
|
|
481
|
+
# ユーザ名を取得
|
482
|
+
def user_name
|
483
|
+
@auth[:userName]
|
484
|
+
end
|
485
|
+
|
337
486
|
# トークンを取得
|
338
487
|
def token
|
339
488
|
@auth[:token]
|
@@ -342,7 +491,7 @@ module Soracom
|
|
342
491
|
private
|
343
492
|
|
344
493
|
# authenticate by email and password
|
345
|
-
def
|
494
|
+
def auth_by_email(email, password, endpoint)
|
346
495
|
endpoint = API_BASE_URL if endpoint.nil?
|
347
496
|
res = RestClient.post endpoint + '/auth',
|
348
497
|
{ email: email, password: password },
|
@@ -365,10 +514,49 @@ module Soracom
|
|
365
514
|
Hash[JSON.parse(res.body).map { |k, v| [k.to_sym, v] }]
|
366
515
|
end
|
367
516
|
|
517
|
+
# authenticate by operator_id and user_name and password
|
518
|
+
def auth_by_user(operator_id, user_name, password, endpoint)
|
519
|
+
endpoint = API_BASE_URL if endpoint.nil?
|
520
|
+
res = RestClient.post endpoint + '/auth',
|
521
|
+
{ operatorId: operator_id, userName: user_name, password: password },
|
522
|
+
'Content-Type' => 'application/json',
|
523
|
+
'Accept' => 'application/json'
|
524
|
+
result = JSON.parse(res.body)
|
525
|
+
fail result['message'] if res.code != '200'
|
526
|
+
Hash[JSON.parse(res.body).map { |k, v| [k.to_sym, v] }]
|
527
|
+
end
|
528
|
+
|
529
|
+
def auth_by_profile(profile)
|
530
|
+
profile_string = open( "#{ENV['HOME']}/.soracom/#{profile}.json").read
|
531
|
+
profile_data = JSON.parse(profile_string)
|
532
|
+
@endpoint = profile_data.fetch('endpoint', @endpoint)
|
533
|
+
if profile_data['authKeyId'] && profile_data['authKey']
|
534
|
+
@auth = auth_by_key(profile_data['authKeyId'], profile_data['authKey'], @endpoint)
|
535
|
+
elsif profile_data['email'] && profile_data['password']
|
536
|
+
@auth = auth_by_email(profile_data['email'], profile_data['password'], @endpoint)
|
537
|
+
elsif profile_data['operatorId'] && profile_data['userName'] && profile_data['password']
|
538
|
+
@auth = auth_by_user(profile_data['operatorId'], profile_data['userName'], profile_data['password'], @endpoint)
|
539
|
+
else
|
540
|
+
return nil
|
541
|
+
end
|
542
|
+
end
|
543
|
+
|
368
544
|
def extract_jwt(jwt)
|
369
545
|
encoded = jwt.split('.')[1]
|
370
546
|
encoded += '=' * (4 - encoded.length % 4) # add padding(=) for Base64
|
371
547
|
Base64.decode64(encoded)
|
372
548
|
end
|
549
|
+
|
550
|
+
def transform_json(json)
|
551
|
+
begin
|
552
|
+
target=JSON.parse(json)
|
553
|
+
if target.class == Hash
|
554
|
+
target = target.map{|k,v| {"key" => k, "value" => v}}
|
555
|
+
end
|
556
|
+
rescue JSON::ParserError
|
557
|
+
abort("ERROR: parameter cannot be parsed as JSON.")
|
558
|
+
end
|
559
|
+
JSON.pretty_generate target
|
560
|
+
end
|
373
561
|
end
|
374
562
|
end
|
data/lib/soracom/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: soracom
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- MATSUI, Motokatsu
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-01-
|
11
|
+
date: 2016-01-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|