Class: Junos::Ez::Group::Provider

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

Overview


_PRIVATE 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, #xml_at_edit, #xml_build_change, #xml_change__active, #xml_change__exist, #xml_change_admin, #xml_change_description, #xml_config_read!, #xml_element_newname

Constructor Details

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

Instance Method Details

#_apply_groupObject



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/junos-ez/group.rb', line 182

def _apply_group
  cfg = Netconf::JunosConfig.new(:TOP)
  xml = cfg.doc
  Nokogiri::XML::Builder.with( xml.at_xpath( 'configuration' )) do |dot|
    if @config and @should[:_active] == :true  
      dot.send :'apply-groups', @name
    else 
      dot.send :'apply-groups', @name, Netconf::JunosConfig::DELETE
    end
  end
  begin
    attr = {}
    attr[:action] = 'replace'
    attr[:format] = 'xml'
    result = @ndev.rpc.load_configuration( xml, attr  )
  rescue Netconf::RpcError => e      
    errs = e.rsp.xpath('//rpc-error[error-severity = "error"]')
    raise e unless errs.empty?
    e.rsp
  else
    result
  end
end

#_load(xml) ⇒ Object



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/junos-ez/group.rb', line 158

def _load ( xml )
  return @config = nil if ( @should[:_exist] == false )
  admin = '' 
  if @should[:format].to_s == 'set'
    @config =  "\ndelete groups #{@name}\n" +
                 "edit groups #{@name}\n" + 
                  File.read( @should[:path] ) 
    admin = @should[:_active] == :false ? 'deactivate' : 'activate'
    @config += "\nquit\n"
    @config += "\n#{admin} groups #{@name}"

  elsif @should[:format].to_s == 'text'
    admin = @should[:_active] == :false ? 'inactive' : 'active'
    admin += ": " unless admin.empty? 
    @config = "groups {\n#{admin} replace: #{@name} {\n" + 
              File.read( @should[:path] ) + "\n}\n}"

  elsif @should[:format].to_s == 'xml'
    xml.at_xpath('groups') << File.read( @should[:path])
    @config = xml
  end
  return @config
end

#build_catalogObject



136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/junos-ez/group.rb', line 136

def build_catalog
return @catalog if list!.empty?
list.each do |grp_name|
  @ndev.rpc.get_configuration{ |xml|
    xml.groups {
        xml.name grp_name
      }
  }.xpath('groups').each do |as_xml|
    @catalog[grp_name] = {}
    xml_read_parser( as_xml, @catalog[grp_name] )
  end
end    
@catalog
end

#build_listObject



127
128
129
130
131
132
133
134
# File 'lib/junos-ez/group.rb', line 127

def build_list
  grp_cfgs = @ndev.rpc.get_configuration{|xml| 
    xml.send(:'groups')
  }.xpath('groups/name').collect do |item|
    item.text
  end 
  return grp_cfgs
end

#write!Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/junos-ez/group.rb', line 100

def write!
  return nil if @should.empty?
  
  @should[:_exist] ||= true
  @should[:_active] ||= :true
  # load the conifguration from file and apply under group
  # hirerachy
  rsp = write_xml_config!( xml_at_top.doc.root )    
  
  # copy the 'should' values into the 'has' values now that 
  # they've been written back to Junos
      
  @has.merge! @should 
  @should.clear
  
  return true
end

#write_xml_config!(xml, opts = {}) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/junos-ez/group.rb', line 78

def write_xml_config!( xml, opts = {} )
  if (@should[:_exist] == true)
    _load ( xml )
    @should[:format] = 'xml' unless @should[:format]
    begin
      attr = {}
      attr[:action] = 'replace'
      attr[:format] = @should[:format].to_s
      result = @ndev.rpc.load_configuration( @config.to_s, attr  )
    rescue Netconf::RpcError => e      
      errs = e.rsp.xpath('//rpc-error[error-severity = "error"]')
      raise e unless errs.empty?
      e.rsp
    else
      result
    end
  else
    super(xml) 
  end
  _apply_group
end

#xml_at_topObject


XML top placement




29
30
31
32
33
34
35
36
# File 'lib/junos-ez/group.rb', line 29

def xml_at_top
  xml = Nokogiri::XML::Builder.new {|xml| xml.configuration {
    xml.groups {
      xml.name @name
      return xml
    }
  }}
end

#xml_change_format(xml) ⇒ Object



62
63
# File 'lib/junos-ez/group.rb', line 62

def xml_change_format( xml )
end

#xml_change_path(xml) ⇒ Object


XML property writers




59
60
# File 'lib/junos-ez/group.rb', line 59

def xml_change_path( xml )
end

#xml_get_has_xml(xml) ⇒ Object


XML property readers




42
43
44
# File 'lib/junos-ez/group.rb', line 42

def xml_get_has_xml( xml ) 
  xml.xpath('//groups')[0]
end

#xml_on_create(xml) ⇒ Object


XML on-create




69
70
# File 'lib/junos-ez/group.rb', line 69

def xml_on_create( xml )
end

#xml_on_delete(xml) ⇒ Object


XML on-delete




75
76
# File 'lib/junos-ez/group.rb', line 75

def xml_on_delete( xml )
end

#xml_read_parser(as_xml, as_hash) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/junos-ez/group.rb', line 46

def xml_read_parser( as_xml, as_hash )    
  set_has_status( as_xml, as_hash )  
  
  grp = as_xml.xpath('name').text
  as_hash[:name] = grp unless grp.empty?

end