Class: HammerCLICsv::CsvCommand::ActivationKeysCommand

Inherits:
BaseCommand
  • Object
show all
Defined in:
lib/hammer_cli_csv/activation_keys.rb

Constant Summary collapse

ORGANIZATION =
'Organization'
DESCRIPTION =
'Description'
LIMIT =
'Limit'
ENVIRONMENT =
'Environment'
CONTENTVIEW =
'Content View'
SYSTEMGROUPS =
'System Groups'
SUBSCRIPTIONS =
'Subscriptions'

Constants inherited from BaseCommand

BaseCommand::COUNT, BaseCommand::NAME

Instance Method Summary collapse

Methods inherited from BaseCommand

#associate_locations, #associate_organizations, #build_os_name, #collect_column, #execute, #export_column, #foreman_architecture, #foreman_domain, #foreman_environment, #foreman_filter, #foreman_location, #foreman_operatingsystem, #foreman_organization, #foreman_partitiontable, #foreman_permission, #foreman_role, #foreman_template_kind, #hammer, #hammer_context, #katello_contentview, #katello_hostcollection, #katello_repository, #katello_subscription, #labelize, #lifecycle_environment, #namify, #pluralize, #split_os_name, #thread_import

Instance Method Details

#create_activationkeys_from_csv(line) ⇒ Object



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
111
112
113
114
115
116
117
118
119
# File 'lib/hammer_cli_csv/activation_keys.rb', line 71

def create_activationkeys_from_csv(line)
  if !@existing[line[ORGANIZATION]]
    @existing[line[ORGANIZATION]] = {}
    @api.resource(:activation_keys)
      .call(:index, {
              'per_page' => 999999,
              'organization_id' => foreman_organization(:name => line[ORGANIZATION])
            })['results'].each do |activationkey|
      @existing[line[ORGANIZATION]][activationkey['name']] = activationkey['id'] if activationkey
    end
  end

  line[COUNT].to_i.times do |number|
    name = namify(line[NAME], number)

    if !@existing[line[ORGANIZATION]].include? name
      print "Creating activation key '#{name}'..." if option_verbose?
      activationkey = @api.resource(:activation_keys)
        .call(:create, {
                'name' => name,
                'environment_id' => lifecycle_environment(line[ORGANIZATION],
                                                          :name => line[ENVIRONMENT]),
                'content_view_id' => katello_contentview(line[ORGANIZATION],
                                                         :name => line[CONTENTVIEW]),
                'description' => line[DESCRIPTION],
                'usage_limit' => usage_limit(line[LIMIT])
              })
      @existing[line[ORGANIZATION]][activationkey['name']] = activationkey['id']
    else
      print "Updating activation key '#{name}'..." if option_verbose?
      activationkey = @api.resource(:activation_keys)
        .call(:update, {
                'id' => @existing[line[ORGANIZATION]][name],
                'name' => name,
                'environment_id' => lifecycle_environment(line[ORGANIZATION],
                                                          :name => line[ENVIRONMENT]),
                'content_view_id' => katello_contentview(line[ORGANIZATION],
                                                         :name => line[CONTENTVIEW]),
                'description' => line[DESCRIPTION],
                'usage_limit' => usage_limit(line[LIMIT])
              })
    end

    update_subscriptions(activationkey, line)
    update_groups(activationkey, line)

    puts 'done' if option_verbose?
  end
end

#exportObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/hammer_cli_csv/activation_keys.rb', line 26

def export
  CSV.open(option_csv_file || '/dev/stdout', 'wb', {:force_quotes => false}) do |csv|
    csv << [NAME, COUNT, ORGANIZATION, DESCRIPTION, LIMIT, ENVIRONMENT, CONTENTVIEW,
            SYSTEMGROUPS, SUBSCRIPTIONS]
    @api.resource(:organizations)
      .call(:index, {
              :per_page => 999999
            })['results'].each do |organization|
      @api.resource(:activation_keys)
        .call(:index, {
                'per_page' => 999999,
                'organization_id' => organization['id']
              })['results'].each do |activationkey|
        puts "Writing activation key '#{activationkey['name']}'" if option_verbose?
        name = namify(activationkey['name'])
        count = 1
        description = activationkey['description']
        limit = activationkey['usage_limit'].to_i < 0 ? 'Unlimited' : sytemgroup['usage_limit']
        environment = activationkey['environment']['label']
        contentview = activationkey['content_view']['name']
        hostcollections = export_column(activationkey, 'systemGroups', 'name')
        subscriptions = CSV.generate do |column|
          column << @api.resource(:subscriptions).call(:index, {
                                                'activation_key_id' => activationkey['id']
                                              })['results'].collect do |subscription|
            amount = subscription['amount'] == 0 ? 'Automatic' : subscription['amount']
            "#{amount}|#{subscription['product_name']}"
          end
        end
        subscriptions.delete!("\n")
        csv << [name, count, organization['label'], description, limit, environment, contentview,
                hostcollections, subscriptions]
      end
    end
  end
end

#importObject



63
64
65
66
67
68
69
# File 'lib/hammer_cli_csv/activation_keys.rb', line 63

def import
  @existing = {}

  thread_import do |line|
    create_activationkeys_from_csv(line)
  end
end

#update_groups(activationkey, line) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/hammer_cli_csv/activation_keys.rb', line 121

def update_groups(activationkey, line)
  if line[SYSTEMGROUPS] && line[SYSTEMGROUPS] != ''
    # TODO: note that existing system groups are not removed
    CSV.parse_line(line[SYSTEMGROUPS], {:skip_blanks => true}).each do |name|
      @api.resource(:host_collections)
        .call(:add_activation_keys, {
                'id' => katello_hostcollection(line[ORGANIZATION], :name => name),
                'activation_key_ids' => [activationkey['id']]
              })
    end
  end
end

#update_subscriptions(activationkey, line) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/hammer_cli_csv/activation_keys.rb', line 134

def update_subscriptions(activationkey, line)
  if line[SUBSCRIPTIONS] && line[SUBSCRIPTIONS] != ''
    subscriptions = CSV.parse_line(line[SUBSCRIPTIONS], {:skip_blanks => true}).collect do |subscription_details|
      (amount, name) = subscription_details.split('|')
      {
        :id => katello_subscription(line[ORGANIZATION], :name => name),
        :quantity => amount
      }
    end

    # TODO: should there be a destroy_all similar to systems?
    @api.resource(:subscriptions)
      .call(:index, {
              'per_page' => 999999,
              'activation_key_id' => activationkey['id']
            })['results'].each do |subscription|
      @api.resource(:subscriptions)
        .call(:destroy, {
                'id' => subscription['id'],
                'activation_key_id' => activationkey['id']
              })
    end

    @api.resource(:subscriptions)
      .call(:create, {
              'activation_key_id' => activationkey['id'],
              'subscriptions' => subscriptions
            })
  end
end

#usage_limit(limit) ⇒ Object



165
166
167
# File 'lib/hammer_cli_csv/activation_keys.rb', line 165

def usage_limit(limit)
  Integer(limit) rescue -1
end