From 6c8d9bae0ae5676c2f5d6743ae367764d8a7518b Mon Sep 17 00:00:00 2001 From: Keenan Brock Date: Tue, 18 Dec 2018 07:35:23 -0500 Subject: [PATCH] Classification.yml don't use parent_id=0 For seeds and import/exports, the levels explain the nesting, so there is no need to pass parent_id This is part of the Classification.parent_id initiative more factory methods that do not need to specify parent_id Remove seed update_all The categories are now properly with parent_id no need to update_all after seeding --- app/models/classification.rb | 17 ++++------- app/models/vm_or_template.rb | 3 +- db/fixtures/classifications.yml | 28 ------------------- lib/task_helpers/imports/tags.rb | 3 +- spec/lib/task_helpers/exports/tags_spec.rb | 3 -- .../imports/data/tags/Import_Test.yaml | 1 - .../imports/data/tags/Location.yaml | 1 - .../imports/data/tags/Tag_Test_Fail_Cat.yml | 1 - .../imports/data/tags/Tag_Test_Fail_Entry.yml | 1 - spec/models/classification_spec.rb | 1 - 10 files changed, 8 insertions(+), 51 deletions(-) diff --git a/app/models/classification.rb b/app/models/classification.rb index 6b5bc875958..2f65713aef0 100644 --- a/app/models/classification.rb +++ b/app/models/classification.rb @@ -375,14 +375,9 @@ def self.export_to_yaml end def export_to_array - h = attributes + h = attributes.except(*%w(id tag_id reserved parent_id)) h["name"] = name - if category? - ["id", "tag_id", "reserved"].each { |k| h.delete(k) } - h["entries"] = entries.collect(&:export_to_array).flatten - else - ["id", "tag_id", "reserved", "parent_id"].each { |k| h.delete(k) } - end + h["entries"] = entries.collect(&:export_to_array).flatten if category? [h] end @@ -395,7 +390,7 @@ def self.import_from_hash(classification, parent = nil) stats = {"categories" => 0, "entries" => 0} - if classification["parent_id"] == 0 # category + if parent.nil? # category cat = find_by_name(classification["name"]) if cat _log.info("Skipping Classification (already in DB): Category: name=[#{classification["name"]}]") @@ -404,6 +399,7 @@ def self.import_from_hash(classification, parent = nil) _log.info("Importing Classification: Category: name=[#{classification["name"]}]") + classification.delete("parent_id") entries = classification.delete("entries") cat = create(classification) stats["categories"] += 1 @@ -445,15 +441,12 @@ def self.seed category = find_by_name(c[:name], my_region_number, (c[:ns] || DEFAULT_NAMESPACE)) next if category - category = new(c.except(:entries)) + category = is_category.new(c.except(:entries)) next unless category.valid? # HACK: Skip seeding if categories aren't valid/unique _log.info("Creating category #{c[:name]}") category.save! add_entries_from_hash(category, c[:entries]) end - - # Fix categories that have a nill parent_id - where(:parent_id => nil).update_all(:parent_id => 0) end def self.sanitize_name(name) diff --git a/app/models/vm_or_template.rb b/app/models/vm_or_template.rb index 2f8a3ae7020..c2b84fa569a 100644 --- a/app/models/vm_or_template.rb +++ b/app/models/vm_or_template.rb @@ -1380,10 +1380,9 @@ def self.folder_category(folder_type) cat_name = "folder_path_#{folder_type}" cat = Classification.find_by_name(cat_name) unless cat - cat = Classification.new( + cat = Classification.is_category.new( :name => cat_name, :description => "Parent Folder Path (#{folder_type == :blue ? "VMs & Templates" : "Hosts & Clusters"})", - :parent_id => 0, :single_value => true, :read_only => true ) diff --git a/db/fixtures/classifications.yml b/db/fixtures/classifications.yml index fffaeec05a1..1e8ededb235 100644 --- a/db/fixtures/classifications.yml +++ b/db/fixtures/classifications.yml @@ -34,7 +34,6 @@ :show: true :name: location :example_text: The geographic location of the resource, such as New York, Chicago, or London. - :parent_id: 0 :default: true :single_value: "1" - :description: Workload @@ -107,7 +106,6 @@ :show: true :name: function :example_text: The workloads a resource provides, such as Web Server, Database, or Firewall. - :parent_id: 0 :default: true :single_value: "0" - :description: Owner @@ -138,7 +136,6 @@ :show: true :name: owner :example_text: The individual or group that owns the resource. - :parent_id: 0 :default: true :single_value: "1" - :description: Environment @@ -183,7 +180,6 @@ :show: true :name: environment :example_text: The resource environment, such as Development, QA, Test, or Production - :parent_id: 0 :default: true :single_value: "1" - :description: User roles @@ -242,7 +238,6 @@ :show: true :name: role :example_text: - :parent_id: 0 :default: true :single_value: "1" :ns: /managed/user @@ -274,7 +269,6 @@ :show: true :name: service_level :example_text: The level of service associated with this resource. - :parent_id: 0 :default: true :single_value: "1" - :description: Customer @@ -285,7 +279,6 @@ :show: true :name: customer :example_text: The name of the customer associated with this resource. - :parent_id: 0 :default: true :single_value: t - :description: Line of Business @@ -296,7 +289,6 @@ :show: true :name: lob :example_text: The Line of Business the resource is assigned to, such as Retail, Trading, or Manufacturing. - :parent_id: 0 :default: true :single_value: "0" - :description: Department @@ -418,7 +410,6 @@ :show: true :name: department :example_text: The department the resource is assigned to, such as HR, Accounting, or Sales. - :parent_id: 0 :default: true :single_value: "0" - :description: Cost Center @@ -442,7 +433,6 @@ :show: true :name: cc :example_text: Cost Center - :parent_id: 0 :default: true :single_value: "1" - :description: Network Location @@ -473,7 +463,6 @@ :show: true :name: network_location :example_text: The network location the resource is to be used, such as DMZ, Internal network or Cloud - :parent_id: 0 :default: true :single_value: "1" - :description: Exclusions @@ -497,7 +486,6 @@ :show: true :name: exclusions :example_text: Operations that the resource may be excluded from, such as Analysis or Cloning. - :parent_id: 0 :default: true :single_value: "0" - :description: Provisioning Scope @@ -521,7 +509,6 @@ :show: true :name: prov_scope :example_text: Provisioning Scope - :parent_id: 0 :default: true :single_value: "0" - :description: EVM Operations @@ -552,7 +539,6 @@ :show: true :name: operations :example_text: Operations - :parent_id: 0 :default: true :single_value: "1" - :description: Quota - Max CPUs @@ -632,7 +618,6 @@ :show: true :name: quota_max_cpu :example_text: Maximum number of CPUs allowed by Quota - :parent_id: 0 :default: true :single_value: "1" - :description: Auto Approve - Max CPU @@ -677,7 +662,6 @@ :show: true :name: prov_max_cpu :example_text: Maximum number of CPUs allowed by Auto Approval - :parent_id: 0 :default: true :single_value: "1" - :description: Auto Approve - Max VM @@ -722,7 +706,6 @@ :show: true :name: prov_max_vm :example_text: Maximum number of VMs allowed by Auto Approval - :parent_id: 0 :default: true :single_value: "1" - :description: Quota - Max Memory @@ -788,7 +771,6 @@ :show: true :name: quota_max_memory :example_text: Maximum Memory allowed by Quota (GB) - :parent_id: 0 :default: true :single_value: "1" - :description: Auto Approve - Max Memory @@ -826,7 +808,6 @@ :show: true :name: prov_max_memory :example_text: Maximum Memory allowed by Auto Approval (GB) - :parent_id: 0 :default: true :single_value: "1" - :description: Quota - Max Storage @@ -885,7 +866,6 @@ :show: true :name: quota_max_storage :example_text: Maximum Storage allowed by Quota (GB) - :parent_id: 0 :default: true :single_value: "1" - :description: Auto Approve - Max Retirement Days @@ -923,7 +903,6 @@ :show: true :name: prov_max_retirement_days :example_text: Maximum Days for Retirement allowed by Auto Approval - :parent_id: 0 :default: true :single_value: "1" - :description: LifeCycle @@ -940,7 +919,6 @@ :show: true :name: lifecycle :example_text: LifeCycle Options - :parent_id: 0 :default: true :single_value: "1" - :description: Migration Group @@ -971,7 +949,6 @@ :show: true :name: migration_group :example_text: Group of VMs prepared for migration. - :parent_id: 0 :default: true :single_value: "1" - :description: Network Mapping @@ -988,7 +965,6 @@ :show: true :name: network_mapping :example_text: Mapping between source network(s) and a destination network for migration. - :parent_id: 0 :default: true :single_value: "1" - :description: Storage Mapping @@ -1019,7 +995,6 @@ :show: true :name: storage_mapping :example_text: Mapping between source storage(s) and a destination storage for migration. - :parent_id: 0 :default: true :single_value: "1" - :description: V2V - Transformation Host @@ -1043,7 +1018,6 @@ :show: true :name: v2v_transformation_host :example_text: Transformation Host role enabled for V2V. - :parent_id: 0 :default: true :single_value: "1" - :description: V2V - Transformation Method @@ -1067,7 +1041,6 @@ :show: true :name: v2v_transformation_method :example_text: Transformation methods supported for V2V. - :parent_id: 0 :default: true :single_value: "0" - :description: Transformation Status @@ -1084,6 +1057,5 @@ :show: true :name: transformation_status :example_text: VM has been migrated. - :parent_id: 0 :default: true :single_value: "1" diff --git a/lib/task_helpers/imports/tags.rb b/lib/task_helpers/imports/tags.rb index 8d111ac09ea..dba76cfafed 100644 --- a/lib/task_helpers/imports/tags.rb +++ b/lib/task_helpers/imports/tags.rb @@ -57,6 +57,7 @@ def import_tags(tag_categories) def import_classification(tag_category) ns = tag_category["ns"] ? tag_category["ns"] : "/managed" tag_category["name"] = tag_category["name"].to_s + tag_category.delete("parent_id") classification = Classification.find_by_name(tag_category['name'], REGION_NUMBER, ns) @@ -65,7 +66,7 @@ def import_classification(tag_category) if classification classification.update_attributes(tag_category.select { |k| UPDATE_CAT_FIELDS.include?(k) }) else - classification = Classification.create(tag_category) + classification = Classification.is_category.create(tag_category) end raise ClassificationYamlError.new("Tag Category error", classification.errors.full_messages) unless classification.valid? diff --git a/spec/lib/task_helpers/exports/tags_spec.rb b/spec/lib/task_helpers/exports/tags_spec.rb index 4ec8862c308..0d859c9bc4e 100644 --- a/spec/lib/task_helpers/exports/tags_spec.rb +++ b/spec/lib/task_helpers/exports/tags_spec.rb @@ -11,7 +11,6 @@ "syntax" => "string", "single_value" => false, "example_text" => nil, - "parent_id" => 0, "show" => true, "default" => nil, "perf_by_tag" => nil, @@ -45,7 +44,6 @@ "syntax" => "string", "single_value" => false, "example_text" => nil, - "parent_id" => 0, "show" => true, "default" => true, "perf_by_tag" => nil, @@ -69,7 +67,6 @@ "syntax" => "string", "single_value" => false, "example_text" => nil, - "parent_id" => 0, "show" => true, "default" => true, "perf_by_tag" => nil, diff --git a/spec/lib/task_helpers/imports/data/tags/Import_Test.yaml b/spec/lib/task_helpers/imports/data/tags/Import_Test.yaml index ca01193df84..a34723c1943 100644 --- a/spec/lib/task_helpers/imports/data/tags/Import_Test.yaml +++ b/spec/lib/task_helpers/imports/data/tags/Import_Test.yaml @@ -5,7 +5,6 @@ syntax: string single_value: true example_text: Tag Import Test - parent_id: 0 show: true default: perf_by_tag: false diff --git a/spec/lib/task_helpers/imports/data/tags/Location.yaml b/spec/lib/task_helpers/imports/data/tags/Location.yaml index 289f67fbdd4..9abf5ac5ba0 100644 --- a/spec/lib/task_helpers/imports/data/tags/Location.yaml +++ b/spec/lib/task_helpers/imports/data/tags/Location.yaml @@ -6,7 +6,6 @@ single_value: true example_text: The geographic location of the resource, such as New York, Chicago, or London. - parent_id: 0 show: true default: true perf_by_tag: diff --git a/spec/lib/task_helpers/imports/data/tags/Tag_Test_Fail_Cat.yml b/spec/lib/task_helpers/imports/data/tags/Tag_Test_Fail_Cat.yml index b1fc64287a7..d7d251b052e 100644 --- a/spec/lib/task_helpers/imports/data/tags/Tag_Test_Fail_Cat.yml +++ b/spec/lib/task_helpers/imports/data/tags/Tag_Test_Fail_Cat.yml @@ -5,7 +5,6 @@ syntax: string single_value: true example_text: Brant Test - parent_id: 0 show: true default: perf_by_tag: false diff --git a/spec/lib/task_helpers/imports/data/tags/Tag_Test_Fail_Entry.yml b/spec/lib/task_helpers/imports/data/tags/Tag_Test_Fail_Entry.yml index ce1fca2ce45..fc1670ac2c5 100644 --- a/spec/lib/task_helpers/imports/data/tags/Tag_Test_Fail_Entry.yml +++ b/spec/lib/task_helpers/imports/data/tags/Tag_Test_Fail_Entry.yml @@ -5,7 +5,6 @@ syntax: string single_value: true example_text: Brant Test - parent_id: 0 show: true default: perf_by_tag: false diff --git a/spec/models/classification_spec.rb b/spec/models/classification_spec.rb index 9544e275a43..52d650d9d5d 100644 --- a/spec/models/classification_spec.rb +++ b/spec/models/classification_spec.rb @@ -408,7 +408,6 @@ :read_only => "0", :syntax => "string", :show => true, - :parent_id => 0, :default => true, :single_value => "1", :entries => [{:description => "Cost Center 001", :name => "001"},