Class: Emasser::Artifacts
- Inherits:
-
SubCommandBase
- Object
- Thor
- SubCommandBase
- Emasser::Artifacts
- 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
-
#export ⇒ Object
rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity.
- #forSystem ⇒ Object
- #remove ⇒ Object
-
#update ⇒ Object
rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity.
- #upload ⇒ Object
Methods inherited from SubCommandBase
Methods included from OutputConverters
#change_to_datetime, #to_output_hash
Methods included from InputConverters
Methods included from OptionsParser
#optional_options, #required_options
Class Method Details
.exit_on_failure? ⇒ Boolean
404 405 406 |
# File 'lib/emasser/get.rb', line 404 def self.exit_on_failure? true end |
Instance Method Details
#export ⇒ Object
rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 |
# File 'lib/emasser/get.rb', line 447 def export = (@_initializer).keys = to_input_hash(, ) if [:printToStdout] .merge!(Emasser::GET_ARTIFACTS_RETURN_TYPE) if [:compress] puts 'Output to stdout - compress'.yellow else puts 'Output to stdout - plain text'.yellow end else # The api method get_system_artifacts_export default return type is # file, and it will output the file to the configured EMASSER_DOWNLOAD_DIR # or to the default output folder 'eMASSerDownloads' export_dir = ENV.fetch('EMASSER_DOWNLOAD_DIR', '') export_dir = 'eMASSerDownloads' if export_dir.empty? puts "Output to #{export_dir} directory".yellow end result = EmassClient::ArtifactsExportApi.new.get_system_artifacts_export( [:systemId], [:filename], ) unless File.extname([:filename]).eql? '.zip' # rubocop:disable Style/SoleNestedConditional, Metrics/BlockNesting if [:printToStdout] if [:compress] puts result.green else begin puts JSON.pretty_generate(JSON.parse(result)).green rescue StandardError puts result.red end end end end # rubocop:enable Style/SoleNestedConditional, Metrics/BlockNesting rescue EmassClient::ApiError => e puts 'Exception when calling ArtifactsApi->get_system_artifacts_export'.red puts to_output_hash(e) end |
#forSystem ⇒ Object
420 421 422 423 424 425 426 427 428 429 430 431 432 433 |
# File 'lib/emasser/get.rb', line 420 def forSystem = (@_initializer).keys = to_input_hash(, ) begin # Get one or many artifacts in a system result = EmassClient::ArtifactsApi.new.get_system_artifacts([:systemId], ) 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 |
#remove ⇒ Object
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 = [] [:files].each do |file| obj = {} obj[:filename] = file body_array << obj end result = EmassClient::ArtifactsApi.new.delete_artifact([:systemId], body_array) puts to_output_hash(result).green rescue EmassClient::ApiError => e puts 'Exception when calling ArtifactsApi->delete_artifact'.red puts to_output_hash(e) end |
#update ⇒ Object
rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 |
# File 'lib/emasser/put.rb', line 580 def update require_fields = EmassClient::ArtifactsRequiredFields.new require_fields.filename = [:filename] require_fields.type = [:type] require_fields.category = [:category] require_fields.is_template = [:isTemplate] # Optional fields optional_fields = EmassClient::ArtifactsOptionalFields.new optional_fields.name = [:name] if [:name] optional_fields.description = [:description] if [:description] optional_fields.reference_page_number = [:referencePageNumber] if [:referencePageNumber] optional_fields.controls = [:controls] if [:controls] optional_fields.assessment_procedures = [:assessmentProcedures] if [:assessmentProcedures] optional_fields.expiration_date = [:expirationDate] if [:expirationDate] optional_fields.last_reviewed_date = [:lastReviewedDate] if [:lastReviewedDate] optional_fields.signed_date = [:signedDate] if [:signedDate] # Build the request body body = {} body = body.merge(require_fields) body = body.merge(optional_fields) body_array = Array.new(1, body) begin result = EmassClient::ArtifactsApi.new.update_artifact_by_system_id([:systemId], body_array) 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 |
#upload ⇒ Object
443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 |
# File 'lib/emasser/post.rb', line 443 def upload = (@_initializer).keys = to_input_hash(, ) # Remove isBulk as it is an options parameter sent to the API. .delete(:is_bulk) # Options contain the default values (type, category, and isTemplate) # They are sent to the API in the form_params option opts = {} opts[:is_bulk] = [:isBulk] opts[:form_params] = # Configure the upload file(s) remove_temp_file = false begin # If we have a single file, could be a zip file if [:files].length == 1 tempfile = File.open([:files][0], 'r') # if we have multiple files zip them into a zip file elsif [:files].length > 1 remove_temp_file = true tempfile = Tempfile.create(['artifacts', '.zip']) Zip::OutputStream.open(tempfile.path) do |z| [: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 else puts 'No file(s) provided!'.yellow end rescue Errno::ENOENT => e warn "File open exception: #{e}".red exit 1 end # Call the API begin result = EmassClient::ArtifactsApi .new .add_artifacts_by_system_id([:systemId], tempfile, 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 # Close the file tempfile.close # Delete the temp file if remove_temp_file FileUtils.remove_file(tempfile, true) end end end |