hammer_cli_foreman 0.0.10 → 0.0.11

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.

Potentially problematic release.


This version of hammer_cli_foreman might be problematic. Click here for more details.

Files changed (46) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +27 -0
  3. data/doc/host_create.md +190 -0
  4. data/lib/hammer_cli_foreman.rb +1 -0
  5. data/lib/hammer_cli_foreman/commands.rb +21 -7
  6. data/lib/hammer_cli_foreman/common_parameter.rb +12 -18
  7. data/lib/hammer_cli_foreman/domain.rb +8 -6
  8. data/lib/hammer_cli_foreman/environment.rb +10 -0
  9. data/lib/hammer_cli_foreman/exception_handler.rb +11 -1
  10. data/lib/hammer_cli_foreman/exceptions.rb +9 -0
  11. data/lib/hammer_cli_foreman/host.rb +107 -16
  12. data/lib/hammer_cli_foreman/hostgroup.rb +10 -0
  13. data/lib/hammer_cli_foreman/puppet_class.rb +10 -1
  14. data/lib/hammer_cli_foreman/resource_supported_test.rb +3 -3
  15. data/lib/hammer_cli_foreman/smart_class_parameter.rb +110 -0
  16. data/lib/hammer_cli_foreman/version.rb +1 -1
  17. data/test/unit/apipie_resource_mock.rb +88 -0
  18. data/test/unit/architecture_test.rb +91 -0
  19. data/test/unit/common_parameter_test.rb +77 -0
  20. data/test/unit/compute_resource_test.rb +94 -0
  21. data/test/unit/domain_test.rb +130 -0
  22. data/test/unit/environment_test.rb +110 -0
  23. data/test/unit/exception_handler_test.rb +48 -0
  24. data/test/unit/fact_test.rb +31 -0
  25. data/test/unit/helpers/command.rb +69 -0
  26. data/test/unit/helpers/resource_disabled.rb +24 -0
  27. data/test/unit/host_test.rb +315 -0
  28. data/test/unit/hostgroup_test.rb +145 -0
  29. data/test/unit/image_test.rb +136 -0
  30. data/test/unit/location_test.rb +109 -0
  31. data/test/unit/media_test.rb +106 -0
  32. data/test/unit/model_test.rb +98 -0
  33. data/test/unit/operating_system_test.rb +151 -0
  34. data/test/unit/organization_test.rb +109 -0
  35. data/test/unit/output/formatters_test.rb +19 -0
  36. data/test/unit/partition_table_test.rb +143 -0
  37. data/test/unit/puppet_class_test.rb +71 -0
  38. data/test/unit/report_test.rb +64 -0
  39. data/test/unit/smart_class_parameter_test.rb +77 -0
  40. data/test/unit/smart_proxy_test.rb +102 -0
  41. data/test/unit/subnet_test.rb +95 -0
  42. data/test/unit/template_test.rb +141 -0
  43. data/test/unit/test_helper.rb +27 -0
  44. data/test/unit/test_output_adapter.rb +19 -0
  45. data/test/unit/user_test.rb +92 -0
  46. metadata +107 -29
@@ -1,13 +1,15 @@
1
1
  require 'hammer_cli/exception_handler'
2
+ require 'hammer_cli_foreman/exceptions'
2
3
 
3
4
  module HammerCLIForeman
4
5
  class ExceptionHandler < HammerCLI::ExceptionHandler
5
6
 
6
7
  def mappings
7
8
  super + [
9
+ [HammerCLIForeman::OperationNotSupportedError, :handle_unsupported_operation],
8
10
  [RestClient::Forbidden, :handle_forbidden],
9
11
  [RestClient::UnprocessableEntity, :handle_unprocessable_entity],
10
- [ArgumentError, :handle_argument_error]
12
+ [ArgumentError, :handle_argument_error],
11
13
  ]
12
14
  end
13
15
 
@@ -28,12 +30,20 @@ module HammerCLIForeman
28
30
  HammerCLI::EX_USAGE
29
31
  end
30
32
 
33
+
31
34
  def handle_forbidden(e)
32
35
  print_error "Forbidden - server refused to process the request"
33
36
  log_full_error e
34
37
  HammerCLI::EX_NOPERM
35
38
  end
36
39
 
40
+
41
+ def handle_unsupported_operation(e)
42
+ print_error e.message
43
+ log_full_error e
44
+ HammerCLI::EX_UNAVAILABLE
45
+ end
46
+
37
47
  end
38
48
  end
39
49
 
@@ -0,0 +1,9 @@
1
+ require 'hammer_cli_foreman/exceptions'
2
+
3
+ module HammerCLIForeman
4
+
5
+ class OperationNotSupportedError < StandardError; end
6
+
7
+ end
8
+
9
+
@@ -4,6 +4,7 @@ require 'hammer_cli_foreman/commands'
4
4
  require 'hammer_cli_foreman/parameter'
5
5
  require 'hammer_cli_foreman/report'
6
6
  require 'hammer_cli_foreman/puppet_class'
7
+ require 'hammer_cli_foreman/smart_class_parameter'
7
8
 
8
9
  module HammerCLIForeman
9
10
 
@@ -59,8 +60,12 @@ module HammerCLIForeman
59
60
  params['host']['host_parameters_attributes'] = parameter_attributes
60
61
  params['host']['compute_attributes'] = compute_attributes || {}
61
62
  params['host']['compute_attributes']['volumes_attributes'] = nested_attributes(volume_list)
62
- params['host']['compute_attributes']['interfaces_attributes'] = nested_attributes(interface_list)
63
- params['host']['compute_attributes']['nics_attributes'] = nested_attributes(interface_list)
63
+ if compute_resource_id
64
+ params['host']['compute_attributes']['interfaces_attributes'] = nested_attributes(interface_list)
65
+ params['host']['compute_attributes']['nics_attributes'] = nested_attributes(interface_list)
66
+ else
67
+ params['host']['interfaces_attributes'] = nested_attributes(interface_list)
68
+ end
64
69
 
65
70
  params
66
71
  end
@@ -87,11 +92,12 @@ module HammerCLIForeman
87
92
  end
88
93
 
89
94
 
90
- class Host < HammerCLI::AbstractCommand
95
+ class Host < HammerCLI::Apipie::Command
96
+
97
+ resource ForemanApi::Resources::Host
98
+
91
99
  class ListCommand < HammerCLIForeman::ListCommand
92
100
  # FIXME: list compute resource (model)
93
- resource ForemanApi::Resources::Host, "index"
94
-
95
101
  output do
96
102
  from "host" do
97
103
  field :id, "Id"
@@ -109,8 +115,6 @@ module HammerCLIForeman
109
115
 
110
116
  class InfoCommand < HammerCLIForeman::InfoCommand
111
117
 
112
- resource ForemanApi::Resources::Host, "show"
113
-
114
118
  def retrieve_data
115
119
  host = super
116
120
  host["host"]["environment_name"] = host["host"]["environment"]["environment"]["name"] rescue nil
@@ -167,13 +171,39 @@ module HammerCLIForeman
167
171
  end
168
172
 
169
173
 
170
- class StatusCommand < HammerCLIForeman::InfoCommand
174
+ class StatusCommand < HammerCLI::Apipie::ReadCommand
175
+
176
+ identifiers :id, :name
171
177
 
172
178
  command_name "status"
173
- resource ForemanApi::Resources::Host, "status"
174
179
 
175
- def print_data(records)
176
- print_message records["status"]
180
+ output do
181
+ field :status, "Status"
182
+ field :power, "Power"
183
+ end
184
+
185
+ def retrieve_data
186
+ {
187
+ :status => get_status,
188
+ :power => get_power_status
189
+ }
190
+ end
191
+
192
+ def get_status
193
+ params = {
194
+ 'id' => get_identifier[0],
195
+ }
196
+ status = resource.call(:status, params)[0]
197
+ status["status"]
198
+ end
199
+
200
+ def get_power_status
201
+ params = {
202
+ 'id' => get_identifier[0],
203
+ 'power_action' => :state
204
+ }
205
+ status = resource.call(:power, params)[0]
206
+ status["power"]
177
207
  end
178
208
  end
179
209
 
@@ -181,7 +211,7 @@ module HammerCLIForeman
181
211
  class PuppetRunCommand < HammerCLIForeman::InfoCommand
182
212
 
183
213
  command_name "puppetrun"
184
- resource ForemanApi::Resources::Host, "puppetrun"
214
+ action "puppetrun"
185
215
 
186
216
  def print_data(records)
187
217
  print_message 'Puppet run triggered'
@@ -260,7 +290,7 @@ module HammerCLIForeman
260
290
 
261
291
  success_message "Host created"
262
292
  failure_message "Could not create the host"
263
- resource ForemanApi::Resources::Host, "create"
293
+ action "create"
264
294
 
265
295
  include HammerCLIForeman::CommonHostUpdateOptions
266
296
 
@@ -278,7 +308,6 @@ module HammerCLIForeman
278
308
 
279
309
  success_message "Host updated"
280
310
  failure_message "Could not update the host"
281
- resource ForemanApi::Resources::Host, "update"
282
311
 
283
312
  include HammerCLIForeman::CommonHostUpdateOptions
284
313
  end
@@ -288,7 +317,6 @@ module HammerCLIForeman
288
317
 
289
318
  success_message "Host deleted"
290
319
  failure_message "Could not delete the host"
291
- resource ForemanApi::Resources::Host, "destroy"
292
320
 
293
321
  apipie_options
294
322
  end
@@ -339,10 +367,73 @@ module HammerCLIForeman
339
367
  end
340
368
  end
341
369
 
370
+
371
+ class StartCommand < HammerCLI::Apipie::WriteCommand
372
+
373
+ identifiers :id, :name
374
+ action "power"
375
+
376
+ command_name "start"
377
+ desc "Power a host on"
378
+ success_message "The host is starting."
379
+
380
+ def power_action
381
+ :start
382
+ end
383
+ end
384
+
385
+
386
+ class StopCommand < HammerCLI::Apipie::WriteCommand
387
+
388
+ option '--force', :flag, "Force turning off a host"
389
+
390
+ identifiers :id, :name
391
+ action "power"
392
+
393
+ command_name "stop"
394
+ desc "Power a host off"
395
+
396
+ def power_action
397
+ if force?
398
+ :cycle
399
+ else
400
+ :stop
401
+ end
402
+ end
403
+
404
+ def success_message
405
+ if force?
406
+ "Power off forced."
407
+ else
408
+ "Powering the host off."
409
+ end
410
+ end
411
+ end
412
+
413
+ class RebootCommand < HammerCLI::Apipie::WriteCommand
414
+
415
+ identifiers :id, :name
416
+ action "power"
417
+
418
+ command_name "reboot"
419
+ desc "Reboot a host"
420
+ success_message "Host reboot started."
421
+
422
+ def power_action
423
+ :soft
424
+ end
425
+ end
426
+
427
+ class SCParamsCommand < HammerCLIForeman::SmartClassParametersList
428
+
429
+ apipie_options :without => [:host_id, :hostgroup_id, :puppetclass_id, :environment_id]
430
+ option ['--id', '--name'], 'HOST_ID', 'host id/name',
431
+ :attribute_name => :host_id, :required => true
432
+ end
433
+
342
434
  autoload_subcommands
343
435
  end
344
436
 
345
437
  end
346
438
 
347
439
  HammerCLI::MainCommand.subcommand 'host', "Manipulate Foreman's hosts.", HammerCLIForeman::Host
348
-
@@ -2,6 +2,7 @@ require 'hammer_cli'
2
2
  require 'foreman_api'
3
3
  require 'hammer_cli_foreman/commands'
4
4
  require 'hammer_cli_foreman/parameter'
5
+ require 'hammer_cli_foreman/smart_class_parameter'
5
6
 
6
7
  module HammerCLIForeman
7
8
 
@@ -137,6 +138,15 @@ module HammerCLIForeman
137
138
  end
138
139
  end
139
140
 
141
+
142
+ class SCParamsCommand < HammerCLIForeman::SmartClassParametersList
143
+
144
+ apipie_options :without => [:host_id, :hostgroup_id, :puppetclass_id, :environment_id]
145
+ option ['--id', '--name'], 'HOSTGROUP_ID', 'hostgroup id/name',
146
+ :attribute_name => :hostgroup_id, :required => true
147
+ end
148
+
149
+
140
150
  autoload_subcommands
141
151
  end
142
152
 
@@ -1,6 +1,7 @@
1
1
  require 'hammer_cli'
2
2
  require 'foreman_api'
3
3
  require 'hammer_cli_foreman/commands'
4
+ require 'hammer_cli_foreman/smart_class_parameter'
4
5
 
5
6
  module HammerCLIForeman
6
7
 
@@ -46,10 +47,18 @@ module HammerCLIForeman
46
47
  end
47
48
 
48
49
 
50
+ class SCParamsCommand < HammerCLIForeman::SmartClassParametersBriefList
51
+
52
+ apipie_options :without => [:host_id, :hostgroup_id, :puppetclass_id, :environment_id]
53
+ option ['--id', '--name'], 'PUPPET_CLASS_ID', 'puppet class id/name',
54
+ :attribute_name => :puppetclass_id, :required => true
55
+ end
56
+
57
+
49
58
  autoload_subcommands
50
59
  end
51
60
 
52
61
  end
53
62
 
54
- HammerCLI::MainCommand.subcommand 'puppet_class', "Browse and read reports.", HammerCLIForeman::PuppetClass
63
+ HammerCLI::MainCommand.subcommand 'puppet_class', "Search Foreman's puppet modules.", HammerCLIForeman::PuppetClass
55
64
 
@@ -1,14 +1,14 @@
1
1
  module HammerCLIForeman
2
2
 
3
+ class OperationNotSupportedError < StandardError; end
4
+
3
5
  module ResourceSupportedTest
4
6
 
5
7
  def execute
6
8
  if resource_supported?
7
9
  super
8
10
  else
9
- error = "The server does not support such operation."
10
- HammerCLI::Output::Output.print_error error, nil, context, :adapter => adapter
11
- 1
11
+ raise OperationNotSupportedError, "The server does not support such operation."
12
12
  end
13
13
  end
14
14
 
@@ -0,0 +1,110 @@
1
+ require 'hammer_cli'
2
+ require 'foreman_api'
3
+ require 'hammer_cli_foreman/commands'
4
+
5
+ module HammerCLIForeman
6
+
7
+ class SmartClassParametersBriefList < HammerCLIForeman::ListCommand
8
+ resource ForemanApi::Resources::SmartClassParameter, "index"
9
+ command_name 'sc_params'
10
+
11
+ output do
12
+ field :id, "Id"
13
+
14
+ field :parameter, "Parameter"
15
+ field :default_value, "Default Value"
16
+ field :override, "Override"
17
+ end
18
+
19
+ def retrieve_data
20
+ res = super
21
+ # FIXME: API returns doubled records, probably just if filtered by puppetclasses
22
+ # it seems group by environment is missing
23
+ # having the uniq to fix that
24
+ res["smart_class_parameters"].uniq
25
+ end
26
+ end
27
+
28
+ class SmartClassParametersList < SmartClassParametersBriefList
29
+
30
+ output do
31
+ from :puppetclass do
32
+ field :name, "Puppet class"
33
+ field :id, "Class Id", Fields::Id
34
+ end
35
+ end
36
+ end
37
+
38
+ class SmartClassParameter < HammerCLI::Apipie::Command
39
+
40
+ resource ForemanApi::Resources::SmartClassParameter
41
+
42
+ class ListCommand < HammerCLIForeman::SmartClassParametersList
43
+ command_name 'list'
44
+ apipie_options
45
+ end
46
+
47
+ class InfoCommand < HammerCLIForeman::InfoCommand
48
+
49
+ output ListCommand.output_definition do
50
+ field :description, "Description"
51
+ field :parameter_type, "Type"
52
+ field :required, "Required"
53
+ field :_environments, "Environments", Fields::List
54
+ field :_environment_ids, "Environment Ids", Fields::List
55
+ label "Validator" do
56
+ field :validator_type, "Type"
57
+ field :validator_rule, "Rule"
58
+ end
59
+ label "Override values" do
60
+ field :override_value_order, "Order", Fields::List
61
+ field :override_values_count, "Count"
62
+ collection :override_values, "Values" do
63
+ label "Value" do
64
+ field :id, 'Id'
65
+ field :match, 'Match'
66
+ field :value, 'Value'
67
+ end
68
+ end
69
+ end
70
+ field :created_at, "Created at", Fields::Date
71
+ field :updated_at, "Updated at", Fields::Date
72
+ end
73
+
74
+ def retrieve_data
75
+ res = super['smart_class_parameter']
76
+ res['override_value_order'] = res['override_value_order'].split("\n")
77
+ res['_environments'] = res['environments'].map { |e| e['environment']['name']}
78
+ res['_environment_ids'] = res['environments'].map { |e| e['environment']['id']}
79
+ res
80
+ end
81
+
82
+ end
83
+
84
+ class UpdateCommand < HammerCLIForeman::UpdateCommand
85
+ # identifiers :id, :name
86
+
87
+ success_message "Parameter updated"
88
+ failure_message "Could not update the parameter"
89
+
90
+ apipie_options :without => [:parameter_type, :validator_type, :id, :override, :required]
91
+
92
+ option "--override", "OVERRIDE", "Override this parameter.",
93
+ :format => HammerCLI::Options::Normalizers::Bool.new
94
+ option "--required", "REQUIRED", "This parameter is required.",
95
+ :format => HammerCLI::Options::Normalizers::Bool.new
96
+ option "--parameter-type", "PARAMETER_TYPE", "Type of the parameter.",
97
+ :format => HammerCLI::Options::Normalizers::Enum.new(
98
+ ['string', 'boolean', 'integer', 'real', 'array', 'hash', 'yaml', 'json'])
99
+ option "--validator-type", "VALIDATOR_TYPE", "Type of the validator.",
100
+ :format => HammerCLI::Options::Normalizers::Enum.new(['regexp', 'list', ''])
101
+ end
102
+
103
+
104
+ autoload_subcommands
105
+
106
+ end
107
+
108
+ HammerCLI::MainCommand.subcommand 'sc_param', "Manipulate Foreman's smart class parameters.", HammerCLIForeman::SmartClassParameter
109
+
110
+ end
@@ -1,5 +1,5 @@
1
1
  module HammerCLIForeman
2
2
  def self.version
3
- @version ||= Gem::Version.new '0.0.10'
3
+ @version ||= Gem::Version.new '0.0.11'
4
4
  end
5
5
  end
@@ -0,0 +1,88 @@
1
+ class ApipieResourceMock
2
+
3
+ def initialize(resource)
4
+ @return_values = {}
5
+ @resource = resource
6
+ @resource.doc["methods"].each do |method|
7
+ stub_method(method["name"], get_return_value(method))
8
+ end
9
+ end
10
+
11
+ def doc
12
+ @resource.doc
13
+ end
14
+
15
+ def new(attrs)
16
+ return self
17
+ end
18
+
19
+ def expects_with(method_name, params, return_value=nil)
20
+ return_value ||= return_value_for(method_name)
21
+ self.expects(method_name).with(params).returns(return_value)
22
+ end
23
+
24
+ def stub_method(method_name, return_value)
25
+ self.stubs(method_name.to_s).returns([return_value, return_value.to_s])
26
+ @return_values[method_name.to_s] = return_value
27
+ end
28
+
29
+ private
30
+
31
+ def return_value_for(method_name)
32
+ @return_values[method_name.to_s]
33
+ end
34
+
35
+ def get_return_value(method)
36
+ return nil if method["examples"].empty?
37
+
38
+ #parse actual json from the example string
39
+ #examples are in format:
40
+ # METHOD /api/some/route
41
+ # <input params in json, multiline>
42
+ # RETURN_CODE
43
+ # <output in json, multiline>
44
+ parse_re = /.*(\n\d+\n)(.*)/m
45
+ json_string = method["examples"][0][parse_re, 2]
46
+ response = JSON.parse(json_string) rescue json_string
47
+
48
+ response
49
+ end
50
+
51
+ end
52
+
53
+
54
+ class ApipieDisabledResourceMock
55
+
56
+ def initialize(resource)
57
+ @resource = resource
58
+ @resource.doc["methods"].each do |method|
59
+ self.stubs(method["name"]).raises(RestClient::ResourceNotFound)
60
+ end
61
+ end
62
+
63
+ def doc
64
+ @resource.doc
65
+ end
66
+
67
+ def new(attrs)
68
+ return self
69
+ end
70
+
71
+ end
72
+
73
+ def mock_resource_method(method, response)
74
+ resource_mock = ApipieResourceMock.new(cmd.class.resource.resource_class)
75
+ resource_mock.stubs(method).returns(response)
76
+ cmd.class.resource resource_mock
77
+ end
78
+
79
+ module ResourceMocks
80
+
81
+ def self.smart_class_parameter
82
+ sc_params = ApipieResourceMock.new(ForemanApi::Resources::SmartClassParameter)
83
+ sc_params.stubs(:index).returns([{ 'smart_class_parameters' => [ {} ] }, ""])
84
+ sc_params.stubs(:show).returns([{ 'smart_class_parameter' => { 'override_value_order' => '', 'environments' => [] }}, ""])
85
+ sc_params
86
+ end
87
+
88
+ end