Class: Puppet::Provider::Junos::Group

Inherits:
Puppet::Provider::Junos
  • Object
show all
Defined in:
lib/puppet/provider/junos/junos_group.rb

Instance Method Summary collapse

Instance Method Details

#apply_groupsObject



144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/puppet/provider/junos/junos_group.rb', line 144

def apply_groups
  cfg = Netconf::JunosConfig.new(:TOP)
  xml = cfg.doc
  Nokogiri::XML::Builder.with( xml.at_xpath( 'configuration' )) do |dot|
    if @config and resource[:active] == :true  
      dot.send :'apply-groups', resource[:name] 
    else 
      dot.send :'apply-groups', resource[:name], Netconf::JunosConfig::DELETE
    end
  end
  @@netdev.edit_config( xml, "xml" )
end

#config_update(name) ⇒ Object



157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/puppet/provider/junos/junos_group.rb', line 157

def config_update( name )
  @ndev_res.update name
  @format = get_format
  if (@format == :set ) or (@format == :text )
    @@netdev.edit_config( @config, @format.to_s )
  else
    @@netdev.edit_config( @ndev_res, "xml" )
  end
  
  if @@netdev.edits_count
    apply_groups
  end

end

#flushObject


Device provider methods expected by Puppet




176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/puppet/provider/junos/junos_group.rb', line 176

def flush
  unless @flush_exec
    ## handle netdev_group attribute change     
    if netdev_res_exists?
      if resource[:ensure] == :absent or 
         resource[:active] != @ndev_res[:active]
        Puppet::Provider::Junos.instance_method(:flush).bind(self).call  
      else
        Puppet.debug( "#{self.resource.type}:: Nothing to flush #{resource[:name]}" ) 
      end
    elsif resource[:ensure] == :present
      Puppet::Provider::Junos.instance_method(:flush).bind(self).call
    else
      Puppet.debug( "#{self.resource.type}:: Nothing to flush #{resource[:name]}" )
    end
    @flush_exec = true
  end    
end

#get_formatObject



137
138
139
140
141
142
# File 'lib/puppet/provider/junos/junos_group.rb', line 137

def get_format
   if properties.include? :format and @config
     return resource[:format]
   end
   return :xml
end

#init_resourceObject


called from #netdev_exists?


Raises:

  • (ArgumentError)


56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/puppet/provider/junos/junos_group.rb', line 56

def init_resource
  @ndev_res ||= NetdevJunos::Resource.new( self, "groups" ) 
  
  resource[:format] ||= 'xml'
  ndev_config = @ndev_res.getconfig
     
  raise ArgumentError unless resource[:path]
  
  grp = ndev_config.xpath("//groups[name=\'#{resource[:name]}\']")[0]
  if grp
    @ndev_res.set_active_state( grp )
  end
  @flush_exec = false
  load

  # Check if apply-group is configured
  @@rpc ||= Puppet::Provider::Junos.netdev.netconf.rpc
  apply_grp_config = @@rpc.get_configuration{ |a_grp|
    a_grp.send('apply-groups')
  }   
  apply_grp = apply_grp_config.xpath("//apply-groups=\'#{resource[:name]}\'")
  if grp and apply_grp then return grp else return false end 
 
end

#loadObject



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/puppet/provider/junos/junos_group.rb', line 82

def load
  return @config = nil if ( resource[:ensure] == :absent )
  admin = '' 
 if resource[:format].to_s == 'set'
    @config =  "\ndelete groups #{resource[:name]}\n" +
                 "edit groups #{resource[:name]}\n" + 
                  File.read( resource[:path] ) 
    unless resource[:active] == @ndev_res[:active]
      admin = resource[:active] == :false ? 'deactivate' : 'activate'
      @config += "\nquit\n"
      @config += "\n#{admin} groups #{resource[:name]}"
    end

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

  elsif resource[:format].to_s == 'xml'
    @config = File.read( resource[:path])
  end
end

#netdev_res_exists?Boolean


triggered from Provider #exists?


Returns:

  • (Boolean)


45
46
47
48
49
50
# File 'lib/puppet/provider/junos/junos_group.rb', line 45

def netdev_res_exists?
  resource[:format] ||=  'xml'
  return false unless ( grp = init_resource )
  @ndev_res[:name] = grp.xpath('name').text.chomp
  return true     
end

#netdev_resxml_edit(xml) ⇒ Object



130
131
132
133
134
135
# File 'lib/puppet/provider/junos/junos_group.rb', line 130

def netdev_resxml_edit( xml )
  if @config  
    xml << @config.to_s
  end 
  return xml
end

#netdev_resxml_top(xml) ⇒ Object

override default ‘top’ method



123
124
125
126
127
128
# File 'lib/puppet/provider/junos/junos_group.rb', line 123

def netdev_resxml_top( xml )
  xml.name resource[:name]
  par = xml.instance_variable_get(:@parent)
  par['replace'] = 'replace' unless resource[:ensure] == :absent
  return xml
end

#refreshObject



195
196
197
198
199
200
201
202
# File 'lib/puppet/provider/junos/junos_group.rb', line 195

def refresh
  unless @flush_exec
    ## handle refresh event from file resource types
    Puppet.debug( "#{self.resource.type}: REFRESH #{resource[:name]}" )
    Puppet::Provider::Junos.instance_method(:flush).bind(self).call
    @flush_exec = true
  end
end

#xml_change_format(xml) ⇒ Object



115
116
# File 'lib/puppet/provider/junos/junos_group.rb', line 115

def xml_change_format( xml )
end

#xml_change_path(xml) ⇒ Object


XML builder methods




112
113
# File 'lib/puppet/provider/junos/junos_group.rb', line 112

def xml_change_path( xml )
end