knife-psearch 0.0.3 → 0.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 166bc4b45854a5ae83079c0ec2bd72971151b00d
4
+ data.tar.gz: 5e7955d0be6902d35a6b6cdadff03fb8a6328f7b
5
+ SHA512:
6
+ metadata.gz: fb1878e420d492a56986b58f78bafd2189b0d03be3b78a7237bbb1b50adbf6d4861a64a2022d83df5458ada4f4e6d45bef096de91ec370346818200f1f2c931b
7
+ data.tar.gz: 4caa4840efd5b0e1ba4a11833432e4ab5bc6c39efa6620f078a429b7a48e4b35fa16bded6cb04dbb7dee5eff24d087095e4104ad5bc84fd623b1d00119d98b35
@@ -5,10 +5,16 @@
5
5
  #
6
6
  # This plugin is not yes officially supported by Opscode
7
7
 
8
+ require 'chef/knife'
9
+ require 'chef/knife/core/node_presenter'
10
+
8
11
  class Psearch < Chef::Knife
9
- banner "knife psearch INDEX SEARCH NAME=DESIRED_KEY_PATH,[NAME=DESIRED_KEY_PATH]"
12
+ banner "knife psearch INDEX SEARCH [NAME=DESIRED_KEY_PATH,[NAME=DESIRED_KEY_PATH]]"
13
+
14
+ include ::Chef::Knife::Core::MultiAttributeReturnOption
10
15
 
11
16
  deps do
17
+ require 'chef/node'
12
18
  require 'chef/search/partial_search'
13
19
  end
14
20
 
@@ -25,6 +31,11 @@ class Psearch < Chef::Knife
25
31
  :default => 0,
26
32
  :proc => lambda { |i| i.to_i }
27
33
 
34
+ option :id_only,
35
+ :short => "-i",
36
+ :long => "--id-only",
37
+ :description => "Show only the ID of matching objects"
38
+
28
39
  option :rows,
29
40
  :short => "-R INT",
30
41
  :long => "--rows INT",
@@ -32,20 +43,67 @@ class Psearch < Chef::Knife
32
43
  :default => 1000,
33
44
  :proc => lambda { |i| i.to_i }
34
45
 
35
- option :attribute,
36
- :short => "-a ATTR",
37
- :long => "--attribute ATTR",
38
- :description => "Show only one attribute"
46
+ DEFAULT_NODE_HASH = {
47
+ "name" => ["name"],
48
+ "chef_environment" => ["chef_environment"],
49
+ "fqdn" => ["fqdn"],
50
+ "ipaddress" => ["ipaddress"],
51
+ "run_list" => ["run_list"],
52
+ "roles" => ["roles"],
53
+ "recipes" => ["recipes"],
54
+ "platform" => ["platform"],
55
+ "tags" => ["tags"]
56
+ }
57
+
58
+ # "id" will be used by the generic presenter automatically when
59
+ # config[:id_only] is true
60
+ ID_ONLY_HASH = {
61
+ "id" => ["name"]
62
+ }
39
63
 
40
64
  def run
41
65
  @index, @search, *@keys = @name_args
66
+ @inflate_nodes = false
67
+
42
68
  args_hash = {}
43
- args_hash[:keys] = build_key_hash
69
+ args_hash[:keys] = if config[:id_only]
70
+ ID_ONLY_HASH
71
+ elsif ! @keys.empty?
72
+ build_key_hash
73
+ elsif ! config[:attribute].nil? && ! config[:attribute].empty?
74
+ # config[:attribute] comes from the -a option,
75
+ # which is provided by Knife::Core::MultiAttributeReturnOption
76
+ build_key_hash_from_attrs(config[:attribute])
77
+ elsif @index == "node"
78
+ DEFAULT_NODE_HASH
79
+ else
80
+ ui.warn("Falling back to full search. Use the -a or -i option to enable parital search")
81
+ nil
82
+ end
83
+
84
+ # Create output similar to knife-search
85
+ # in the default case by creating Chef::Node objects
86
+ # and using the node presenter
87
+ if args_hash[:keys] == DEFAULT_NODE_HASH && @index == 'node'
88
+ Chef::Log.debug("Using NodePresenter for output")
89
+ ui.use_presenter ::Chef::Knife::Core::NodePresenter
90
+ @inflate_nodes = true
91
+ end
92
+
44
93
  args_hash[:sort] = config[:sort]
45
94
  args_hash[:start] = config[:start]
46
95
  args_hash[:rows] = config[:rows]
47
96
  results = Chef::PartialSearch.new.search(@index, @search, args_hash)
48
- ui.output ui.format_for_display(results.first)
97
+ print_results(results.first)
98
+ end
99
+
100
+ def build_key_hash_from_attrs(attrs)
101
+ key_hash = {}
102
+ attrs.each do |a|
103
+ key_hash[a] = a.split(".")
104
+ end
105
+ key_hash["id"] = [ "name" ] unless key_hash.has_key?("name")
106
+ key_hash
49
107
  end
50
108
 
51
109
  def build_key_hash
@@ -60,4 +118,19 @@ class Psearch < Chef::Knife
60
118
  key_hash["name"] = [ "name" ] unless key_hash.has_key?("name")
61
119
  key_hash
62
120
  end
121
+
122
+ def print_results(items)
123
+ items.each do |res|
124
+ res = create_node(res) if @inflate_nodes
125
+ ui.output ui.format_for_display(res)
126
+ puts "\n" if ! config[:id_only]
127
+ end
128
+ end
129
+
130
+ def create_node(node_data)
131
+ node_data['attributes'] = node_data.reject do |key, value|
132
+ ["name", "chef_environment", "run_list"].include?(key)
133
+ end
134
+ Chef::Node.json_create(node_data)
135
+ end
63
136
  end
@@ -33,7 +33,7 @@ class Chef
33
33
  attr_accessor :rest
34
34
 
35
35
  def initialize(url=nil)
36
- @rest = ::Chef::REST.new(url || ::Chef::Config[:search_url])
36
+ @rest = ::Chef::REST.new(url || ::Chef::Config[:chef_server_url])
37
37
  end
38
38
 
39
39
  # Search Solr for objects of a given type, for a given query. If you give
@@ -1,3 +1,3 @@
1
1
  module KnifePSearch
2
- VERSION = "0.0.3"
2
+ VERSION = "0.9.0"
3
3
  end
metadata CHANGED
@@ -1,15 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: knife-psearch
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
5
- prerelease:
4
+ version: 0.9.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - Steven Danna
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2013-02-28 00:00:00.000000000 Z
11
+ date: 2014-11-19 00:00:00.000000000 Z
13
12
  dependencies: []
14
13
  description: Knife Plugin for Chef 11's Partial Search
15
14
  email: steve@opscode.com
@@ -24,26 +23,26 @@ files:
24
23
  - lib/knife-psearch/version.rb
25
24
  homepage: http://wiki.opscode.com/display/chef
26
25
  licenses: []
26
+ metadata: {}
27
27
  post_install_message:
28
28
  rdoc_options: []
29
29
  require_paths:
30
30
  - lib
31
31
  required_ruby_version: !ruby/object:Gem::Requirement
32
- none: false
33
32
  requirements:
34
- - - ! '>='
33
+ - - ">="
35
34
  - !ruby/object:Gem::Version
36
35
  version: '0'
37
36
  required_rubygems_version: !ruby/object:Gem::Requirement
38
- none: false
39
37
  requirements:
40
- - - ! '>='
38
+ - - ">="
41
39
  - !ruby/object:Gem::Version
42
40
  version: '0'
43
41
  requirements: []
44
42
  rubyforge_project:
45
- rubygems_version: 1.8.23
43
+ rubygems_version: 2.4.1
46
44
  signing_key:
47
- specification_version: 3
45
+ specification_version: 4
48
46
  summary: Knife Plugin for Chef 11's Partial Search
49
47
  test_files: []
48
+ has_rdoc: