Class: Junos::Ez::Users::Provider

Inherits:
Provider::Parent show all
Defined in:
lib/junos-ez/system/users.rb,
lib/junos-ez/system/users.rb,
lib/junos-ez/system/users.rb,
lib/junos-ez/system/users.rb

Overview


Resource Methods


Instance Attribute Summary

Attributes inherited from Provider::Parent

#catalog, #has, #list, #name, #ndev, #parent, #properties, #providers, #should

Instance Method Summary collapse

Methods inherited from Provider::Parent

#[], #[]=, #activate!, #active?, #catalog!, #create, #create!, #create_from_hash!, #create_from_yaml!, #deactivate!, #delete!, #each, #exists?, #init_has, #initialize, #is_new?, #is_provider?, #list!, #name_decorated, #need_write?, #read!, #rename!, #reorder!, #select, #to_h, #to_h_expanded, #to_yaml, #with, #write!, #xml_at_edit, #xml_build_change, #xml_change__active, #xml_change__exist, #xml_change_admin, #xml_change_description, #xml_config_read!, #xml_element_newname, #xml_on_create, #xml_on_delete

Constructor Details

This class inherits a constructor from Junos::Ez::Provider::Parent

Instance Method Details

#build_catalogObject



127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/junos-ez/system/users.rb', line 127

def build_catalog
  @catalog = {}
  @ndev.rpc.get_configuration{ |x| x.system {
    x.
  }}
  .xpath('//user').each do |user|
    name = user.xpath('name').text
    @catalog[name] = {}
    xml_read_parser( user, @catalog[name] )
  end
  @catalog
end

#build_listObject



118
119
120
121
122
123
124
125
# File 'lib/junos-ez/system/users.rb', line 118

def build_list
  @ndev.rpc.get_configuration{ |x| x.system {
    x. {
      x.user({:recurse => 'false'})
    }
  }}
  .xpath('//user/name').collect{ |i| i.text }
end

#get_userauth_provdObject

@@ need to move this code into the main provider @@ as a utility …



179
180
181
182
183
184
# 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 = {}) ⇒ Object


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


Raises:

  • (ArgumentError)


192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# 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) ⇒ Object


change the password by providing it in plain-text




152
153
154
155
156
157
158
159
# 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) ⇒ Object


get a Hash that is used as the ‘name’ for obtaining a resource for Junos::Ez::UserAuths::Provider




166
167
168
169
170
171
172
# 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_topObject


XML top placement




37
38
39
40
41
42
43
# File 'lib/junos-ez/system/users.rb', line 37

def xml_at_top
  Nokogiri::XML::Builder.new{|x| x.configuration{ 
    x.system { x. { x.user { 
      x.name @name
      return x
  }}}}}
end

#xml_change_class(xml) ⇒ Object

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! …



98
99
100
101
102
103
104
# 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) ⇒ Object



88
89
90
# 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) ⇒ Object


XML writers




82
83
84
85
86
# 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) ⇒ Object



106
107
108
# 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) ⇒ Object


XML readers




49
50
51
# 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) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# 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