Skip to content
This repository was archived by the owner on Oct 24, 2022. It is now read-only.

Commit f92e695

Browse files
David Davisakofink
authored andcommitted
Fixes #19927 - Drop and readd rules when importing filters
This is a change to the way we handle filter rules. Basically, there is no way to ensure uniqueness among filter rules so updating rules isn't possible. The package filter rule has a unique check but it simply checks ALL fields to see if the rule exists already. This doesn't really allow for matching though since any field change would constitute a new rule. Since we're importing/updating filters and not rules, I think dropping the rules and recreating them is the best way to handle updates.
1 parent ebb0044 commit f92e695

File tree

6 files changed

+64724
-18875
lines changed

6 files changed

+64724
-18875
lines changed

lib/hammer_cli_csv/content_view_filters.rb

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -106,12 +106,15 @@ def create_filters_from_csv(line)
106106
})
107107
end
108108

109-
existing_rules = {}
109+
# drop existing rules
110110
@api.resource(:content_view_filter_rules).call(:index, {
111111
'per_page' => 999999,
112112
'content_view_filter_id' => filter_id
113113
})['results'].each do |rule|
114-
existing_rules[rule['name']] = rule
114+
@api.resource(:content_view_filter_rules).call(:destroy, {
115+
'content_view_filter_id' => filter_id,
116+
'id' => rule['id']
117+
})
115118
end
116119

117120
collect_column(line[RULES]) do |rule|
@@ -138,16 +141,8 @@ def create_filters_from_csv(line)
138141
raise "Unknown type '#{type}' from '#{line[RULES]}'"
139142
end
140143

141-
rule = existing_rules[name]
142-
if !rule
143-
print "." if option_verbose?
144-
rule = @api.resource(:content_view_filter_rules).call(:create, params)
145-
existing_rules[rule['name']] = rule
146-
else
147-
print "." if option_verbose?
148-
params['id'] = rule['id']
149-
@api.resource(:content_view_filter_rules).call(:update, params)
150-
end
144+
print "." if option_verbose?
145+
@api.resource(:content_view_filter_rules).call(:create, params)
151146
end
152147

153148
puts 'done' if option_verbose?

test/csv_test_helper.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,23 @@ def content_view_delete(name)
130130
end
131131
end
132132

133+
def content_view_filter_delete(org, cv, name)
134+
id = nil
135+
stdout,stderr = capture {
136+
hammer.run(%W(content-view filter list --search name=#{name} --content-view #{cv} --organization #{org}))
137+
}
138+
lines = stdout.split("\n")
139+
if lines.length == 5
140+
id = lines[3].split(" ")[0]
141+
end
142+
143+
if id
144+
stdout,stderr = capture {
145+
hammer.run(%W(content-view filter delete --id #{id}))
146+
}
147+
end
148+
end
149+
133150
require File.join(File.dirname(__FILE__), 'apipie_resource_mock')
134151
require File.join(File.dirname(__FILE__), 'helpers/command')
135152
require File.join(File.dirname(__FILE__), 'helpers/resource_disabled')

0 commit comments

Comments
 (0)