Class: Emasser::Artifacts

Inherits:
SubCommandBase show all
Defined in:
lib/emasser/get.rb,
lib/emasser/put.rb,
lib/emasser/post.rb,
lib/emasser/delete.rb

Overview

Remove one or many artifacts in a system

Endpoint:

/api/systems/{systemId}/artifacts - Delete one or more artifacts (files) from a system

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from SubCommandBase

banner

Methods included from OutputConverters

#to_output_hash

Methods included from InputConverters

#to_input_hash

Methods included from OptionsParser

#optional_options, #required_options

Class Method Details

.exit_on_failure?Boolean

Returns:

  • (Boolean)


392
393
394
# File 'lib/emasser/get.rb', line 392

def self.exit_on_failure?
  true
end

Instance Method Details

#exportObject

NOTE: compress is a required parameter, however Thor does not allow a boolean type to be required because it automatically creates a –no-compress option, which is confusing in the help output:

[--compress], [--no-compress]  # BOOLEAN - true or false.


431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
# File 'lib/emasser/get.rb', line 431

def export
  optional_options_keys = optional_options(@_initializer).keys
  optional_options = to_input_hash(optional_options_keys, options)
  optional_options.merge!(Emasser::GET_ARTIFACTS_RETURN_TYPE)

  result = EmassClient::ArtifactsExportApi.new.get_system_artifacts_export(
    options[:systemId], options[:filename], optional_options
  )
  if options[:compress]
    p result.green
  else
    begin
      puts JSON.pretty_generate(JSON.parse(result)).green
    rescue StandardError
      puts result.red
    end
  end
rescue EmassClient::ApiError => e
  puts 'Exception when calling ArtifactsApi->get_system_artifacts_export'.red
  puts to_output_hash(e)
end

#forSystemObject



406
407
408
409
410
411
412
413
414
415
416
417
418
419
# File 'lib/emasser/get.rb', line 406

def forSystem
  optional_options_keys = optional_options(@_initializer).keys
  optional_options = to_input_hash(optional_options_keys, options)

  begin
    # Get one or many artifacts in a system
    result = EmassClient::ArtifactsApi.new.get_system_artifacts(options[:systemId],
                                                                optional_options)
    puts to_output_hash(result).green
  rescue EmassClient::ApiError => e
    puts 'Exception when calling ArtifactsApi->get_system_artifacts'.red
    puts to_output_hash(e)
  end
end

#removeObject



120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/emasser/delete.rb', line 120

def remove
  body_array = []
  options[:files].each do |file|
    obj = {}
    obj[:filename] = file
    body_array << obj
  end

  result = EmassClient::ArtifactsApi.new.delete_artifact(body_array, options[:systemId])
  puts to_output_hash(result).green
rescue EmassClient::ApiError => e
  puts 'Exception when calling ArtifactsApi->delete_artifact'.red
  puts to_output_hash(e)
end

#updateObject

rubocop:disable Metrics/CyclomaticComplexity



541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
# File 'lib/emasser/put.rb', line 541

def update
  body = EmassClient::ArtifactsRequestPutBody.new
  body.filename = options[:filename]
  body.type = options[:type]
  body.category = options[:category]
  body.is_template = options[:isTemplate]
  # Optional fields
  body.description = options[:description] if options[:description]
  body.ref_page_number = options[:refPageNumber] if options[:refPageNumber]
  body.ccis = options[:ccis] if options[:ccis]
  body.controls = options[:controls] if options[:controls]
  body.artifact_expiration_date = options[:artifactExpirationDate] if options[:artifactExpirationDate]
  body.last_reviewed_date = options[:lastReviewedDate] if options[:lastReviewedDate]

  body_array = Array.new(1, body)

  begin
    result = EmassClient::ArtifactsApi.new.update_artifact_by_system_id(body_array, options[:systemId])
    puts to_output_hash(result).green
  rescue EmassClient::ApiError => e
    puts 'Exception when calling ArtifactsApi->update_artifact_by_system_id'.red
    puts to_output_hash(e)
  end
end

#uploadObject



375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
# File 'lib/emasser/post.rb', line 375

def upload
  optional_options_keys = optional_options(@_initializer).keys
  optional_options = to_input_hash(optional_options_keys, options)
  # Remove the isTemplate as we can't use the required = true.
  optional_options.delete(:is_template)

  opts = {}
  opts[:form_params] = optional_options

  tempfile = Tempfile.create(['artifacts', '.zip'])

  Zip::OutputStream.open(tempfile.path) do |z|
    options[:files].each do |file|
      # Add file name to the archive: Don't use the full path
      z.put_next_entry(File.basename(file))
      # Add the file to the archive
      z.print File.read(file)
    end
  end

  begin
    result = EmassClient::ArtifactsApi
             .new
             .add_artifacts_by_system_id(options[:isTemplate], options[:type],
                                         options[:category], tempfile, options[:systemId], opts)
    puts to_output_hash(result).green
  rescue EmassClient::ApiError => e
    puts 'Exception when calling ArtifactsApi->add_artifacts_by_system_id'.red
    puts to_output_hash(e)
  ensure
    # Delete the temp file
    unless File.exist? tempfile
      tempfile.close
      FileUtils.remove_file(tempfile, true)
    end
  end
end