Project

General

Profile

Revision 04be28e1

Added by Thomas McKay almost 6 years ago

fixes #17453 - use PUT /rhsm/consumers/:id to update facts

View differences:

lib/hammer_cli_csv/content_hosts.rb
148 148

  
149 149
      def import
150 150
        raise _("--columns option only relevant with --export") unless option_columns.nil?
151
        remote = @server_status['plugins'].detect { |plugin| plugin['name'] == 'foreman_csv' }
152
        if remote.nil?
153
          import_locally
154
        else
155
          import_remotely
156
        end
157
      end
158

  
159
      def import_remotely
160
        params = {'content' => ::File.new(::File.expand_path(option_file), 'rb')}
161
        headers = {:content_type => 'multipart/form-data', :multipart => true}
162
        task_progress(@api.resource(:csv).call(:import_content_hosts, params, headers))
163
      end
164 151

  
165
      def import_locally
166 152
        @existing = {}
167 153
        @hypervisor_guests = {}
168 154
        @all_subscriptions = {}
......
270 256
          @hypervisor_guests[@existing[hypervisor]] << "#{line[ORGANIZATION]}/#{name}"
271 257
        end
272 258

  
259
        update_facts(host, line)
273 260
        update_host_collections(host, line)
274 261
        update_subscriptions(host, line, true)
275 262

  
276 263
        puts _('done') if option_verbose?
277 264
      end
278 265

  
279
      def facts(name, line)
280
        facts = {}
281
        facts['system.certificate_version'] = '3.2'  # Required for auto-attach to work
266
      def facts(name, line, facts = {})
267
        facts['system.certificate_version'] ||= '3.2'  # Required for auto-attach to work
282 268
        facts['network.hostname'] = name
283
        facts['cpu.core(s)_per_socket'] = line[CORES]
284
        facts['cpu.cpu_socket(s)'] = line[SOCKETS]
285
        facts['memory.memtotal'] = line[RAM]
286
        facts['uname.machine'] = line[ARCHITECTURE]
287
        (facts['distribution.name'], facts['distribution.version']) = os_name_version(line[OPERATINGSYSTEM])
288
        facts['virt.is_guest'] = line[VIRTUAL] == 'Yes' ? true : false
289
        facts['virt.uuid'] = "#{line[ORGANIZATION]}/#{name}" if facts['virt.is_guest']
290
        facts['cpu.cpu(s)'] = 1
269
        facts['cpu.core(s)_per_socket'] = line[CORES] if !line[CORES].nil? && !line[CORES].empty?
270
        facts['cpu.cpu_socket(s)'] = line[SOCKETS] if !line[SOCKETS].nil? && !line[SOCKETS].empty?
271
        facts['memory.memtotal'] = line[RAM] if !line[RAM].nil? && !line[RAM].empty?
272
        facts['uname.machine'] = line[ARCHITECTURE] if !line[ARCHITECTURE].nil? && !line[ARCHITECTURE].empty?
273
        (facts['distribution.name'], facts['distribution.version']) = os_name_version(line[OPERATINGSYSTEM]) if !line[OPERATINGSYSTEM].nil? && !line[OPERATINGSYSTEM].empty?
274
        if !line[VIRTUAL].nil? && !line[VIRTUAL].empty?
275
          facts['virt.is_guest'] = line[VIRTUAL] == 'Yes' ? true : false
276
          facts['virt.uuid'] = "#{line[ORGANIZATION]}/#{name}" if facts['virt.uuid'].nil? && facts['virt.is_guest']
277
        end
278
        facts['cpu.cpu(s)'] ||= 1
291 279
        facts
292 280
      end
293 281

  
282
      def update_facts(host, line)
283
        return if host['subscription_facet_attributes'].nil?
284

  
285
        url = "#{@server}/rhsm/consumers/#{host['subscription_facet_attributes']['uuid']}"
286
        uri = URI(url)
287
        nethttp = Net::HTTP.new(uri.host, uri.port)
288
        nethttp.use_ssl = uri.scheme == 'https'
289
        nethttp.verify_mode = OpenSSL::SSL::VERIFY_NONE
290
        nethttp.start do |http|
291
          request = Net::HTTP::Get.new(uri.request_uri)
292
          request = authenticate_request(request)
293
          response = http.request(request)
294
          results = JSON.parse(response.body)
295

  
296
          facts = facts(host['name'], line, results['facts'])
297

  
298
          request = Net::HTTP::Put.new(uri.request_uri, {'Content-Type' => 'application/json'})
299
          request = authenticate_request(request)
300
          request.body = {:facts => facts}.to_json
301
          response = http.request(request)
302
          JSON.parse(response.body)
303
        end
304

  
305
      end
306

  
294 307
      def update_host_collections(host, line)
295 308
        # TODO: http://projects.theforeman.org/issues/16234
296 309
        # return nil if line[HOSTCOLLECTIONS].nil? || line[HOSTCOLLECTIONS].empty?

Also available in: Unified diff