How to use upload method of InspecPlugins.Compliance Package

Best Inspec_ruby code snippet using InspecPlugins.Compliance.upload

cli.rb

Source:cli.rb Github

copy

Full Screen

...100 puts "Profile #{profile_name} is not available in Chef Compliance."101 exit 1102 end103 end104 desc 'upload PATH', 'uploads a local profile to Chef Compliance'105 option :overwrite, type: :boolean, default: false,106 desc: 'Overwrite existing profile on Server.'107 option :owner, type: :string, required: false,108 desc: 'Owner that should own the profile'109 def upload(path) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize, PerceivedComplexity, Metrics/CyclomaticComplexity110 config = InspecPlugins::Compliance::Configuration.new111 return if !loggedin(config)112 # set owner to config113 config['owner'] = options['owner'] || config['user']114 unless File.exist?(path)115 puts "Directory #{path} does not exist."116 exit 1117 end118 vendor_deps(path, options) if File.directory?(path)119 o = options.dup120 configure_logger(o)121 # only run against the mock backend, otherwise we run against the local system122 o[:backend] = Inspec::Backend.create(Inspec::Config.mock)123 o[:check_mode] = true124 o[:vendor_cache] = Inspec::Cache.new(o[:vendor_cache])125 # check the profile, we only allow to upload valid profiles126 profile = Inspec::Profile.for_target(path, o)127 # start verification process128 error_count = 0129 error = lambda { |msg|130 error_count += 1131 puts msg132 }133 result = profile.check134 unless result[:summary][:valid]135 error.call('Profile check failed. Please fix the profile before upload.')136 else137 puts('Profile is valid')138 end139 # determine user information140 if (config['token'].nil? && config['refresh_token'].nil?) || config['user'].nil?141 error.call('Please login via `inspec compliance login`')142 end143 # read profile name from inspec.yml144 profile_name = profile.params[:name]145 # read profile version from inspec.yml146 profile_version = profile.params[:version]147 # check that the profile is not uploaded already,148 # confirm upload to the user (overwrite with --force)149 if InspecPlugins::Compliance::API.exist?(config, "#{config['owner']}/#{profile_name}##{profile_version}") && !options['overwrite']150 error.call('Profile exists on the server, use --overwrite')151 end152 # abort if we found an error153 if error_count > 0154 puts "Found #{error_count} error(s)"155 exit 1156 end157 # if it is a directory, tar it to tmp directory158 generated = false159 if File.directory?(path)160 generated = true161 archive_path = Dir::Tmpname.create([profile_name, '.tar.gz']) {}162 puts "Generate temporary profile archive at #{archive_path}"163 profile.archive({ output: archive_path, ignore_errors: false, overwrite: true })164 else165 archive_path = path166 end167 puts "Start upload to #{config['owner']}/#{profile_name}"168 pname = ERB::Util.url_encode(profile_name)169 if InspecPlugins::Compliance::API.is_automate_server?(config) || InspecPlugins::Compliance::API.is_automate2_server?(config)170 puts 'Uploading to Chef Automate'171 else172 puts 'Uploading to Chef Compliance'173 end174 success, msg = InspecPlugins::Compliance::API.upload(config, config['owner'], pname, archive_path)175 # delete temp file if it was temporary generated176 File.delete(archive_path) if generated && File.exist?(archive_path)177 if success178 puts 'Successfully uploaded profile'179 else180 puts 'Error during profile upload:'181 puts msg182 exit 1183 end184 end185 desc 'version', 'displays the version of the Chef Compliance server'186 def version187 config = InspecPlugins::Compliance::Configuration.new188 info = InspecPlugins::Compliance::API.version(config)189 if !info.nil? && info['version']190 puts "Name: #{info['api']}"191 puts "Version: #{info['version']}"192 else193 puts 'Could not determine server version.'194 exit 1...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful