Skip to content

Commit 2e2a002

Browse files
committed
Add methods to clear PluginMetadata repositories
Fixes #10691
1 parent 122adbd commit 2e2a002

3 files changed

Lines changed: 92 additions & 5 deletions

File tree

logstash-core/lib/logstash/plugin_metadata.rb

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,14 @@ module LogStash
2121
# that doesn't break when installed onto a Logstash that doesn't have those features, e.g.:
2222
#
2323
# ~~~
24-
# if defined?(LogStash::PluginMetadata)
25-
# LogStash::PluginMetadata.set(id, :foo, bar)
26-
# end
24+
#
25+
# plugin_metadata.set(:foo, bar) if defined?(plugin_metadata?)
26+
#
2727
# ~~~
2828
#
2929
# @since 7.1
3030
class PluginMetadata
31+
3132
Thread.exclusive do
3233
@registry = ThreadSafe::Cache.new unless defined?(@registry)
3334
end
@@ -55,6 +56,17 @@ def exists?(plugin_id)
5556
@registry.key?(plugin_id)
5657
end
5758

59+
##
60+
# Deletes, and then clears the contents of an existing PluginMetadata object for the given plugin id if one exists
61+
#
62+
# @param plugin_id [String]
63+
#
64+
# @return [Boolean]
65+
def delete_for_plugin(plugin_id)
66+
old_registry = @registry.delete(plugin_id)
67+
old_registry.clear unless old_registry.nil?
68+
end
69+
5870
##
5971
# @api private
6072
def reset!
@@ -104,5 +116,23 @@ def get(key)
104116
def set?(key)
105117
@datastore.key?(key)
106118
end
119+
120+
##
121+
# Delete the metadata key for this plugin, returning the previous value (if any)
122+
#
123+
# @param key [Symbol]
124+
#
125+
# @return [Object]
126+
def delete(key)
127+
@datastore.delete(key)
128+
end
129+
130+
##
131+
# Clear all metadata keys for this plugin
132+
#
133+
# @return [Object]
134+
def clear
135+
@datastore.clear
136+
end
107137
end
108138
end

logstash-core/spec/logstash/plugin_spec.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,10 @@ def register; end
312312
expect(plugin_instance.plugin_metadata).to be_a_kind_of(LogStash::PluginMetadata)
313313
end
314314

315+
it "PluginMetadata is defined" do
316+
expect(defined?(plugin_instance.plugin_metadata)).to be_truthy
317+
end
318+
315319
if config_override.include?('id')
316320
it "will be shared between instance of plugins" do
317321
expect(plugin_instance.plugin_metadata).to equal(plugin.new(config).plugin_metadata)

logstash-core/spec/plugin_metadata_spec.rb

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,20 @@
3333
end
3434
end
3535
end
36+
describe '#delete_for_plugin' do
37+
before(:each) { registry.for_plugin(plugin_id).set(:foo, 'bar') }
38+
it 'deletes the registry' do
39+
expect(registry.exists?(plugin_id)).to be true
40+
registry.delete_for_plugin(plugin_id)
41+
expect(registry.exists?(plugin_id)).to be false
42+
end
43+
it 'deletes the data inside the registry' do
44+
plugin_registry = registry.for_plugin(plugin_id)
45+
registry.delete_for_plugin(plugin_id)
46+
expect(plugin_registry.set?(:foo)).to be false
47+
end
48+
end
49+
3650
end
3751

3852
describe 'instance' do
@@ -49,14 +63,15 @@
4963
end
5064
end
5165
context 'when the key is set' do
52-
before(:each) { instance.set(:foo, 'bananas') }
66+
let (:val) { 'bananas'}
67+
before(:each) { instance.set(:foo, val) }
5368

5469
it 'sets the new value' do
5570
instance.set(:foo, 'bar')
5671
expect(instance.get(:foo)).to eq('bar')
5772
end
5873
it 'returns the previous associated value' do
59-
expect(instance.set(:foo, 'bar')).to eq('bananas')
74+
expect(instance.set(:foo, 'bar')).to eq(val)
6075
end
6176
context 'when the new value is nil' do
6277
it 'unsets the value' do
@@ -94,5 +109,43 @@
94109
end
95110
end
96111
end
112+
113+
describe '#delete' do
114+
context 'when the key is set' do
115+
let (:val) { 'bananas' }
116+
before(:each) { instance.set(:foo, val)}
117+
it 'returns the value' do
118+
expect(instance.delete(:foo)).to be val
119+
end
120+
it 'removes the key' do
121+
instance.delete(:foo)
122+
expect(instance.set?(:foo)).to be false
123+
end
124+
end
125+
context 'when the key is not set' do
126+
it 'returns nil' do
127+
expect(instance.delete(:foo)).to be nil
128+
end
129+
130+
it 'should not be set' do
131+
instance.delete(:foo)
132+
expect(instance.set?(:foo)).to be false
133+
end
134+
end
135+
end
136+
137+
describe '#clear' do
138+
context 'when the key is set' do
139+
before(:each) do
140+
instance.set(:foo, 'bananas')
141+
instance.set(:bar, 'more bananas')
142+
end
143+
it 'removes all keys' do
144+
instance.clear
145+
expect(instance.set?(:foo)).to be false
146+
expect(instance.set?(:bar)).to be false
147+
end
148+
end
149+
end
97150
end
98151
end

0 commit comments

Comments
 (0)