From f52eb46cd0028fcc45518d37c870c13f6a9478dd Mon Sep 17 00:00:00 2001 From: Sandeep Reddy Date: Mon, 19 Feb 2024 13:39:13 +0530 Subject: [PATCH] Update resource pool feature identifiers --- ...urcepoolidentifierstomiqproductfeatures.rb | 63 +++++++++++++++++ ...oolidentifierstomiqproductfeatures_spec.rb | 68 +++++++++++++++++++ 2 files changed, 131 insertions(+) create mode 100644 db/migrate/20240516101409_updateresourcepoolidentifierstomiqproductfeatures.rb create mode 100644 spec/migrations/20240516101409_updateresourcepoolidentifierstomiqproductfeatures_spec.rb diff --git a/db/migrate/20240516101409_updateresourcepoolidentifierstomiqproductfeatures.rb b/db/migrate/20240516101409_updateresourcepoolidentifierstomiqproductfeatures.rb new file mode 100644 index 00000000..c566fac3 --- /dev/null +++ b/db/migrate/20240516101409_updateresourcepoolidentifierstomiqproductfeatures.rb @@ -0,0 +1,63 @@ +class Updateresourcepoolidentifierstomiqproductfeatures < ActiveRecord::Migration[6.1] + class MiqProductFeature < ActiveRecord::Base; end + + FEATURE_MAPPING_UPDATE = { + 'resource_pool' => 'resource_pool_cloud', + 'resource_pool_view' => 'resource_pool_cloud_view', + 'resource_pool_show_list' => 'resource_pool_cloud_show_list', + 'resource_pool_show' => 'resource_pool_cloud_show', + 'resource_pool_control' => 'resource_pool_cloud_control', + 'resource_pool_tag' => 'resource_pool_cloud_tag', + 'resource_pool_protect' => 'resource_pool_cloud_protect', + 'resource_pool_admin' => 'resource_pool_cloud_admin', + 'resource_pool_delete' => 'resource_pool_cloud_delete' + }.freeze + + FEATURE_MAPPING_CREATE = { + "resource_pool_infra" => {:name => "Infrastructure Resource Pools", :description => "Access Everything under Infrastructure Resource Pools", :feature_type => "node"}, + "resource_pool_infra_view" => {:name => "View", :description => "Display Lists of Infrastructure Resource Pools", :feature_type => "view", :parent => "resource_pool_infra"}, + "resource_pool_infra_show_list" => {:name => "List", :description => "Display Lists of Infrastructure Resource Pools", :feature_type => "view", :parent => "resource_pool_infra_view"}, + "resource_pool_infra_show" => {:name => "Show", :description => "Display Individual Infrastructure Resource Pools", :feature_type => "view", :parent => "resource_pool_infra_view"}, + "resource_pool_infra_control" => {:name => "Operate", :description => "Perform Operations on Infrastructure Resource Pools", :feature_type => "control", :parent => "resource_pool_infra"}, + "resource_pool_infra_tag" => {:name => "Edit Tags", :description => "Edit Tags of Infrastructure Resource Pools", :feature_type => "control", :parent => "resource_pool_infra_control"}, + "resource_pool_infra_protect" => {:name => "Manage Policies", :description => "Manage Policies of Infrastructure Resource Pools", :feature_type => "control", :parent => "resource_pool_infra_control"}, + "resource_pool_infra_admin" => {:name => "Modify", :description => "Modify Infrastructure Resource Pools", :feature_type => "admin", :parent => "resource_pool_infra"}, + "resource_pool_infra_delete" => {:name => "Remove", :description => "Remove Infrastructure Resource Pools", :feature_type => "admin", :parent => "resource_pool_infra_admin"} + }.freeze + + def up + return if MiqProductFeature.none? + + say_with_time('Updating resource_pool features to resource_pool_cloud') do + FEATURE_MAPPING_UPDATE.each do |from, to| + MiqProductFeature.find_by(:identifier => from)&.update!(:identifier => to) + end + end + + say_with_time('Adding new Infrastructure Resource Pool features to miq_product_features') do + FEATURE_MAPPING_CREATE.each do |identifier, attributes| + parent = MiqProductFeature.find_by(:identifier => attributes[:parent]) if attributes[:parent] + MiqProductFeature.find_or_create_by!(:identifier => identifier) do |feature| + feature.name = attributes[:name] + feature.description = attributes[:description] + feature.feature_type = attributes[:feature_type] + feature.parent_id = parent.id if parent + end + end + end + end + + def down + return if MiqProductFeature.none? + + say_with_time('Reverting resource_pool_cloud features back to resource_pool') do + FEATURE_MAPPING_UPDATE.each do |to, from| + MiqProductFeature.find_by(:identifier => from)&.update!(:identifier => to) + end + end + + say_with_time('Removing Infrastructure Resource Pool features from miq_product_features') do + MiqProductFeature.where(:identifier => FEATURE_MAPPING_CREATE.keys).delete_all + end + end +end diff --git a/spec/migrations/20240516101409_updateresourcepoolidentifierstomiqproductfeatures_spec.rb b/spec/migrations/20240516101409_updateresourcepoolidentifierstomiqproductfeatures_spec.rb new file mode 100644 index 00000000..0ae3cccb --- /dev/null +++ b/spec/migrations/20240516101409_updateresourcepoolidentifierstomiqproductfeatures_spec.rb @@ -0,0 +1,68 @@ +require_migration + +describe Updateresourcepoolidentifierstomiqproductfeatures do + let(:miq_product_feature) { migration_stub(:MiqProductFeature) } + + before do + %w[resource_pool resource_pool_view resource_pool_show_list resource_pool_show resource_pool_control resource_pool_tag resource_pool_protect resource_pool_admin resource_pool_delete].each do |identifier| + miq_product_feature.create!(:identifier => identifier) + end + end + + migration_context :up do + it "updates existing resource_pool features to resource_pool_cloud" do + migrate + + described_class::FEATURE_MAPPING_UPDATE.each do |old_identifier, new_identifier| + expect(miq_product_feature.exists?(:identifier => old_identifier)).to be_falsy + expect(miq_product_feature.exists?(:identifier => new_identifier)).to be_truthy + end + end + + it "creates new resource_pool_infra features" do + migrate + + described_class::FEATURE_MAPPING_CREATE.each do |identifier, attributes| + feature = miq_product_feature.find_by(:identifier => identifier) + expect(feature).not_to be_nil + expect(feature.name).to eq(attributes[:name]) + expect(feature.description).to eq(attributes[:description]) + expect(feature.feature_type).to eq(attributes[:feature_type]) + end + end + end + + migration_context :down do + before do + described_class::FEATURE_MAPPING_UPDATE.each do |_old_identifier, new_identifier| + miq_product_feature.create!(:identifier => new_identifier) + end + + described_class::FEATURE_MAPPING_CREATE.each do |identifier, attributes| + miq_product_feature.create!( + :identifier => identifier, + :name => attributes[:name], + :description => attributes[:description], + :feature_type => attributes[:feature_type] + ) + end + end + + it "reverts resource_pool_cloud features back to resource_pool" do + migrate + + described_class::FEATURE_MAPPING_UPDATE.each do |old_identifier, new_identifier| + expect(miq_product_feature.exists?(:identifier => new_identifier)).to be_falsy + expect(miq_product_feature.exists?(:identifier => old_identifier)).to be_truthy + end + end + + it "removes newly created resource_pool_infra features" do + migrate + + described_class::FEATURE_MAPPING_CREATE.each_key do |identifier| + expect(miq_product_feature.exists?(:identifier => identifier)).to be_falsy + end + end + end +end