Module: Jerakia::Client::CLI::Lookup

Included in:
Jerakia::Client::CLI
Defined in:
lib/jerakia/client/cli/lookup.rb

Class Method Summary collapse

Class Method Details

.included(thor) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/jerakia/client/cli/lookup.rb', line 9

def self.included(thor)
  thor.class_eval do
    desc 'lookup [KEY]', 'Lookup [KEY] with Jerakia'

    option :host,
      aliases: :H,
      type: :string,
      default: nil,
      desc: 'Hostname or IP to connect to'

    option :port,
      aliases: :P,
      type: :string,
      default: nil,
      desc: 'Port to connect to'

    option :token,
      aliases: :T,
      type: :string,
      default: nil,
      desc: "Token to use for authorization, if not provided a jerakia.yaml will be searched for (see docs)"

    option :api,
      aliases: :a,
      type: :string,
      default: nil,
      desc: 'API Version to implement (see docs)'

    option :policy,
           aliases: :p,
           type: :string,
           default: 'default',
           desc: 'Lookup policy'
    option :namespace,
           aliases: :n,
           type: :string,
           default: '',
           desc: 'Lookup namespace'
    option :type,
           aliases: :t,
           type: :string,
           default: 'first',
           desc: 'Lookup type'
    option :metadata,
           aliases: :m,
           type: :hash,
           default: {},
           desc: 'Metadata to send with the request'
    option :scope,
           aliases: :s,
           type: :string,
           desc: 'Scope handler',
           default: 'metadata'
    option :scope_options,
           type: :hash,
           default: {},
           desc: 'Key/value pairs to be passed to the scope handler'
    option :merge_type,
           aliases: :m,
           type: :string,
           default: 'array',
           desc: 'Merge type'
    option :verbose,
           aliases: :v,
           type: :boolean,
           desc: 'Print verbose information'
    option :debug,
           aliases: :D,
           type: :boolean,
           desc: 'Debug information to console, implies --log-level debug'

    option :output,
           aliases: :o,
           type: :string,
           default: 'json',
           desc: 'Output format, yaml, json or msgpack'

    option :content_type,
           aliases: :c,
           type: :string,
           default: 'json',
           desc: 'Content type, json or msgpack'

    def lookup(key)
      client = Jerakia::Client.new({
        :host  => options[:host],
        :port  => options[:port],
        :api   => options[:api],
        :token => options[:token],
        :content_type => options[:content_type],
      })

      lookup_opts = {
        :namespace     => options[:namespace].split(/::/),
        :policy        => options[:policy].to_sym,
        :lookup_type   => options[:type].to_sym,
        :merge         => options[:merge_type].to_sym,
        :scope         => options[:scope].to_sym,
        :use_schema    => options[:schema]
      }

      if options[:metadata]
        options[:metadata].each do |k,v|
          lookup_opts["metadata_#{k}".to_sym] = v
        end
      end

      if options[:scope]
        options[:scope_options].each do |k,v|
          lookup_opts["scope_#{k}".to_sym] = v
        end
      end

      response = client.lookup(key, lookup_opts)
      answer = response['payload']
      case options[:output]
      when 'json'
        puts answer.to_json
      when 'yaml'
        puts answer.to_yaml
      when 'msgpack'
        puts answer.to_msgpack
      else
        raise Jerakia::Client::Error, "Unknown output type #{options[:output]}"
      end
    end
  end
end