Class: EnfCli::Cmd::Captive

Inherits:
EnfThor
  • Object
show all
Defined in:
lib/enfcli/commands/captive.rb

Instance Method Summary collapse

Methods inherited from EnfThor

capture_stdout, command_help, handle_argument_error, help

Instance Method Details

#create_deviceObject



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/enfcli/commands/captive.rb', line 157

def create_device
  try_with_rescue_in_session do
    new_device_hash = {
      serial_number: options[:'device-id'],
    }

    mac1 = options[:'mac-addr1']
    mac2 = options[:'mac-addr2']
    mac3 = options[:'mac-addr3']
    mac4 = options[:'mac-addr4']

    if mac1 or mac2 or mac3 or mac4
      mac_hash = {}
      mac_hash[1] = mac1 if mac1
      mac_hash[2] = mac2 if mac2
      mac_hash[3] = mac3 if mac3
      mac_hash[4] = mac4 if mac4
      new_device_hash[:mac_address] = mac_hash
    end

    dev_name = options[:'device-name']
    profile_id = options[:profile_id]
    new_device_hash[:device_name] = dev_name if dev_name
    if profile_id
      profile_hash = {}
      profile_hash[:id] = profile_id
      new_device_hash[:profile] = profile_hash
    end

    model = options[:model]
    new_device_hash[:model] = model if model

    # send the new device request
    device_data = EnfApi::Captive.instance.create_device new_device_hash

    # display the response
    display_device device_data
  end
end

#create_firmware_updateObject



573
574
575
576
577
578
579
580
581
582
583
584
# File 'lib/enfcli/commands/captive.rb', line 573

def create_firmware_update
  try_with_rescue_in_session do
    filename = options[:update]

    # reading the whole file - shouldn't get more than a few KB
    content = File.read filename
    update_hash = JSON.parse(content)

    resp_data = EnfApi::Captive.instance.create_firmware_update update_hash
    display_update_detail resp_data
  end
end

#create_profileObject



286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
# File 'lib/enfcli/commands/captive.rb', line 286

def create_profile
  try_with_rescue_in_session do
    profile_name = options[:'profile-name']
    new_profile_hash = {
      name: profile_name,
      config: { mode: options[:'device-mode'] },
    }

    wifi = options[:'wifi-id']
    if wifi
      new_profile_hash[:config][:wifi] = {}
      new_profile_hash[:config][:wifi][:id] = wifi
    end

    fw_update = options[:'update-id']
    if fw_update
      new_profile_hash[:config][:firmware] = {}
      new_profile_hash[:config][:firmware][:id] = fw_update if fw_update
    end

    # send the POST to create a new profile
    profile = EnfApi::Captive.instance.create_profile new_profile_hash
    display_profile profile
  end
end

#create_scheduleObject



457
458
459
460
461
462
463
464
465
466
467
468
# File 'lib/enfcli/commands/captive.rb', line 457

def create_schedule
  try_with_rescue_in_session do
    filename = options[:schedule]

    # reading the whole file - shouldn't get more than a few KB
    content = File.read filename
    sched_hash = JSON.parse(content)

    resp_data = EnfApi::Captive.instance.create_schedule sched_hash
    display_schedule_detail resp_data
  end
end

#create_wifi_configurationObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/enfcli/commands/captive.rb', line 54

def create_wifi_configuration
  try_with_rescue_in_session do
    json_file_name = options[:'wifi-config-file']

    # read in the entire file - yes it is slurping the file, but it should
    # never be bigger than a few KB.
    content = File.read json_file_name
    json_hash = JSON.parse(content)

    resp_data = EnfApi::Captive.instance.create_wifi_configuration json_hash

    # display the data
    display_wifi_detail resp_data
  end
end

#delete_scheduleObject



522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
# File 'lib/enfcli/commands/captive.rb', line 522

def delete_schedule
  try_with_rescue_in_session do
    sched_id = options[:'schedule-id']

    # get the name of the schedule
    resp = EnfApi::Captive.instance.get_schedule(sched_id)
    name = resp[:name]

    resp = EnfApi::Captive.instance.delete_schedule sched_id

    if (resp.code == 200)
      say "Successfully deleted schedule named: #{name}"
    elsif (resp.code == 409)
      say "Schedule #{name} is being used by pending updates and cannot be deleted."
    else
      say "Failed to delete schedule #{name} with code #{resp.code}"
    end
  end
end

#get_deviceObject



217
218
219
220
221
222
223
224
225
# File 'lib/enfcli/commands/captive.rb', line 217

def get_device
  try_with_rescue_in_session do
    device_id = ERB::Util::url_encode(options[:'device-id'])

    data = EnfApi::Captive.instance.get_device device_id

    display_device data
  end
end

#get_device_statusObject



262
263
264
265
266
267
268
269
# File 'lib/enfcli/commands/captive.rb', line 262

def get_device_status
  try_with_rescue_in_session do
    device_id = ERB::Util::url_encode(options[:'device-id'])
    status = EnfApi::Captive.instance.get_device_status device_id

    display_device_status status
  end
end

#get_firmware_infoObject



439
440
441
442
443
444
445
446
447
# File 'lib/enfcli/commands/captive.rb', line 439

def get_firmware_info
  try_with_rescue_in_session do
    version = options[:version]

    fw_info = EnfApi::Captive.instance.get_firmware_info version

    display_firmware_detail fw_info
  end
end

#get_firmware_updateObject



557
558
559
560
561
562
563
564
# File 'lib/enfcli/commands/captive.rb', line 557

def get_firmware_update
  try_with_rescue_in_session do
    update_id = options[:'update-id']

    update_data = EnfApi::Captive.instance.get_firmware_update update_id
    display_update_detail update_data
  end
end

#get_profileObject

TODO - server doesn’t support version yet.

method_option :version, default: nil, type: :integer, banner: 'VERSION',
                        desc: 'Get a specific version.'


333
334
335
336
337
338
# File 'lib/enfcli/commands/captive.rb', line 333

def get_profile
  try_with_rescue_in_session do
    profile = EnfApi::Captive.instance.get_profile options[:'profile-id']
    display_profile profile
  end
end

#get_scheduleObject



486
487
488
489
490
491
492
# File 'lib/enfcli/commands/captive.rb', line 486

def get_schedule
  try_with_rescue_in_session do
    sched_id = options[:'schedule-id']
    sched_data = EnfApi::Captive.instance.get_schedule sched_id
    display_schedule_detail sched_data
  end
end

#get_wifi_configurationObject



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/enfcli/commands/captive.rb', line 85

def get_wifi_configuration
  try_with_rescue_in_session do
    ## TODO - Currently, this only handles wifi-id, add the others to v2

    wifi_id = options[:'wifi-id']
    profile_id = options[:'profile-id']
    device_id = options[:'device-id']
    wifi_name = options[:'wifi-name']
    version = options[:version]

    num_query_opts = 0
    num_query_opts += 1 if wifi_id
    num_query_opts += 1 if profile_id
    num_query_opts += 1 if device_id
    num_query_opts += 1 if wifi_name

    raise "ERROR: Exactly one of: --wifi-id, --profile-id, --device-id, or --wifi-name must be specified." if num_query_opts != 1

    if wifi_id
      wifi_config = EnfApi::Captive.instance.get_wifi_configuration wifi_id, version

      # display the data
      display_wifi_detail wifi_config
    end
  end
end

#list_devicesObject



202
203
204
205
206
207
208
209
# File 'lib/enfcli/commands/captive.rb', line 202

def list_devices
  try_with_rescue_in_session do
    network = options[:network]
    data = EnfApi::Captive.instance.list_devices network

    display_device_list data
  end
end

#list_firmware_imagesObject



390
391
392
393
394
395
# File 'lib/enfcli/commands/captive.rb', line 390

def list_firmware_images
  try_with_rescue_in_session do
    data = EnfApi::Captive.instance.list_firmware_images
    display_firmware_image_list data
  end
end

#list_firmware_updatesObject



545
546
547
548
549
550
# File 'lib/enfcli/commands/captive.rb', line 545

def list_firmware_updates
  try_with_rescue_in_session do
    updates_list = EnfApi::Captive.instance.list_firmware_updates
    display_updates_list updates_list
  end
end

#list_profilesObject



317
318
319
320
321
322
323
# File 'lib/enfcli/commands/captive.rb', line 317

def list_profiles
  try_with_rescue_in_session do
    query_name = options[:name]
    profiles = EnfApi::Captive.instance.list_profiles query_name
    display_profile_list profiles
  end
end

#list_schedulesObject



473
474
475
476
477
478
# File 'lib/enfcli/commands/captive.rb', line 473

def list_schedules
  try_with_rescue_in_session do
    sched_list = EnfApi::Captive.instance.list_schedules
    display_schedule_list sched_list
  end
end

#list_wifi_configurationsObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/enfcli/commands/captive.rb', line 34

def list_wifi_configurations
  try_with_rescue_in_session do
    ## TODO: V1 is only listing all of the wifi configurations, it is not
    ## using the options - add the queries based on the options given.
    wifi_configs = EnfApi::Captive.instance.list_wifi_configurations

    if wifi_configs.class == Array
      # display the data
      display_wifi_configs wifi_configs
    else
      say "No WiFi configurations found."
    end
  end
end

#modify_firmware_updateObject



595
596
597
598
599
600
601
602
603
604
605
606
607
# File 'lib/enfcli/commands/captive.rb', line 595

def modify_firmware_update
  try_with_rescue_in_session do
    update_id = options[:'update-id']
    filename = options[:update]

    # reading the whole file - shouldn't get more than a few KB
    content = File.read filename
    update_hash = JSON.parse(content)

    resp_data = EnfApi::Captive.instance.modify_firmware_update update_id, update_hash
    display_update_detail resp_data
  end
end

#update_deviceObject



236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/enfcli/commands/captive.rb', line 236

def update_device
  try_with_rescue_in_session do
    id = ERB::Util::url_encode(options[:'device-id'])
    name = options[:'device-name']
    profile_id = options[:'profile-id']

    raise "At least one option needs to change." if name == nil && profile_id == nil

    update_hash = {}
    update_hash[:device_name] = name if name
    if profile_id
      update_hash[:profile] = {}
      update_hash[:profile][:id] = profile_id
    end

    data = EnfApi::Captive.instance.update_device id, update_hash
    display_device data
  end
end

#update_profileObject



359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
# File 'lib/enfcli/commands/captive.rb', line 359

def update_profile
  try_with_rescue_in_session do
    id = options[:'profile-id']
    name = options[:'profile-name']
    mode = options[:'device-mode']
    wifi_id = options[:'wifi-id']
    fw_update = options[:'update-id']

    raise "At least one option needs to change." if name == nil && mode == nil && wifi_id == nil && fw_update == nil

    update_hash = {}
    update_hash[:name] = name if name
    update_hash[:config] = {} if wifi_id || mode || fw_update
    update_hash[:config][:mode] = mode if mode
    if wifi_id
      update_hash[:config][:wifi] = {}
      update_hash[:config][:wifi][:id] = wifi_id
    end

    if fw_update
      update_hash[:config][:firmware] = {}
      update_hash[:config][:firmware][:id] = fw_update
    end
    profile = EnfApi::Captive.instance.update_profile id, update_hash
    display_profile profile
  end
end

#update_scheduleObject



502
503
504
505
506
507
508
509
510
511
512
513
514
# File 'lib/enfcli/commands/captive.rb', line 502

def update_schedule
  try_with_rescue_in_session do
    sched_id = options[:'schedule-id']
    filename = options[:schedule]

    # read in whole schedule file
    content = File.read filename
    sched_hash = JSON.parse(content)

    resp_data = EnfApi::Captive.instance.update_schedule sched_id, sched_hash
    display_schedule_detail resp_data
  end
end

#update_wifi_configurationObject



125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/enfcli/commands/captive.rb', line 125

def update_wifi_configuration
  try_with_rescue_in_session do
    json_file_name = options[:'wifi-config-file']
    # read in the entire file - yes it is slurping the file, but it should
    # never be bigger than a few KB.
    content = File.read json_file_name
    json_hash = JSON.parse(content)

    resp_data = EnfApi::Captive.instance.update_wifi_configuration options[:'wifi-id'], json_hash
    display_wifi_detail resp_data
  end
end

#upload_firmware_imageObject



412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
# File 'lib/enfcli/commands/captive.rb', line 412

def upload_firmware_image
  try_with_rescue_in_session do
    filename = options[:'image-file']
    version = options[:version]
    image_name = options[:'image-name']

    temp_img_name = File.basename(filename)
    image_name ||= temp_img_name

    raise "image-name does not match image-file" if temp_img_name != image_name

    # This version PUTs an empty body
    resp = EnfApi::Captive.instance.upload_firmware_image version, image_name

    if (resp.code == 200)
      say "Upload complete."
    else
      say "Upload failed with code #{resp.code}"
    end
  end
end