Class: HammerCLIImport::ImportCommand::ActivationKeyImportCommand

Inherits:
BaseCommand
  • Object
show all
Includes:
ImportTools::ContentView::Include, ImportTools::LifecycleEnvironment::Include
Defined in:
lib/hammer_cli_import/activationkey.rb

Instance Attribute Summary

Attributes included from PersistentMap::Extend

#map_description, #map_target_entity, #maps

Instance Method Summary collapse

Methods included from ImportTools::LifecycleEnvironment::Include

#get_env

Methods included from ImportTools::ContentView::Include

#create_composite_content_view, #delete_content_view, #publish_content_view

Methods inherited from BaseCommand

#_compare_hash, #_create_entity, #api_call, api_call, api_init, #create_entity, csv_columns, #cvs_iterate, #data_dir, #delete, #delete_entity, #delete_entity_by_import_id, #execute, #find_uniq, #found_errors, #get_cache, #get_original_id, #get_translated_id, #import, #initialize, #last_in_cache?, #list_server_entities, #load_cache, #lookup_entity, #lookup_entity_in_array, #lookup_entity_in_cache, #map_entity, #mapped_api_call, #post_delete, #print_summary, #process_error, #recognizable_error, #report_summary, #split_multival, #to_singular, #unmap_entity, #update_entity, #wait_for_task, #was_translated

Methods included from PersistentMap::Extend

#persistent_map, #persistent_maps

Methods included from ImportTools::ImportLogging::Extend

#add_logging_options

Methods included from AsyncTasksReactor::Extend

#add_async_tasks_reactor_options

Methods included from AsyncTasksReactor::Include

#atr_exit, #atr_init, #postpone_till, #wait_for

Methods included from ImportTools::Exceptional::Include

#handle_missing_and_supress, #silently

Methods included from ImportTools::Task::Include

#annotate_tasks

Methods included from ImportTools::ImportLogging::Include

#debug, #error, #fatal, #info, #log, #logtrace, #progress, #setup_logging, #warn

Methods included from PersistentMap::Include

#load_persistent_maps, #map_target_entity, #maps, #prune_persistent_maps, #save_persistent_maps

Constructor Details

This class inherits a constructor from HammerCLIImport::BaseCommand

Instance Method Details

#associate_host_collections(ak_id, server_group_ids) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/hammer_cli_import/activationkey.rb', line 51

def associate_host_collections(ak_id, server_group_ids)
  translated_ids = server_group_ids.collect { |sg_id| get_translated_id(:host_collections, sg_id) }
  info "  Associating activation key [#{ak_id}] with host collections [#{translated_ids.join(', ')}]"
  api_call(
    :activation_keys,
    :add_host_collections,
    {
      :id => ak_id,
      :host_collection_ids => translated_ids
    })
end

#delete_single_row(data) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
# File 'lib/hammer_cli_import/activationkey.rb', line 142

def delete_single_row(data)
  unless @pm[:activation_keys][data['token']]
    info to_singular(:activation_keys).capitalize + ' with id ' + data['token'] +
      " wasn't imported. Skipping deletion."
    return
  end
  ak = @cache[:activation_keys][get_translated_id(:activation_keys, data['token'])]
  delete_entity(:activation_keys, data['token'])
  delete_content_view(ak['content_view']['id'].to_i, :ak_content_views) if
    ak['content_view'] && was_translated(:ak_content_views, ak['content_view']['id'])
end

#import_single_row(data) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/hammer_cli_import/activationkey.rb', line 63

def import_single_row(data)
  if data['base_channel_id'].nil?
    # if base channel id is empty,
    # 'Spacewalk Default' was used on Sat5
    info "Skipping activation-key #{data['token']}: Migrating activation-keys with " \
      "'Red Hat Satellite Default' as base channel is not supported."
    report_summary :skipped, :activation_keys
    return
  end
  ak_hash = mk_ak_hash data
  ak = create_entity(:activation_keys, ak_hash, data['token'])
  if (data['server_group_id'])
    associate_host_collections(ak['id'], split_multival(data['server_group_id']))
  end
  @ak_content_views ||= {}
  @ak_content_views[ak['id'].to_i] ||= Set.new
  if data['base_channel_id']
    split_multival(data['base_channel_id']).each do |base_channel_id|
      @ak_content_views[ak['id'].to_i] << begin
        get_translated_id(:redhat_content_views, [data['org_id'].to_i, base_channel_id])
      rescue HammerCLIImport::MissingObjectError
        begin
          get_translated_id(:content_views, base_channel_id)
        rescue HammerCLIImport::MissingObjectError
          error "Can't find content view for channel ID [#{base_channel_id}] for key [#{data['token']}]"
        end
      end
    end
  else
    # if base channel id is empty,
    # 'Spacewalk Default' was used on Sat5
    # Since we can not migrate them due to
    # bug 1126924, we skip it right at the beggining
    # of this function.
    debug ' Red Hat Satellite Default activation keys are not supported.'
  end
  split_multival(data['child_channel_id']).each do |child_ch|
    @ak_content_views[ak['id'].to_i] << begin
      get_translated_id(:redhat_content_views, [data['org_id'].to_i, child_ch])
    rescue HammerCLIImport::MissingObjectError
      begin
        get_translated_id(:content_views, child_ch)
      rescue HammerCLIImport::MissingObjectError
        error "Can't find content view for channel ID [#{child_ch}] for key [#{data['token']}]"
      end
    end
  end
end

#mk_ak_hash(data) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/hammer_cli_import/activationkey.rb', line 38

def mk_ak_hash(data)
  usage_limit = 'unlimited'
  usage_limit = data['usage_limit'].to_i if data['usage_limit']
  debug "  Activation key usage_limit: #{usage_limit}"
  {
    :name => data['token'],
    :organization_id => get_translated_id(:organizations, data['org_id'].to_i),
    :label => data['token'],
    :description => data['note'],
    :usage_limit => usage_limit
  }
end

#post_import(_csv_file) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/hammer_cli_import/activationkey.rb', line 112

def post_import(_csv_file)
  return unless @ak_content_views
  @ak_content_views.each do |ak_id, cvs|
    if cvs.include? nil
      warn "Skipping content view association for activation key [#{ak_id}]. Dependent content views not ready."
      next
    end
    handle_missing_and_supress "processing activation key #{ak_id}" do
      ak = lookup_entity(:activation_keys, ak_id)
      ak_cv_hash = {}
      org_id = lookup_entity_in_cache(:organizations, {'label' => ak['organization']['label']})['id']
      ak_cv_hash[:content_view_id] = create_composite_content_view(
        :ak_content_views,
        org_id,
        "ak_#{ak_id}",
        "Composite content view for activation key #{ak['name']}",
        cvs)
      ak_cv_hash[:environment_id] = get_env(org_id, 'Library')['id']
      ak_cv_hash[:organization_id] = org_id
      if ak_cv_hash[:content_view_id]
        info "  Associating activation key [#{ak_id}] with content view [#{ak_cv_hash[:content_view_id]}]"
        # associate the content view with the activation key
        update_entity(:activation_keys, ak_id, ak_cv_hash)
      else
        info '  Skipping content-view associations.'
      end
    end
  end
end