class Junos::Ez::Users::Provider
Provider
Resource Methods
Provider
Collection Methods
Resource Methods
Public Instance Methods
build_catalog()
click to toggle source
# File lib/junos-ez/system/users.rb, line 127 def build_catalog @catalog = {} @ndev.rpc.get_configuration{ |x| x.system { x.login }} .xpath('//user').each do |user| name = user.xpath('name').text @catalog[name] = {} xml_read_parser( user, @catalog[name] ) end @catalog end
build_list()
click to toggle source
# File lib/junos-ez/system/users.rb, line 118 def build_list @ndev.rpc.get_configuration{ |x| x.system { x.login { x.user({:recurse => 'false'}) } }} .xpath('//user/name').collect{ |i| i.text } end
get_userauth_provd()
click to toggle source
@@ need to move this code into the main provider @@ as a utility …
# File lib/junos-ez/system/users.rb, line 179 def get_userauth_provd @ndev.providers.each do |p| obj = @ndev.send(p) return obj if obj.class == Junos::Ez::UserAuths::Provider end end
load_ssh_key!( opts = {} )
click to toggle source
load an SSH public key & return the resulting key object. You can provide the publickey either as :publickey or contents will be read from :filename
# File lib/junos-ez/system/users.rb, line 192 def load_ssh_key!( opts = {} ) publickey = opts[:publickey] || File.read( opts[:filename] ).strip raise ArgumentError, "no public-key specified" unless publickey # nab the provider for handling ssh-keys, since we'll use that # for key resource management @auth_provd ||= get_userauth_provd raise StandardError, "No Junos::Ez::UserAuths::Provider" unless @auth_provd # extract the key-type from the public key. keytype = publickey[0..6] keytype = 'ssh-dsa' if keytype == 'ssh-dss' raise ArgumentError, "Unknown ssh key-type #{keytype}" unless Junos::Ez::UserAuths::VALID_KEY_TYPES.include? keytype # ok, we've got everything we need to add the key, so here we go. key_name = {:user => @name, :keytype => keytype, :publickey => publickey } key = @auth_provd[ key_name ] key.write! # return the key in case the caller wants it key end
password=(plain_text)
click to toggle source
change the password by providing it in plain-text
# File lib/junos-ez/system/users.rb, line 152 def password=(plain_text) xml = xml_at_top xml.authentication { xml.send(:'plain-text-password-value', plain_text) } @ndev.rpc.load_configuration( xml ) return true end
ssh_key( keytype, index = 0 )
click to toggle source
get a Hash that is used as the 'name' for obtaining a resource for Junos::Ez::UserAuths::Provider
# File lib/junos-ez/system/users.rb, line 166 def ssh_key( keytype, index = 0 ) return nil unless @has[:ssh_keys] return nil unless @has[:ssh_keys][keytype] ret_h = {:user => @name, :keytype => keytype} ret_h[:publickey] = @has[:ssh_keys][keytype][index] ret_h end
xml_at_top()
click to toggle source
XML top placement
# File lib/junos-ez/system/users.rb, line 37 def xml_at_top Nokogiri::XML::Builder.new{|x| x.configuration{ x.system { x.login { x.user { x.name @name return x }}}}} end
xml_change_class( xml )
click to toggle source
changing the 'gid' is changing the Junos
'class' element so, what is tough here is that the Nokogiri Builder mech won't allow us to use the string 'class' since it conflicts with the Ruby language. So we need to add the 'class' element the hard way, yo! …
# File lib/junos-ez/system/users.rb, line 98 def xml_change_class( xml ) par = xml.instance_variable_get(:@parent) doc = xml.instance_variable_get(:@doc) user_class = Nokogiri::XML::Node.new('class', doc ) user_class.content = @should[:class] par.add_child( user_class ) end
xml_change_fullname( xml )
click to toggle source
# File lib/junos-ez/system/users.rb, line 88 def xml_change_fullname( xml ) xml_set_or_delete( xml, 'full-name', @should[:fullname] ) end
xml_change_password( xml )
click to toggle source
XML writers
# File lib/junos-ez/system/users.rb, line 82 def xml_change_password( xml ) xml.authentication { xml_set_or_delete( xml, 'encrypted-password', @should[:password] ) } end
xml_change_uid( xml )
click to toggle source
# File lib/junos-ez/system/users.rb, line 106 def xml_change_uid( xml ) xml_set_or_delete( xml, 'uid', @should[:uid] ) end
xml_get_has_xml( xml )
click to toggle source
XML readers
# File lib/junos-ez/system/users.rb, line 49 def xml_get_has_xml( xml ) xml.xpath('//user')[0] end
xml_read_parser( as_xml, as_hash )
click to toggle source
# File lib/junos-ez/system/users.rb, line 53 def xml_read_parser( as_xml, as_hash ) set_has_status( as_xml, as_hash ) as_hash[:uid] = as_xml.xpath('uid').text as_hash[:class] = as_xml.xpath('class').text xml_when_item(as_xml.xpath('full-name')) {|i| as_hash[:fullname] = i.text } xml_when_item(as_xml.xpath('authentication/encrypted-password')) {|i| as_hash[:password] = i.text } # READ-ONLY capture the keys unless (keys = as_xml.xpath('authentication/ssh-rsa')).empty? as_hash[:ssh_keys] ||= {} as_hash[:ssh_keys]['ssh-rsa'] = keys.collect{|key| key.text.strip} end unless (keys = as_xml.xpath('authentication/ssh-dsa')).empty? as_hash[:ssh_keys] ||= {} as_hash[:ssh_keys]['ssh-dsa'] = keys.collect{|key| key.text.strip} end end