Class: CFA::NmConnection

Inherits:
BaseModel
  • Object
show all
Defined in:
src/lib/cfa/nm_connection.rb

Overview

Class to handle NetworkManager connection configuration files

Examples:

Reading the connection name

file = NmConnection.new("/etc/NetworkManager/system-connections/eth0.nmconnection")
file.load
puts file.connection["id"]

See Also:

Constant Summary collapse

KNOWN_SECTIONS =
[
  "bond", "bridge", "connection", "ethernet", "ethernet_s390_options", "ipv4", "ipv6", "vlan",
  "wifi", "wifi_security"
].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, file_handler: nil) ⇒ NmConnection

Constructor

Parameters:

  • path (String)

    File path

  • file_handler (.read, .write) (defaults to: nil)

    Object to read/write the file.



75
76
77
# File 'src/lib/cfa/nm_connection.rb', line 75

def initialize(path, file_handler: nil)
  super(AugeasParser.new("NetworkManager.lns"), path, file_handler: file_handler)
end

Instance Attribute Details

#file_pathString (readonly)

Returns File path.

Returns:

  • (String)

    File path



41
42
43
# File 'src/lib/cfa/nm_connection.rb', line 41

def file_path
  @file_path
end

Class Method Details

.for(conn) ⇒ NmConnection

Returns the file corresponding to a connection

Parameters:

  • conn (ConnectionConfig::Base)

    Connection configuration

Returns:



48
49
50
51
# File 'src/lib/cfa/nm_connection.rb', line 48

def for(conn)
  path = SYSTEM_CONNECTIONS_PATH.join(file_basename_for(conn)).sub_ext(FILE_EXT)
  new(path)
end

Instance Method Details

#add_collection(section, name, values) ⇒ Object

Sets an array's property under an specific section

Array properties are written as a numbered variable. This method takes care of numbering them according to the variable name.

Examples:

Write IPv4 addresses

ipv4_addresses = ["192.168.1.100/24", "192.168.20.200/32"]
file.add_collection("ipv4", "address", ipv4_addresses)

# Writes:
# [ipv4]
# address1="192.168.1.100/24"
# address2="192.168.20.200/32"

Parameters:

  • section (String)

    section name

  • name (String)

    variable name to be used

  • values (Array<String>)

    variable values



109
110
111
112
113
114
115
# File 'src/lib/cfa/nm_connection.rb', line 109

def add_collection(section, name, values)
  section = section_for(section)

  values.each_with_index do |ip, index|
    section["#{name}#{index + 1}"] = ip
  end
end

#exist?Boolean

Determines whether the file exist

Returns:

  • (Boolean)

    true if the file exist, false otherwise



120
121
122
# File 'src/lib/cfa/nm_connection.rb', line 120

def exist?
  ::File.exist?(::File.join(Yast::WFM.scr_root, file_path))
end

#section_for(name) ⇒ AugeasTree

Returns the augeas tree for the given section

If the given section does not exist, it returns an empty one

Parameters:

  • name (String)

    section name

Returns:



85
86
87
88
89
90
# File 'src/lib/cfa/nm_connection.rb', line 85

def section_for(name)
  sname = name.gsub("_", "-")
  return data[sname] if data[sname]

  data[sname] ||= CFA::AugeasTree.new
end