Class: Junos::Ez::Provider::Parent
- Inherits:
-
Object
- Object
- Junos::Ez::Provider::Parent
- Defined in:
- lib/junos-ez/provider.rb
Direct Known Subclasses
Config::Provider, FS::Provider, Group::Provider, IPports::Provider, L1ports::Provider, L2ports::Provider, LAGports::Provider, RE::Provider, StaticHosts::Provider, StaticRoutes::Provider, SysConfig::Provider, UserAuths::Provider, Users::Provider, Vlans::Provider
Instance Attribute Summary collapse
-
#catalog ⇒ Object
Returns the value of attribute catalog.
-
#has ⇒ Object
Returns the value of attribute has.
-
#list ⇒ Object
————————————————————— Provider methods to obtain collection information as ‘list’ - array of named items ‘catalog’ - hash of all items with properties —————————————————————.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#ndev ⇒ Object
readonly
Returns the value of attribute ndev.
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
-
#properties ⇒ Object
Returns the value of attribute properties.
-
#providers ⇒ Object
Returns the value of attribute providers.
-
#should ⇒ Object
Returns the value of attribute should.
Instance Method Summary collapse
-
#[](property) ⇒ Object
————————————————————— [property] resource property reader or [“name”] resource selector from provider —————————————————————.
-
#[]=(property, rval) ⇒ Object
————————————————————— []= property writer (@should) —————————————————————.
-
#activate! ⇒ Object
————————————————————— Junos activation controls —————————————————————.
-
#active? ⇒ Boolean
————————————————————— ‘active?’ - is the resource config active in Junos —————————————————————.
- #catalog! ⇒ Object
-
#create(name = nil, prop_hash = {}) {|newbie| ... } ⇒ Object
—————————————————————- ‘create’ will build a new object, but does not write the contents back to the device.
-
#create!(*args) ⇒ Object
—————————————————————- ‘create!’ is just a helper to call create and then write the config assuming create returns ok.
- #create_from_hash!(as_hash, opts = {}) ⇒ Object
-
#create_from_yaml!(opts = {}) ⇒ Object
—————————————————————- YAML / HASH methods —————————————————————-.
- #deactivate! ⇒ Object
-
#delete! ⇒ Object
————————————————————— ‘delete!’ will cause the item to be removed from the Junos configuration —————————————————————.
-
#each(&block) ⇒ Object
————————————————————— Provider ‘each` - iterate through each managed resource as obtained from the `list` instance variable.
-
#exists? ⇒ Boolean
————————————————————— ‘exists?’ - does the resource exist in the Juos config —————————————————————.
-
#init_has ⇒ Object
‘init_has’ is called when creating a new managed object or when a caller attempts to retrieve a non-existing one.
-
#initialize(p_obj, name = nil, opts = {}) ⇒ Parent
constructor
p_obj - the parent object name - the name of the resource, or nil if this is a provider opts - options to the provider/resource.
-
#is_new? ⇒ Boolean
————————————————————— is_new? - indicates if this is a new resource —————————————————————.
-
#is_provider? ⇒ Boolean
————————————————————— ‘is_provider?’ - indicates if this object instance is a provider object, rather than a specific instance of the object —————————————————————.
- #list! ⇒ Object
-
#name_decorated(name = @name) ⇒ Object
@@@ helper method, probably needs to go into ‘private section @@@ TBD.
-
#need_write? ⇒ Boolean
————————————————————— Provider writer methods —————————————————————.
- #read! ⇒ Object
- #rename!(new_name) ⇒ Object
-
#reorder!(opts) ⇒ Object
————————————————————— Junos reorder method.
-
#select(name) ⇒ Object
————————————————————— ‘select’ a resource from a provider —————————————————————.
-
#to_h(which = :read) ⇒ Object
————————————————————— ‘to_h’ lets us look at the read/write hash structures —————————————————————.
- #to_h_expanded(opts = {}) ⇒ Object
- #to_yaml(opts = {}) ⇒ Object
-
#with(given_list, &block) ⇒ Object
————————————————————— Provider ‘with` - iterate through each managed resource as obtained from the `given_list` instance variable.
- #write! ⇒ Object
-
#xml_at_edit ⇒ Object
————————————————————— XML writer methods —————————————————————.
- #xml_at_top ⇒ Object
-
#xml_build_change(xml_at_here = nil) ⇒ Object
‘xml_build_change’ is used to create the Junos XML configuration structure.
- #xml_change__active(xml) ⇒ Object
- #xml_change__exist(xml) ⇒ Object
-
#xml_change_admin(xml) ⇒ Object
————————————————————— XML common write “change” methods —————————————————————.
- #xml_change_description(xml) ⇒ Object
-
#xml_config_read! ⇒ Object
‘xml_config_read!’ is ued to retrieve the configuration from the Junos device.
-
#xml_element_newname(new_name) ⇒ Object
by default, simply allow the new name.
-
#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.
- #xml_on_create(xml) ⇒ Object
- #xml_on_delete(xml) ⇒ Object
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
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/junos-ez/provider.rb', line 82 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
#catalog ⇒ Object
Returns the value of attribute catalog.
76 77 78 |
# File 'lib/junos-ez/provider.rb', line 76 def catalog @catalog end |
#has ⇒ Object
Returns the value of attribute has.
75 76 77 |
# File 'lib/junos-ez/provider.rb', line 75 def has @has end |
#list ⇒ Object
Provider methods to obtain collection information as ‘list’ - array of named items ‘catalog’ - hash of all items with properties
178 179 180 |
# File 'lib/junos-ez/provider.rb', line 178 def list @list end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
73 74 75 |
# File 'lib/junos-ez/provider.rb', line 73 def name @name end |
#ndev ⇒ Object (readonly)
Returns the value of attribute ndev.
73 74 75 |
# File 'lib/junos-ez/provider.rb', line 73 def ndev @ndev end |
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
73 74 75 |
# File 'lib/junos-ez/provider.rb', line 73 def parent @parent end |
#properties ⇒ Object
Returns the value of attribute properties.
75 76 77 |
# File 'lib/junos-ez/provider.rb', line 75 def properties @properties end |
#providers ⇒ Object
Returns the value of attribute providers.
74 75 76 |
# File 'lib/junos-ez/provider.rb', line 74 def providers @providers end |
#should ⇒ Object
Returns the value of attribute should.
75 76 77 |
# File 'lib/junos-ez/provider.rb', line 75 def should @should end |
Instance Method Details
#[](property) ⇒ Object
- property
-
resource property reader or
- “name”
-
resource selector from provider
117 118 119 120 121 122 123 124 125 |
# File 'lib/junos-ez/provider.rb', line 117 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)
131 132 133 134 135 136 |
# File 'lib/junos-ez/provider.rb', line 131 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
299 300 301 302 303 |
# File 'lib/junos-ez/provider.rb', line 299 def activate! return nil if @should[:_active] == true @should[:_active] = true write! end |
#active? ⇒ Boolean
‘active?’ - is the resource config active in Junos
160 161 162 163 |
# File 'lib/junos-ez/provider.rb', line 160 def active? false unless exists? @has[:_active] end |
#catalog! ⇒ Object
191 192 193 194 |
# File 'lib/junos-ez/provider.rb', line 191 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.
207 208 209 210 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 |
# File 'lib/junos-ez/provider.rb', line 207 def create( name = nil, prop_hash = {} ) ## 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!(*args) ⇒ Object
‘create!’ is just a helper to call create and then write the config assuming create returns ok.
247 248 249 250 251 252 |
# File 'lib/junos-ez/provider.rb', line 247 def create!( *args ) newbie = create( *args ) return nil unless newbie newbie.write! newbie end |
#create_from_hash!(as_hash, opts = {}) ⇒ Object
264 265 266 |
# File 'lib/junos-ez/provider.rb', line 264 def create_from_hash!( as_hash, opts = {} ) write_xml_config! ( as_hash, opts ) end |
#create_from_yaml!(opts = {}) ⇒ Object
YAML / HASH methods
258 259 260 261 262 |
# File 'lib/junos-ez/provider.rb', line 258 def create_from_yaml!( opts = {} ) raise ArgumentError "Missing :filename param" unless opts[:filename] as_hash = YAML.load_file( opts[:filename] ) write_xml_config! ( as_hash, opts ) end |
#deactivate! ⇒ Object
305 306 307 308 309 |
# File 'lib/junos-ez/provider.rb', line 305 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
284 285 286 287 288 289 290 291 292 293 |
# File 'lib/junos-ez/provider.rb', line 284 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
364 365 366 367 |
# File 'lib/junos-ez/provider.rb', line 364 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
154 |
# File 'lib/junos-ez/provider.rb', line 154 def exists?; @has[:_exist]; end |
#init_has ⇒ Object
‘init_has’ is called when creating a new managed object or when a caller attempts to retrieve a non-existing one
387 |
# File 'lib/junos-ez/provider.rb', line 387 def init_has; nil end |
#is_new? ⇒ Boolean
is_new? - indicates if this is a new resource
110 |
# File 'lib/junos-ez/provider.rb', line 110 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
104 |
# File 'lib/junos-ez/provider.rb', line 104 def is_provider?; @name.nil? end |
#list! ⇒ Object
182 183 184 185 |
# File 'lib/junos-ez/provider.rb', line 182 def list! @list.clear @list = build_list end |
#name_decorated(name = @name) ⇒ Object
@@@ helper method, probably needs to go into ‘private section @@@ TBD
168 169 170 |
# File 'lib/junos-ez/provider.rb', line 168 def name_decorated( name = @name ) self.class.to_s + "['" + name + "']" end |
#need_write? ⇒ Boolean
Provider writer methods
432 |
# File 'lib/junos-ez/provider.rb', line 432 def need_write?; not @should.empty? end |
#read! ⇒ Object
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 |
# File 'lib/junos-ez/provider.rb', line 402 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
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 |
# File 'lib/junos-ez/provider.rb', line 318 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
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 |
# File 'lib/junos-ez/provider.rb', line 342 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
142 143 144 145 146 147 148 |
# File 'lib/junos-ez/provider.rb', line 142 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
520 521 522 |
# File 'lib/junos-ez/provider.rb', line 520 def to_h( which = :read ) { @name => (which == :read) ? @has : @should } end |
#to_h_expanded(opts = {}) ⇒ Object
268 269 270 |
# File 'lib/junos-ez/provider.rb', line 268 def ( opts = {} ) to_h( opts ) end |
#to_yaml(opts = {}) ⇒ Object
272 273 274 275 276 277 |
# File 'lib/junos-ez/provider.rb', line 272 def to_yaml( opts = {} ) out_hash = ( 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
375 376 377 378 |
# File 'lib/junos-ez/provider.rb', line 375 def with( given_list, &block ) raise ArgumentError, "not a provider" unless is_provider? given_list.each{ |name| yield select( name ) } end |
#write! ⇒ Object
434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 |
# File 'lib/junos-ez/provider.rb', line 434 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_edit ⇒ Object
XML writer methods
464 |
# File 'lib/junos-ez/provider.rb', line 464 def xml_at_edit; nil; end |
#xml_at_top ⇒ Object
465 |
# File 'lib/junos-ez/provider.rb', line 465 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
483 484 485 486 487 488 489 490 491 492 493 494 495 496 |
# File 'lib/junos-ez/provider.rb', line 483 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
510 511 512 513 514 |
# File 'lib/junos-ez/provider.rb', line 510 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
469 470 471 472 473 474 475 476 |
# File 'lib/junos-ez/provider.rb', line 469 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
502 503 504 |
# File 'lib/junos-ez/provider.rb', line 502 def xml_change_admin( xml ) xml.disable (@should[:admin] == :up ) ? Netconf::JunosConfig::DELETE : nil end |
#xml_change_description(xml) ⇒ Object
506 507 508 |
# File 'lib/junos-ez/provider.rb', line 506 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
398 399 400 |
# File 'lib/junos-ez/provider.rb', line 398 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
316 |
# File 'lib/junos-ez/provider.rb', line 316 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
393 |
# File 'lib/junos-ez/provider.rb', line 393 def xml_get_has_xml( xml ); nil end |
#xml_on_create(xml) ⇒ Object
466 |
# File 'lib/junos-ez/provider.rb', line 466 def xml_on_create( xml ); nil; end |
#xml_on_delete(xml) ⇒ Object
467 |
# File 'lib/junos-ez/provider.rb', line 467 def xml_on_delete( xml ); nil; end |