class WavefrontCli::Subcommand::Import

Stuff to import an object

Attributes

options[R]
wf[R]

Public Class Methods

new(calling_class, options) click to toggle source
# File lib/wavefront-cli/subcommands/import.rb, line 13
def initialize(calling_class, options)
  @calling_class = calling_class
  @wf = calling_class.wf
  @options = options
  @message = 'IMPORTED'
end

Public Instance Methods

run!() click to toggle source
# File lib/wavefront-cli/subcommands/import.rb, line 20
def run!
  errs = 0

  [raw_input].flatten.each do |obj|
    resp = import_object(obj)
    next if options[:noop]

    errs += 1 unless resp.ok?
    puts import_message(obj, resp)
  end

  exit errs
end

Private Instance Methods

import_message(obj, resp) click to toggle source
# File lib/wavefront-cli/subcommands/import.rb, line 40
def import_message(obj, resp)
  format('%-15<id>s %-10<status>s %<message>s',
         id: obj[:id] || obj[:url],
         status: resp.ok? ? @message : 'FAILED',
         message: resp.status.message)
end
import_object(raw) click to toggle source
# File lib/wavefront-cli/subcommands/import.rb, line 47
def import_object(raw)
  raw = preprocess_rawfile(raw) if respond_to?(:preprocess_rawfile)
  prepped = @calling_class.import_to_create(raw)

  if options[:upsert]
    import_upsert(raw, prepped)
  elsif options[:update]
    @message = 'UPDATED'
    import_update(raw)
  else
    wf.create(prepped)
  end
end
import_update(raw) click to toggle source
# File lib/wavefront-cli/subcommands/import.rb, line 73
def import_update(raw)
  wf.update(raw[:id], raw, false)
end
import_upsert(raw, prepped) click to toggle source
# File lib/wavefront-cli/subcommands/import.rb, line 61
def import_upsert(raw, prepped)
  update_call = import_update(raw)

  if update_call.ok?
    @message = 'UPDATED'
    return update_call
  end

  puts 'update failed, inserting' if options[:verbose] || options[:debug]
  wf.create(prepped)
end
raw_input() click to toggle source
# File lib/wavefront-cli/subcommands/import.rb, line 36
def raw_input
  WavefrontCli::Helper::LoadFile.new(options[:'<file>']).load
end