diff --git a/app/models/vm_or_template.rb b/app/models/vm_or_template.rb index 16a602e77900..230051f56dec 100644 --- a/app/models/vm_or_template.rb +++ b/app/models/vm_or_template.rb @@ -1618,6 +1618,21 @@ def self.vms_by_ipaddress(ipaddress) end end + def self.miq_expression_includes_any_ipaddresses_arel(ipaddress) + vms = arel_table + networks = Network.arel_table + hardwares = Hardware.arel_table + + match_grouping = networks[:ipaddress].matches("%#{ipaddress}%") + .or(networks[:ipv6address].matches("%#{ipaddress}%")) + + query = hardwares.project(1) + .join(networks).on(networks[:hardware_id].eq(hardwares[:id])) + .where(hardwares[:vm_or_template_id].eq(vms[:id]).and(match_grouping)) + .take(1) + Arel::Nodes::SqlLiteral.new("1").eq(query) + end + def self.scan_by_property(property, value, _options = {}) _log.info("scan_vm_by_property called with property:[#{property}] value:[#{value}]") case property diff --git a/lib/miq_expression.rb b/lib/miq_expression.rb index b909bc21c477..97e528767f5b 100644 --- a/lib/miq_expression.rb +++ b/lib/miq_expression.rb @@ -344,6 +344,15 @@ def sql_supports_atom?(exp) # TODO: Support includes operator for sub-sub-tables return false end + when "includes any", "includes all", "includes only" + # Support this only from the main model (for now) + if exp[operator].keys.include?("field") && exp[operator]["field"].split(".").length == 1 + model, field = exp[operator]["field"].split("-") + method = "miq_expression_#{operator.downcase.tr(' ', '_')}_#{field}_arel" + return model.constantize.respond_to?(method) + else + return false + end when "find", "regular expression matches", "regular expression does not match", "key exists", "value exists" return false else @@ -1343,6 +1352,11 @@ def to_arel(exp, tz) escape = nil case_sensitive = true arel_attribute.matches("%#{parsed_value}%", escape, case_sensitive) + when "includes all", "includes any", "includes only" + method = "miq_expression_" + method << "#{operator.downcase.tr(' ', '_')}_" + method << "#{field.column}_arel" + field.model.send(method, parsed_value) when "starts with" escape = nil case_sensitive = true