Class: Wdmc::User

Inherits:
Thor
  • Object
show all
Includes:
Enumerable
Defined in:
lib/wdmc/user.rb

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ User

Returns a new instance of User.



7
8
9
10
# File 'lib/wdmc/user.rb', line 7

def initialize(*args)
  @wdmc = Wdmc::Client.new
  super
end

Instance Method Details

#create(name) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/wdmc/user.rb', line 44

def create( name )
  password = Base64.strict_encode64(options[:password]) if options[:password]
  user_exists = @wdmc.user_exists?( name )
  abort "\nUser does not exist: ".color(:yellow) + "#{name}".color(:cyan) if user_exists.include?( name )
  begin
    groups = ['cloudholders']
    groups.push options[:group_names] if options[:group_names]
    data = {
      :email => options[:email],
      :username => name,
      :password => password,
      :fullname => options[:fullname],
      :is_admin => options[:admin],
      :group_names => groups.join(','),
      :first_name => options[:first_name],
      :last_name => options[:last_name]
    }
    puts "Create user:\s".color(:whitesmoke) + "OK".color(:green) if @wdmc.add_user( data )
  rescue RestClient::ExceptionWithResponse => e
    puts eval(e.response)[:users][:error_message].color(:orange)
  end
end

#delete(name) ⇒ Object



95
96
97
98
99
100
101
102
103
# File 'lib/wdmc/user.rb', line 95

def delete( name )
  user_exists = @wdmc.user_exists?( name )
  abort "\nUser does not exist: ".color(:yellow) + "#{name}".color(:cyan) unless user_exists.include?( name )
  unless options['force']
    puts "\nAre you sure you want to delete this user?\s".color(:orange) + "#{name}".color(:whitesmoke)
    return unless yes?("DELETE? (yes/no):")
  end
  puts "Delete user:\s".color(:whitesmoke) + "OK".color(:green) if @wdmc.delete_user( name )
end

#listObject



13
14
15
16
17
18
19
# File 'lib/wdmc/user.rb', line 13

def list
  users = @wdmc.all_users
  puts "Users".upcase.color(:magenta)
  users.each do |user|
    puts "\s- #{user[:username]}"
  end
end

#show(name) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/wdmc/user.rb', line 22

def show( name )
  users = @wdmc.find_user( name )
  user_exists = @wdmc.user_exists?( name )
  abort "\nUser does not exist: ".color(:yellow) + "#{name}".color(:cyan) unless user_exists.include?( name )
  users.each do |user|
    puts "Username:\s".upcase.color(:magenta) + user[:username]
    puts "\sUser ID\t\t: ".color(:whitesmoke) + "#{user[:user_id]}"
    puts "\sFullname\t\t: ".color(:whitesmoke) + "#{user[:fullname]}" unless user[:fullname].empty?
    puts "\sAdmin\t\t\t: ".color(:whitesmoke) + "#{user[:is_admin]}"
    puts "\sPassword set\t\t: ".color(:whitesmoke) + "#{user[:is_password]}".color(:green) if user[:is_password] == 'true'
    puts "\sPassword set\t\t: ".color(:whitesmoke) + "#{user[:is_password]}".color(:orange) if user[:is_password] == 'false'
  end
end

#update(name) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/wdmc/user.rb', line 74

def update( name )
  password = Base64.strict_encode64(options[:password]) if options[:password]
  user_exists = @wdmc.user_exists?( name )
  abort "\nUser does not exist: ".color(:yellow) + "#{name}".color(:cyan) unless user_exists.include?( name )
  begin
    data = {
      :username => options[:new_username] || name,
      :password =>  password,
      :fullname => options[:fullname],
      :is_admin => options[:admin],
      :first_name => options[:first_name],
      :last_name => options[:last_name]
    }
    puts "Update user:\s".color(:whitesmoke) + "OK".color(:green) if @wdmc.update_user( name, data )
  rescue RestClient::ExceptionWithResponse => e
    puts eval(e.response)[:users] #[:error_message].color(:orange)
  end
end