Skip to content

Commit

Permalink
Merge branch 'feature-logging-125' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
jerearista committed Mar 14, 2018
2 parents ae98ee6 + 9c2f218 commit 7bc4e2d
Show file tree
Hide file tree
Showing 7 changed files with 257 additions and 89 deletions.
49 changes: 15 additions & 34 deletions lib/puppet/provider/eos_config/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,6 @@ def command=(value)
@property_flush[:command] = value
end

def section=(value)
@property_flush[:section] = value
end

def regexp=(value)
@property_flush[:regexp] = value
end

##
# get_config returns the part of the running-config to evaluate.
# If the section property is set then return the part of the
Expand All @@ -81,36 +73,25 @@ def regexp=(value)
#
# @return [String] Part or all of the running config
def get_config
if @property_hash[:section]
return node.get_config(param: @property_hash[:section], as_string: true)
if @resource[:section]
return node.get_config(params: "section #{@resource[:section]}", as_string: true)
end
node.running_config
end
private :get_config

##
# config_exists? checks the regexp against the running config if the
# regexp is known. If not, and the command is known, then check to see
# if the command is set in the running config.
#
# @api private
#
# @return [Boolean] Configuration is set
def config_exists?
def config_exists?(value)
exists = false
cfg = get_config
return exists unless cfg
if !@property_hash[:regexp].nil?
regexp = Regexp.new(@property_hash[:regexp])
exists = true unless cfg.scan(regexp).empty?
else
unless @property_hash[:command].nil?
exists = true unless cfg.scan(@property_hash[:command]).empty?
end
if !@resource[:regexp].nil?
regexp = Regexp.new(@resource[:regexp])
return true if cfg.scan(regexp).empty?
end
unless value.nil?
exists = true unless cfg.scan(value).empty?
end
exists
end
private :config_exists?

##
# run_cmd runs resource[:command] on the switch.
Expand All @@ -119,18 +100,18 @@ def config_exists?
#
# @return [String] Part or all of the running config
def run_cmd
commands = []
commands << @property_hash[:section] if @property_hash[:section]
commands << @property_hash[:command]
node.config(commands)
cmds = []
cmds << @resource[:section] if @resource[:section]
cmds << @property_flush[:command]
node.config(cmds)
end
private :run_cmd

def flush
@property_hash.merge!(@property_flush)

# Run the command if the resource does not exist
run_cmd unless config_exists?
# Run the command
run_cmd

@property_flush = {}
end
Expand Down
53 changes: 45 additions & 8 deletions lib/puppet/provider/eos_logging_host/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,24 +51,61 @@

def self.instances
result = node.api('logging').get
return [] unless result
result[:hosts].each.map do |(name, _)|
provider_hash = { :name => name, :ensure => :present }
new(provider_hash)
result[:hosts].each_with_object([]) do |(host, attr), arry|
provider_hash = { :name => host, :ensure => :present }
provider_hash[:port] = attr[:port]
provider_hash[:protocol] = attr[:protocol]
provider_hash[:vrf] = attr[:vrf] if attr[:vrf]
arry << new(provider_hash)
end
end

def initialize(resource = {})
super(resource)
@property_flush = {}
end

def exists?
@property_hash[:ensure] == :present
end

def port=(value)
@property_flush[:port] = value
end

def protocol=(value)
@property_flush[:protocol] = value
end

def vrf=(value)
@property_flush[:vrf] = value
end

def create
node.api('logging').add_host(resource[:name])
@property_hash = { :name => resource[:name], :ensure => :present }
@property_flush = resource.to_hash
@property_flush[:ensure] = :present
end

def destroy
node.api('logging').remove_host(resource[:name])
@property_hash = { :name => resource[:name], :ensure => :absent }
@property_flush = resource.to_hash
@property_flush[:ensure] = :absent
end

def flush
@property_hash.merge!(@property_flush)
opts = {}
opts[:port] = @property_hash[:port] ? @property_hash[:port] : 514
protocol = @property_hash[:protocol]
opts[:protocol] = protocol ? protocol : :udp
opts[:vrf] = @property_hash[:vrf] if @property_hash[:vrf]

if @property_flush[:ensure] == :absent
node.api('logging').remove_host(resource[:name], opts)
@property_hash = { name: resource[:name], ensure: :absent }
return
end

node.api('logging').add_host(resource[:name], opts)
@property_flush = {}
end
end
62 changes: 36 additions & 26 deletions lib/puppet/type/eos_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,18 @@

Puppet::Type.newtype(:eos_config) do
@doc = <<-EOS
Apply arbitrary configuration commands in Arista EOS. Commands will only
be applied based on the absence or presence of regular expression matches.
configuration for a specific command. If the command are either
present or absent, the eos_config will configure the node using
the command argument.
Apply arbitrary configuration commands to Arista EOS. Commands will only
be applied based on the matching a regular expression, if supplied. If the
command is not already present, within the given section, if provided,
eos_config will configure the node using the supplied command.
Examples:
eos_config { 'Set location':
command => 'snmp-server location Here',
}
# Only manage the description if an description exists
eos_config { 'Set interface description':
section => 'interface Ethernet1',
command => 'description My Description',
Expand All @@ -56,7 +56,7 @@

# Parameters

newparam(:name) do
newparam(:name, namevar: true) do
desc <<-EOS
The name parameter is the name associated with the resource.
EOS
Expand All @@ -65,57 +65,67 @@
unless value.is_a? String
fail "value #{value.inspect} is invalid, must be a String."
end
if @resource.original_parameters[:command].nil?
fail "Required property 'command' is missing."
end
end
end

# Properties (state management)

newproperty(:command) do
newparam(:section) do
desc <<-EOS
Specifies the configuration command to send to the node if the
regexp does not evalute to true.
Restricts the configuration evaluation to a single configuration
section. If the configuration section argument is not provided,
then the global configuration is used.
EOS

validate do |value|
case value
when String
super(value)
validate_features_per_value(value)
else fail "value #{value.inspect} is invalid, must be a String."
end
end
end

newproperty(:section) do
newparam(:regexp) do
desc <<-EOS
Restricts the configuration evaluation to a single configuration
section. If the configuration section argument is not provided,
then the global configuration is used.
Specifies the regular expression to use to evaluate the current nodes
running configuration. This optional argument will default to use the
command argument if none is provided.
EOS

validate do |value|
case value
when String
super(value)
validate_features_per_value(value)
else fail "value #{value.inspect} is invalid, must be a String."
end
end
end

newproperty(:regexp) do
# Properties (state management)

newproperty(:command) do
desc <<-EOS
Specifies the regular expression to use to evaluate the current nodes
running configuration. This optional argument will default to use the
command argument if none is provided.
Specifies the configuration command to send to the node if the
regexp does not evalute to true.
EOS

validate do |value|
case value
when String
super(value)
validate_features_per_value(value)
else fail "value #{value.inspect} is invalid, must be a String."
unless value.is_a? String
fail "value #{value.inspect} is invalid, must be a String."
end
end

# First verify that all of our checks pass.
def retrieve
# Return self.should if the command exists to trick Puppet into thinking we are in_sync?
# Return :notrun to trigger evaluation
# Wouldn't just setting in_sync? work?
if provider.config_exists?(value)
return self.should
else
return :notrun
end
end
end
Expand Down
44 changes: 44 additions & 0 deletions lib/puppet/type/eos_logging_host.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@
Example:
eos_logging_host { '10.0.0.150': }
eos_logging_host { '10.0.0.151':
port => 8514,
protocol => 'tcp',
vrf => 'mgmt',
}
EOS

Expand All @@ -52,7 +57,46 @@
The parameter specifies the name for the logging host. It should be in
either IP format or FQDN format.
EOS

validate do |value|
if value.is_a? String then super(value)
else fail "value #{value.inspect} is invalid, must be a String."
end
end
end

# Properties (state management)

newproperty(:port) do
desc <<-EOS
Port to which logs will be sent on the host. Default: 514
EOS

munge { |value| Integer(value) }

validate do |value|
unless value.to_i.between?(1, 65_535)
fail "value #{value.inspect} must be between 1 and 65535"
end
end
end

newproperty(:protocol) do
desc <<-EOS
Protocol may be 'udp' or 'tcp'. Default: 'udp'
EOS
newvalues(:udp, :tcp)
end

newproperty(:vrf) do
desc <<-EOS
If present, configures the logging host in a non-default VRF.
EOS

validate do |value|
if value.is_a? String then super(value)
else fail "value #{value.inspect} is invalid, must be a String."
end
end
end
end

0 comments on commit 7bc4e2d

Please sign in to comment.