Class: Ldap::Cli

Inherits:
Object
  • Object
show all
Defined in:
lib/ldap/cli.rb,
lib/ldap/cli/version.rb

Overview

Tool for reading/writing entries in an LDAP directory to/from CSV files

Constant Summary collapse

VERSION =
'0.3.0'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, headers, table_dn) ⇒ Cli

Returns a new instance of Cli.



15
16
17
18
19
# File 'lib/ldap/cli.rb', line 15

def initialize(config, headers, table_dn)
  @ldap = Net::LDAP.new config
  @headers = headers
  @table_dn = table_dn
end

Instance Attribute Details

#errorsObject

Returns the value of attribute errors.



13
14
15
# File 'lib/ldap/cli.rb', line 13

def errors
  @errors
end

#headersObject

Returns the value of attribute headers.



13
14
15
# File 'lib/ldap/cli.rb', line 13

def headers
  @headers
end

#ldapObject

Returns the value of attribute ldap.



13
14
15
# File 'lib/ldap/cli.rb', line 13

def ldap
  @ldap
end

#table_dnObject

Returns the value of attribute table_dn.



13
14
15
# File 'lib/ldap/cli.rb', line 13

def table_dn
  @table_dn
end

Instance Method Details

#bindObject



21
22
23
# File 'lib/ldap/cli.rb', line 21

def bind
  ldap.bind
end

#delete(args) ⇒ Object



25
26
27
# File 'lib/ldap/cli.rb', line 25

def delete(args)
  ldap.delete(args)
end

#export(args = {}) ⇒ Object



42
43
44
45
46
47
48
49
# File 'lib/ldap/cli.rb', line 42

def export(args = {})
  @output_file_path = args[:output_file_path]
  @filter = args[:filter]
  add_header
  export_data
rescue StandardError => e
  puts "ERROR: On LdapCli#export: #{e.message}"
end

#import(input_file_path = nil) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/ldap/cli.rb', line 34

def import(input_file_path = nil)
  @errors = []
  CSV.foreach(input_file_path, headers: true, skip_blanks: true) do |row|
    valid_headers row.headers
    save_record(row)
  end
end

#valid_headers(input_headers = []) ⇒ Object



29
30
31
32
# File 'lib/ldap/cli.rb', line 29

def valid_headers(input_headers = [])
  no_header = 'Require headers to process the file.'
  raise no_header unless (input_headers - headers).empty?
end