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 #263

Open
wants to merge 20 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .sync.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ spec/spec_helper.rb:
spec_overrides: "require 'spec_helper_methods'"
Gemfile:
optional:
':development':
- gem: 'puppet-resource_api'
':test':
- gem: 'ipaddress'
- gem: 'rspec-its'
- gem: 'puppet-resource_api'
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,17 @@ group :test do
gem 'simplecov-console', :require => false
gem 'parallel_tests', :require => false
gem 'ipaddress', :require => false
gem 'net-ip', :require => false
gem 'rspec-its', :require => false
gem 'puppet-resource_api', :require => false
end

group :development do
gem 'travis', :require => false
gem 'travis-lint', :require => false
gem 'guard-rake', :require => false
gem 'overcommit', '>= 0.39.1', :require => false
gem 'puppet-resource_api', :require => false
end

group :system_tests do
Expand Down
74 changes: 0 additions & 74 deletions lib/facter/network.rb

This file was deleted.

95 changes: 95 additions & 0 deletions lib/puppet/provider/network_route/network_route.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
require 'net/ip'
require 'puppet/resource_api/simple_provider'

# Implementation for the network_route type using the Resource API.
class Puppet::Provider::NetworkRoute::NetworkRoute
# include PuppetX::FileMapper

def routes_list
routes = []
Net::IP.routes.each do |route|
routes.push(route.to_h)
end
routes
end

def get(_context)
routes_list.map do |route|
default = if route[:prefix] == 'default'
true
else
false
end

{
ensure: 'present',
prefix: route[:prefix],
default_route: default,
gateway: route[:via],
interface: route[:dev],
metric: route[:metric],
table: route[:table],
source: route[:src],
scope: route[:scope],
protocol: route[:proto],
mtu: route[:mtu]
}.compact!
end
end

def puppet_munge(should)
should.delete(:ensure)
if should[:default_route]
should[:prefix] = 'default'
should.delete(:default_route)
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) if should[:table]
should[:src] = should.delete(:source) if should[:source]
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
104 changes: 0 additions & 104 deletions lib/puppet/provider/network_route/redhat.rb

This file was deleted.

Loading