class RHC::Commands::Member

Public Instance Methods

add(members) click to toggle source
# File lib/rhc/commands/member.rb, line 182
def add(members)
  target = find_membership_container :writable => true

  role = get_role_option(options, target)
  type = get_type_option(options)
  global = !!options.global

  raise ArgumentError, 'You must pass at least one member to this command.' unless members.present?
  raise ArgumentError, "The --global option can only be used with '--type team'." if global && !team?(type)
  
  say "Adding #{pluralize(members.length, role_name(role))} to #{target.class.model_name.downcase} ... "
  
  members = search_teams(members, global).map{|member| member.id} if team?(type) && !options.ids
  target.update_members(changes_for(members, role, type))

  success "done"

  0
end
list(_) click to toggle source
# File lib/rhc/commands/member.rb, line 79
def list(_)
  target = find_membership_container

  members = target.members
  if options.all
    show_members = members.sort
  else
    show_members = members.select do |m| 
      if m.owner?
        true
      elsif m.explicit_role?
        true
      elsif m.from.any? {|f| f["type"] != "team" }
        true
      else
        false
      end
    end.sort
  end
  show_name = show_members.any?{ |m| m.name.presence && m.name != m.login }
  show_login = show_members.any?{ |m| m.login.presence }
  
  if show_members.present?
    say table(show_members.map do |member|
      [
        ((member.name || "") if show_name),
        ((member.login || "") if show_login),
        role_description(member, member.teams(members)),
        (member.id if options.ids),
        member.type
      ].compact
    end, :header => [
      ('Name' if show_name),
      ('Login' if show_login),
      'Role',
      ("ID" if options.ids),
      "Type"
    ].compact)
  else
    info "The #{target.class.model_name.downcase} #{target.name} does not have any members."
  end

  if show_members.count < members.count
    paragraph do
      info "Pass --all to display all members, including members of teams."
    end
  end

  0
end
remove(members) click to toggle source
# File lib/rhc/commands/member.rb, line 278
def remove(members)
  target = find_membership_container :writable => true
  type = get_type_option(options)

  if options.all
    say "Removing all members from #{target.class.model_name.downcase} ... "
    target.delete_members
    success "done"

  else
    raise ArgumentError, 'You must pass at least one member to this command.' unless members.present?

    say "Removing #{pluralize(members.length, 'member')} from #{target.class.model_name.downcase} ... "

    members = search_team_members(target.members, members).map{|member| member.id} if team?(type) && !options.ids
    target.update_members(changes_for(members, 'none', type))

    success "done"
  end

  0
end
update(members) click to toggle source
# File lib/rhc/commands/member.rb, line 236
def update(members)
  target = find_domain
  role = get_role_option(options, target)
  type = get_type_option(options)

  raise ArgumentError, 'You must pass at least one member to this command.' unless members.present?
  
  say "Updating #{pluralize(members.length, role_name(role))} to #{target.class.model_name.downcase} ... "
  
  members = search_team_members(target.members, members).map{|member| member.id} if team?(type) && !options.ids
  target.update_members(changes_for(members, role, type))

  success "done"

  0
end

Protected Instance Methods

changes_for(members, role, type) click to toggle source
# File lib/rhc/commands/member.rb, line 311
def changes_for(members, role, type)
  members.map do |m|
    h = {:role => role, :type => type}
    h[options.ids ||  team?(type) ? :id : :login] = m
    h
  end
end
get_role_option(options, target) click to toggle source
# File lib/rhc/commands/member.rb, line 302
def get_role_option(options, target)
  options.role || target.default_member_role
end
get_type_option(options) click to toggle source
# File lib/rhc/commands/member.rb, line 306
def get_type_option(options)
  type = options.__hash__[:type]
  type || 'user'
end
role_description(member, teams=[]) click to toggle source
# File lib/rhc/commands/member.rb, line 408
def role_description(member, teams=[])
  if member.owner?
    "#{member.role} (owner)"
  elsif member.explicit_role != member.role && member.from.all? {|f| f['type'] == 'domain'}
    "#{member.role} (via domain)"
  elsif member.explicit_role != member.role && teams.present? && (teams_with_role = teams.select{|t| t.role == member.role }).present?
    "#{member.role} (via #{teams_with_role.map(&:name).sort.join(', ')})"
  else
    member.role
  end
end
search_team_members(members, names) click to toggle source
# File lib/rhc/commands/member.rb, line 368
def search_team_members(members, names)
  r = []
  team_members = members.select(&:team?)
  names.each do |name|

    team_for_name = nil
    suggestions = nil

    if (exact_matches = team_members.select{|team| team.name == name }).present?
      if exact_matches.length == 1
        team_for_name = exact_matches.first
      else
        raise RHC::MemberNotFoundException.new("There is more than one member team named '#{name}'. " +
          "Please use the --ids flag and specify the exact id of the team you want to manage.")
      end

    elsif (case_insensitive_matches = team_members.select{|team| team.name =~ /^#{Regexp.escape(name)}$/}).present?
      if case_insensitive_matches.length == 1
        team_for_name = case_insensitive_matches.first
      else
        suggestions = case_insensitive_matches
      end

    else
      suggestions = team_members.select{|t| t.name =~ /#{Regexp.escape(name)}/}
    end

    if team_for_name
      r << team_for_name
    elsif suggestions.present?
      raise RHC::MemberNotFoundException.new("No member team found with the name '#{name}'. " +
        "Did you mean one of the following?\n#{suggestions[0..50].map(&:name).join(", ")}")
    else
      raise RHC::MemberNotFoundException.new("No member team found with the name '#{name}'.")
    end

  end
  r.flatten
end
search_teams(team_names, global=false) click to toggle source
# File lib/rhc/commands/member.rb, line 323
def search_teams(team_names, global=false)
  r = []
  team_names.each do |team_name|
    teams_for_name = 
      global ? 
        rest_client.search_teams(team_name, global) : 
        rest_client.search_owned_teams(team_name)

    team_for_name = nil
    suggestions = nil

    if (exact_matches = teams_for_name.select {|t| t.name == team_name }).present?
      if exact_matches.length == 1
        team_for_name = exact_matches.first
      else
        raise RHC::TeamNotFoundException.new("There is more than one team named '#{team_name}'. " +
          "Please use the --ids flag and specify the exact id of the team you want to manage.")
      end

    elsif (case_insensitive_matches = teams_for_name.select {|t| t.name =~ /^#{Regexp.escape(team_name)}$/ }).present?
      if case_insensitive_matches.length == 1
        team_for_name = case_insensitive_matches.first
      else
        suggestions = case_insensitive_matches
      end

    else
      suggestions = teams_for_name
    end


    if team_for_name
      r << team_for_name
    elsif suggestions.present?
      msg = global ? "No global team found with the name '#{team_name}'." : "You do not have a team named '#{team_name}'."
      raise RHC::TeamNotFoundException.new(msg + " Did you mean one of the following?\n#{suggestions[0..50].map(&:name).join(", ")}")
    else
      msg = global ? "No global team found with the name '#{team_name}'." : "You do not have a team named '#{team_name}'."
      raise RHC::TeamNotFoundException.new(msg)
    end

  end
  r.flatten
end
team?(type) click to toggle source
# File lib/rhc/commands/member.rb, line 319
def team?(type)
  type == 'team'
end