Skip to content

Commit

Permalink
Improve cfa model
Browse files Browse the repository at this point in the history
  • Loading branch information
joseivanlopez committed Dec 1, 2016
1 parent d17d130 commit e380186
Showing 1 changed file with 33 additions and 37 deletions.
70 changes: 33 additions & 37 deletions src/lib/cfa/ntp_conf.rb
Expand Up @@ -9,20 +9,22 @@ module CFA
# @see http://www.rubydoc.info/github/config-files-api/config_files_api/CFA/AugeasParser
class NtpConf < ::CFA::BaseModel
PARSER = CFA::AugeasParser.new("ntp.lns")

PATH = "/etc/ntp.conf".freeze
COLLECTION_KEYS = %w(

RECORD_ENTRIES = %w(
server
peer
broadcast
broadcastclient
manycast
manycastclient
multicastclient
manycastserver
fudge
restrict
#comment
action
).freeze

COLLECTION_KEYS = (RECORD_ENTRIES + %w(#comment action)).freeze

def initialize(file_handler: nil)
super(PARSER, PATH, file_handler: file_handler)
end
Expand Down Expand Up @@ -63,17 +65,6 @@ def fix_collection_keys(entries)
class RecordCollection
include Enumerable

RECORD_ENTRIES = %w(
server
peer
broadcast
manycastclient
multicastclient
manycastserver
fudge
restrict
).freeze

def initialize(augeas_tree)
@augeas_tree = augeas_tree
end
Expand Down Expand Up @@ -114,25 +105,19 @@ def ==(other)
private

def record_entries
@augeas_tree.data.select do |d|
RECORD_ENTRIES.include?(d[:key].gsub("[]", ""))
matcher = Matcher.new do |k, _v|
RECORD_ENTRIES.include?(k.gsub("[]", ""))
end
@augeas_tree.select(matcher)
end
end

# Base class to represent a general ntp entry.
class Record
def self.record_class(key)
case key.gsub("[]", "")
when "server" then ServerRecord
when "peer" then PeerRecord
when "broadcast" then BroadcastRecord
when "broadcastclient" then BroadcastclientRecord
when "manycast" then ManycastRecord
when "manycastclient" then ManycastclientRecord
when "fudge" then FudgeRecord
when "restrict" then RestrictRecord
end
entry_type = key.gsub("[]", "")
record_class = ["::CFA::NtpConf::", entry_type.capitalize, "Record"].join
Kernel.const_get(record_class)
end

def self.new_from_augeas(augeas_entry)
Expand Down Expand Up @@ -209,20 +194,27 @@ def create_tree_value
def split_raw_options(raw_options)
raw_options.to_s.strip.gsub(/\s+/, " ").split(" ")
end

def augeas_options
tree_value.tree.select(options_matcher)
end

def options_matcher
Matcher.new { |k, _v| !k.include?("#comment") }
end
end

# Base class to represent a ntp command entry, as
# server, peer, broadcast, etc.
class CommandRecord < Record
def options
return [] unless tree_value?
current_options = tree_value.tree.data.reject { |d| d[:key].include?("#comment") }
current_options.map { |option| option[:key] }
augeas_options.map { |option| option[:key] }
end

def options=(options)
create_tree_value unless tree_value?
tree_value.tree.data.reject! { |d| d[:key] != "#comment" }
tree_value.tree.delete(options_matcher)
options.each { |option| tree_value.tree.add(option, nil) }
end
end
Expand Down Expand Up @@ -271,15 +263,14 @@ def initialize

def options
return {} unless tree_value?
current_options = tree_value.tree.data.reject { |d| d[:key].to_s.include?("#comment") }
current_options.each_with_object({}) do |option, opts|
augeas_options.each_with_object({}) do |option, opts|
opts[option[:key]] = option[:value]
end
end

def options=(options)
create_tree_value unless tree_value?
tree_value.tree.data.reject! { |d| d[:key] != "#comment" }
tree_value.tree.delete(options_matcher)
options.each { |k, v| tree_value.tree.add(k, v) }
end

Expand All @@ -303,15 +294,20 @@ def initialize

def options
return [] unless tree_value?
current_options = tree_value.tree.data.select { |d| d[:key] == "action[]" }
current_options.map { |option| option[:value] }
augeas_options.map { |option| option[:value] }
end

def options=(options)
create_tree_value unless tree_value?
tree_value.tree.data.reject! { |d| d[:key].include?("action") }
tree_value.tree.delete(options_matcher)
options.each { |option| tree_value.tree.add("action[]", option) }
end

private

def options_matcher
Matcher.new { |k, _v| k.include?("action") }
end
end
end
end

0 comments on commit e380186

Please sign in to comment.