hammer_cli_foreman 0.17.0 → 0.17.1

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.
Files changed (29) hide show
  1. checksums.yaml +4 -4
  2. data/doc/host_create.md +2 -2
  3. data/doc/release_notes.md +5 -0
  4. data/lib/hammer_cli_foreman.rb +7 -1
  5. data/lib/hammer_cli_foreman/associating_commands.rb +9 -7
  6. data/lib/hammer_cli_foreman/combination.rb +4 -0
  7. data/lib/hammer_cli_foreman/command_extensions.rb +3 -0
  8. data/lib/hammer_cli_foreman/command_extensions/option_sources.rb +16 -0
  9. data/lib/hammer_cli_foreman/command_extensions/puppet_environment.rb +23 -0
  10. data/lib/hammer_cli_foreman/command_extensions/puppet_environments.rb +23 -0
  11. data/lib/hammer_cli_foreman/commands.rb +27 -16
  12. data/lib/hammer_cli_foreman/host.rb +3 -0
  13. data/lib/hammer_cli_foreman/hostgroup.rb +4 -0
  14. data/lib/hammer_cli_foreman/id_resolver.rb +52 -3
  15. data/lib/hammer_cli_foreman/location.rb +1 -1
  16. data/lib/hammer_cli_foreman/option_sources.rb +1 -0
  17. data/lib/hammer_cli_foreman/option_sources/puppet_environment_params.rb +59 -0
  18. data/lib/hammer_cli_foreman/organization.rb +5 -1
  19. data/lib/hammer_cli_foreman/{environment.rb → puppet_environment.rb} +3 -2
  20. data/lib/hammer_cli_foreman/smart_class_parameter.rb +2 -0
  21. data/lib/hammer_cli_foreman/smart_proxy.rb +2 -0
  22. data/lib/hammer_cli_foreman/testing/api_expectations.rb +5 -1
  23. data/lib/hammer_cli_foreman/version.rb +1 -1
  24. data/test/functional/hostgroup/create_test.rb +34 -0
  25. data/test/functional/template_test.rb +42 -10
  26. data/test/unit/helpers/command.rb +8 -0
  27. data/test/unit/id_resolver_test.rb +22 -18
  28. data/test/unit/{environment_test.rb → puppet_environment_test.rb} +10 -8
  29. metadata +10 -5
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 879dc32762958668d6e95ffb5ff7ba04222b42be763aad77bd00045763074d45
4
- data.tar.gz: 75334fc224d49d58d2b0f3ff9b420db02fa25d84df2f8595736e4fe92872fdcf
3
+ metadata.gz: bf971111e45df5faaf2deeb304771676bcfa6e8de748e84f094725d309dc2e5b
4
+ data.tar.gz: 7de28c7b33572b008e5e40eeb417b95f446015a3052c46e888837ae20d5c2af4
5
5
  SHA512:
6
- metadata.gz: 6e0f879c792cadf51b3a53fcac50297f5620a203b1a7e2d0c85df655926f29da00f5452734a79e79f000fb3ab030ff3058cf569e3339c5991d942a2fb2145d20
7
- data.tar.gz: 46800432d77b3cea0c324995b5cd4ba3a49d69bde6f6d4ba2fc6bd0e1b7c2a69f7ed6148f3489ba3a7912f2742396844a31666a29ea71076598f956349ccafa6
6
+ metadata.gz: 5f3df104c1d2555838f0012819795156bbb9a06de830bc18e5ec85537756f9f0520ac3475acc852a1bfcda7846986327280b2f1355f83853bc79f9cf1d58f446
7
+ data.tar.gz: c273ec8d72e082eba1fd6b81c0312cdc7eb3f0ea0412db12d035b1ede224448ed65d290f6f666f5cc06fe7565dfa2f8724e959965afa526d70098845fd1f2a09
data/doc/host_create.md CHANGED
@@ -39,8 +39,6 @@ Options:
39
39
  --domain-id DOMAIN_ID Numerical ID or domain name
40
40
  --enabled ENABLED Include this host within Foreman reporting
41
41
  One of true/false, yes/no, 1/0.
42
- --environment ENVIRONMENT_NAME Environment name
43
- --environment-id ENVIRONMENT_ID
44
42
  --hostgroup HOSTGROUP_NAME Hostgroup name
45
43
  --hostgroup-id HOSTGROUP_ID
46
44
  --hostgroup-title HOSTGROUP_TITLE Hostgroup title
@@ -96,6 +94,8 @@ Options:
96
94
  --puppet-ca-proxy-id PUPPET_CA_PROXY_ID Puppet CA proxy ID
97
95
  --puppet-class-ids PUPPET_CLASS_IDS Comma separated list of values. Values containing comma should be quoted or escaped with backslash
98
96
  --puppet-classes PUPPET_CLASS_NAMES Comma separated list of values. Values containing comma should be quoted or escaped with backslash
97
+ --puppet-environment PUPPET_ENVIRONMENT_NAME Puppet Environment name
98
+ --puppet-environment-id PUPPET_ENVIRONMENT_ID
99
99
  --puppet-proxy PUPPET_PROXY_NAME
100
100
  --puppet-proxy-id PUPPET_PROXY_ID Puppet proxy ID
101
101
  --pxe-loader PXE_LOADER DHCP filename option (Grub2/PXELinux by default)
data/doc/release_notes.md CHANGED
@@ -1,5 +1,10 @@
1
1
  Release notes
2
2
  =============
3
+ ### 0.17.1 (2019-07-10)
4
+ * Consistent puppet environment naming in hammer ([#23204](http://projects.theforeman.org/issues/23204))
5
+ * Create hostgroup with puppet classes ([#24717](http://projects.theforeman.org/issues/24717))
6
+ * Fix help on commands with no resource ([#26865](http://projects.theforeman.org/issues/26865))
7
+
3
8
  ### 0.17.0 (2019-04-24)
4
9
  * Add public key option in create compute resource CLI ([#25491](http://projects.theforeman.org/issues/25491))
5
10
  * Background template rendering ([PR #414](https://github.com/theforeman/hammer-cli-foreman/pull/414)) ([#26355](http://projects.theforeman.org/issues/26355))
@@ -22,6 +22,7 @@ module HammerCLIForeman
22
22
  require 'hammer_cli_foreman/logger'
23
23
 
24
24
  begin
25
+ require 'hammer_cli_foreman/command_extensions'
25
26
  require 'hammer_cli_foreman/commands'
26
27
  require 'hammer_cli_foreman/associating_commands'
27
28
  require 'hammer_cli_foreman/references'
@@ -58,7 +59,12 @@ module HammerCLIForeman
58
59
  )
59
60
 
60
61
  HammerCLI::MainCommand.lazy_subcommand('environment', _("Manipulate environments"),
61
- 'HammerCLIForeman::Environment', 'hammer_cli_foreman/environment'
62
+ 'HammerCLIForeman::PuppetEnvironment', 'hammer_cli_foreman/puppet_environment',
63
+ :warning => _('%{env} command is deprecated and will be removed in one of the future versions. Please use %{puppet_env} command instead.') % {:env => 'environment', :puppet_env => 'puppet-environment'}
64
+ )
65
+
66
+ HammerCLI::MainCommand.lazy_subcommand('puppet-environment', _("Manipulate Puppet environments"),
67
+ 'HammerCLIForeman::PuppetEnvironment', 'hammer_cli_foreman/puppet_environment'
62
68
  )
63
69
 
64
70
  HammerCLI::MainCommand.lazy_subcommand('fact', _("Search facts"),
@@ -53,23 +53,27 @@ module HammerCLIForeman
53
53
  end
54
54
  end
55
55
 
56
- module Environment
56
+ module PuppetEnvironment
57
57
  extend CommandExtension
58
58
 
59
- class AddEnvironmentCommand < HammerCLIForeman::AddAssociatedCommand
59
+ class AddPuppetEnvironmentCommand < HammerCLIForeman::AddAssociatedCommand
60
60
  associated_resource :environments
61
- desc _("Associate an environment")
61
+ desc _('Associate a Puppet environment')
62
62
 
63
63
  success_message _("The environment has been associated.")
64
64
  failure_message _("Could not associate the environment")
65
+
66
+ extend_with(HammerCLIForeman::CommandExtensions::PuppetEnvironment.new)
65
67
  end
66
68
 
67
- class RemoveEnvironmentCommand < HammerCLIForeman::RemoveAssociatedCommand
69
+ class RemovePuppetEnvironmentCommand < HammerCLIForeman::RemoveAssociatedCommand
68
70
  associated_resource :environments
69
- desc _("Disassociate an environment")
71
+ desc _('Disassociate a Puppet environment')
70
72
 
71
73
  success_message _("The environment has been disassociated.")
72
74
  failure_message _("Could not disassociate the environment")
75
+
76
+ extend_with(HammerCLIForeman::CommandExtensions::PuppetEnvironment.new)
73
77
  end
74
78
  end
75
79
 
@@ -372,5 +376,3 @@ module HammerCLIForeman
372
376
 
373
377
  end
374
378
  end
375
-
376
-
@@ -58,6 +58,8 @@ module HammerCLIForeman
58
58
  o.expand(:all).except(:config_templates,)
59
59
  o.without(:config_template_id)
60
60
  end
61
+
62
+ extend_with(HammerCLIForeman::CommandExtensions::PuppetEnvironment.new)
61
63
  end
62
64
 
63
65
  class CreateCombination < HammerCLIForeman::CreateCommand
@@ -70,6 +72,8 @@ module HammerCLIForeman
70
72
  o.expand(:all).except(:config_templates,)
71
73
  o.without(:config_template_id)
72
74
  end
75
+
76
+ extend_with(HammerCLIForeman::CommandExtensions::PuppetEnvironment.new)
73
77
  end
74
78
 
75
79
  class DeleteCombination < HammerCLIForeman::DeleteCommand
@@ -0,0 +1,3 @@
1
+ require 'hammer_cli_foreman/command_extensions/puppet_environment'
2
+ require 'hammer_cli_foreman/command_extensions/puppet_environments'
3
+ require 'hammer_cli_foreman/command_extensions/option_sources'
@@ -0,0 +1,16 @@
1
+ module HammerCLIForeman
2
+ module CommandExtensions
3
+ class OptionSources < HammerCLI::CommandExtensions
4
+ inheritable true
5
+
6
+ option_sources do |sources, command|
7
+ id_resolution = HammerCLI::Options::ProcessorList.new(name: 'IdResolution')
8
+ id_resolution << HammerCLIForeman::OptionSources::IdParams.new(command)
9
+ id_resolution << HammerCLIForeman::OptionSources::IdsParams.new(command)
10
+ id_resolution << HammerCLIForeman::OptionSources::SelfParam.new(command)
11
+
12
+ sources << id_resolution
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,23 @@
1
+ module HammerCLIForeman
2
+ module CommandExtensions
3
+ class PuppetEnvironment < HammerCLI::CommandExtensions
4
+ # Remove when support of --environment options is ended.
5
+ option '--environment', 'ENVIRONMENT_NAME', _('Environment name'),
6
+ attribute_name: :option_environment_name,
7
+ deprecated: { '--environment' => _('Use --puppet-environment instead') }
8
+ option '--environment-id', 'ENVIRONMENT_ID', _(''),
9
+ format: HammerCLI::Options::Normalizers::Number.new,
10
+ attribute_name: :option_environment_id,
11
+ deprecated: { '--environment-id' => _('Use --puppet-environment-id instead') }
12
+
13
+ option_sources do |sources, command|
14
+ sources.find_by_name('IdResolution').insert_relative(
15
+ :after,
16
+ 'IdParams',
17
+ HammerCLIForeman::OptionSources::PuppetEnvironmentParams.new(command)
18
+ )
19
+ sources
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,23 @@
1
+ module HammerCLIForeman
2
+ module CommandExtensions
3
+ class PuppetEnvironments < HammerCLI::CommandExtensions
4
+ # Remove when support of --environments options is ended.
5
+ option '--environments', 'ENVIRONMENT_NAMES', _(''),
6
+ attribute_name: :option_environment_names,
7
+ deprecated: { '--environments' => _('Use --puppet-environments instead') }
8
+ option '--environment-ids', 'ENVIRONMENT_IDS', _('Environment IDs'),
9
+ format: HammerCLI::Options::Normalizers::List.new,
10
+ attribute_name: :option_environment_ids,
11
+ deprecated: { '--environment-ids' => _('Use --puppet-environment-ids instead') }
12
+
13
+ option_sources do |sources, command|
14
+ sources.find_by_name('IdResolution').insert_relative(
15
+ :after,
16
+ 'IdsParams',
17
+ HammerCLIForeman::OptionSources::PuppetEnvironmentParams.new(command)
18
+ )
19
+ sources
20
+ end
21
+ end
22
+ end
23
+ end
@@ -8,7 +8,14 @@ module HammerCLIForeman
8
8
  :ptable => :partition_table,
9
9
  :ptables => :partition_tables,
10
10
  :puppetclass => :puppet_class,
11
- :puppetclasses => :puppet_classes
11
+ :puppetclasses => :puppet_classes,
12
+ :environment => :puppet_environment,
13
+ :environments => :puppet_environments
14
+ }
15
+
16
+ RESOURCE_ALIAS_NAME_MAPPING = {
17
+ environment: :puppet_environment,
18
+ environments: :puppet_environments
12
19
  }
13
20
 
14
21
  def self.foreman_api
@@ -107,6 +114,17 @@ module HammerCLIForeman
107
114
  HammerCLIForeman::RESOURCE_NAME_MAPPING
108
115
  end
109
116
 
117
+ def self.resource_alias_name_mapping
118
+ HammerCLIForeman::RESOURCE_ALIAS_NAME_MAPPING
119
+ end
120
+
121
+ def self.alias_name_for_resource(resource, singular: true)
122
+ return nil if resource.nil?
123
+ return resource_alias_name_mapping[resource.name.to_sym] unless singular
124
+
125
+ resource_alias_name_mapping[resource.singular_name.to_sym]
126
+ end
127
+
110
128
  def self.build_options(builder_params={})
111
129
  builder_params[:resource_mapping] ||= resource_name_mapping
112
130
  builder_params = HammerCLIForeman::BuildParams.new(builder_params)
@@ -121,6 +139,7 @@ module HammerCLIForeman
121
139
  end
122
140
 
123
141
  def get_resource_id(resource, options={})
142
+ resource_alias = self.class.alias_name_for_resource(resource)
124
143
  all_opts = options[:all_options] || all_options
125
144
  if options[:scoped]
126
145
  opts = resolver.scoped_options(resource.singular_name, all_opts, :single)
@@ -128,7 +147,7 @@ module HammerCLIForeman
128
147
  opts = all_opts
129
148
  end
130
149
  begin
131
- resolver.send("#{resource.singular_name}_id", opts)
150
+ resolver.send("#{resource_alias || resource.singular_name}_id", opts)
132
151
  rescue HammerCLIForeman::MissingSearchOptions => e
133
152
  if (options[:required] == true || resource_search_requested(resource, opts))
134
153
  logger.info "Error occured while searching for #{resource.singular_name}"
@@ -138,10 +157,11 @@ module HammerCLIForeman
138
157
  end
139
158
 
140
159
  def get_resource_ids(resource, options={})
160
+ resource_alias = self.class.alias_name_for_resource(resource)
141
161
  all_opts = options[:all_options] || all_options
142
162
  opts = resolver.scoped_options(resource.singular_name, all_opts, :multi)
143
163
  begin
144
- resolver.send("#{resource.singular_name}_ids", opts)
164
+ resolver.send("#{resource_alias || resource.singular_name}_ids", opts)
145
165
  rescue HammerCLIForeman::MissingSearchOptions => e
146
166
  if (options[:required] == true || resource_search_requested(resource, opts, true))
147
167
  logger.info "Error occured while searching for #{resource.name}"
@@ -188,16 +208,7 @@ module HammerCLIForeman
188
208
  params_pruned
189
209
  end
190
210
 
191
- def option_sources
192
- sources = super
193
-
194
- id_resolution = HammerCLI::Options::ProcessorList.new(name: 'IdResolution')
195
- id_resolution << HammerCLIForeman::OptionSources::IdParams.new(self)
196
- id_resolution << HammerCLIForeman::OptionSources::IdsParams.new(self)
197
- id_resolution << HammerCLIForeman::OptionSources::SelfParam.new(self)
198
-
199
- sources << id_resolution
200
- end
211
+ extend_with(HammerCLIForeman::CommandExtensions::OptionSources.new)
201
212
 
202
213
  private
203
214
 
@@ -254,9 +265,9 @@ module HammerCLIForeman
254
265
  end
255
266
 
256
267
  def help
257
- meta = resource.action(action).apidoc[:metadata]
258
- return super if meta.nil?
268
+ return super unless resource
259
269
 
270
+ meta = resource.action(action).apidoc[:metadata]
260
271
  if meta && meta[:search] && respond_to?(:option_search)
261
272
  self.class.extend_help do |h|
262
273
  h.section(_('Search fields'), id: :search_fields_section) do |h|
@@ -303,7 +314,7 @@ module HammerCLIForeman
303
314
  ]
304
315
  end
305
316
  end
306
-
317
+
307
318
  def search_field_help_value(field)
308
319
  if field[:values] && field[:values].is_a?(Array)
309
320
  _('Values') + ': ' + field[:values].join(', ')
@@ -300,6 +300,7 @@ module HammerCLIForeman
300
300
  end
301
301
  end
302
302
 
303
+ extend_with(HammerCLIForeman::CommandExtensions::PuppetEnvironment.new)
303
304
  end
304
305
 
305
306
  class UpdateCommand < HammerCLIForeman::UpdateCommand
@@ -308,6 +309,8 @@ module HammerCLIForeman
308
309
 
309
310
  include HammerCLIForeman::Hosts::CommonUpdateOptions
310
311
  include HammerCLIForeman::Hosts::CommonUpdateHelp
312
+
313
+ extend_with(HammerCLIForeman::CommandExtensions::PuppetEnvironment.new)
311
314
  end
312
315
 
313
316
 
@@ -115,6 +115,8 @@ module HammerCLIForeman
115
115
  failure_message _("Could not create the hostgroup")
116
116
 
117
117
  build_options
118
+
119
+ extend_with(HammerCLIForeman::CommandExtensions::PuppetEnvironment.new)
118
120
  end
119
121
 
120
122
 
@@ -125,6 +127,8 @@ module HammerCLIForeman
125
127
  failure_message _("Could not update the hostgroup")
126
128
 
127
129
  build_options
130
+
131
+ extend_with(HammerCLIForeman::CommandExtensions::PuppetEnvironment.new)
128
132
  end
129
133
 
130
134
 
@@ -38,7 +38,8 @@ module HammerCLIForeman
38
38
  :compute_resource => [ s_name(_("Compute resource name")) ],
39
39
  :compute_profile => [ s_name(_("Compute profile name")) ],
40
40
  :domain => [ s_name(_("Domain name")) ],
41
- :environment => [ s_name(_("Environment name")) ],
41
+ :environment => [s_name(_('Puppet environment name'))],
42
+ :puppet_environment => [s_name(_('Puppet environment name'))],
42
43
  :fact_value => [],
43
44
  :filter => [],
44
45
  :host => [ s_name(_("Host name")) ],
@@ -77,6 +78,7 @@ module HammerCLIForeman
77
78
  end
78
79
 
79
80
  class IdResolver
81
+ ALL_PER_PAGE = 1000
80
82
 
81
83
  attr_reader :api
82
84
 
@@ -121,6 +123,22 @@ module HammerCLIForeman
121
123
  options[HammerCLI.option_accessor_name("ids")] || find_puppetclasses(options).collect { |c| c['id'] }
122
124
  end
123
125
 
126
+ def environment_id(options)
127
+ puppet_environment_id(options)
128
+ end
129
+
130
+ def puppet_environment_id(options)
131
+ get_id(:environments, options)
132
+ end
133
+
134
+ def environment_ids(options)
135
+ puppet_environment_ids(options)
136
+ end
137
+
138
+ def puppet_environment_ids(options)
139
+ get_ids(:environments, options)
140
+ end
141
+
124
142
  def searchables(resource)
125
143
  resource = @api.resource(resource) if resource.is_a? Symbol
126
144
  @searchables.for(resource)
@@ -218,12 +236,28 @@ module HammerCLIForeman
218
236
  search_options[param.name] ||= send(param.name, scoped_options(*scoped_options_params))
219
237
  end
220
238
  search_options = route_options(options, action).merge(search_options)
221
-
222
- results = resource.call(action_name, search_options)
239
+ expected_count = expected_record_count(options, resource, :multi)
240
+ results = retrieve_all(resource, action_name, search_options, expected_count)
223
241
  results = HammerCLIForeman.collection_to_common_format(results)
224
242
  results
225
243
  end
226
244
 
245
+ def retrieve_all(resource, action, search_options, expected_count = 1)
246
+ return resource.call(action, search_options) unless action == :index
247
+
248
+ search_options[:per_page] = ALL_PER_PAGE
249
+ search_options[:page] = 1
250
+ data = resource.call(action, search_options)
251
+ last_page = ([expected_count, 1].max.to_f / ALL_PER_PAGE).ceil
252
+ all = data
253
+ while search_options[:page] != last_page
254
+ search_options[:page] += 1
255
+ data = resource.call(action, search_options)
256
+ all = deep_merge(all, data)
257
+ end
258
+ all
259
+ end
260
+
227
261
  def route_options(options, action)
228
262
  return {} if action.routes.any? { |r| r.params_in_path.empty? }
229
263
 
@@ -343,6 +377,21 @@ module HammerCLIForeman
343
377
  {}
344
378
  end
345
379
 
380
+ private
381
+
382
+ def deep_merge(dest, src)
383
+ dest = dest.clone
384
+ dest.merge!(src) do |_key, old_val, new_val|
385
+ if old_val.is_a?(Hash) && new_val.is_a?(Hash)
386
+ deep_merge(old_val, new_val)
387
+ elsif old_val.is_a?(Array) && new_val.is_a?(Array)
388
+ old_val += new_val
389
+ else
390
+ new_val
391
+ end
392
+ end
393
+ end
394
+
346
395
  end
347
396
 
348
397
  end
@@ -106,7 +106,7 @@ module HammerCLIForeman
106
106
  end
107
107
 
108
108
  HammerCLIForeman::AssociatingCommands::Hostgroup.extend_command(self)
109
- HammerCLIForeman::AssociatingCommands::Environment.extend_command(self)
109
+ HammerCLIForeman::AssociatingCommands::PuppetEnvironment.extend_command(self)
110
110
  HammerCLIForeman::AssociatingCommands::Domain.extend_command(self)
111
111
  HammerCLIForeman::AssociatingCommands::Medium.extend_command(self)
112
112
  HammerCLIForeman::AssociatingCommands::Subnet.extend_command(self)
@@ -2,3 +2,4 @@ require 'hammer_cli_foreman/option_sources/id_params'
2
2
  require 'hammer_cli_foreman/option_sources/ids_params'
3
3
  require 'hammer_cli_foreman/option_sources/self_param'
4
4
  require 'hammer_cli_foreman/option_sources/user_params'
5
+ require 'hammer_cli_foreman/option_sources/puppet_environment_params'
@@ -0,0 +1,59 @@
1
+ module HammerCLIForeman
2
+ module OptionSources
3
+ class PuppetEnvironmentParams < HammerCLI::Options::Sources::Base
4
+ def initialize(command)
5
+ @command = command
6
+ end
7
+
8
+ def get_options(_defined_options, result)
9
+ if result['option_environment_id'].nil? &&
10
+ (@command.respond_to?(:option_environment_id) ||
11
+ @command.respond_to?(:option_environment_name))
12
+ put_puppet_environment_id(result)
13
+ end
14
+ if result['option_environment_ids'].nil? &&
15
+ (@command.respond_to?(:option_environment_ids) ||
16
+ @command.respond_to?(:option_environment_names))
17
+ put_puppet_environment_ids(result)
18
+ end
19
+ result
20
+ end
21
+
22
+ private
23
+
24
+ def put_puppet_environment_id(result)
25
+ if result['option_environment_name'].nil?
26
+ if @command.option_environment_id
27
+ result['option_environment_id'] = @command.option_environment_id
28
+ elsif @command.option_environment_name
29
+ result['option_environment_name'] = @command.option_environment_name
30
+ result['option_environment_id'] = @command.resolver.puppet_environment_id(
31
+ @command.resolver.scoped_options('environment', result, :single)
32
+ )
33
+ end
34
+ else
35
+ result['option_environment_id'] = @command.resolver.puppet_environment_id(
36
+ @command.resolver.scoped_options('environment', result, :single)
37
+ )
38
+ end
39
+ end
40
+
41
+ def put_puppet_environment_ids(result)
42
+ if result['option_environment_names'].nil?
43
+ if @command.option_environment_ids
44
+ result['option_environment_ids'] = @command.option_environment_ids
45
+ elsif @command.option_environment_names
46
+ result['option_environment_names'] = @command.option_environment_names
47
+ result['option_environment_ids'] = @command.resolver.puppet_environment_ids(
48
+ @command.resolver.scoped_options('environment', result, :multi)
49
+ )
50
+ end
51
+ else
52
+ result['option_environment_ids'] = @command.resolver.puppet_environment_ids(
53
+ @command.resolver.scoped_options('environment', result, :multi)
54
+ )
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end
@@ -57,6 +57,8 @@ module HammerCLIForeman
57
57
  failure_message _("Could not create the organization")
58
58
 
59
59
  build_options
60
+
61
+ extend_with(HammerCLIForeman::CommandExtensions::PuppetEnvironments.new)
60
62
  end
61
63
 
62
64
 
@@ -69,6 +71,8 @@ module HammerCLIForeman
69
71
  failure_message _("Could not update the organization")
70
72
 
71
73
  build_options
74
+
75
+ extend_with(HammerCLIForeman::CommandExtensions::PuppetEnvironments.new)
72
76
  end
73
77
 
74
78
 
@@ -108,7 +112,7 @@ module HammerCLIForeman
108
112
 
109
113
 
110
114
  HammerCLIForeman::AssociatingCommands::Hostgroup.extend_command(self)
111
- HammerCLIForeman::AssociatingCommands::Environment.extend_command(self)
115
+ HammerCLIForeman::AssociatingCommands::PuppetEnvironment.extend_command(self)
112
116
  HammerCLIForeman::AssociatingCommands::Domain.extend_command(self)
113
117
  HammerCLIForeman::AssociatingCommands::Medium.extend_command(self)
114
118
  HammerCLIForeman::AssociatingCommands::Subnet.extend_command(self)
@@ -1,10 +1,9 @@
1
1
  require 'hammer_cli'
2
2
  require 'hammer_cli_foreman/smart_class_parameter'
3
3
 
4
-
5
4
  module HammerCLIForeman
6
5
 
7
- class Environment < HammerCLIForeman::Command
6
+ class PuppetEnvironment < HammerCLIForeman::Command
8
7
 
9
8
  resource :environments
10
9
 
@@ -55,6 +54,8 @@ module HammerCLIForeman
55
54
 
56
55
  class SCParamsCommand < HammerCLIForeman::SmartClassParametersList
57
56
  build_options_for :environments
57
+
58
+ extend_with(HammerCLIForeman::CommandExtensions::PuppetEnvironment.new)
58
59
  end
59
60
 
60
61
 
@@ -51,6 +51,8 @@ module HammerCLIForeman
51
51
  end
52
52
 
53
53
  build_options
54
+
55
+ extend_with(HammerCLIForeman::CommandExtensions::PuppetEnvironment.new)
54
56
  end
55
57
 
56
58
  class InfoCommand < HammerCLIForeman::InfoCommand
@@ -107,6 +107,8 @@ module HammerCLIForeman
107
107
  def print_data(record)
108
108
  print_record(output_definition, record)
109
109
  end
110
+
111
+ extend_with(HammerCLIForeman::CommandExtensions::PuppetEnvironment.new)
110
112
  end
111
113
 
112
114
 
@@ -173,7 +173,11 @@ module HammerCLIForeman
173
173
  end
174
174
 
175
175
  def index_response(items, options={})
176
- cnt = items.length
176
+ cnt = if items.is_a?(Hash)
177
+ items.keys.count
178
+ else
179
+ items.length
180
+ end
177
181
  {
178
182
  "total" => options.fetch(:total, cnt),
179
183
  "subtotal" => options.fetch(:subtotal, cnt),
@@ -1,5 +1,5 @@
1
1
  module HammerCLIForeman
2
2
  def self.version
3
- @version ||= Gem::Version.new '0.17.0'
3
+ @version ||= Gem::Version.new '0.17.1'
4
4
  end
5
5
  end
@@ -246,6 +246,40 @@ module HammerCLIForeman
246
246
  run_cmd(%w(hostgroup create --name hg1 --puppet-classes pc1,pc2))
247
247
  end
248
248
 
249
+ it 'allows puppet class names that exceeds entries_per_page' do
250
+ search_objects = []
251
+ response_objects = []
252
+ ids = []
253
+ names = []
254
+ 1100.times.with_object('pc').each do |id, name|
255
+ next_name = "\"#{name}#{id}\""
256
+ search_objects << "name = #{next_name}"
257
+ response_objects << { 'id' => id, 'name' => next_name }
258
+ ids << id
259
+ names << next_name
260
+ end
261
+
262
+ api_expects(:puppetclasses, :index) do |p|
263
+ p[:search] == search_objects.join(' or ') &&
264
+ p[:page].to_i == 1 &&
265
+ p[:per_page].to_i == HammerCLIForeman::IdResolver::ALL_PER_PAGE
266
+ end.returns(
267
+ index_response('puppetclasses' => response_objects[0...1000]))
268
+
269
+ api_expects(:puppetclasses, :index) do |p|
270
+ p[:search] == search_objects.join(' or ') &&
271
+ p[:page].to_i == 2 &&
272
+ p[:per_page].to_i == HammerCLIForeman::IdResolver::ALL_PER_PAGE
273
+ end.returns(index_response('puppetclasses' => response_objects[1000...1100]))
274
+
275
+ api_expects(:hostgroups, :create) do |p|
276
+ p['hostgroup']['name'] == 'hg1' &&
277
+ p['hostgroup']['puppetclass_ids'] == ids
278
+ end
279
+
280
+ run_cmd(%w[hostgroup create --name hg1 --puppet-classes] << names.join(',').tr('"', ''))
281
+ end
282
+
249
283
  it 'allows puppet proxy id' do
250
284
  api_expects(:hostgroups, :create).with_params({
251
285
  :hostgroup => {
@@ -129,14 +129,45 @@ describe 'template' do
129
129
  @cmd = %w(template combination)
130
130
  end
131
131
 
132
- it 'should create new combination' do
132
+ it 'should create new combination with warning' do
133
133
  params = ['create','--provisioning-template-id=10', '--hostgroup-id=1', '--environment-id=1']
134
134
  expected_result = success_result("Template combination created.\n")
135
+ expected_result.expected_err = "Warning: Option --environment-id is deprecated. Use --puppet-environment-id instead\n"
136
+ api_expects(:template_combinations, :create, 'Create template combination') do |params|
137
+ params['provisioning_template_id'] == 10 &&
138
+ params['hostgroup_id'] == 1 &&
139
+ params['environment_id'] == 1 &&
140
+ params['template_combination'] == {'environment_id' => 1, 'hostgroup_id' => 1}
141
+ end
142
+
143
+ result = run_cmd(@cmd + params)
144
+ assert_cmd(expected_result, result)
145
+ end
146
+
147
+ it 'should create new combination' do
148
+ params = ['create','--provisioning-template-id=10', '--hostgroup-id=1', '--puppet-environment-id=1']
149
+ expected_result = success_result("Template combination created.\n")
135
150
  api_expects(:template_combinations, :create, 'Create template combination') do |params|
136
- params["provisioning_template_id"] == 10 &&
137
- params["hostgroup_id"] == 1 &&
138
- params["environment_id"] == 1 &&
139
- params["template_combination"] == {"environment_id" => 1, "hostgroup_id" => 1}
151
+ params['provisioning_template_id'] == 10 &&
152
+ params['hostgroup_id'] == 1 &&
153
+ params['environment_id'] == 1 &&
154
+ params['template_combination'] == {'environment_id' => 1, 'hostgroup_id' => 1}
155
+ end
156
+
157
+ result = run_cmd(@cmd + params)
158
+ assert_cmd(expected_result, result)
159
+ end
160
+
161
+ it 'should update combination with warning' do
162
+ params = ['update', '--id=3', '--provisioning-template-id=10', '--hostgroup-id=1', '--environment-id=1']
163
+ expected_result = success_result("Template combination updated.\n")
164
+ expected_result.expected_err = "Warning: Option --environment-id is deprecated. Use --puppet-environment-id instead\n"
165
+ api_expects(:template_combinations, :update, 'Update template combination') do |params|
166
+ params['id'] == '3' &&
167
+ params['provisioning_template_id'] == 10 &&
168
+ params['hostgroup_id'] == 1 &&
169
+ params['environment_id'] == 1 &&
170
+ params['template_combination'] == { 'environment_id' => 1, 'hostgroup_id' => 1 }
140
171
  end
141
172
 
142
173
  result = run_cmd(@cmd + params)
@@ -144,13 +175,14 @@ describe 'template' do
144
175
  end
145
176
 
146
177
  it 'should update combination' do
147
- params = ['update','--id=3','--provisioning-template-id=10', '--hostgroup-id=1', '--environment-id=1']
178
+ params = ['update', '--id=3', '--provisioning-template-id=10', '--hostgroup-id=1', '--puppet-environment-id=1']
148
179
  expected_result = success_result("Template combination updated.\n")
149
180
  api_expects(:template_combinations, :update, 'Update template combination') do |params|
150
- params["provisioning_template_id"] == 10 &&
151
- params["hostgroup_id"] == 1 &&
152
- params["environment_id"] == 1 &&
153
- params["template_combination"] == {"environment_id" => 1, "hostgroup_id" => 1}
181
+ params['id'] == '3' &&
182
+ params['provisioning_template_id'] == 10 &&
183
+ params['hostgroup_id'] == 1 &&
184
+ params['environment_id'] == 1 &&
185
+ params['template_combination'] == { 'environment_id' => 1, 'hostgroup_id' => 1 }
154
186
  end
155
187
 
156
188
  result = run_cmd(@cmd + params)
@@ -17,6 +17,14 @@ class IdResolverTestProxy
17
17
  @original_resolver.searchables(resource)
18
18
  end
19
19
 
20
+ def puppet_environment_id(options)
21
+ environment_id(options)
22
+ end
23
+
24
+ def puppet_environment_ids(options)
25
+ environment_ids(options)
26
+ end
27
+
20
28
  protected
21
29
 
22
30
  def define_id_finders
@@ -122,14 +122,14 @@ describe HammerCLIForeman::IdResolver do
122
122
  let(:resolver_run) { proc { resolver.user_id({"option_name" => "John Doe"}) } }
123
123
 
124
124
  it "raises exception when no resource is found" do
125
- ResourceMocks.mock_action_call(:users, :index, [])
125
+ ResourceMocks.mock_action_call(:users, :index, index_response([]))
126
126
 
127
127
  err = resolver_run.must_raise HammerCLIForeman::ResolverError
128
128
  err.message.must_equal "user not found."
129
129
  end
130
130
 
131
131
  it "raises exception when multiple resources are found" do
132
- ResourceMocks.mock_action_call(:users, :index, [john, jane])
132
+ ResourceMocks.mock_action_call(:users, :index, index_response([john, jane]))
133
133
 
134
134
  err = resolver_run.must_raise HammerCLIForeman::ResolverError
135
135
  err.message.must_equal "Found more than one user."
@@ -156,7 +156,7 @@ describe HammerCLIForeman::IdResolver do
156
156
  end
157
157
 
158
158
  it "returns id of the resource" do
159
- ResourceMocks.mock_action_call(:users, :index, [john])
159
+ ResourceMocks.mock_action_call(:users, :index, index_response([john]))
160
160
 
161
161
  resolver_run.call.must_equal john_id
162
162
  end
@@ -167,16 +167,16 @@ describe HammerCLIForeman::IdResolver do
167
167
  let(:resolver_run) { proc { resolver.post_id({"option_name" => "Post 11", "option_user_name" => "John Doe"}) } }
168
168
 
169
169
  it "raises exception when no resource is found" do
170
- ResourceMocks.mock_action_call(:posts, :index, [])
171
- ResourceMocks.mock_action_call(:users, :index, [])
170
+ ResourceMocks.mock_action_call(:posts, :index, index_response([]))
171
+ ResourceMocks.mock_action_call(:users, :index, index_response([]))
172
172
 
173
173
  err = resolver_run.must_raise HammerCLIForeman::ResolverError
174
174
  err.message.must_equal "user not found."
175
175
  end
176
176
 
177
177
  it "raises exception when multiple resources are found" do
178
- ResourceMocks.mock_action_call(:posts, :index, [])
179
- ResourceMocks.mock_action_call(:users, :index, [john, jane])
178
+ ResourceMocks.mock_action_call(:posts, :index, index_response([]))
179
+ ResourceMocks.mock_action_call(:users, :index, index_response([john, jane]))
180
180
 
181
181
  err = resolver_run.must_raise HammerCLIForeman::ResolverError
182
182
  err.message.must_equal "Found more than one user."
@@ -199,12 +199,16 @@ describe HammerCLIForeman::IdResolver do
199
199
  end
200
200
 
201
201
  it "returns id of the resource" do
202
- ResourceMocks.mock_action_call(:posts, :index, [
203
- {"id" => 11, "name" => "Post 11"}
204
- ])
205
- ResourceMocks.mock_action_call(:users, :index, [
206
- {"id" => 22, "name" => "User 22"}
207
- ])
202
+ ResourceMocks.mock_action_call(:posts, :index, index_response(
203
+ [
204
+ {"id" => 11, "name" => "Post 11"}
205
+ ]
206
+ ))
207
+ ResourceMocks.mock_action_call(:users, :index, index_response(
208
+ [
209
+ {"id" => 22, "name" => "User 22"}
210
+ ]
211
+ ))
208
212
  resolver_run.call.must_equal 11
209
213
  end
210
214
  end
@@ -248,13 +252,13 @@ describe HammerCLIForeman::IdResolver do
248
252
  ( resource == :users &&
249
253
  action == :index &&
250
254
  params[:search] == "name = \"John Doe\" or name = \"Jane Doe\"")
251
- end.returns([john, jane])
255
+ end.returns(index_response([john, jane]))
252
256
 
253
257
  assert_equal [john_id, jane_id], resolver.user_ids({"option_names" => ["John Doe", "Jane Doe"]})
254
258
  end
255
259
 
256
260
  it "raises exception when wrong number of resources is found even with conflicting options" do
257
- ResourceMocks.mock_action_call(:users, :index, [john])
261
+ ResourceMocks.mock_action_call(:users, :index, index_response([john]))
258
262
 
259
263
  assert_raises HammerCLIForeman::ResolverError do
260
264
  resolver.user_ids({"option_names" => ["John Doe", "Jane Doe"], "option_name" => "group1"})
@@ -262,7 +266,7 @@ describe HammerCLIForeman::IdResolver do
262
266
  end
263
267
 
264
268
  it "raises exception when wrong number of resources is found" do
265
- ResourceMocks.mock_action_call(:users, :index, [john])
269
+ ResourceMocks.mock_action_call(:users, :index, index_response([john]))
266
270
 
267
271
  assert_raises HammerCLIForeman::ResolverError do
268
272
  resolver.user_ids({"option_names" => ["John Doe", "Jane Doe"]})
@@ -270,7 +274,7 @@ describe HammerCLIForeman::IdResolver do
270
274
  end
271
275
 
272
276
  it "raises exception when no search options were set" do
273
- ResourceMocks.mock_action_call(:users, :index, [john])
277
+ ResourceMocks.mock_action_call(:users, :index, index_response([john]))
274
278
 
275
279
  assert_raises HammerCLIForeman::MissingSearchOptions do
276
280
  resolver.user_ids({})
@@ -278,7 +282,7 @@ describe HammerCLIForeman::IdResolver do
278
282
  end
279
283
 
280
284
  it "returns empty array for empty input" do
281
- ResourceMocks.mock_action_call(:users, :index, [john, jane])
285
+ ResourceMocks.mock_action_call(:users, :index, index_response([john, jane]))
282
286
 
283
287
  assert_equal [], resolver.user_ids({"option_names" => []})
284
288
  end
@@ -1,9 +1,9 @@
1
1
  require File.join(File.dirname(__FILE__), 'test_helper')
2
2
  require File.join(File.dirname(__FILE__), 'apipie_resource_mock')
3
3
 
4
- require 'hammer_cli_foreman/environment'
4
+ require 'hammer_cli_foreman/puppet_environment'
5
5
 
6
- describe HammerCLIForeman::Environment do
6
+ describe HammerCLIForeman::PuppetEnvironment do
7
7
 
8
8
  include CommandTestHelper
9
9
 
@@ -12,7 +12,7 @@ describe HammerCLIForeman::Environment do
12
12
  ResourceMocks.mock_action_call(:environments, :index, [])
13
13
  end
14
14
 
15
- let(:cmd) { HammerCLIForeman::Environment::ListCommand.new("", ctx) }
15
+ let(:cmd) { HammerCLIForeman::PuppetEnvironment::ListCommand.new("", ctx) }
16
16
 
17
17
  context "parameters" do
18
18
  it_should_accept "no arguments"
@@ -32,7 +32,7 @@ describe HammerCLIForeman::Environment do
32
32
 
33
33
  context "InfoCommand" do
34
34
 
35
- let(:cmd) { HammerCLIForeman::Environment::InfoCommand.new("", ctx) }
35
+ let(:cmd) { HammerCLIForeman::PuppetEnvironment::InfoCommand.new("", ctx) }
36
36
 
37
37
  context "parameters" do
38
38
  it_should_accept "id", ["--id=1"]
@@ -56,7 +56,7 @@ describe HammerCLIForeman::Environment do
56
56
 
57
57
  context "CreateCommand" do
58
58
 
59
- let(:cmd) { HammerCLIForeman::Environment::CreateCommand.new("", ctx) }
59
+ let(:cmd) { HammerCLIForeman::PuppetEnvironment::CreateCommand.new("", ctx) }
60
60
 
61
61
  context "parameters" do
62
62
  it_should_accept "name", ["--name=env"]
@@ -69,7 +69,7 @@ describe HammerCLIForeman::Environment do
69
69
 
70
70
  context "DeleteCommand" do
71
71
 
72
- let(:cmd) { HammerCLIForeman::Environment::DeleteCommand.new("", ctx) }
72
+ let(:cmd) { HammerCLIForeman::PuppetEnvironment::DeleteCommand.new("", ctx) }
73
73
 
74
74
  context "parameters" do
75
75
  it_should_accept "name", ["--name=env"]
@@ -82,7 +82,7 @@ describe HammerCLIForeman::Environment do
82
82
 
83
83
  context "UpdateCommand" do
84
84
 
85
- let(:cmd) { HammerCLIForeman::Environment::UpdateCommand.new("", ctx) }
85
+ let(:cmd) { HammerCLIForeman::PuppetEnvironment::UpdateCommand.new("", ctx) }
86
86
 
87
87
  context "parameters" do
88
88
  it_should_accept "name", ["--name=env", "--new-name=env2"]
@@ -99,11 +99,13 @@ describe HammerCLIForeman::Environment do
99
99
  ResourceMocks.smart_class_parameters_index
100
100
  end
101
101
 
102
- let(:cmd) { HammerCLIForeman::Environment::SCParamsCommand.new("", ctx) }
102
+ let(:cmd) { HammerCLIForeman::PuppetEnvironment::SCParamsCommand.new("", ctx) }
103
103
 
104
104
  context "parameters" do
105
105
  it_should_accept "environment", ["--environment=env"]
106
106
  it_should_accept "environment-id", ["--environment-id=1"]
107
+ it_should_accept "puppet-environment", ["--puppet-environment=env"]
108
+ it_should_accept "puppet-environment-id", ["--puppet-environment-id=1"]
107
109
  # it_should_fail_with "name or id missing", [] # TODO: temporarily disabled, parameters are checked in the id resolver
108
110
  end
109
111
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hammer_cli_foreman
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.17.0
4
+ version: 0.17.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tomáš Strachota
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2019-04-24 00:00:00.000000000 Z
12
+ date: 2019-07-10 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: hammer_cli
@@ -100,6 +100,10 @@ files:
100
100
  - lib/hammer_cli_foreman/auth_source.rb
101
101
  - lib/hammer_cli_foreman/auth_source_ldap.rb
102
102
  - lib/hammer_cli_foreman/combination.rb
103
+ - lib/hammer_cli_foreman/command_extensions.rb
104
+ - lib/hammer_cli_foreman/command_extensions/option_sources.rb
105
+ - lib/hammer_cli_foreman/command_extensions/puppet_environment.rb
106
+ - lib/hammer_cli_foreman/command_extensions/puppet_environments.rb
103
107
  - lib/hammer_cli_foreman/commands.rb
104
108
  - lib/hammer_cli_foreman/common_parameter.rb
105
109
  - lib/hammer_cli_foreman/compute_attribute.rb
@@ -120,7 +124,6 @@ files:
120
124
  - lib/hammer_cli_foreman/defaults.rb
121
125
  - lib/hammer_cli_foreman/dependency_resolver.rb
122
126
  - lib/hammer_cli_foreman/domain.rb
123
- - lib/hammer_cli_foreman/environment.rb
124
127
  - lib/hammer_cli_foreman/exception_handler.rb
125
128
  - lib/hammer_cli_foreman/exceptions.rb
126
129
  - lib/hammer_cli_foreman/external_usergroup.rb
@@ -143,6 +146,7 @@ files:
143
146
  - lib/hammer_cli_foreman/option_sources.rb
144
147
  - lib/hammer_cli_foreman/option_sources/id_params.rb
145
148
  - lib/hammer_cli_foreman/option_sources/ids_params.rb
149
+ - lib/hammer_cli_foreman/option_sources/puppet_environment_params.rb
146
150
  - lib/hammer_cli_foreman/option_sources/self_param.rb
147
151
  - lib/hammer_cli_foreman/option_sources/user_params.rb
148
152
  - lib/hammer_cli_foreman/organization.rb
@@ -154,6 +158,7 @@ files:
154
158
  - lib/hammer_cli_foreman/partition_table.rb
155
159
  - lib/hammer_cli_foreman/personal_access_token.rb
156
160
  - lib/hammer_cli_foreman/puppet_class.rb
161
+ - lib/hammer_cli_foreman/puppet_environment.rb
157
162
  - lib/hammer_cli_foreman/realm.rb
158
163
  - lib/hammer_cli_foreman/references.rb
159
164
  - lib/hammer_cli_foreman/report_template.rb
@@ -479,7 +484,6 @@ files:
479
484
  - test/unit/defaults_test.rb
480
485
  - test/unit/dependency_resolver_test.rb
481
486
  - test/unit/domain_test.rb
482
- - test/unit/environment_test.rb
483
487
  - test/unit/exception_handler_test.rb
484
488
  - test/unit/external_usergroup_test.rb
485
489
  - test/unit/fact_test.rb
@@ -504,6 +508,7 @@ files:
504
508
  - test/unit/param_filters_test.rb
505
509
  - test/unit/partition_table_test.rb
506
510
  - test/unit/puppet_class_test.rb
511
+ - test/unit/puppet_environment_test.rb
507
512
  - test/unit/realm_test.rb
508
513
  - test/unit/role_test.rb
509
514
  - test/unit/settings_test.rb
@@ -784,7 +789,6 @@ test_files:
784
789
  - test/reports/TEST-HammerCLIForeman-Architecture-InfoCommand-parameters.xml
785
790
  - test/unit/operating_system_test.rb
786
791
  - test/unit/location_test.rb
787
- - test/unit/environment_test.rb
788
792
  - test/unit/common_parameter_test.rb
789
793
  - test/unit/settings_test.rb
790
794
  - test/unit/image_test.rb
@@ -812,6 +816,7 @@ test_files:
812
816
  - test/unit/config_report_test.rb
813
817
  - test/unit/user_test.rb
814
818
  - test/unit/filter_test.rb
819
+ - test/unit/puppet_environment_test.rb
815
820
  - test/unit/output/formatters_test.rb
816
821
  - test/unit/host_test.rb
817
822
  - test/unit/subnet_test.rb