Class: Jerakia::Lookup

Inherits:
Object
  • Object
show all
Defined in:
lib/jerakia/lookup.rb,
lib/jerakia/lookup/plugin_config.rb

Defined Under Namespace

Classes: Plugin, PluginConfig, PluginFactory

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, opts, req, scope) ⇒ Lookup

Returns a new instance of Lookup.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/jerakia/lookup.rb', line 19

def initialize(name, opts, req, scope)
  @name = name
  @request = req
  @valid = true
  @scope_object = scope
  @output_filters = []
  @proceed = true
  @pluginfactory = Jerakia::Lookup::PluginFactory.new

  # Validate options passed to the lookup
  #
  valid_opts = [:use]

  opts.keys.each do |opt_key|
    unless valid_opts.include?(opt_key)
      raise Jerakia::PolicyError, "Unknown option #{opt_key} for lookup #{name}"
    end
  end

  if opts[:use]
    Array(opts[:use]).flatten.each do |plugin|
      plugin_load(plugin)
    end
  end
end

Instance Attribute Details

#datasource(source, opts = {}) ⇒ Object

Returns the value of attribute datasource.



9
10
11
# File 'lib/jerakia/lookup.rb', line 9

def datasource
  @datasource
end

#lookuptypeObject (readonly)

Returns the value of attribute lookuptype.



12
13
14
# File 'lib/jerakia/lookup.rb', line 12

def lookuptype
  @lookuptype
end

#nameObject (readonly)

Returns the value of attribute name.



15
16
17
# File 'lib/jerakia/lookup.rb', line 15

def name
  @name
end

#output_filtersObject (readonly)

Returns the value of attribute output_filters.



14
15
16
# File 'lib/jerakia/lookup.rb', line 14

def output_filters
  @output_filters
end

#pluginfactoryObject (readonly)

Returns the value of attribute pluginfactory.



16
17
18
# File 'lib/jerakia/lookup.rb', line 16

def pluginfactory
  @pluginfactory
end

#proceedObject

Returns the value of attribute proceed.



11
12
13
# File 'lib/jerakia/lookup.rb', line 11

def proceed
  @proceed
end

#requestObject

Returns the value of attribute request.



8
9
10
# File 'lib/jerakia/lookup.rb', line 8

def request
  @request
end

#scope_objectObject (readonly)

Returns the value of attribute scope_object.



13
14
15
# File 'lib/jerakia/lookup.rb', line 13

def scope_object
  @scope_object
end

#validObject

Returns the value of attribute valid.



10
11
12
# File 'lib/jerakia/lookup.rb', line 10

def valid
  @valid
end

Instance Method Details

#confine(key = nil, match) ⇒ Object



120
121
122
123
124
125
126
# File 'lib/jerakia/lookup.rb', line 120

def confine(key = nil, match)
  if key
    invalidate if get_matches(key, match).empty?
  else
    invalidate
  end
end

#continueObject

lookup function: continue Will cause Jerakia to continue to the next lookup in the policy which is the default behaviour



101
102
103
# File 'lib/jerakia/lookup.rb', line 101

def continue
  @proceed = true
end

#exclude(key = nil, match) ⇒ Object



128
129
130
131
132
# File 'lib/jerakia/lookup.rb', line 128

def exclude(key = nil, match)
  if key
    invalidate unless get_matches(key, match).empty?
  end
end

#get_datasourceObject



61
62
63
# File 'lib/jerakia/lookup.rb', line 61

def get_datasource
  @datasource
end

#get_matches(key, match) ⇒ Object



116
117
118
# File 'lib/jerakia/lookup.rb', line 116

def get_matches(key, match)
  matches = Array(match).select { |m| key[Regexp.new(m)] == key }
end

#invalidateObject

lookup function: invalidate Setting invalidate will mean this lookup will be skipped in the policy



108
109
110
# File 'lib/jerakia/lookup.rb', line 108

def invalidate
  @valid = false
end

#output_filter(name, opts = {}) ⇒ Object



77
78
79
# File 'lib/jerakia/lookup.rb', line 77

def output_filter(name, opts = {})
  @output_filters << { :name => name, :opts => opts }
end

#pluginObject



57
58
59
# File 'lib/jerakia/lookup.rb', line 57

def plugin
  pluginfactory
end

#plugin_config(plugin) ⇒ Object

Retrieve plugin specific configuration from the global configuration file gets passed to the plugin instance upon initilization.



48
49
50
# File 'lib/jerakia/lookup.rb', line 48

def plugin_config(plugin)
  Jerakia::Lookup::PluginConfig.new(plugin)
end

#plugin_load(plugin) ⇒ Object



52
53
54
55
# File 'lib/jerakia/lookup.rb', line 52

def plugin_load(plugin)
  Jerakia.log.debug("Loading plugin #{plugin}")
  pluginfactory.register(plugin, Jerakia::Lookup::Plugin.new(self, plugin_config(plugin)))
end

#proceed?Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/jerakia/lookup.rb', line 81

def proceed?
  proceed
end

#runObject



134
135
136
137
138
139
140
141
142
# File 'lib/jerakia/lookup.rb', line 134

def run
  Jerakia.log.verbose("lookup: #{@name} key: #{@request.key} namespace: #{@request.namespace.join('/')}")
  @datasource.run
  response = @datasource.response
  @output_filters.each do |filter|
    response.filter! filter[:name], filter[:opts]
  end
  response
end

#scopeObject

If set, Jerakia will pass each Jerakia::Response object to an output filter plugin



73
74
75
# File 'lib/jerakia/lookup.rb', line 73

def scope
  scope_object.value
end

#stopObject

lookup function: stop Enabling stop sets @proceed to false, which will cause Jerakia not to load any more lookups in the policy if this lookup is deemed as valid. If the lookup is invalidated than Jerakia will progress to the next lookup. This is useful in conjuction with the confine plugin where we want to segregate some lookups but not worry about excluding from later lookups



93
94
95
# File 'lib/jerakia/lookup.rb', line 93

def stop
  @proceed = false
end

#valid?Boolean

Returns:

  • (Boolean)


112
113
114
# File 'lib/jerakia/lookup.rb', line 112

def valid?
  valid
end