Method: Morpheus::Cli::PreseedScriptsCommand#update

Defined in:
lib/morpheus/cli/commands/preseed_scripts_command.rb

#update(args) ⇒ Object



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# File 'lib/morpheus/cli/commands/preseed_scripts_command.rb', line 204

def update(args)
  options = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[preseed-script] [options]")
    build_option_type_options(opts, options, update_preseed_script_option_types(false))
    build_common_options(opts, options, [:options, :json, :dry_run])
  end
  optparse.parse!(args)
  if args.count < 1
    puts optparse
    return 1
  end
  connect(options)

  begin
    preseed_script = find_preseed_script_by_name_or_id(args[0])

    payload = {
      'preseedScript' => {id: preseed_script["id"]}
    }

    params = options[:options] || {}
    #puts "parsed params is : #{params.inspect}"
    params = params.select {|k,v| params[k].to_s != "" }
    if params.empty?
      print_red_alert "Specify at least one option to update"
      puts optparse
      return 1
    end

    # prompt for Script Content unless --file is passed.
    # my_options = add_preseed_script_option_types()
    # if options[:options]['file']
    #   my_options = my_options.reject {|it| it['fieldName'] == 'content' }
    # # elsif options[:options]['content']
    # #   my_options = my_options.reject {|it| it['fieldName'] == 'file' }
    # else
    #   my_options = my_options.reject {|it| it['fieldName'] == 'file' }
    # end
    # params = Morpheus::Cli::OptionTypes.prompt(my_options, options[:options], @api_client, options[:params])
    script_file = params.delete('file')
    if script_file
      if !File.exist?(script_file)
        print_red_alert "File not found: #{script_file}"
        return 1
      end
      payload['preseedScript']['content'] = File.read(script_file)
    end
    payload['preseedScript'].merge!(params)
    @preseed_scripts_interface.setopts(options)
    if options[:dry_run]
      print_dry_run @preseed_scripts_interface.dry.update(preseed_script["id"], payload)
      return
    end

    json_response = @preseed_scripts_interface.update(preseed_script["id"], payload)
    if options[:json]
      print JSON.pretty_generate(json_response)
      print "\n"
    else
      print_green_success "Updated preseed script #{preseed_script['fileName']}"
      get([preseed_script['id']])
    end
    return 0
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    return 1
  end
end