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

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 14 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
2 changes: 2 additions & 0 deletions .sync.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ spec/spec_helper.rb:
spec_overrides: "require 'spec_helper_methods'"
Gemfile:
optional:
':development':
- gem: 'puppet-resource_api'
':test':
- gem: 'ipaddress'
- gem: 'rspec-its'
4 changes: 3 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def location_for(place, fake_version = nil)
end

group :test do
gem 'puppetlabs_spec_helper', '~> 2.5.0', :require => false
gem 'puppetlabs_spec_helper', '~> 2.9.1', :require => false
gem 'rspec-puppet', '~> 2.5', :require => false
gem 'rspec-puppet-facts', :require => false
gem 'rspec-puppet-utils', :require => false
Expand All @@ -31,6 +31,7 @@ group :test do
gem 'rack', '~> 1.0', :require => false if RUBY_VERSION < '2.2.2'
gem 'parallel_tests', :require => false
gem 'ipaddress', :require => false
gem 'net-ip', :require => false
gem 'rspec-its', :require => false
end

Expand All @@ -39,6 +40,7 @@ group :development do
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
68 changes: 0 additions & 68 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