Project

General

Profile

Revision 2f22006a

Added by Thomas McKay about 4 years ago

fixes #18864 - option to clear subscriptions first

View differences:

lib/hammer_cli_csv/content_hosts.rb
12 12
      end
13 13

  
14 14
      option %w(--itemized-subscriptions), :flag, _('Export one subscription per row, only process update subscriptions on import')
15
      option %w(--clear-subscriptions), :flag, _('When processing --itemized-subscriptions, clear existing subscriptions first')
15 16
      option %w(--columns), 'COLUMN_NAMES', _('Comma separated list of column names to export')
16 17

  
17 18
      SEARCH = 'Search'
......
86 87
      end
87 88

  
88 89
      def export(csv)
90
        raise _("--clear-subscriptions option only relevant during import") if option_clear_subscriptions?
91

  
89 92
        if option_itemized_subscriptions?
90 93
          export_itemized_subscriptions csv
91 94
        else
......
151 154
        raise _("--columns option only relevant with --export") unless option_columns.nil?
152 155

  
153 156
        @existing = {}
157
        @visited = {}
154 158
        @hypervisor_guests = {}
155 159
        @all_subscriptions = {}
156 160

  
......
214 218

  
215 219
        print(_("Updating subscriptions for content host '%{name}'...") % {:name => name}) if option_verbose?
216 220
        host = @api.resource(:hosts).call(:show, {:id => @existing[name]})
217
        update_subscriptions(host, line, false)
221
        remove_existing = clear_subscriptions?(host['name'])
222
        update_subscriptions(host, line, remove_existing)
218 223
        puts _('done') if option_verbose?
219 224
      end
220 225

  
......
347 352
            'full_results' => true
348 353
        })['results']
349 354
        if remove_existing && existing_subscriptions.length != 0
355
          print _("clearing existing subscriptions...") if option_verbose?
350 356
          existing_subscriptions.map! do |existing_subscription|
351
            {:id => existing_subscription['id'], :quantity => existing_subscription['quantity_consumed']}
357
            subs = [{:id => existing_subscription['id'], :quantity => existing_subscription['quantity_consumed']}]
358
            @api.resource(:host_subscriptions).call(:remove_subscriptions, {
359
              'host_id' => host['id'],
360
              'subscriptions' => subs
361
            })
352 362
          end
353
          @api.resource(:host_subscriptions).call(:remove_subscriptions, {
354
            'host_id' => host['id'],
355
            'subscriptions' => existing_subscriptions
356
          })
357 363
          existing_subscriptions = []
358 364
        end
359 365

  
......
425 431
          # http://projects.theforeman.org/issues/6307
426 432
          total = @api.resource(:hosts).call(:index, {
427 433
              'organization_id' => foreman_organization(:name => line[ORGANIZATION]),
434
              'search' => option_search,
428 435
              'per_page' => 1
429
          })['total'].to_i
436
          })['subtotal'].to_i
430 437
          (total / 20 + 1).to_i.times do |page|
431 438
            @api.resource(:hosts).call(:index, {
432 439
                'organization_id' => foreman_organization(:name => line[ORGANIZATION]),
440
                'search' => option_search,
433 441
                'page' => page + 1,
434 442
                'per_page' => 20
435 443
            })['results'].each do |host|
......
599 607
        hypervisor = namify(hypervisor, 1) if !hypervisor.nil? && !hypervisor.empty?
600 608
        hypervisor
601 609
      end
610

  
611
      def clear_subscriptions?(name)
612
        if option_clear_subscriptions? && !@visited.include?(name)
613
          @visited[name] = true
614
          true
615
        else
616
          false
617
        end
618
      end
602 619
    end
603 620
  end
604 621
end
test/resources/content_hosts_test.rb
17 17
     csv content-hosts [OPTIONS]
18 18

  
19 19
Options:
20
 --clear-subscriptions         When processing --itemized-subscriptions, clear existing subscriptions first
20 21
 --columns COLUMN_NAMES        Comma separated list of column names to export
21 22
 --continue-on-error           Continue processing even if individual resource error
22 23
 --export                      Export current data instead of importing
......
56 57
      stop_vcr
57 58
    end
58 59

  
60
    def test_export_with_clear_subscriptions
61
      start_vcr
62
      set_user 'admin'
63

  
64
      stdout,stderr = capture {
65
        hammer.run(%W{--reload-cache csv content-hosts --export --clear-subscriptions})
66
      }
67
      assert_equal "Error: --clear-subscriptions option only relevant during import\n", stderr
68
      assert_equal stdout, ''
69

  
70
      stop_vcr
71
    end
72

  
59 73
    def test_export_with_columns
60 74
      start_vcr
61 75
      set_user 'admin'
......
186 200
        hammer.run(%W{--reload-cache csv content-hosts --verbose --file #{file.path}})
187 201
      }
188 202
      assert_equal '', stderr
189
      assert_equal "Updating content host 'testaaa0'...done\nUpdating content host 'testaaa1'...done\n", stdout
203
      assert_equal "Updating content host 'testaaa0'...clearing existing subscriptions...done\nUpdating content host 'testaaa1'...clearing existing subscriptions...done\n", stdout
190 204

  
191 205

  
192 206
      %w{testaaa0 testaaa1 testbbb0 testbbb1 testbbb2}.each do |hostname|
......
196 210
      stop_vcr
197 211
    end
198 212

  
213
    # import a single line, then import again w/ clearing
214
    def test_import_single_line_clear_subs
215
      start_vcr
216
      set_user 'admin'
217

  
218
      hostname = 'tester1'
219

  
220
      file = Tempfile.new('content_hosts_test')
221
      file.write("Name,Organization,Environment,Content View,Host Collections,Virtual,Host,OS,Arch,Sockets,RAM,Cores,SLA,Products,Subscriptions\n")
222
      file.write("#{hostname},Test Corporation,Library,Default Organization View,\"\",Yes,,RHEL 7.2,x86_64,2,3882752,1,\"\",\"69|Red Hat Enterprise Linux Server,290|Red Hat OpenShift Enterprise\",\"\"\"1|RH00001|Red Hat Enterprise Linux for Virtual Datacenters, Premium\"\"\"\n")
223
      file.rewind
224

  
225
      stdout,stderr = capture {
226
        hammer.run(%W{--reload-cache csv content-hosts --verbose --file #{file.path}})
227
      }
228
      assert_equal '', stderr
229
      assert_equal "Creating content host '#{hostname}'...done\n", stdout
230

  
231
      file = Tempfile.new('content_hosts_test')
232
      file.write("Name,Organization,Environment,Content View,Host Collections,Virtual,Host,OS,Arch,Sockets,RAM,Cores,SLA,Products,Subscriptions\n")
233
      file.write("#{hostname},Test Corporation,Library,Default Organization View,\"\",Yes,,RHEL 7.2,x86_64,2,3882752,1,\"\",\"69|Red Hat Enterprise Linux Server,290|Red Hat OpenShift Enterprise\",\"\"\"|RH00004|Red Hat Enterprise Linux Server, Standard (Physical or Virtual Nodes)\"\"\"\n")
234
      file.rewind
235

  
236
      stdout,stderr = capture {
237
        hammer.run(%W{--reload-cache csv content-hosts --verbose --file #{file.path} --clear-subscriptions})
238
      }
239
      assert_equal '', stderr
240
      assert_equal "Updating content host '#{hostname}'...clearing existing subscriptions...done\n", stdout
241

  
242
      stdout,stderr = capture {
243
        hammer.run(%W{--reload-cache csv content-hosts --export --organization Test\ Corporation --itemized-subscriptions --search name=#{hostname}})
244
      }
245
      assert_equal '', stderr
246
      lines = stdout.split("\n")
247
      lines.select! { |line| line.match(/tester1.*/) }
248
      assert_equal 1, lines.length
249
      assert_match(/.*Test Corporation,Library,Default Organization View,"",Yes,,RHEL 7.2,x86_64,2,3882752,1.*/, lines[0])
250
      assert_match(/.*Red Hat Enterprise Linux Server, Standard.*/, lines[0])
251
      host_delete(hostname)
252

  
253
      stop_vcr
254
    end
255

  
199 256
    def test_columns_config
200 257
      start_vcr
201 258
      set_user 'admin'

Also available in: Unified diff