Skip to content

Commit

Permalink
Classification: Use nil for no parent
Browse files Browse the repository at this point in the history
  • Loading branch information
kbrock committed Jan 8, 2019
1 parent 9a94052 commit 1de6131
Show file tree
Hide file tree
Showing 11 changed files with 30 additions and 55 deletions.
32 changes: 18 additions & 14 deletions app/models/classification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@ class Classification < ApplicationRecord

validates :syntax, :inclusion => {:in => %w( string integer boolean ),
:message => "should be one of 'string', 'integer' or 'boolean'"}
before_validation :nil_parent

scope :visible, -> { where(:show => true) }
scope :read_only, -> { where(:read_only => true) }
scope :writeable, -> { where(:read_only => false) }

scope :is_category, -> { where(:parent_id => 0) }
scope :is_entry, -> { where.not(:parent_id => 0) }
scope :is_category, -> { where(:parent_id => nil) }
scope :is_entry, -> { where.not(:parent_id => nil) }

scope :with_writable_parents, -> { includes(:parent).where(:parents_classifications => { :read_only => false}) }

Expand Down Expand Up @@ -196,7 +197,7 @@ def self.get_tags_from_object(obj)
end

def self.create_category!(options)
self.create!(options.merge(:parent_id => 0))
self.create!(options.merge(:parent_id => nil))
end

def self.categories(region_id = my_region_number, ns = DEFAULT_NAMESPACE)
Expand Down Expand Up @@ -306,7 +307,7 @@ def to_tag
end

def category?
parent_id == 0
parent_id.nil?
end

def category
Expand All @@ -327,11 +328,13 @@ def find_entry_by_name(name, region_id = my_region_number)
self.class.find_by_name(name, region_id, ns, self)
end

def self.find_by_name(name, region_id = my_region_number, ns = DEFAULT_NAMESPACE, parent_id = 0)
# @param parent_id [Integer|Parent] node for the parent. This is only passed in for find_entry_by_name
def self.find_by_name(name, region_id = my_region_number, ns = DEFAULT_NAMESPACE, parent_id = nil)
find_by_names([name], region_id, ns, parent_id).first
end

def self.find_by_names(names, region_id = my_region_number, ns = DEFAULT_NAMESPACE, parent_id = 0)
# @param parent_id [Integer|Parent] node for the parent. This is only passed in for find_entry_by_name
def self.find_by_names(names, region_id = my_region_number, ns = DEFAULT_NAMESPACE, parent_id = nil)
tag_names = names.map { |name| name2tag(name, parent_id, ns) }
# NOTE: tags is a subselect - not an array of ids
tags = Tag.in_region(region_id).where(:name => tag_names).select(:id)
Expand Down Expand Up @@ -395,7 +398,7 @@ def self.import_from_hash(classification, parent = nil)

stats = {"categories" => 0, "entries" => 0}

if classification["parent_id"] == 0 # category
if parent.nil?
cat = find_by_name(classification["name"])
if cat
_log.info("Skipping Classification (already in DB): Category: name=[#{classification["name"]}]")
Expand Down Expand Up @@ -451,9 +454,6 @@ def self.seed
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)
Expand Down Expand Up @@ -492,8 +492,8 @@ def validate_format_of_name
end
end

def self.name2tag(name, parent_id = 0, ns = DEFAULT_NAMESPACE)
if parent_id == 0
def self.name2tag(name, parent_id = nil, ns = DEFAULT_NAMESPACE)
if parent_id == 0 || parent_id.nil?
File.join(ns, name)
else
c = parent_id.kind_of?(Classification) ? parent_id : Classification.find(parent_id)
Expand Down Expand Up @@ -523,12 +523,12 @@ def self.tag2human(tag)
private_class_method :tag2human

def find_tag
tag_name = Classification.name2tag(name, parent_id, ns)
tag_name = Classification.name2tag(name, parent, ns)
Tag.in_region(region_id).find_by(:name => tag_name)
end

def save_tag
tag_name = Classification.name2tag(name, parent_id, ns)
tag_name = Classification.name2tag(name, parent, ns)
self.tag = Tag.in_region(region_id).find_or_create_by(:name => tag_name)
end

Expand Down Expand Up @@ -559,4 +559,8 @@ def delete_tags_and_entries

delete_tag_and_taggings
end

def nil_parent
self.parent_id = nil if parent_id == 0
end
end
1 change: 0 additions & 1 deletion app/models/vm_or_template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1383,7 +1383,6 @@ def self.folder_category(folder_type)
cat = Classification.new(
:name => cat_name,
:description => "Parent Folder Path (#{folder_type == :blue ? "VMs & Templates" : "Hosts & Clusters"})",
:parent_id => 0,
:single_value => true,
:read_only => true
)
Expand Down
28 changes: 0 additions & 28 deletions db/fixtures/classifications.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -242,7 +238,6 @@
:show: true
:name: role
:example_text:
:parent_id: 0
:default: true
:single_value: "1"
:ns: /managed/user
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -442,7 +433,6 @@
:show: true
:name: cc
:example_text: Cost Center
:parent_id: 0
:default: true
:single_value: "1"
- :description: Network Location
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -521,7 +509,6 @@
:show: true
:name: prov_scope
:example_text: Provisioning Scope
:parent_id: 0
:default: true
:single_value: "0"
- :description: EVM Operations
Expand Down Expand Up @@ -552,7 +539,6 @@
:show: true
:name: operations
:example_text: Operations
:parent_id: 0
:default: true
:single_value: "1"
- :description: Quota - Max CPUs
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -940,7 +919,6 @@
:show: true
:name: lifecycle
:example_text: LifeCycle Options
:parent_id: 0
:default: true
:single_value: "1"
- :description: Migration Group
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -1084,6 +1057,5 @@
:show: true
:name: transformation_status
:example_text: VM has been migrated.
:parent_id: 0
:default: true
:single_value: "1"
1 change: 0 additions & 1 deletion spec/factories/categories.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,5 @@
factory(:category) do
sequence(:name) { |n| "category_#{seq_padded_for_sorting(n)}" }
sequence(:description) { |n| "category #{seq_padded_for_sorting(n)}" }
parent_id 0
end
end
1 change: 0 additions & 1 deletion spec/factories/classification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
factory :classification do
sequence(:name) { |n| "category_#{seq_padded_for_sorting(n)}" }
sequence(:description) { |n| "category #{seq_padded_for_sorting(n)}" }
parent_id 0
end

factory :classification_tag, :class => :Classification do
Expand Down
3 changes: 0 additions & 3 deletions spec/lib/task_helpers/exports/tags_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
"syntax" => "string",
"single_value" => false,
"example_text" => nil,
"parent_id" => 0,
"show" => true,
"default" => nil,
"perf_by_tag" => nil,
Expand Down Expand Up @@ -45,7 +44,6 @@
"syntax" => "string",
"single_value" => false,
"example_text" => nil,
"parent_id" => 0,
"show" => true,
"default" => true,
"perf_by_tag" => nil,
Expand All @@ -69,7 +67,6 @@
"syntax" => "string",
"single_value" => false,
"example_text" => nil,
"parent_id" => 0,
"show" => true,
"default" => true,
"perf_by_tag" => nil,
Expand Down
1 change: 0 additions & 1 deletion spec/lib/task_helpers/imports/data/tags/Import_Test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
syntax: string
single_value: true
example_text: Tag Import Test
parent_id: 0
show: true
default:
perf_by_tag: false
Expand Down
1 change: 0 additions & 1 deletion spec/lib/task_helpers/imports/data/tags/Location.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
syntax: string
single_value: true
example_text: Brant Test
parent_id: 0
show: true
default:
perf_by_tag: false
Expand Down
Loading

0 comments on commit 1de6131

Please sign in to comment.