1
|
module HammerCLICsv
|
2
|
class CsvCommand
|
3
|
class ActivationKeysCommand < BaseCommand
|
4
|
command_name 'activation-keys'
|
5
|
desc _('import or export activation keys')
|
6
|
|
7
|
ORGANIZATION = 'Organization'
|
8
|
DESCRIPTION = 'Description'
|
9
|
LIMIT = 'Limit'
|
10
|
ENVIRONMENT = 'Environment'
|
11
|
CONTENTVIEW = 'Content View'
|
12
|
HOSTCOLLECTIONS = 'Host Collections'
|
13
|
SERVICELEVEL = "Service Level"
|
14
|
RELEASEVER = "Release Version"
|
15
|
AUTOATTACH = "Auto-Attach"
|
16
|
SUBSCRIPTIONS = 'Subscriptions'
|
17
|
|
18
|
def export(csv)
|
19
|
csv << [NAME, ORGANIZATION, DESCRIPTION, LIMIT, ENVIRONMENT, CONTENTVIEW,
|
20
|
HOSTCOLLECTIONS, AUTOATTACH, SERVICELEVEL, RELEASEVER, SUBSCRIPTIONS]
|
21
|
@api.resource(:organizations).call(:index, {
|
22
|
:per_page => 999999
|
23
|
})['results'].each do |organization|
|
24
|
next if option_organization && organization['name'] != option_organization
|
25
|
|
26
|
@api.resource(:activation_keys).call(:index, {
|
27
|
'per_page' => 999999,
|
28
|
'organization_id' => organization['id']
|
29
|
})['results'].each do |activationkey|
|
30
|
name = namify(activationkey['name'])
|
31
|
count = 1
|
32
|
description = activationkey['description']
|
33
|
limit = activationkey['unlimited_content_hosts'] ? 'Unlimited' : activationkey['max_content_hosts']
|
34
|
environment = activationkey['environment']['label']
|
35
|
contentview = activationkey['content_view']['name']
|
36
|
hostcollections = export_column(activationkey, 'host_collections', 'name')
|
37
|
autoattach = activationkey['auto_attach'] ? 'Yes' : 'No'
|
38
|
servicelevel = activationkey['service_level']
|
39
|
releasever = activationkey['release_version']
|
40
|
subscriptions = CSV.generate do |column|
|
41
|
column << @api.resource(:subscriptions).call(:index, {
|
42
|
'organization_id' => organization['id'],
|
43
|
'activation_key_id' => activationkey['id']
|
44
|
})['results'].collect do |subscription|
|
45
|
amount = subscription['amount'] == 0 ? 'Automatic' : subscription['amount']
|
46
|
sku = subscription['product_id'].match(/\A[0-9]/) ? 'Custom' : subscription['product_id']
|
47
|
"#{amount}|#{sku}|#{subscription['product_name']}"
|
48
|
end
|
49
|
end
|
50
|
subscriptions.delete!("\n")
|
51
|
csv << [name, count, organization['name'], description, limit, environment, contentview,
|
52
|
hostcollections, servicelevel, releasever, autoattach, subscriptions]
|
53
|
end
|
54
|
end
|
55
|
end
|
56
|
|
57
|
def import
|
58
|
@existing = {}
|
59
|
|
60
|
thread_import do |line|
|
61
|
create_activationkeys_from_csv(line)
|
62
|
end
|
63
|
end
|
64
|
|
65
|
def create_activationkeys_from_csv(line)
|
66
|
return if option_organization && line[ORGANIZATION] != option_organization
|
67
|
|
68
|
if !@existing[line[ORGANIZATION]]
|
69
|
@existing[line[ORGANIZATION]] = {}
|
70
|
@api.resource(:activation_keys).call(:index, {
|
71
|
'per_page' => 999999,
|
72
|
'organization_id' => foreman_organization(:name => line[ORGANIZATION])
|
73
|
})['results'].each do |activationkey|
|
74
|
@existing[line[ORGANIZATION]][activationkey['name']] = activationkey['id'] if activationkey
|
75
|
end
|
76
|
end
|
77
|
|
78
|
count(line[COUNT]).times do |number|
|
79
|
name = namify(line[NAME], number)
|
80
|
|
81
|
params = {
|
82
|
'organization_id' => foreman_organization(:name => line[ORGANIZATION]),
|
83
|
'name' => name,
|
84
|
'environment_id' => lifecycle_environment(line[ORGANIZATION],
|
85
|
:name => line[ENVIRONMENT]),
|
86
|
'content_view_id' => katello_contentview(line[ORGANIZATION],
|
87
|
:name => line[CONTENTVIEW]),
|
88
|
'description' => line[DESCRIPTION],
|
89
|
'unlimited_content_hosts' => (line[LIMIT] == 'Unlimited') ? true : false,
|
90
|
'max_content_hosts' => (line[LIMIT] == 'Unlimited') ? nil : line[LIMIT].to_i
|
91
|
}
|
92
|
params['auto_attach'] = (line[AUTOATTACH] == 'Yes' ? true : false) if params['auto_attach']
|
93
|
params['service_level'] = line[SERVICELEVEL].nil? || line[SERVICELEVEL].empty? ? nil : line[SERVICELEVEL]
|
94
|
params['release_version'] = line[RELEASEVER].nil? || line[RELEASEVER].empty? ? nil : line[RELEASEVER]
|
95
|
if !@existing[line[ORGANIZATION]].include? name
|
96
|
print _("Creating activation key '%{name}'...") % {:name => name} if option_verbose?
|
97
|
activationkey = @api.resource(:activation_keys).call(:create, params)
|
98
|
@existing[line[ORGANIZATION]][activationkey['name']] = activationkey['id']
|
99
|
else
|
100
|
print _("Updating activation key '%{name}'...") % {:name => name} if option_verbose?
|
101
|
params['id'] = @existing[line[ORGANIZATION]][name]
|
102
|
activationkey = @api.resource(:activation_keys).call(:update, params)
|
103
|
end
|
104
|
|
105
|
update_subscriptions(activationkey, line)
|
106
|
update_groups(activationkey, line)
|
107
|
|
108
|
puts _('done') if option_verbose?
|
109
|
end
|
110
|
end
|
111
|
|
112
|
def update_groups(activationkey, line)
|
113
|
if line[HOSTCOLLECTIONS] && line[HOSTCOLLECTIONS] != ''
|
114
|
|
115
|
CSV.parse_line(line[HOSTCOLLECTIONS], {:skip_blanks => true}).each do |name|
|
116
|
@api.resource(:activation_keys).call(:add_host_collections, {
|
117
|
'id' => activationkey['id'],
|
118
|
'host_collection_ids' => [katello_hostcollection(line[ORGANIZATION], :name => name)]
|
119
|
})
|
120
|
end
|
121
|
end
|
122
|
end
|
123
|
|
124
|
def update_subscriptions(activationkey, line)
|
125
|
if line[SUBSCRIPTIONS] && line[SUBSCRIPTIONS] != ''
|
126
|
subscriptions = CSV.parse_line(line[SUBSCRIPTIONS], {:skip_blanks => true}).collect do |subscription_details|
|
127
|
(amount, sku, name) = subscription_details.split('|')
|
128
|
{
|
129
|
:id => katello_subscription(line[ORGANIZATION], :name => name),
|
130
|
:quantity => (amount.nil? || amount == 'Automatic') ? 0 : amount
|
131
|
}
|
132
|
end
|
133
|
|
134
|
existing_subscriptions = @api.resource(:subscriptions).call(:index, {
|
135
|
'organization_id' => foreman_organization(:name => line[ORGANIZATION]),
|
136
|
'per_page' => 999999,
|
137
|
'activation_key_id' => activationkey['id']
|
138
|
})['results']
|
139
|
if existing_subscriptions.length > 0
|
140
|
@api.resource(:activation_keys).call(:remove_subscriptions, {
|
141
|
'id' => activationkey['id'],
|
142
|
'subscriptions' => existing_subscriptions
|
143
|
})
|
144
|
end
|
145
|
|
146
|
@api.resource(:activation_keys).call(:add_subscriptions, {
|
147
|
'id' => activationkey['id'],
|
148
|
'subscriptions' => subscriptions
|
149
|
})
|
150
|
end
|
151
|
end
|
152
|
|
153
|
def usage_limit(limit)
|
154
|
Integer(limit) rescue -1
|
155
|
end
|
156
|
end
|
157
|
end
|
158
|
end
|