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

fixes #9296 - Ensure bond has a mac address of one of attached iface #2142

Closed
wants to merge 1 commit into from
Closed
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
12 changes: 12 additions & 0 deletions app/models/nic/bond.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class Bond < Managed
validates :attached_devices, :format => { :with => /\A[a-z0-9#{SEPARATOR}.:_-]+\Z/ }, :allow_blank => true

before_save :ensure_virtual
after_save :ensure_mac
Copy link
Member

Choose a reason for hiding this comment

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

shouldn't this happen before validation?


register_to_enc_transformation :type, lambda { |type| type.constantize.humanized_name }

Expand Down Expand Up @@ -44,6 +45,17 @@ def ensure_virtual
def enc_attributes
@enc_attributes ||= (super + %w(mode attached_devices bond_options))
end

def ensure_mac
mac_addresses = attached_devices_identifiers.collect { |a|
Copy link
Member

Choose a reason for hiding this comment

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

perhaps this would be better

mac_addresses = self.host.interfaces.where(:identifier => attached_devices_identifiers).pluck(:mac)

self.host.interfaces.where(identifier: a).first.mac
}
self.mac = nil unless mac_addresses.include? self.mac
Copy link
Member

Choose a reason for hiding this comment

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

I'd combine L53-L56 like this

mac_addresses = mac_addresses.compact # actually this should get to previous mac_addresses SQL...
self.mac =  mac_addresses.first unless mac_addresses.include?(self.mac)

overall I'm not sure about this, we'd silently override user custom input if it's another mac, I'd rather override the mac only if it's nil, what do you think?

unless self.mac
self.mac = mac_addresses.first
end
self.update_column(:mac, self.mac)
Copy link
Member

Choose a reason for hiding this comment

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

if we move it to before validation, it will be saved in normal save chain (within transaction)

end
end

Base.register_type(Bond)
Expand Down