Class: JSS::NetworkSegment

Inherits:
APIObject show all
Includes:
Comparable, Creatable, Updatable
Defined in:
lib/jss-api/api_object/network_segment.rb,
lib/jss-api.rb

Overview

A Network Segment in the JSS

See Also:

Constant Summary collapse

RSRC_BASE =

the REST resource base

"networksegments"
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 ]
@@network_ranges =

Class Variables

nil

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ NetworkSegment

Returns a new instance of NetworkSegment.



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/jss-api/api_object/network_segment.rb', line 179

def initialize(args = {} )

  super args

  if args[:id] == :new
    raise MissingDataError, "Missing :starting_address." unless args[:starting_address]
    raise MissingDataError, "Missing :ending_address or :cidr." unless args[:ending_address] or args[:cidr]
    @init_data[:starting_address] = args[:starting_address]
    @init_data[:ending_address] = args[:ending_address]
    @init_data[:cidr] = args[:cidr].to_i
  end

  @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]
  @starting_address = IPAddr.new @init_data[:starting_address]
  @swu_server = @init_data[:swu_server]
  @url = @init_data[:url]

  ### by now, we must have either an ending address or a cidr
  ### along with a starting address, so figure out the other one.
  if @init_data[:ending_address]
    @ending_address = IPAddr.new @init_data[:ending_address]
    @cidr = IPAddr.jss_cidr_from_ends(@starting_address,@ending_address)
  else
    @cidr = @init_data[:cidr].to_i if @init_data[:cidr]
    @ending_address = IPAddr.jss_ending_address(@starting_address, @cidr)
  end # if args[:cidr]

  ### we now have all our data, make our unique identifier, the startingaddr/cidr
  @uid = "#{@starting_address}/#{@cidr}"

  ### the IPAddr object for this whole net segment
  @subnet = IPAddr.new @uid

end

Instance Attribute Details

#buildingString

Returns building for this segment. Must be one of the buildings in the JSS.

Returns:

  • (String)

    building for this segment. Must be one of the buildings in the JSS



147
148
149
# File 'lib/jss-api/api_object/network_segment.rb', line 147

def building
  @building
end

#cidrInteger

Returns the CIDR.

Returns:

  • (Integer)

    the CIDR



144
145
146
# File 'lib/jss-api/api_object/network_segment.rb', line 144

def cidr
  @cidr
end

#departmentString

Returns department for this segment. Must be one of the depts in the JSS.

Returns:

  • (String)

    department for this segment. Must be one of the depts in the JSS



150
151
152
# File 'lib/jss-api/api_object/network_segment.rb', line 150

def department
  @department
end

#distribution_pointString

Returns the name of the distribution point to be used from this network segment.

Returns:

  • (String)

    the name of the distribution point to be used from this network segment



153
154
155
# File 'lib/jss-api/api_object/network_segment.rb', line 153

def distribution_point
  @distribution_point
end

#ending_addressIPAddr

Returns ending IP adresss.

Returns:

  • (IPAddr)

    ending IP adresss



141
142
143
# File 'lib/jss-api/api_object/network_segment.rb', line 141

def ending_address
  @ending_address
end

#need_to_updateBoolean (readonly) Originally defined in module Updatable

Returns do we have unsaved changes?.

Returns:

  • (Boolean)

    do we have unsaved changes?

#netboot_serverString

Returns the netboot server for this segment.

Returns:

  • (String)

    the netboot server for this segment



159
160
161
# File 'lib/jss-api/api_object/network_segment.rb', line 159

def netboot_server
  @netboot_server
end

#override_buildingsBoolean

Returns should machines checking in from this segment update their building.

Returns:

  • (Boolean)

    should machines checking in from this segment update their building



168
169
170
# File 'lib/jss-api/api_object/network_segment.rb', line 168

def override_buildings
  @override_buildings
end

#override_departmentsBoolean

Returns should machines checking in from this segment update their dept.

Returns:

  • (Boolean)

    should machines checking in from this segment update their dept



165
166
167
# File 'lib/jss-api/api_object/network_segment.rb', line 165

def override_departments
  @override_departments
end

#starting_addressIPAddr

Returns starting IP adresss.

Returns:

  • (IPAddr)

    starting IP adresss



138
139
140
# File 'lib/jss-api/api_object/network_segment.rb', line 138

def starting_address
  @starting_address
end

#subnetIPAddr (readonly) Also known as: range

Returns the IPAddr object representing this network segment, created from the uid.

Returns:

  • (IPAddr)

    the IPAddr object representing this network segment, created from the uid



174
175
176
# File 'lib/jss-api/api_object/network_segment.rb', line 174

def subnet
  @subnet
end

#swu_serverString

Returns the swupdate server for this segment.

Returns:

  • (String)

    the swupdate server for this segment.



162
163
164
# File 'lib/jss-api/api_object/network_segment.rb', line 162

def swu_server
  @swu_server
end

#uidString (readonly) Also known as: identifier

Returns the unique identifier for this subnet, regardless of the JSS id.

Returns:

  • (String)

    the unique identifier for this subnet, regardless of the JSS id



171
172
173
# File 'lib/jss-api/api_object/network_segment.rb', line 171

def uid
  @uid
end

#urlString (readonly)

Returns the mount url for the distribution point.

Returns:

  • (String)

    the mount url for the distribution point



156
157
158
# File 'lib/jss-api/api_object/network_segment.rb', line 156

def url
  @url
end

Class Method Details

.my_network_segmentArray<Integer>

Find the current network segment ids for the machine running this code

Returns:

  • (Array<Integer>)

    the NetworkSegment ids for this machine right now.



126
127
128
# File 'lib/jss-api/api_object/network_segment.rb', line 126

def self.my_network_segment
  network_segment_for_ip JSS::Client.my_ip_address
end

.network_ranges(refresh = false) ⇒ Hash{Integer => IPAddr}

All NetworkSegments in the jss as IPAddr objects representing the subnet as a masked IPv4 address.

Using the #include? and #to_range methods on those objects is very useful.

Returns:

  • (Hash{Integer => IPAddr})

    the network segments as masked IPv4 addresses



90
91
92
93
94
95
96
# File 'lib/jss-api/api_object/network_segment.rb', line 90

def self.network_ranges(refresh = false)
  @@network_ranges = nil if refresh
  return @@network_ranges if @@network_ranges
  @@network_ranges = {}
  self.all.each{|ns| @@network_ranges[ns[:id]] =  IPAddr.jss_masked_v4addr(ns[:starting_address], ns[:ending_address])}
  @@network_ranges
end

.network_segment_for_ip(ip) ⇒ 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.

Parameters:

Returns:

  • (Array<Integer>)

    the ids of the NetworkSegments containing the given ip



114
115
116
117
118
119
# File 'lib/jss-api/api_object/network_segment.rb', line 114

def self.network_segment_for_ip(ip)
  ok_ip = IPAddr.new(ip)
  matches = []
  self.network_ranges.each{ |id, subnet| matches << id if subnet.include?(ok_ip) }
  matches
end

.subnets(refresh = false) ⇒ Object

An alias for network_ranges



101
# File 'lib/jss-api/api_object/network_segment.rb', line 101

def self.subnets(refresh = false); self.network_ranges refresh; end

Instance Method Details

#<=>(other) ⇒ -1, ...

Thanks to Comparable, we can tell if we’re equal or not.

See Comparable#<=>

Returns:

  • (-1, 0, 1)

    ar we less than, equal or greater than the other?



226
227
228
# File 'lib/jss-api/api_object/network_segment.rb', line 226

def <=> (other)
  self.subnet <=> other.subnet
end

#createInteger Originally defined in module Creatable

Create a new object in the JSS.

Returns:

  • (Integer)

    the jss ID of the newly created object

Raises:

#include?(some_addr) ⇒ Boolean

is a given address in this network segment?

Parameters:

Returns:

  • (Boolean)


381
382
383
# File 'lib/jss-api/api_object/network_segment.rb', line 381

def include? (some_addr)
  @subnet.include?  IPAddr.new(some_addr)
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.

Parameters:

  • newname (String)

    the new name

Raises:

#updateBoolean Originally defined in module Updatable

Save changes to the JSS

Returns:

  • (Boolean)

    success

Raises: