Class: KnifeTopo::TopoSearch

Inherits:
Chef::Knife::Search
  • Object
show all
Includes:
CommandHelper
Defined in:
lib/chef/knife/topo_search.rb

Overview

knife topo search

Instance Method Summary collapse

Methods included from CommandHelper

#check_chef_env, #initialize_cmd_args, #most_common, #resource_exists?, #run_cmd

Instance Method Details

#combine(query, group_query, constraint) ⇒ Object



84
85
86
87
88
89
90
91
# File 'lib/chef/knife/topo_search.rb', line 84

def combine(query, group_query, constraint)
  find_in_topo = config[:topo] || config[:no_topo].nil?
  if find_in_topo
    query ? "#{constraint} AND #{group_query}" : constraint
  else
    query ? "#{group_query} NOT #{constraint}" : "NOT #{constraint}"
  end
end

#constrain_query(query, topo_name) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
# File 'lib/chef/knife/topo_search.rb', line 72

def constrain_query(query, topo_name)
  # group existing query workaround for strange behavior with
  # NOTs and invalid query if put brackets round them
  group_query = query && !query.start_with?('NOT') ? "(#{query})" : query

  # search specific topologies or all/none
  constraint = topo_name ? 'topo_name:' + topo_name : 'topo_name:*'

  # combine the grouped query and constraint
  combine(query, group_query, constraint)
end

#runObject



48
49
50
51
52
53
54
55
# File 'lib/chef/knife/topo_search.rb', line 48

def run
  setup_query
  super
rescue StandardError => e
  raise if Chef::Config[:verbosity] == 2
  ui.error "Topology search for \"#{@query}\" exited with error"
  humanize_exception(e)
end

#setup_queryObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/chef/knife/topo_search.rb', line 57

def setup_query
  query_str = @name_args[0] || config[:query]
  topo_query = constrain_query(query_str, config[:topo])

  # force a node search
  @name_args[0] = 'node'

  # override any query
  if config[:query]
    config[:query] = topo_query
  else
    @name_args[1] = topo_query
  end
end