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



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

def building
  @building
end

#cidrInteger



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

def cidr
  @cidr
end

#departmentString



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

def department
  @department
end

#distribution_pointString



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

def distribution_point
  @distribution_point
end

#ending_addressIPAddr



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

#netboot_serverString



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

def netboot_server
  @netboot_server
end

#override_buildingsBoolean



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

def override_buildings
  @override_buildings
end

#override_departmentsBoolean



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

def override_departments
  @override_departments
end

#starting_addressIPAddr



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



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

def subnet
  @subnet
end

#swu_serverString



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



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

def uid
  @uid
end

#urlString (readonly)



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



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.



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.



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#<=>



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.

#include?(some_addr) ⇒ Boolean

is a given address in this network segment?



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.

#updateBoolean Originally defined in module Updatable

Save changes to the JSS