class AdminModule::Rake::PpmTasks

Attributes

action[R]
env[RW]
format[R]
name[RW]
path[RW]
stop_on_exception[R]
to[RW]
valid_actions[R]

Public Class Methods

install() click to toggle source
# File lib/admin_module/rake/ppm_tasks.rb, line 218
def install
  new.install
end
new(task_name = '', desc = "") { |self| ... } click to toggle source
# File lib/admin_module/rake/ppm_tasks.rb, line 26
def initialize(task_name = '', desc = "")
  @valid_actions = ['import', 'export', 'dups', 'list']
  @task_name, @desc = task_name, desc

  @stop_on_exception = true

  yield self if block_given?

  define_task
end

Public Instance Methods

action=(task_action) click to toggle source
# File lib/admin_module/rake/ppm_tasks.rb, line 57
def action=(task_action)
  raise "action must be one of #{valid_types.join(', ')}" unless valid_actions.include?(task_action.downcase)
  @action = task_action
end
assert_env_is_configured(arg) click to toggle source
# File lib/admin_module/rake/ppm_tasks.rb, line 153
def assert_env_is_configured arg
  unless AdminModule.configuration.credentials.key? arg
    init_msg = "Have you initialized your config file?\n Try: admin_module config init <filedir>"
    env_msg = "Have you configured your environments?\n Try: admin_module config add env <envname> <url>"
    raise "Unknown environment: #{arg}\n#{init_msg}\n\n#{env_msg}"
  end
end
assert_provided(value, msg) click to toggle source
# File lib/admin_module/rake/ppm_tasks.rb, line 147
def assert_provided value, msg
  if value.nil? || value.empty?
    raise msg
  end
end
commit() click to toggle source

Execute the task (action)

# File lib/admin_module/rake/ppm_tasks.rb, line 78
def commit
  validate_params

  client = AdminModule::Client.new
  client.env = env

  if self.respond_to? action
    self.send(action, client)
    return
  else
    raise "Unknown action - #{action}"
  end

rescue Exception => e
  raise e if stop_on_exception == true
ensure
  client.quit
end
dups(client) click to toggle source
# File lib/admin_module/rake/ppm_tasks.rb, line 106
def dups client
  result = Array.new
  result = client.ppms.dups
  output_method = "output_as_#{@format}"

  if self.respond_to? output_method
    self.send(output_method, result)
  else
    $stderr << "Invalid format: #{@format}"
  end
end
export(client) click to toggle source
# File lib/admin_module/rake/ppm_tasks.rb, line 122
def export client
  $stdout << client.ppms.export(path)
end
format=(output_format) click to toggle source
# File lib/admin_module/rake/ppm_tasks.rb, line 67
def format=(output_format)
  output_format = 'txt' if output_format.nil?
  valid_formats = %w(txt csv)
  raise "format must be one of #{valid_formats.join(', ')}" unless valid_formats.include?(output_format.downcase)
  @format = output_format
end
import(client) click to toggle source
# File lib/admin_module/rake/ppm_tasks.rb, line 118
def import client
  $stdout << client.ppms.import(path)
end
install() click to toggle source

Instance method to generate the tasks; For each environment where we have configured credentials, generate a task for each available action.

# File lib/admin_module/rake/ppm_tasks.rb, line 229
def install
  AdminModule.configuration.credentials.keys.each do |e|
    valid_actions.each do |action|
      AdminModule::Rake::PpmTasks.new("am:#{e}:ppm:#{action}", "#{action} #{e} ppms") do |t|
        t.env = e
        t.action = action
      end
    end
  end
end
list(client) click to toggle source

Actions

# File lib/admin_module/rake/ppm_tasks.rb, line 101
def list client
  $stdout << client.ppms.list.join("\n")
  $stdout << "\n"
end
output_as_csv(data) click to toggle source
# File lib/admin_module/rake/ppm_tasks.rb, line 200
def output_as_csv data
  if data.count > 0
    $stdout << "Name,ID\n"
  else
    return
  end

  data.each do |dp|
    $stdout << "#{dp[:name]},#{dp[:id]}\n"
  end
end
output_as_txt(data) click to toggle source
# File lib/admin_module/rake/ppm_tasks.rb, line 187
def output_as_txt data
  if data.count > 0
    $stdout << "        Name                ID\n"
    $stdout << '-'*79 << "\n"
  else
    return
  end

  data.each do |dp|
    $stdout << "#{dp[:name]}\t#{dp[:id]}\n"
  end
end
required_args_for_action() click to toggle source

Define the task args, based on the action. Used when programatically generating the tasks.

# File lib/admin_module/rake/ppm_tasks.rb, line 166
def required_args_for_action
  args = []

  case action
  when 'import'
    args << :path

  when 'export'
    args << :path

  when 'dups'
    args << :format

  else
    # Noop
  end

  args
end
set_vars(args) click to toggle source

Add each arg passed to the task, as an instance variable of the task

# File lib/admin_module/rake/ppm_tasks.rb, line 49
def set_vars args
  args.each do |arg,val|
    instance_variable_set "@#{arg}", val
  end

  args
end
stop_on_exception=(do_stop) click to toggle source
# File lib/admin_module/rake/ppm_tasks.rb, line 62
def stop_on_exception=(do_stop)
  raise ArgumentError, 'Expecting true or false' unless do_stop === true || do_stop === false
  @stop_on_exception = do_stop
end
validate_params() click to toggle source

Verify we have the needed parameters (arguments) to perform the task action.

# File lib/admin_module/rake/ppm_tasks.rb, line 131
def validate_params
  assert_provided env, 'Missing "env"'
  assert_provided action, 'Missing "action"'

  case action
  when 'import'
    assert_provided path, 'Missing "path"'

  when 'export'
    assert_provided path, 'Missing "path"'

  end

  assert_env_is_configured env
end