Skip to content

Commit

Permalink
Remove tags from entitlement filters from the deleted tag's region
Browse files Browse the repository at this point in the history
The before_destroy callback wasn't previously scoped to the tag's region.
From the global region, usually 99, you could delete a tag from region 1
with value "/managed/costcenter/004".  We would then remove that value from
filters with that tag from entitlements in region 99, even though we deleted
a region 1 tag. The same named tag could still exist in region 99.

This commit ensures that if you delete a region 99 tag, we'll only update filters
in entitlements in region 99.
  • Loading branch information
jrafanie committed Feb 27, 2024
1 parent e883fa0 commit 04b4de4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
2 changes: 1 addition & 1 deletion app/models/tag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ def self.controlled_by_mapping
private

def remove_from_managed_filters
Entitlement.remove_tag_from_all_managed_filters(name)
Entitlement.with_region(self.class.id_to_region(id)) { Entitlement.remove_tag_from_all_managed_filters(name) }
end

def name_path
Expand Down
19 changes: 19 additions & 0 deletions spec/models/tag_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -168,5 +168,24 @@
MiqGroup.all.each { |group| expect(group.get_managed_filters).to match_array(expected_filters) }
expect(Tag.all).to be_empty
end

it "removing a tag will only remove it from entitlement filters in the tag's region" do
tag
other_region = ApplicationRecord.my_region_number + 1
other_region_tag = FactoryBot.create(:tag, :name => "/managed/my_name/test", :id => ApplicationRecord.id_in_region(1, other_region))
other_region_miq_group = FactoryBot.create(:miq_group, :entitlement => Entitlement.create!, :id => ApplicationRecord.id_in_region(1, other_region))
other_region_miq_group.entitlement.set_managed_filters(filters)
other_region_miq_group.save

other_region_tag.destroy

# tag and filters from other region are deleted and updated
Tag.in_region(other_region) { expect(Tag.all).to be_empty }
MiqGroup.in_region(other_region) { MiqGroup.all.each { |group| expect(group.get_managed_filters).to be_empty } }

# current region tag/filters are not changed
expect(Tag.pluck(:id)).to eq([tag.id])
MiqGroup.all.each { |group| expect(group.get_managed_filters).to match_array(filters) }
end
end
end

0 comments on commit 04b4de4

Please sign in to comment.