Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rails 7.1 friendly attribute changes #23337

Merged
merged 3 commits into from
Feb 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions app/models/automation_request.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
class AutomationRequest < MiqRequest
alias_attribute :automation_tasks, :miq_request_tasks

TASK_DESCRIPTION = N_('Automation Request')
DEFAULT_NAMESPACE = "SYSTEM"
DEFAULT_CLASS = "PROCESS"
DEFAULT_INSTANCE = "AUTOMATION_REQUEST"
SOURCE_CLASS_NAME = nil

def automation_tasks
miq_request_tasks
end

def automation_tasks=(objects)
self.miq_request_tasks = objects
end

##############################################
# uri_parts: instance=IIII|message=MMMM or any subset thereof
# parameters: var1=vvvvv|var2=wwww|var3=xxxxx
Expand Down
9 changes: 8 additions & 1 deletion app/models/automation_task.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
class AutomationTask < MiqRequestTask
alias_attribute :automation_request, :miq_request
AUTOMATE_DRIVES = false

def automation_request
miq_request
end

def automation_request=(object)
self.miq_request = object
end

def self.get_description(_request_obj)
"Automation Task"
end
Expand Down
5 changes: 4 additions & 1 deletion app/models/ext_management_system.rb
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,6 @@ def validate_zone_not_maintenance_when_ems_enabled?
virtual_sum :total_cloud_memory, :vms, :ram_size

alias_method :clusters, :ems_clusters # Used by web-services to return clusters as the property name
alias_attribute :to_s, :name

default_value_for :enabled, true

Expand Down Expand Up @@ -423,6 +422,10 @@ def self.short_name
end
end

def to_s
name
end

def self.base_manager
(ancestors.select { |klass| klass < ::ExtManagementSystem } - [::ManageIQ::Providers::BaseManager]).last
end
Expand Down
3 changes: 0 additions & 3 deletions app/models/flavor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ class Flavor < ApplicationRecord
alias_attribute :cpus, :cpu_total_cores
alias_attribute :cpu_cores, :cpu_cores_per_socket

virtual_column :cpus, :type => :integer
virtual_column :cpu_cores, :type => :integer

def name_with_details
details = if cpus == 1
if root_disk_size.nil?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ class ManageIQ::Providers::EmbeddedAutomationManager::Authentication < ManageIQ:
# other models

alias_attribute :manager_id, :resource_id
alias_attribute :manager, :resource

after_create :set_manager_ref

supports :create
Expand All @@ -16,6 +14,14 @@ class ManageIQ::Providers::EmbeddedAutomationManager::Authentication < ManageIQ:
EXTRA_ATTRIBUTES = {}.freeze
API_ATTRIBUTES = COMMON_ATTRIBUTES.merge(EXTRA_ATTRIBUTES).freeze

def manager
resource
end

def manager=(object)
self.resource = object
end

def self.display_name(number = 1)
n_('Credential', 'Credentials', number)
end
Expand Down
31 changes: 25 additions & 6 deletions app/models/miq_provision.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,43 @@ class MiqProvision < MiqProvisionTask
include StateMachine
include Tagging

alias_attribute :miq_provision_request, :miq_request # Legacy provisioning support
alias_attribute :provision_type, :request_type # Legacy provisioning support
Comment on lines -19 to -20
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we want to keep these comments?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can add them to each method if it makes sense.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was one method that lost it: #23338

alias_attribute :vm, :destination
alias_attribute :vm_template, :source

alias_attribute :provision_type, :request_type # Legacy provisioning support
before_create :set_template_and_networking

virtual_belongs_to :miq_provision_request # Legacy provisioning support
virtual_belongs_to :vm
virtual_belongs_to :vm_template
virtual_column :placement_auto, :type => :boolean
virtual_column :provision_type, :type => :string # Legacy provisioning support

scope :with_miq_request_id, ->(request_id) { where(:miq_request_id => request_id) }

CLONE_SYNCHRONOUS = false
CLONE_TIME_LIMIT = 4.hours

def miq_provision_request
miq_request
end

def miq_provision_request=(object)
self.miq_request = object
end

def vm
destination
end

def vm=(object)
self.destination = object
end

def vm_template
source
end

def vm_template=(object)
self.source = object
end

def self.base_model
MiqProvision
end
Expand Down
20 changes: 16 additions & 4 deletions app/models/miq_provision_request.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
class MiqProvisionRequest < MiqRequest
alias_attribute :vm_template, :source
alias_attribute :provision_type, :request_type
alias_attribute :miq_provisions, :miq_request_tasks
alias_attribute :src_vm_id, :source_id

delegate :my_zone, :to => :source
Expand All @@ -20,11 +18,25 @@ class MiqProvisionRequest < MiqRequest
default_value_for(:source_id) { |r| r.get_option(:src_vm_id) || r.get_option(:source_id) }
default_value_for :source_type, "VmOrTemplate"

virtual_column :provision_type, :type => :string

include MiqProvisionMixin
include MiqProvisionQuotaMixin

def vm_template
source
end

def vm_template=(object)
self.source = object
end

def miq_provisions
miq_request_tasks
end

def miq_provisions=(objects)
self.miq_request_tasks = objects
end

def self.request_task_class_from(attribs)
source_id = MiqRequestMixin.get_option(:src_vm_id, nil, attribs['options'])
vm_or_template = source_vm_or_template!(source_id)
Expand Down
1 change: 0 additions & 1 deletion app/models/miq_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ class MiqRequest < ApplicationRecord
virtual_column :v_workflow_class, :type => :string, :uses => :workflow
virtual_column :request_type_display, :type => :string
virtual_column :resource_type, :type => :string
virtual_column :state, :type => :string
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

handled by alias_attribute on line 23 ☝️


delegate :allowed_tags, :to => :workflow, :prefix => :v, :allow_nil => true
delegate :class, :to => :workflow, :prefix => :v_workflow
Expand Down
4 changes: 3 additions & 1 deletion app/models/mixins/deprecation_mixin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ def deprecate_attribute(old_attribute, new_attribute, type:)
end

def deprecate_attribute_methods(old_attribute, new_attribute)
alias_attribute old_attribute, new_attribute
define_method(old_attribute) { self.send(new_attribute) }
define_method("#{old_attribute}=") { |object| self.send("#{new_attribute}=", object) }
define_method("#{old_attribute}?") { self.send("#{new_attribute}?") }
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

handles non-attribute calls to deprecate_attribute

["", "=", "?"].each { |suffix| Vmdb::Deprecation.deprecate_methods(self, "#{old_attribute}#{suffix}" => "#{new_attribute}#{suffix}") }
end
end
Expand Down
9 changes: 8 additions & 1 deletion app/models/persistent_volume.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,18 @@ class PersistentVolume < ContainerVolume
delegate :name, :to => :parent, :prefix => true, :allow_nil => true
has_many :container_volumes, -> { where(:type => 'ContainerVolume') }, :through => :persistent_volume_claim
has_many :parents, -> { distinct }, :through => :container_volumes, :source_type => 'ContainerGroup'
alias_attribute :container_groups, :parents

virtual_attribute :parent_name, :string
virtual_attribute :storage_capacity, :string

def container_groups
parents
end

def container_groups=(objects)
self.parents = objects
end

def storage_capacity
capacity[:storage]
end
Expand Down
8 changes: 7 additions & 1 deletion app/models/physical_switch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,13 @@ class PhysicalSwitch < Switch
:through => :connected_components,
:source => :managed_entity

alias_attribute :physical_servers, :connected_physical_servers
def physical_servers
connected_physical_servers
end

def physical_servers=(objects)
self.connected_physical_servers = objects
end

def my_zone
ems = ext_management_system
Expand Down
9 changes: 8 additions & 1 deletion app/models/resource_group.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
class ResourceGroup < ApplicationRecord
acts_as_miq_taggable
alias_attribute :images, :templates

belongs_to :ext_management_system, :foreign_key => :ems_id

Expand All @@ -13,4 +12,12 @@ class ResourceGroup < ApplicationRecord
has_many :cloud_networks, :dependent => :nullify
has_many :network_ports, :dependent => :nullify
has_many :security_groups, :dependent => :nullify

def images
templates
end

def images=(objects)
self.templates = objects
end
end
10 changes: 9 additions & 1 deletion app/models/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,18 @@ class Service < ApplicationRecord
supports :retire

alias parent_service parent
alias_attribute :service, :parent

deprecate_attribute :display, :visible, :type => :boolean
virtual_belongs_to :service

def service
parent
end

def service=(object)
self.parent = object
end

def power_states
vms.map(&:power_state)
end
Expand Down
10 changes: 8 additions & 2 deletions app/models/service_template_provision_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ class ServiceTemplateProvisionRequest < MiqRequest

after_create :process_service_order

alias_attribute :service_template, :source

virtual_has_one :picture
virtual_has_one :service_template
virtual_has_one :provision_dialog
Expand All @@ -25,6 +23,14 @@ class ServiceTemplateProvisionRequest < MiqRequest
alias_method :user, :get_user
include MiqProvisionQuotaMixin

def service_template
source
end

def service_template=(object)
self.source = object
end

def process_service_order
if cancel_requested?
do_cancel
Expand Down
1 change: 0 additions & 1 deletion app/models/tenant_quota.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ class TenantQuota < ApplicationRecord
scope :templates_allocated, -> { where(:name => :templates_allocated) }

virtual_column :name, :type => :string
virtual_column :total, :type => :integer
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

handled by alias_attribute on line 61 👇

virtual_column :used, :type => :float
virtual_column :allocated, :type => :float
virtual_column :available, :type => :float
Expand Down
10 changes: 8 additions & 2 deletions app/models/vm_cloud_reconfigure_task.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
class VmCloudReconfigureTask < MiqRequestTask
alias_attribute :vm, :source

validate :validate_request_type, :validate_state

AUTOMATE_DRIVES = false
Expand All @@ -25,6 +23,14 @@ def self.get_description(req_obj)
"#{request_class::TASK_DESCRIPTION} for: #{name}"
end

def vm
source
end

def vm=(object)
self.source = object
end

def after_request_task_create
update(:description => get_description)
end
Expand Down
10 changes: 8 additions & 2 deletions app/models/vm_migrate_task.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
class VmMigrateTask < MiqRequestTask
alias_attribute :vm, :source

validate :validate_request_type, :validate_state
default_value_for :request_type, "vm_migrate"

AUTOMATE_DRIVES = true

def vm
source
end

def vm=(object)
self.source = object
end

def self.base_model
VmMigrateTask
end
Expand Down
10 changes: 8 additions & 2 deletions app/models/vm_reconfigure_task.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
class VmReconfigureTask < MiqRequestTask
alias_attribute :vm, :source

validate :validate_request_type, :validate_state

AUTOMATE_DRIVES = false

def vm
source
end

def vm=(object)
self.source = object
end

def self.base_model
VmReconfigureTask
end
Expand Down
9 changes: 8 additions & 1 deletion app/models/vm_retire_task.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
class VmRetireTask < MiqRetireTask
alias_attribute :vm, :source
default_value_for :request_type, "vm_retire"

def vm
source
end

def vm=(object)
self.source = object
end

def self.base_model
VmRetireTask
end
Expand Down
12 changes: 0 additions & 12 deletions spec/models/mixins/deprecation_mixin_spec.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,4 @@
RSpec.describe DeprecationMixin do
# Host.deprecate_attribute :address, :hostname
context ".arel_table" do
# this is defining an alias
# it is not typical for aliases to work through arel_table
# may need to get rid of this in the future
it "works for deprecate_attribute columns" do
expect(Host.attribute_supported_by_sql?(:address)).to eq(true)
Copy link
Member Author

@jrafanie jrafanie Feb 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This arel_table context was testing the internals. We're no longer using virtual columns or alias_attribute as someone could use deprecate_attribute on associations or attributes. The former won't work in rails 7.1 without warnings and will not work with virtual columns. It's much simpler to handle attributes or associations through runtime delegations although there is some performance implications we're accepting for this flexibility. We can bring that back in a different way if we need it.

expect(Host.arel_table[:address]).to_not be_nil
expect(Host.arel_table[:address].name).to eq("hostname") # typically this is a symbol. not perfect but it works
end
end

# Host.deprecate_attribute :address, :hostname
context ".visible_attribute_names" do
it "hides deprecate_attribute columns" do
Expand Down
Loading