class Ec2Iam::CLI

Public Instance Methods

create(user_name) click to toggle source
# File lib/ec2iam/cli.rb, line 14
def create(user_name)
  if options['all-profiles']
    keys_array = []
    IamConfig::CONFIG.each do |profile, credentials|
      set_client(profile)
      say("On #{profile}:")
      keys_array << { profile: profile, credentials: create_user(user_name) }
    end

    IamConfig.write_keys(user_name, keys_array) if options[:save]
  else
    options[:profile] ? set_client(options[:profile]) : set_client

    if options[:save]
      IamConfig.write_key(user_name, IamConfig.format_key(@client.profile, create_user(user_name)))
    else
      create_user(user_name)
    end
  end
end
create_user(user_name) click to toggle source
# File lib/ec2iam/cli.rb, line 80
def create_user(user_name)
  begin
    user = @client.iam.users.create(user_name)
    user.groups.add(@client.group)
    say("create #{user_name} done.", :green)
    access_key = user.access_keys.create
    credentials = access_key.credentials
    say("#{IamConfig.format_key(@client.profile, credentials)}", :green)
    credentials
  rescue AWS::IAM::Errors::EntityAlreadyExists
    say("User '#{user_name}' has already exists. Please retry with another name.", :red)
  end
end
delete!(user_name) click to toggle source
# File lib/ec2iam/cli.rb, line 43
def delete!(user_name)
  if options['all-profiles']
    IamConfig::CONFIG.each do |profile, credentials|
      set_client(profile)
      say("On #{profile}:")
      delete_user!(user_name)
    end
  else
    options[:profile] ? set_client(options[:profile]) : set_client
    delete_user!(user_name)
  end
end
delete_user!(user_name) click to toggle source
# File lib/ec2iam/cli.rb, line 94
def delete_user!(user_name)
  begin
    @client.iam.users[user_name].delete!
    say("delete #{user_name}.", :red)
  rescue AWS::IAM::Errors::NoSuchEntity
    say("User '#{user_name}' Not Found. Please retry with another name.", :red)
  end
end
list() click to toggle source
# File lib/ec2iam/cli.rb, line 57
def list
  if options['all-profiles']
    IamConfig::CONFIG.each do |profile, credentials|
      set_client(profile)
      say("On #{profile}:")
      list_user
    end
  else
    options[:profile] ? set_client(options[:profile]) : set_client
    list_user
  end
end
list_user() click to toggle source
# File lib/ec2iam/cli.rb, line 103
def list_user
  @client.iam.users.each {|u| puts u.name}
end
set_client(profile='default') click to toggle source
# File lib/ec2iam/cli.rb, line 71
def set_client(profile='default')
  begin
    @client = IamConfig.new(profile)
  rescue AccountKeyNotFound
    say("account_key #{profile} was not found on iam.yml", :red)
    exit(1)
  end
end