Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Rework network_route provider with resource_api #245

Closed
wants to merge 15 commits into from
Closed
Prev Previous commit
Next Next commit
Fully functioning provider for network_route
dhollinger committed May 30, 2018
commit d16f67c6a532174a5f0c58a67d09f47ff5a985fe
44 changes: 40 additions & 4 deletions lib/puppet/provider/network_route/network_route.rb
Original file line number Diff line number Diff line change
@@ -9,7 +9,7 @@ class Puppet::Provider::NetworkRoute::NetworkRoute
def routes_list
routes = []
Net::IP.routes.each do |route|
routes.push(route.instance_variables.each_with_object({}) { |var, hash| hash[var.to_s.delete("@")] = route.instance_variable_get(var) })
routes.push(route.to_h)
end
routes
end
@@ -43,18 +43,54 @@ def puppet_munge(should)
if should[:default_route]
should[:prefix] = 'default'
should.delete(:default_route)
should.delete(:prefix)
else
should[:prefix] = should.delete(:prefix)
end
should[:via] = should.delete(:gateway) if should[:gateway]
should[:dev] = should.delete(:interface) if should[:interface]
should[:metric] = should.delete(:metric)
should[:table] = should.delete(:table)
should[:table] = should.delete(:table) if should[:table]
should[:src] = should.delete(:source) if should[:source]
should[:scope] = should.delete(:scope)
should[:scope] = should.delete(:scope) if should[:scope]
should[:proto] = should.delete(:protocol)
should[:mtu] = should.delete(:mtu) if should[:mtu]
should
end

def set(context, changes)
changes.each do |name, change|
is = change.key?(:is) ? change[:is] : get_single(name)
should = change[:should]

is = { prefix: name, ensure: 'absent' } if is.nil?
should = { prefix: name, ensure: 'absent' } if should.nil?

if is[:ensure].to_s == 'absent' && should[:ensure].to_s == 'present'
create(context, name, should)
elsif is[:ensure] == should[:ensure] && is != should
update(context, name, should)
elsif is[:ensure].to_s == 'present' && should[:ensure].to_s == 'absent'
delete(context, name, should)
end
end
end

def create(context, name, should)
puppet_munge(should)
route = Net::IP::Route.new(should)
Net::IP.routes.add(route)
end

def update(context, name, should)
puppet_munge(should)
route = Net::IP::Route.new(should)
Net::IP.routes.flush(route.prefix)
Net::IP.routes.add(route)
end

def delete(context, name, should)
puppet_munge(should)
route = Net::IP::Route.new(should)
Net::IP.routes.flush(route.prefix)
end
end
8 changes: 3 additions & 5 deletions lib/puppet/type/network_route.rb
Original file line number Diff line number Diff line change
@@ -34,23 +34,21 @@
default: '100',
},
table: {
type: 'String',
type: 'Optional[String]',
desc: 'table to add this route.',
default: 'local',
},
source: {
type: 'Optional[String]',
desc: 'the source address to prefer using when sending to the destinations covered by route prefix.',
},
scope: {
type: 'Enum["global", "nowhere", "host", "link", "site"]',
type: 'Optional[Enum["global", "nowhere", "host", "link", "site"]]',
desc: 'scope of the destinations covered by the route prefix.',
default: 'global',
},
protocol: {
type: 'String',
desc: 'routing protocol identifier of this route.',
default: 'boot',
default: 'static',
},
mtu: {
type: 'Optional[String]',