Skip to content

Commit

Permalink
changes from review
Browse files Browse the repository at this point in the history
  • Loading branch information
jreidinger committed Oct 12, 2016
1 parent 183af41 commit 69fb68e
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 15 deletions.
29 changes: 23 additions & 6 deletions src/lib/cfa/hosts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,22 @@ def initialize(file_handler: nil)
super(PARSER, PATH, file_handler: file_handler)
end

# returns old format of hosts used in Host module.
# @return [Hash<Array<String>>] format is hash where key is ip and value
# is array which contain strings and each string represent one line in hosts
# table with comma separated hostnames, where first is canonical and rest
# are aliases
def hosts
matcher = Matcher.new { |k,v| k =~ /^\d*$/ }
matcher = Matcher.new { |k, _v| k =~ /^\d*$/ }
data.select(matcher).each_with_object({}) do |host, result|
entry = host[:value]
result[entry["ipaddr"]] ||= []
result[entry["ipaddr"]] << single_host_entry(entry)
end
end

# Returns single entry from hosts for given ip or empty array if not found
# @see {#hosts}
def host(ip)
hosts = data.select(ip_matcher(ip))

Expand All @@ -32,6 +39,7 @@ def host(ip)
end
end

# deletes all occurences of given ip in host table
def delete_host(ip)
entries = data.select(ip_matcher(ip))
if entries.empty?
Expand Down Expand Up @@ -95,10 +103,13 @@ def add_host(ip, canonical, aliases = [])
data.add(unique_id, entry_line)
end

# removes hostname from all entries in hosts table.
# if it is only hostname for given ip, ip is removed
# if it is canonical part, then first alias is used as canonical hostname
def remove_hostname(hostname)
entries = data.select(hostname_matcher(hostname))
entries.each do |entry|
entry = entry[:value]
entries.each do |pair|
entry = pair[:value]
if entry["canonical"] == hostname
aliases = aliases_for(entry)
if aliases.empty?
Expand All @@ -116,26 +127,32 @@ def remove_hostname(hostname)

private

# returns matcher for cfa to find entries with given ip
def ip_matcher(ip)
Matcher.new { |k, v| v["ipaddr"] == ip }
Matcher.new { |_k, v| v["ipaddr"] == ip }
end

# returns matcher for cfa to find entries with given hostname
def hostname_matcher(hostname)
Matcher.new do |k, v|
Matcher.new do |_k, v|
v["canonical"] == hostname || aliases_for(v).include?(hostname)
end
end

# returns aliases as array even if there is only one
def aliases_for(entry)
entry["alias[]"] ? entry.collection("alias").map{ |a| a } : [entry["alias"]].compact
entry["alias[]"] ? entry.collection("alias").map { |a| a } : [entry["alias"]].compact
end

# generate old format string with first canonical and then aliases
# all separated by space
def single_host_entry(entry)
result = [entry["canonical"]]
result.concat(aliases_for(entry))
result.join(" ")
end

# helper to generate unique id for cfa entry
def unique_id
id = 1
loop do
Expand Down
4 changes: 0 additions & 4 deletions src/modules/Host.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@
# you may find current contact information at www.novell.com
#
# **************************************************************************
# File: modules/Host.ycp
# Package: Network configuration
# Summary: Hosts data (/etc/hosts)
# Authors: Michal Svec <msvec@suse.cz>
#
require "yast"
require "yast2/execute"
Expand Down
10 changes: 5 additions & 5 deletions test/host_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
end

describe ".name_map" do
# FIXME make value API better
# FIXME: make value API better
it "returns hash with ip as key and hostnames as value" do
Yast::Host.Read

Expand Down Expand Up @@ -78,10 +78,10 @@
Yast::Host.add_name("10.100.128.72", "test3 test3.suse.cz")

expect(Yast::Host.names("10.100.128.72")).to eq([
"pepa.labs.suse.cz pepa pepa2",
"test test2.suse.cz",
"test3 test3.suse.cz"
])
"pepa.labs.suse.cz pepa pepa2",
"test test2.suse.cz",
"test3 test3.suse.cz"
])
end
end

Expand Down

0 comments on commit 69fb68e

Please sign in to comment.