lib/jss/api_connection.rb in ruby-jss-1.1.3 vs lib/jss/api_connection.rb in ruby-jss-1.2.0b1
- old
+ new
@@ -322,10 +322,13 @@
# The Default SSL Version
DFT_SSL_VERSION = 'TLSv1_2'.freeze
RSRC_NOT_FOUND_MSG = 'The requested resource was not found'.freeze
+ # These classes are extendable, and may need cache flushing for EA definitions
+ EXTENDABLE_CLASSES = [JSS::Computer, JSS::MobileDevice, JSS::User].freeze
+
# Attributes
#####################################
# @return [String] the username who's connected to the JSS API
attr_reader :user
@@ -418,14 +421,12 @@
#
# If not, you must call {#connect} before accessing the API.
#
def initialize(args = {})
@name = args.delete :name
- @name ||= :disconnected
+ @name ||= :unknown
@connected = false
- @object_list_cache = {}
- @ext_attr_definition_cache = {}
connect args unless args.empty?
end # init
# Instance Methods
#####################################
@@ -465,10 +466,13 @@
# @option args :timeout[Integer] the number of seconds before an API call times out, defaults to 60
#
# @return [true]
#
def connect(args = {})
+ # new connections always get new caches
+ flushcache
+
args[:no_port_specified] = args[:port].to_s.empty?
args = apply_connection_defaults args
# ensure an integer
args[:port] &&= args[:port].to_i
@@ -964,19 +968,37 @@
end
# Empty all cached lists from this connection
# then run garbage collection to clear any available memory
#
+ # If an APIObject Subclass's RSRC_LIST_KEY is specified, only the caches
+ # for that class are flushed (e.g. :computers, :comptuer_groups)
+ #
# NOTE if you've referenced objects in these caches, those objects
# won't be removed from memory, but all cached data will be recached
# as needed.
#
+ # @param key[Symbol, Class] Flush only the caches for the given RSRC_LIST_KEY. or
+ # the EAdef cache for the given extendable class. If nil (the default)
+ # flushes all caches
+ #
# @return [void]
#
- def flushcache
- @object_list_cache = {}
- @ext_attr_definition_cache = {}
+ def flushcache(key = nil)
+ if EXTENDABLE_CLASSES.include? key
+ @ext_attr_definition_cache[key] = {}
+ elsif key
+ map_key_pfx = "#{key}_map_"
+ @object_list_cache.delete_if do |cache_key, _cache|
+ cache_key == key || cache_key.to_s.start_with?(map_key_pfx)
+ end
+ @ext_attr_definition_cache
+ else
+ @object_list_cache = {}
+ @ext_attr_definition_cache = {}
+ end
+
GC.start
end
# Remove the various cached data
# from the instance_variables used to create
@@ -999,11 +1021,11 @@
####################################
private
# raise exception if not connected
def validate_connected
- raise JSS::InvalidConnectionError, 'Not Connected. Use .connect first.' unless connected?
+ raise JSS::InvalidConnectionError, "Connection '#{@name}' Not Connected. Use .connect first." unless connected?
end
# Apply defaults from the JSS::CONFIG,
# then from the JSS::Client,
# then from the module defaults
@@ -1256,12 +1278,12 @@
# @param (See JSS::APIConnection#connect)
#
# @return [APIConnection] the new, active connection
#
def self.new_api_connection(args = {})
+ args[:name] ||= :default
@api = APIConnection.new args
- @api
end
# Switch the connection used for all API interactions to the
# one provided. See {JSS::APIConnection} for details and examples
# of using multiple connections
@@ -1280,19 +1302,19 @@
# Make the default connection (Stored in JSS::API) active
#
# @return [void]
#
def self.use_default_connection
- use_api_connection API
+ use_api_connection @api
end
# The currently active JSS::APIConnection instance.
#
# @return [JSS::APIConnection]
#
def self.api
- @api
+ @api ||= APIConnection.new name: :default
end
# aliases of module methods
class << self
@@ -1308,10 +1330,10 @@
alias activate_connection use_api_connection
end
# create the default connection
- new_api_connection(name: :default) unless @api
+ new_api_connection unless @api
# Save the default connection in the API constant,
# mostly for backward compatibility.
API = @api unless defined? API