Class: Junos::Ez::Provider::Parent

Inherits:
Object
  • Object
show all
Defined in:
lib/junos-ez/provider.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(p_obj, name = nil, opts = {}) ⇒ Parent

p_obj - the parent object name - the name of the resource, or nil if this is a provider opts - options to the provider/resource. :parent is reserved



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/junos-ez/provider.rb', line 86

def initialize( p_obj, name = nil, opts = {} )
  
  @providers = []
  @parent = opts[:parent] || nil    
  @ndev = p_obj.instance_variable_get(:@ndev) || p_obj
  @name = name
  @opts = opts
  
  @list = []                # array list of item names
  @catalog = {}             # hash catalog of named items
      
  return unless @name           
  # resources only from here ...
  @has = {}         # properties read-from Junos
  @should = {}      # properties to write-back to Junos
end

Instance Attribute Details

#catalogObject

Returns the value of attribute catalog.



80
81
82
# File 'lib/junos-ez/provider.rb', line 80

def catalog
  @catalog
end

#hasObject

Returns the value of attribute has.



79
80
81
# File 'lib/junos-ez/provider.rb', line 79

def has
  @has
end

#listObject


Provider methods to obtain collection information as ‘list’ - array of named items ‘catalog’ - hash of all items with properties




182
183
184
# File 'lib/junos-ez/provider.rb', line 182

def list
  @list
end

#nameObject (readonly)

Returns the value of attribute name.



77
78
79
# File 'lib/junos-ez/provider.rb', line 77

def name
  @name
end

#ndevObject (readonly)

Returns the value of attribute ndev.



77
78
79
# File 'lib/junos-ez/provider.rb', line 77

def ndev
  @ndev
end

#parentObject (readonly)

Returns the value of attribute parent.



77
78
79
# File 'lib/junos-ez/provider.rb', line 77

def parent
  @parent
end

#propertiesObject

Returns the value of attribute properties.



79
80
81
# File 'lib/junos-ez/provider.rb', line 79

def properties
  @properties
end

#providersObject

Returns the value of attribute providers.



78
79
80
# File 'lib/junos-ez/provider.rb', line 78

def providers
  @providers
end

#shouldObject

Returns the value of attribute should.



79
80
81
# File 'lib/junos-ez/provider.rb', line 79

def should
  @should
end

Instance Method Details

#[](property) ⇒ Object


property

resource property reader or

“name”

resource selector from provider




121
122
123
124
125
126
127
128
129
# File 'lib/junos-ez/provider.rb', line 121

def []( property )
  return self.select( property ) if is_provider?
  
  # if there is already something in the write-back, then use
  # it before using from the read-cache
  
  return @should[property] if @should[property]
  return @has[property] if @has    
end

#[]=(property, rval) ⇒ Object


[]= property writer (@should)


Raises:

  • (ArgumentError)


135
136
137
138
139
140
# File 'lib/junos-ez/provider.rb', line 135

def []=( property, rval )
  raise ArgumentError, "This is not a provider instance" if is_provider?
  raise ArgumentError, "Invalid property['#{property.to_s}']" unless properties.include? property
  
  @should[property] = rval
end

#activate!Object


Junos activation controls




303
304
305
306
307
# File 'lib/junos-ez/provider.rb', line 303

def activate!
  return nil if @should[:_active] == true        
  @should[:_active] = true
  write!
end

#active?Boolean


‘active?’ - is the resource config active in Junos


Returns:

  • (Boolean)


164
165
166
167
# File 'lib/junos-ez/provider.rb', line 164

def active?
  false unless exists?
  @has[:_active]
end

#catalog!Object



195
196
197
198
# File 'lib/junos-ez/provider.rb', line 195

def catalog!
  @catalog.clear
  @catalog = build_catalog
end

#create(name = nil, prop_hash = {}) {|newbie| ... } ⇒ Object


‘create’ will build a new object, but does not write the contents back to the device. The caller can chain the write! method if desired Alternative, the caller can use ‘create!’ which does write to the device.


Yields:

  • (newbie)

Raises:

  • (ArgumentError)


211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/junos-ez/provider.rb', line 211

def create( name = nil, prop_hash = {}, &block )
      
  ## if this is an existing object, then we shouldn't 
  ## allow the caller to create something.
  
  raise ArgumentError, "Not called by provider!" unless is_provider?
    
  ## if we're here, then we're creating an entirely new
  ## instance of this object.  We should check to see if
  ## it first exists, eh?  So allow the caller to specify
  ## if they want an exception if it already exists; overloading
  ## the use of the prop_hash[:_exist], yo!
  
  newbie = self.select( name )    
  if prop_hash[:_exist]
    raise ArgumentError,  name_decorated(name) + " already exists" if newbie.exists? 
  end
      
  prop_hash.each{ |k,v| newbie[k] = v } unless prop_hash.empty?
  
  ## default mark the newly created object as should exist and should
  ## be active (if not already set)
  
  newbie[:_exist] = true
  newbie[:_active] ||= true    
  
  ## if a block is provided, then pass the block the new object
  ## the caller is then expected to set the properies
  
  yield( newbie ) if block_given?   
  
  ## return the new object    
  return newbie    
end

#create!(name = nil, prop_hash = {}, &block) ⇒ Object


‘create!’ is just a helper to call create and then write the config assuming create returns ok.




251
252
253
254
255
256
# File 'lib/junos-ez/provider.rb', line 251

def create!( name = nil, prop_hash = {}, &block )
  newbie = create( name, prop_hash, block )
  return nil unless newbie
  newbie.write!
  newbie
end

#create_from_hash!(as_hash, opts = {}) ⇒ Object



268
269
270
# File 'lib/junos-ez/provider.rb', line 268

def create_from_hash!( as_hash, opts = {} )
  write_xml_config! xml_from_h_expanded( as_hash, opts )     
end

#create_from_yaml!(opts = {}) ⇒ Object


YAML / HASH methods




262
263
264
265
266
# File 'lib/junos-ez/provider.rb', line 262

def create_from_yaml!( opts = {} )
  raise ArgumentError "Missing :filename param" unless opts[:filename]        
  as_hash = YAML.load_file( opts[:filename] )
  write_xml_config! xml_from_h_expanded( as_hash, opts )     
end

#deactivate!Object



309
310
311
312
313
# File 'lib/junos-ez/provider.rb', line 309

def deactivate!
  return nil if @should[:_active] == false    
  @should[:_active] = false
  write!
end

#delete!Object


‘delete!’ will cause the item to be removed from the Junos configuration




288
289
290
291
292
293
294
295
296
297
# File 'lib/junos-ez/provider.rb', line 288

def delete!
  return nil unless exists?    
  xml = xml_at_top
  par = xml.instance_variable_get(:@parent)    
  par['delete'] = 'delete'
  xml_on_delete( xml )
  rsp = write_xml_config!( xml.doc.root )
  @has[:_exist] = false
  true # rsp ... don't return XML, but let's hear from the community...
end

#each(&block) ⇒ Object


Provider ‘each` - iterate through each managed resource as obtained from the `list` instance variable. select the object and pass it to the provided block


Raises:

  • (ArgumentError)


368
369
370
371
# File 'lib/junos-ez/provider.rb', line 368

def each( &block )
  raise ArgumentError, "not a provider" unless is_provider?
  list.each{ |name| yield select(name ) }
end

#exists?Boolean


‘exists?’ - does the resource exist in the Juos config


Returns:

  • (Boolean)


158
# File 'lib/junos-ez/provider.rb', line 158

def exists?; @has[:_exist]; end

#init_hasObject

‘init_has’ is called when creating a new managed object or when a caller attempts to retrieve a non-existing one



391
# File 'lib/junos-ez/provider.rb', line 391

def init_has; nil end

#is_new?Boolean


is_new? - indicates if this is a new resource


Returns:

  • (Boolean)


114
# File 'lib/junos-ez/provider.rb', line 114

def is_new?; (@has[:_exist] == false) || false end

#is_provider?Boolean


‘is_provider?’ - indicates if this object instance is a provider object, rather than a specific instance of the object


Returns:

  • (Boolean)


108
# File 'lib/junos-ez/provider.rb', line 108

def is_provider?; @name.nil? end

#list!Object



186
187
188
189
# File 'lib/junos-ez/provider.rb', line 186

def list!
  @list.clear
  @list = build_list
end

#name_decorated(name = @name) ⇒ Object

@@@ helper method, probably needs to go into ‘private section @@@ TBD



172
173
174
# File 'lib/junos-ez/provider.rb', line 172

def name_decorated( name = @name )
  self.class.to_s + "['" + name + "']"
end

#need_write?Boolean


Provider writer methods


Returns:

  • (Boolean)


436
# File 'lib/junos-ez/provider.rb', line 436

def need_write?; not @should.empty? end

#read!Object



406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
# File 'lib/junos-ez/provider.rb', line 406

def read!
  @has.clear    
  cfg_xml = xml_config_read!
  @has_xml = xml_get_has_xml( cfg_xml )

  ## if the thing doesn't exist in Junos, then mark the @has
  ## structure accordingly and call the object init_has for
  ## any defaults
  
  unless @has_xml
    @has[:_exist] ||= false      
    @has[:_active] ||= true
    init_has
    return nil
  end
  
  ## xml_read_parser *MUST* be implmented by the provider class
  ## it is used to parse the XML into the HASH structure.  It
  ## returns true/false
  
  xml_read_parser( @has_xml, @has )  
  
  ## return the Hash representation
  self.has
end

#rename!(new_name) ⇒ Object



322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
# File 'lib/junos-ez/provider.rb', line 322

def rename!( new_name )
  return nil unless exists?
  
  xml = xml_at_top
  par = xml.instance_variable_get(:@parent)    
  new_ele_name = xml_element_newname( new_name )
  
  return nil unless new_ele_name
  
  par['rename'] = 'rename'
  par['name'] = new_ele_name

  rsp = write_xml_config!( xml.doc.root )
  @name = new_name
  rsp        
end

#reorder!(opts) ⇒ Object


Junos reorder method

opts = item-name, opts = item-name


Raises:

  • (ArgumentError)


346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
# File 'lib/junos-ez/provider.rb', line 346

def reorder!( opts )
  return nil unless exists?
  
  ## validate opts hash
  ctrl, name = opts.first
  raise ArgumentError, "Invalid operation #{ctrl}" unless [:before,:after].include? ctrl
  
  xml = xml_at_top
  par = xml.instance_variable_get(:@parent)
  par['insert'] = ctrl.to_s
  par['name'] = name
  rsp = write_xml_config! ( xml.doc.root )
  
  return rsp    
end

#select(name) ⇒ Object


‘select’ a resource from a provider


Raises:

  • (ArgumentError)


146
147
148
149
150
151
152
# File 'lib/junos-ez/provider.rb', line 146

def select( name )
  raise ArgumentError, "This is not a provider instance" unless is_provider?
  this = self.class.new( @ndev, name, @opts )
  this.properties = self.properties
  this.read!    
  this        
end

#to_h(which = :read) ⇒ Object


‘to_h’ lets us look at the read/write hash structures




524
525
526
# File 'lib/junos-ez/provider.rb', line 524

def to_h( which = :read )
  { @name => (which == :read) ? @has : @should }    
end

#to_h_expanded(opts = {}) ⇒ Object



272
273
274
# File 'lib/junos-ez/provider.rb', line 272

def to_h_expanded( opts = {} ) 
  to_h( opts ) 
end

#to_yaml(opts = {}) ⇒ Object



276
277
278
279
280
281
# File 'lib/junos-ez/provider.rb', line 276

def to_yaml( opts = {} ) 
  out_hash = to_h_expanded( opts )
  out_yaml = out_hash.to_yaml        
  File.open( opts[:filename], "w" ){|f| f.puts out_hash.to_yaml } if opts[:filename]   
  out_yaml    
end

#with(given_list, &block) ⇒ Object


Provider ‘with` - iterate through each managed resource as obtained from the `given_list` instance variable.

select the object and pass it to the provided block


Raises:

  • (ArgumentError)


379
380
381
382
# File 'lib/junos-ez/provider.rb', line 379

def with( given_list, &block )
  raise ArgumentError, "not a provider" unless is_provider?
  given_list.each{ |name| yield select( name ) }
end

#write!Object



438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
# File 'lib/junos-ez/provider.rb', line 438

def write!
  return nil if @should.empty?
  
  @should[:_exist] ||= true
  
  # create the necessary chagnes and push them to the Junos
  # device.  If an error occurs, it will be raised
  
  xml_change = xml_build_change            
  return nil unless xml_change
  rsp = write_xml_config!( xml_change )    
  
  # copy the 'should' values into the 'has' values now that 
  # they've been written back to Junos
      
  @has.merge! @should 
  @should.clear
  
  # returning 'true' for now.  might need to change this back
  # to 'rsp' depending on the community feedback.  general approach is to not have to 
  # deal with XML, unless it's an exception case.  the only time rsp is really
  # needed is to look at warnings; i.e. not-errors.  errors will generate an exception, yo!
  
  return true
end

#xml_at_editObject


XML writer methods




468
# File 'lib/junos-ez/provider.rb', line 468

def xml_at_edit; nil; end

#xml_at_topObject



469
# File 'lib/junos-ez/provider.rb', line 469

def xml_at_top; nil; end

#xml_build_change(xml_at_here = nil) ⇒ Object

‘xml_build_change’ is used to create the Junos XML configuration structure. Generally speaking it should not be called by code outside the providers, but sometimes we might want to, so don’t make it private



487
488
489
490
491
492
493
494
495
496
497
498
499
500
# File 'lib/junos-ez/provider.rb', line 487

def xml_build_change( xml_at_here = nil )
  edit_at = xml_at_here || xml_at_edit || xml_at_top
  
  if @should[:_exist] == false
    xml_change__exist( edit_at )
    return edit_at.doc.root
  end
  
  changed = false
  @should.keys.each do |prop|
    changed = true if self.send( "xml_change_#{prop}", edit_at )
  end 
  (changed) ? edit_at.doc.root : nil
end

#xml_change__active(xml) ⇒ Object



514
515
516
517
518
# File 'lib/junos-ez/provider.rb', line 514

def xml_change__active( xml )
  par = xml.instance_variable_get(:@parent)
  value = @should[:_active]  ? 'active' : 'inactive'
  par[value] = value # attribute name is same as value
end

#xml_change__exist(xml) ⇒ Object



473
474
475
476
477
478
479
480
# File 'lib/junos-ez/provider.rb', line 473

def xml_change__exist( xml )
  return xml_on_create( xml ) if @should[:_exist]    
  
  par = xml.instance_variable_get(:@parent)
  par['delete'] = 'delete'
  
  return xml_on_delete( xml )
end

#xml_change_admin(xml) ⇒ Object


XML common write “change” methods




506
507
508
# File 'lib/junos-ez/provider.rb', line 506

def xml_change_admin( xml )
  xml.disable (@should[:admin] == :up ) ? Netconf::JunosConfig::DELETE : nil
end

#xml_change_description(xml) ⇒ Object



510
511
512
# File 'lib/junos-ez/provider.rb', line 510

def xml_change_description( xml )
  xml_set_or_delete( xml, 'description', @should[:description] )
end

#xml_config_read!Object

‘xml_config_read!’ is ued to retrieve the configuration from the Junos device



402
403
404
# File 'lib/junos-ez/provider.rb', line 402

def xml_config_read!
  @ndev.rpc.get_configuration( xml_at_top )    
end

#xml_element_newname(new_name) ⇒ Object

by default, simply allow the new name



320
# File 'lib/junos-ez/provider.rb', line 320

def xml_element_newname( new_name); new_name end

#xml_get_has_xml(xml) ⇒ Object

‘xml_get_has_xml’ - used to retrieve the starting location of the actual XML data for the managed object (as compared to the top of the configuration document



397
# File 'lib/junos-ez/provider.rb', line 397

def xml_get_has_xml( xml ); nil end

#xml_on_create(xml) ⇒ Object



470
# File 'lib/junos-ez/provider.rb', line 470

def xml_on_create( xml ); nil; end

#xml_on_delete(xml) ⇒ Object



471
# File 'lib/junos-ez/provider.rb', line 471

def xml_on_delete( xml ); nil; end