Project

General

Profile

Revision 5146ac68

Added by Thomas McKay almost 7 years ago

myriad changes in attempt to get import working against master/develop

View differences:

lib/hammer_cli_csv/content_hosts.rb
165 165
        if !@hypervisor_guests.empty?
166 166
          print(_('Updating hypervisor and guest associations...')) if option_verbose?
167 167
          @hypervisor_guests.each do |host_id, guest_ids|
168
            @api.resource(:systems).call(:update, {
168
            @api.resource(:hosts).call(:update, {
169 169
                'id' => host_id,
170
                'guest_ids' => guest_ids
170
                'host' => {
171
                    'guest_ids' => guest_ids
172
                }
171 173
            })
172 174
          end
173 175
          puts _('done') if option_verbose?
......
178 180
        return if option_organization && line[ORGANIZATION] != option_organization
179 181

  
180 182
        if !@existing[line[ORGANIZATION]]
181
          @existing[line[ORGANIZATION]] = {}
183
          @existing[line[ORGANIZATION]] = true
182 184
          # Fetching all content hosts is too slow and times out due to the complexity of the data
183 185
          # rendered in the json.
184 186
          # http://projects.theforeman.org/issues/6307
185
          total = @api.resource(:systems).call(:index, {
187
          total = @api.resource(:hosts).call(:index, {
186 188
              'organization_id' => foreman_organization(:name => line[ORGANIZATION]),
187 189
              'per_page' => 1
188 190
          })['total'].to_i
189 191
          (total / 20 + 2).to_i.times do |page|
190
            @api.resource(:systems).call(:index, {
192
            @api.resource(:hosts).call(:index, {
191 193
                'organization_id' => foreman_organization(:name => line[ORGANIZATION]),
192 194
                'page' => page + 1,
193 195
                'per_page' => 20
194 196
            })['results'].each do |host|
195
              @existing[line[ORGANIZATION]][host['name']] = host['uuid'] if host
197
              @existing[host['name']] = {
198
                :host => host['id'],
199
                :subscription => host['subscription']['id'],
200
                :content => host['content']['id']
201
              }
196 202
            end
197 203
          end
198 204
        end
......
200 206
        count(line[COUNT]).times do |number|
201 207
          name = namify(line[NAME], number)
202 208

  
203
          if !@existing[line[ORGANIZATION]].include? name
209
          if !@existing.include? name
204 210
            print(_("Creating content host '%{name}'...") % {:name => name}) if option_verbose?
205
            host_id = @api.resource(:systems).call(:create, {
211
            host_id = @api.resource(:host_subscriptions).call(:register, {
206 212
                'name' => name,
207 213
                'organization_id' => foreman_organization(:name => line[ORGANIZATION]),
208
                'environment_id' => lifecycle_environment(line[ORGANIZATION], :name => line[ENVIRONMENT]),
214
                'lifecycle_environment_id' => lifecycle_environment(line[ORGANIZATION], :name => line[ENVIRONMENT]),
209 215
                'content_view_id' => katello_contentview(line[ORGANIZATION], :name => line[CONTENTVIEW]),
210 216
                'facts' => facts(name, line),
211 217
                'installed_products' => products(line),
212 218
                'service_level' => line[SLA],
213 219
                'type' => 'system'
214
            })['uuid']
215
            @existing[line[ORGANIZATION]][name] = host_id
220
            })['id']
221
            @existing[name] = host_id
216 222
          else
223
            # TODO: remove passing facet IDs to update
224
            # Bug #13849 - updating a host's facet should not require the facet id to be included in facet params
225
            #              http://projects.theforeman.org/issues/13849
217 226
            print(_("Updating content host '%{name}'...") % {:name => name}) if option_verbose?
218
            host_id = @api.resource(:systems).call(:update, {
219
                'id' => @existing[line[ORGANIZATION]][name],
220
                'system' => {
227
            host_id = @api.resource(:hosts).call(:update, {
228
                'id' => @existing[name][:host],
229
                'host' => {
221 230
                    'name' => name,
222
                    'environment_id' => lifecycle_environment(line[ORGANIZATION], :name => line[ENVIRONMENT]),
223
                    'content_view_id' => katello_contentview(line[ORGANIZATION], :name => line[CONTENTVIEW]),
224
                    'facts' => facts(name, line),
225
                    'installed_products' => products(line)
226
                },
227
                'facts' => facts(name, line),
228
                'installed_products' => products(line),  # TODO: http://projects.theforeman.org/issues/9191,
229
                'service_level' => line[SLA]
230
            })['uuid']
231
                    'content_facet_attributes' => {
232
                        'id' => @existing[name][:content],
233
                        'lifecycle_environment_id' => lifecycle_environment(line[ORGANIZATION], :name => line[ENVIRONMENT]),
234
                        'content_view_id' => katello_contentview(line[ORGANIZATION], :name => line[CONTENTVIEW])
235
                    },
236
                    'subscription_facet_attributes' => {
237
                        'id' => @existing[name][:subscription],
238
                        'facts' => facts(name, line),
239
                        # TODO: PUT /hosts subscription_facet_attributes missing "installed_products"
240
                        # http://projects.theforeman.org/issues/13854
241
                        #'installed_products' => products(line),
242
                        'service_level' => line[SLA]
243
                    }
244
                }
245
            })['host_id']
231 246
          end
232 247

  
233 248
          if line[VIRTUAL] == 'Yes' && line[HOST]
234
            raise "Content host '#{line[HOST]}' not found" if !@existing[line[ORGANIZATION]][line[HOST]]
235
            @hypervisor_guests[@existing[line[ORGANIZATION]][line[HOST]]] ||= []
236
            @hypervisor_guests[@existing[line[ORGANIZATION]][line[HOST]]] << "#{line[ORGANIZATION]}/#{name}"
249
            raise "Content host '#{line[HOST]}' not found" if !@existing[line[HOST]]
250
            @hypervisor_guests[@existing[line[HOST]]] ||= []
251
            @hypervisor_guests[@existing[line[HOST]]] << @existing[name]
237 252
          end
238 253

  
254
          update_host_facts(host_id, line)
239 255
          update_host_collections(host_id, line)
240 256
          update_subscriptions(host_id, line)
241 257

  
......
262 278
        facts
263 279
      end
264 280

  
281
      def update_host_facts(host_id, line)
282
      end
283

  
265 284
      def update_host_collections(host_id, line)
266 285
        return nil if !line[HOSTCOLLECTIONS]
267 286
        CSV.parse_line(line[HOSTCOLLECTIONS]).each do |hostcollection_name|
268
          @api.resource(:host_collections).call(:add_systems, {
287
          @api.resource(:host_collections).call(:add_hosts, {
269 288
              'id' => katello_hostcollection(line[ORGANIZATION], :name => hostcollection_name),
270
              'system_ids' => [host_id]
289
              'hosts_ids' => [host_id]
271 290
          })
272 291
        end
273 292
      end
......
299 318
      end
300 319

  
301 320
      def update_subscriptions(host_id, line)
302
        existing_subscriptions = @api.resource(:systems).call(:subscriptions, {
303
            'per_page' => 999999,
304
            'id' => host_id
321
        existing_subscriptions = @api.resource(:host_subscriptions).call(:index, {
322
            'host_id' => host_id
305 323
        })['results']
306
        if existing_subscriptions.length > 0
307
          @api.resource(:subscriptions).call(:destroy, {
308
            'system_id' => host_id,
309
            'id' => existing_subscriptions[0]['id']
324
        if existing_subscriptions.length != 0
325
          @api.resource(:host_subscriptions).call(:remove_subscriptions, {
326
            'host_id' => host_id,
327
            'subscriptions' => existing_subscriptions
310 328
          })
311 329
        end
312 330

  
313
        # existing_subscriptions = @api.resource(:subscriptions).call(:index, {
314
        #     'organization_id' => foreman_organization(:name => line[ORGANIZATION]),
315
        #     'per_page' => 999999,
316
        #     'system_id' => host_id
317
        # })['results']
318
        # if existing_subscriptions.length > 0
319
        #   @api.resource(:subscriptions).call(:destroy, {
320
        #     'system_id' => host_id,
321
        #     'id' => existing_subscriptions[0]['id']
322
        #   })
323
        # end
324

  
325 331
        return if line[SUBSCRIPTIONS].nil? || line[SUBSCRIPTIONS].empty?
326 332

  
327 333
        subscriptions = CSV.parse_line(line[SUBSCRIPTIONS], {:skip_blanks => true}).collect do |details|
......
332 338
          }
333 339
        end
334 340

  
335
        @api.resource(:subscriptions).call(:create, {
336
            'system_id' => host_id,
341
        @api.resource(:host_subscriptions).call(:add_subscriptions, {
342
            'host_id' => host_id,
337 343
            'subscriptions' => subscriptions
338 344
        })
339 345
      end

Also available in: Unified diff