Class: JSS::NetworkSegment
- Defined in:
- lib/jss/api_object/network_segment.rb,
lib/jss.rb
Overview
A Network Segment in the JSS
Constant Summary collapse
- RSRC_BASE =
the REST resource base
'networksegments'.freeze
- RSRC_LIST_KEY =
the hash key used for the JSON list output of all objects in the JSS
:network_segments
- RSRC_OBJECT_KEY =
The hash key used for the JSON object output. It’s also used in various error messages
:network_segment
- VALID_DATA_KEYS =
these keys, as well as :id and :name, are present in valid API JSON data for this class
[:distribution_point, :starting_address, :override_departments].freeze
- OBJECT_HISTORY_OBJECT_TYPE =
the object type for this object in the object history table. See APIObject#add_object_history_entry
43
Instance Attribute Summary collapse
-
#building ⇒ String
Building for this segment.
-
#department ⇒ String
Department for this segment.
-
#distribution_point ⇒ String
The name of the distribution point to be used from this network segment.
-
#ending_address ⇒ IPAddr
Ending IP adresss.
-
#need_to_update ⇒ Boolean
included
from Updatable
readonly
Do we have unsaved changes?.
-
#netboot_server ⇒ String
The netboot server for this segment.
-
#override_buildings ⇒ Boolean
Should machines checking in from this segment update their building.
-
#override_departments ⇒ Boolean
Should machines checking in from this segment update their dept.
-
#starting_address ⇒ IPAddr
Starting IP adresss.
-
#swu_server ⇒ String
The swupdate server for this segment.
-
#url ⇒ String
readonly
The mount url for the distribution point.
Class Method Summary collapse
-
.ip_range(starting_address: nil, ending_address: nil, mask: nil, cidr: nil) ⇒ Range<IPAddr>
Given a starting address & ending address, mask, or cidr, return a Range object of IPAddr objects.
-
.masked_starting_address(starting_address: nil, mask: nil, cidr: nil) ⇒ String
If we are given a mask or cidr, append them to the starting_address.
-
.my_network_segment(api: JSS.api) ⇒ Object
deprecated
Deprecated.
use my_network_segments
-
.my_network_segments(api: JSS.api) ⇒ Array<Integer>
Find the current network segment ids for the machine running this code.
-
.network_ranges(refresh = false, api: JSS.api) ⇒ Hash{Integer => Range}
All NetworkSegments in the jss as IPAddr object Ranges representing the Segment, e.g.
-
.network_segment_for_ip(ip, api: JSS.api) ⇒ Object
deprecated
Deprecated.
use network_segments_for_ip
-
.network_segments_for_ip(ip, refresh = false, api: JSS.api) ⇒ Array<Integer>
Find the ids of the network segments that contain a given IP address.
-
.subnets(refresh = false, api: JSS.api) ⇒ Object
An alias for NetworkSegment.network_ranges.
-
.validate_ip_range(startip, endip) ⇒ void
Raise an exception if a given starting ip is higher than a given ending ip.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
Does this network segment equal another? equality means the ranges are equal.
-
#cidr=(newval) ⇒ void
(also: #mask=)
set the ending address by applying a new cidr (e.g. 24) or mask (e.g. 255.255.255.0).
-
#clone(new_name, api: nil) ⇒ APIObject
included
from Creatable
make a clone of this API object, with a new name.
-
#create(api: nil) ⇒ Integer
included
from Creatable
Create a new object in the JSS.
-
#include?(thing) ⇒ Boolean
Does this network segment include an address or another segment? Inclusion means the other is completely inside this one.
-
#initialize(args = {}) ⇒ NetworkSegment
constructor
Instantiate a NetworkSegment.
-
#name=(newname) ⇒ void
included
from Updatable
Change the name of this item Remember to #update to push changes to the server.
-
#overlap?(other_segment) ⇒ Boolean
Does this network segment overlap with another?.
-
#range ⇒ Range<IPAddr>
(also: #to_range)
a Range built from the start and end addresses.
-
#set_ip_range(starting_address: nil, ending_address: nil, mask: nil, cidr: nil) ⇒ void
set a new starting and ending addr at the same time.
-
#update ⇒ Boolean
included
from Updatable
Save changes to the JSS.
Constructor Details
#initialize(args = {}) ⇒ NetworkSegment
Instantiate a NetworkSegment
addresses can be provided when using id: :new
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 |
# File 'lib/jss/api_object/network_segment.rb', line 262 def initialize(args = {}) super args if args[:id] == :new range = self.class.ip_range( starting_address: args[:starting_address], ending_address: args[:ending_address], mask: args[:mask], cidr: args[:cidr] ) @init_data[:starting_address] = range.begin.to_s @init_data[:ending_address] = range.end.to_s end @starting_address = IPAddr.new @init_data[:starting_address] @ending_address = IPAddr.new @init_data[:ending_address] @building = @init_data[:building] @department = @init_data[:department] @distribution_point = @init_data[:distribution_point] @netboot_server = @init_data[:netboot_server] @override_buildings = @init_data[:override_buildings] @override_departments = @init_data[:override_departments] @swu_server = @init_data[:swu_server] @url = @init_data[:url] end |
Instance Attribute Details
#building ⇒ String
Returns building for this segment. Must be one of the buildings in the JSS.
234 235 236 |
# File 'lib/jss/api_object/network_segment.rb', line 234 def building @building end |
#department ⇒ String
Returns department for this segment. Must be one of the depts in the JSS.
237 238 239 |
# File 'lib/jss/api_object/network_segment.rb', line 237 def department @department end |
#distribution_point ⇒ String
Returns the name of the distribution point to be used from this network segment.
240 241 242 |
# File 'lib/jss/api_object/network_segment.rb', line 240 def distribution_point @distribution_point end |
#ending_address ⇒ IPAddr
Returns ending IP adresss.
231 232 233 |
# File 'lib/jss/api_object/network_segment.rb', line 231 def ending_address @ending_address end |
#need_to_update ⇒ Boolean (readonly) Originally defined in module Updatable
Returns do we have unsaved changes?.
#netboot_server ⇒ String
Returns the netboot server for this segment.
246 247 248 |
# File 'lib/jss/api_object/network_segment.rb', line 246 def netboot_server @netboot_server end |
#override_buildings ⇒ Boolean
Returns should machines checking in from this segment update their building.
255 256 257 |
# File 'lib/jss/api_object/network_segment.rb', line 255 def override_buildings @override_buildings end |
#override_departments ⇒ Boolean
Returns should machines checking in from this segment update their dept.
252 253 254 |
# File 'lib/jss/api_object/network_segment.rb', line 252 def override_departments @override_departments end |
#starting_address ⇒ IPAddr
Returns starting IP adresss.
228 229 230 |
# File 'lib/jss/api_object/network_segment.rb', line 228 def starting_address @starting_address end |
#swu_server ⇒ String
Returns the swupdate server for this segment.
249 250 251 |
# File 'lib/jss/api_object/network_segment.rb', line 249 def swu_server @swu_server end |
#url ⇒ String (readonly)
Returns the mount url for the distribution point.
243 244 245 |
# File 'lib/jss/api_object/network_segment.rb', line 243 def url @url end |
Class Method Details
.ip_range(starting_address: nil, ending_address: nil, mask: nil, cidr: nil) ⇒ Range<IPAddr>
Given a starting address & ending address, mask, or cidr, return a Range object of IPAddr objects.
starting_address: must be provided, and may be a masked address, in which case nothing else is needed.
If starting_address: is an unmasked address, then one of ending_address: cidr: or mask: must be provided.
If given, ending_address: overrides mask:, cidr:, and a masked starting_address:
These give the same result:
ip_range starting_address: ‘192.168.1.0’, ending_address: ‘192.168.1.255’ ip_range starting_address: ‘192.168.1.0’, mask: ‘255.255.255.0’ ip_range starting_address: ‘192.168.1.0’, cidr: 24 ip_range starting_address: ‘192.168.1.0/24’ ip_range starting_address: ‘192.168.1.0/255.255.255.0’
All the above will produce:
#<IPAddr: IPv4:192.168.1.0/255.255.255.255>..#<IPAddr: IPv4:192.168.1.255/255.255.255.255>
An exception is raised if the starting address is above the ending address.
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/jss/api_object/network_segment.rb', line 137 def self.ip_range(starting_address: nil, ending_address: nil, mask: nil, cidr: nil) raise JSS::MissingDataError, 'starting_address: must be provided' unless starting_address starting_address = masked_starting_address(starting_address: starting_address, mask: mask, cidr: cidr) if ending_address startip = IPAddr.new starting_address.split('/').first endip = IPAddr.new ending_address.to_s validate_ip_range(startip, endip) else raise ArgumentError, 'Must provide ending_address:, mask:, cidr: or a masked starting_address:' unless starting_address.include? '/' subnet = IPAddr.new starting_address startip = subnet.to_range.first.mask 32 endip = subnet.to_range.last.mask 32 end startip..endip end |
.masked_starting_address(starting_address: nil, mask: nil, cidr: nil) ⇒ String
If we are given a mask or cidr, append them to the starting_address
168 169 170 171 |
# File 'lib/jss/api_object/network_segment.rb', line 168 def self.masked_starting_address(starting_address: nil, mask: nil, cidr: nil) starting_address = "#{starting}/#{mask || cidr}" if mask || cidr starting_address.to_s end |
.my_network_segment(api: JSS.api) ⇒ Object
use my_network_segments
Backward compatibility
220 221 222 |
# File 'lib/jss/api_object/network_segment.rb', line 220 def self.my_network_segment(api: JSS.api) my_network_segments api: api end |
.my_network_segments(api: JSS.api) ⇒ Array<Integer>
Find the current network segment ids for the machine running this code
214 215 216 |
# File 'lib/jss/api_object/network_segment.rb', line 214 def self.my_network_segments(api: JSS.api) network_segment_for_ip JSS::Client.my_ip_address, api: api end |
.network_ranges(refresh = false, api: JSS.api) ⇒ Hash{Integer => Range}
All NetworkSegments in the jss as IPAddr object Ranges representing the Segment, e.g. with starting = 10.24.9.1 and ending = 10.24.15.254 the range looks like:
<IPAddr: IPv4:10.24.9.1/255.255.255.255>..#<IPAddr: IPv4:10.24.15.254/255.255.255.255>
Using the #include? method on those Ranges is very useful.
86 87 88 |
# File 'lib/jss/api_object/network_segment.rb', line 86 def self.network_ranges(refresh = false, api: JSS.api) api.network_ranges refresh end |
.network_segment_for_ip(ip, api: JSS.api) ⇒ Object
use network_segments_for_ip
Backward compatibility
206 207 208 |
# File 'lib/jss/api_object/network_segment.rb', line 206 def self.network_segment_for_ip(ip, api: JSS.api) network_segments_for_ip(ip, api: api) end |
.network_segments_for_ip(ip, refresh = false, api: JSS.api) ⇒ Array<Integer>
Find the ids of the network segments that contain a given IP address.
Even tho IPAddr.include? will take a String or an IPAddr I convert the ip to an IPAddr so that an exception will be raised if the ip isn’t a valid ip.
200 201 202 |
# File 'lib/jss/api_object/network_segment.rb', line 200 def self.network_segments_for_ip(ip, refresh = false, api: JSS.api) api.network_segments_for_ip ip, refresh end |
.subnets(refresh = false, api: JSS.api) ⇒ Object
An alias for network_ranges
DEPRECATED: This will be going away in a future release.
96 97 98 |
# File 'lib/jss/api_object/network_segment.rb', line 96 def self.subnets(refresh = false, api: JSS.api) network_ranges refresh, api: api end |
.validate_ip_range(startip, endip) ⇒ void
This method returns an undefined value.
Raise an exception if a given starting ip is higher than a given ending ip
181 182 183 184 |
# File 'lib/jss/api_object/network_segment.rb', line 181 def self.validate_ip_range(startip, endip) return nil if IPAddr.new(startip.to_s) <= IPAddr.new(endip.to_s) raise JSS::InvalidDataError, "Starting IP #{startip} is higher than ending ip #{endip} " end |
Instance Method Details
#==(other) ⇒ Boolean
Does this network segment equal another? equality means the ranges are equal
334 335 336 337 338 |
# File 'lib/jss/api_object/network_segment.rb', line 334 def ==(other) raise TypeError, 'Argument must be a JSS::NetworkSegment' unless \ other.is_a? JSS::NetworkSegment range == other.range end |
#cidr=(newval) ⇒ void Also known as: mask=
This method returns an undefined value.
set the ending address by applying a new cidr (e.g. 24) or mask (e.g. 255.255.255.0)
461 462 463 464 465 466 |
# File 'lib/jss/api_object/network_segment.rb', line 461 def cidr=(newval) new_end = IPAddr.new("#{@starting_address}/#{newval}").to_range.end.mask 32 self.class.validate_ip_range(@starting_address, new_end) @ending_address = new_end @need_to_update = true end |
#clone(new_name, api: nil) ⇒ APIObject Originally defined in module Creatable
make a clone of this API object, with a new name. The class must be creatable
#create(api: nil) ⇒ Integer Originally defined in module Creatable
Create a new object in the JSS.
#include?(thing) ⇒ Boolean
Does this network segment include an address or another segment? Inclusion means the other is completely inside this one.
318 319 320 321 322 323 324 325 |
# File 'lib/jss/api_object/network_segment.rb', line 318 def include?(thing) if thing.is_a? JSS::NetworkSegment @starting_address <= thing.range.begin && @ending_address >= thing.range.end else thing = IPAddr.new thing.to_s range.include? thing end end |
#name=(newname) ⇒ void Originally defined in module Updatable
This method returns an undefined value.
Change the name of this item Remember to #update to push changes to the server.
#overlap?(other_segment) ⇒ Boolean
Does this network segment overlap with another?
304 305 306 307 308 309 |
# File 'lib/jss/api_object/network_segment.rb', line 304 def overlap?(other_segment) raise TypeError, 'Argument must be a JSS::NetworkSegment' unless \ other_segment.is_a? JSS::NetworkSegment other_range = other_segment.range range.include?(other_range.begin) || range.include?(other_range.end) end |
#range ⇒ Range<IPAddr> Also known as: to_range
a Range built from the start and end addresses. To be used for finding inclusion and overlaps.
294 295 296 |
# File 'lib/jss/api_object/network_segment.rb', line 294 def range @starting_address..@ending_address end |
#set_ip_range(starting_address: nil, ending_address: nil, mask: nil, cidr: nil) ⇒ void
This method returns an undefined value.
set a new starting and ending addr at the same time.
and ending addresses.
485 486 487 488 489 490 491 492 493 494 495 |
# File 'lib/jss/api_object/network_segment.rb', line 485 def set_ip_range(starting_address: nil, ending_address: nil, mask: nil, cidr: nil) range = self.class.ip_range( starting_address: starting_address, ending_address: ending_address, mask: mask, cidr: cidr ) @starting_address = range.first @ending_address = range.last @need_to_update = true end |
#update ⇒ Boolean Originally defined in module Updatable
Save changes to the JSS