From 61ce705a11270b3ab994d989e33426d874d68f9b Mon Sep 17 00:00:00 2001 From: Jillian Tullo Date: Mon, 19 Feb 2018 10:19:56 -0500 Subject: [PATCH] Add lans as a virtual relationship to ems_cluster Lans need to be exposed via the API for the creation of transformation mappings. By adding this virtual relationship, they are able to be queried via `/api/clusters/:id?attributes=lans` There will be a lans collection added to the API in a follow-up PR --- app/models/ems_cluster.rb | 2 ++ app/models/mixins/aggregation_mixin/methods.rb | 6 ++++++ 2 files changed, 8 insertions(+) diff --git a/app/models/ems_cluster.rb b/app/models/ems_cluster.rb index cb6784f51ca..c7a9eb6b4f6 100644 --- a/app/models/ems_cluster.rb +++ b/app/models/ems_cluster.rb @@ -34,6 +34,8 @@ class EmsCluster < ApplicationRecord virtual_has_many :storages, :uses => {:hosts => :storages} virtual_has_many :resource_pools, :uses => :all_relationships + virtual_has_many :lans, :uses => {:hosts => :lans} + has_many :failover_hosts, -> { failover }, :class_name => "Host" include SerializedEmsRefObjMixin diff --git a/app/models/mixins/aggregation_mixin/methods.rb b/app/models/mixins/aggregation_mixin/methods.rb index 25a3b568228..6ef80dcaa00 100644 --- a/app/models/mixins/aggregation_mixin/methods.rb +++ b/app/models/mixins/aggregation_mixin/methods.rb @@ -47,5 +47,11 @@ def aggregate_hardware(from, field, targets = nil) hdws.inject(0) { |t, hdw| t + hdw.send(field).to_i } end + + def lans + hosts = all_hosts + MiqPreloader.preload(hosts, :lans) + hosts.flat_map(&:lans).compact.uniq + end end end