Class: WireGuard::Admin::Repository
- Inherits:
-
Object
- Object
- WireGuard::Admin::Repository
- Defined in:
- lib/wire_guard/admin/repository.rb
Overview
The place where networks, clients and servers can be found and are persisted
Defined Under Namespace
Classes: NetworkAlreadyExists, NetworkNotSpecified, PeerAlreadyExists, UnknownNetwork, UnknownPeer
Instance Attribute Summary collapse
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Instance Method Summary collapse
-
#add_network(network) ⇒ Object
Add a new network.
-
#add_peer(network, peer) ⇒ Object
Add a peer to the given network.
- #clients(network) ⇒ Object
-
#delete_network(network) ⇒ Object
Delete an existing network.
-
#find_network(network) ⇒ Object
Find a network within all known ones.
-
#find_peer(network, name) ⇒ Object
Find a peer by name within the given network.
-
#initialize(path) ⇒ Repository
constructor
A new instance of Repository.
-
#networks ⇒ Object
Get all networks.
-
#next_address(network) ⇒ Object
Find the next address within the given network that is not assigned to a peer.
-
#peers(network) ⇒ Object
Get all peers of the given network.
-
#remove_peer(network, peer_or_name) ⇒ Object
Remove a peer from the given network.
- #servers(network) ⇒ Object
Constructor Details
#initialize(path) ⇒ Repository
Returns a new instance of Repository.
58 59 60 61 |
# File 'lib/wire_guard/admin/repository.rb', line 58 def initialize(path) @path = path @backend = PStore.new(@path) end |
Instance Attribute Details
#path ⇒ Object (readonly)
Returns the value of attribute path.
56 57 58 |
# File 'lib/wire_guard/admin/repository.rb', line 56 def path @path end |
Instance Method Details
#add_network(network) ⇒ Object
Add a new network
106 107 108 109 110 111 112 113 114 |
# File 'lib/wire_guard/admin/repository.rb', line 106 def add_network(network) raise ArgumentError, 'network must be an IP address range' unless network.is_a?(IPAddr) @backend.transaction do raise NetworkAlreadyExists, network if @backend.root?(network) @backend[network] = [] end end |
#add_peer(network, peer) ⇒ Object
Add a peer to the given network
132 133 134 135 136 137 138 139 140 |
# File 'lib/wire_guard/admin/repository.rb', line 132 def add_peer(network, peer) raise PeerAlreadyExists.new(peer, network) if find_peer(network, peer.name) @backend.transaction do raise UnknownNetwork, network unless @backend.root?(network) @backend[network] << peer end end |
#clients(network) ⇒ Object
176 177 178 |
# File 'lib/wire_guard/admin/repository.rb', line 176 def clients(network) peers(network).select { |p| p.is_a?(Client) } end |
#delete_network(network) ⇒ Object
Delete an existing network
119 120 121 122 123 124 125 126 127 |
# File 'lib/wire_guard/admin/repository.rb', line 119 def delete_network(network) raise ArgumentError, 'network must be an IP address range' unless network.is_a?(IPAddr) @backend.transaction do raise UnknownNetwork, network unless @backend.root?(network) @backend.delete(network) end end |
#find_network(network) ⇒ Object
Find a network within all known ones
75 76 77 78 79 |
# File 'lib/wire_guard/admin/repository.rb', line 75 def find_network(network) raise ArgumentError, 'network must be an IP address range' unless network.is_a?(IPAddr) networks.select { |n| n == network }.first end |
#find_peer(network, name) ⇒ Object
Find a peer by name within the given network
84 85 86 87 88 |
# File 'lib/wire_guard/admin/repository.rb', line 84 def find_peer(network, name) raise ArgumentError, 'network must be an IP address range' unless network.is_a?(IPAddr) peers(network).select { |p| p.name == name }.first end |
#networks ⇒ Object
Get all networks
66 67 68 69 70 |
# File 'lib/wire_guard/admin/repository.rb', line 66 def networks @backend.transaction do @backend.roots end end |
#next_address(network) ⇒ Object
Find the next address within the given network that is not assigned to a peer
164 165 166 167 168 169 170 |
# File 'lib/wire_guard/admin/repository.rb', line 164 def next_address(network) raise ArgumentError, 'network must be an IP address range' unless network.is_a?(IPAddr) peers(network).inject(network.succ) do |candidate, peer| candidate == peer.ip ? candidate.succ : peer.ip end end |
#peers(network) ⇒ Object
Get all peers of the given network
93 94 95 96 97 98 99 100 101 |
# File 'lib/wire_guard/admin/repository.rb', line 93 def peers(network) raise ArgumentError, 'network must be an IP address range' unless network.is_a?(IPAddr) @backend.transaction do raise UnknownNetwork, network unless @backend.root?(network) @backend[network] end end |
#remove_peer(network, peer_or_name) ⇒ Object
Remove a peer from the given network
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/wire_guard/admin/repository.rb', line 145 def remove_peer(network, peer_or_name) name = if peer_or_name.respond_to?(:name) peer_or_name.name else peer_or_name end raise UnknownPeer.new(name, network) unless find_peer(network, name) @backend.transaction do raise UnknownNetwork, network unless @backend.root?(network) @backend[network].delete(name) end end |
#servers(network) ⇒ Object
172 173 174 |
# File 'lib/wire_guard/admin/repository.rb', line 172 def servers(network) peers(network).select { |p| p.is_a?(Server) } end |