1
|
require 'rest_client'
|
2
|
|
3
|
module HammerCLICsv
|
4
|
class CsvCommand
|
5
|
class SubscriptionsCommand < BaseCommand
|
6
|
include ::HammerCLICsv::Utils::Subscriptions
|
7
|
|
8
|
command_name 'subscriptions'
|
9
|
desc 'import or export subscriptions'
|
10
|
|
11
|
def self.supported?
|
12
|
true
|
13
|
end
|
14
|
|
15
|
option %w(--in-portal), :flag, _('Import subscription comment lines into portal'),
|
16
|
:hidden => true
|
17
|
option %w(--portal-username), 'PORTAL_USERNAME', 'Portal username',
|
18
|
:hidden => true
|
19
|
option %w(--portal-password), 'PORTAL_PASSWORD', 'Portal password',
|
20
|
:hidden => true
|
21
|
option %w(--portal-hock), 'PORTAL_HOCK', 'Portal subscription creation address',
|
22
|
:hidden => true
|
23
|
option %w(--portal), 'PORTAL', 'Portal subscription access address',
|
24
|
:default => "https://subscription.rhn.redhat.com:443",
|
25
|
:hidden => true
|
26
|
|
27
|
ORGANIZATION = 'Organization'
|
28
|
MANIFEST = 'Manifest File'
|
29
|
|
30
|
def export(csv)
|
31
|
csv << [NAME, ORGANIZATION, MANIFEST, SUBS_NAME, SUBS_QUANTITY, SUBS_SKU, SUBS_CONTRACT, SUBS_ACCOUNT, SUBS_START, SUBS_END]
|
32
|
@api.resource(:organizations).call(:index, {:per_page => 999999})['results'].each do |organization|
|
33
|
next if option_organization && organization['name'] != option_organization
|
34
|
organization = @api.resource(:organizations).call(:show, {'id' => organization['id']})
|
35
|
export_manifest(csv, organization)
|
36
|
export_subscriptions(csv, organization)
|
37
|
end
|
38
|
end
|
39
|
|
40
|
def export_manifest(csv, organization)
|
41
|
info = organization['owner_details']['upstreamConsumer']
|
42
|
return if info.nil?
|
43
|
|
44
|
csv << ["Manifest Name", organization['name'], info['name']]
|
45
|
csv << ["Manifest URL", organization['name'], "https://#{info['webUrl']}#{info['uuid']}"]
|
46
|
end
|
47
|
|
48
|
def export_subscriptions(csv, organization)
|
49
|
@api.resource(:subscriptions).call(:index, {
|
50
|
'per_page' => 999999,
|
51
|
'organization_id' => organization['id']
|
52
|
})['results'].each do |subscription|
|
53
|
next if subscription['product_id'].to_i != 0
|
54
|
name = subscription['host'].nil? ? "Subscription" : "Guest Subscription for Host '#{subscription['host']['name']}'"
|
55
|
quantity = subscription['quantity'] < 0 ? "Unlimited" : subscription['quantity']
|
56
|
csv << [name, organization['name'], nil, subscription['name'],
|
57
|
quantity, subscription['product_id'], subscription['contract_number'],
|
58
|
subscription['account_number'],
|
59
|
subscription['start_date'], subscription['end_date']]
|
60
|
end
|
61
|
end
|
62
|
|
63
|
def import
|
64
|
if option_in_portal?
|
65
|
import_into_portal
|
66
|
else
|
67
|
thread_import do |line|
|
68
|
if line[NAME] == 'Manifest' && line[MANIFEST] && !line[MANIFEST].empty?
|
69
|
import_manifest(line[ORGANIZATION], line[MANIFEST])
|
70
|
end
|
71
|
end
|
72
|
end
|
73
|
end
|
74
|
|
75
|
def import_manifest(organization_name, filename)
|
76
|
return if option_organization && organization_name != option_organization
|
77
|
print(_("Importing manifest '%{filename}' into organization '%{organization}'...") % {:filename => filename, :organization => organization_name}) if option_verbose?
|
78
|
args = %W{
|
79
|
--server #{ @server } --username #{ @username } --password #{ @password }
|
80
|
subscription upload --file #{ filename }
|
81
|
--organization-id #{ foreman_organization(:name => organization_name) }
|
82
|
}
|
83
|
hammer.run(args)
|
84
|
puts(_("done")) if option_verbose?
|
85
|
end
|
86
|
|
87
|
def import_into_portal
|
88
|
raise _("--portal-username and --portal-password required") unless option_portal_username && option_portal_password
|
89
|
raise _("--portal required") unless option_portal
|
90
|
|
91
|
@manifests = {}
|
92
|
|
93
|
thread_import do |line|
|
94
|
return if option_organization && line[ORGANIZATION] != option_organization
|
95
|
|
96
|
@manifests[line[ORGANIZATION]] ||= {}
|
97
|
import_subscription(line)
|
98
|
end
|
99
|
|
100
|
@manifests.each do |organization, manifest|
|
101
|
print _("Downloading manifest for organization '%{organization}...") % {:organization => organization} if option_verbose?
|
102
|
api = rest_client("/subscription/consumers/#{manifest[:manifest]['uuid']}/export")
|
103
|
data = api.get({'accept' => 'application/zip'})
|
104
|
filename = manifest[:file] || "#{manifest[:name]}.zip"
|
105
|
print _("writing to file '%{filename}'...") % {:filename => filename} if option_verbose?
|
106
|
File.open(filename, 'w') do |f|
|
107
|
f.binmode
|
108
|
f.write data
|
109
|
end
|
110
|
puts _("done") if option_verbose?
|
111
|
import_manifest(organization, filename)
|
112
|
end
|
113
|
end
|
114
|
|
115
|
def import_subscription(line)
|
116
|
case line[NAME]
|
117
|
when "Manifest Name"
|
118
|
print _("Checking manifest '%{name}'...") % {:name => line[MANIFEST]} if option_verbose?
|
119
|
@manifests[line[ORGANIZATION]][:name] = line[MANIFEST]
|
120
|
@manifests[line[ORGANIZATION]][:manifest] = get_or_create_manifest(line)
|
121
|
puts _("done") if option_verbose?
|
122
|
when "Manifest URL"
|
123
|
|
124
|
when "Manifest"
|
125
|
@manifests[line[ORGANIZATION]][:file] = line[MANIFEST]
|
126
|
when "Subscription"
|
127
|
manifest = @manifests[line[ORGANIZATION]][:manifest]
|
128
|
raise _('Manifest Name row is required before updating from Subscription rows') unless manifest
|
129
|
line[SUBS_QUANTITY] = line[SUBS_QUANTITY].to_i
|
130
|
add_subscription(line, manifest)
|
131
|
else
|
132
|
|
133
|
end
|
134
|
end
|
135
|
|
136
|
def add_subscription(line, manifest)
|
137
|
if find_existing_subscription(line, manifest)
|
138
|
puts _("'%{name}' of quantity %{quantity} already attached") %
|
139
|
{:name => line[SUBS_NAME], :quantity => line[SUBS_QUANTITY]} if option_verbose?
|
140
|
return
|
141
|
end
|
142
|
print _("Attaching '%{name}' of quantity %{quantity}...") %
|
143
|
{:name => line[SUBS_NAME], :quantity => line[SUBS_QUANTITY]} if option_verbose?
|
144
|
manifest['available_subscriptions'] ||= get_available_subscriptions(manifest)
|
145
|
attach_subscription(line, manifest)
|
146
|
puts _('done')
|
147
|
end
|
148
|
|
149
|
def attach_subscription(line, manifest)
|
150
|
manifest['available_subscriptions'].each do |subscription|
|
151
|
if subscription['productId'] == line[SUBS_SKU] && subscription['quantity'] >= line[SUBS_QUANTITY]
|
152
|
api = rest_client("/subscription/consumers/#{manifest['uuid']}/entitlements?pool=#{subscription['id']}&quantity=#{line[SUBS_QUANTITY]}")
|
153
|
results = api.post({}.to_json)
|
154
|
subscription['quantity'] -= line[SUBS_QUANTITY]
|
155
|
return
|
156
|
end
|
157
|
end
|
158
|
print _('subscription unavailable...')
|
159
|
end
|
160
|
|
161
|
def get_available_subscriptions(manifest)
|
162
|
api = rest_client("/subscription/pools/?consumer=#{manifest['uuid']}&listall=false")
|
163
|
JSON.parse(api.get)
|
164
|
end
|
165
|
|
166
|
def find_existing_subscription(line, manifest)
|
167
|
manifest['subscriptions'].each do |subscription|
|
168
|
if !subscription['csv_matched'] && subscription['pool']['productId'] == line[SUBS_SKU] && subscription['quantity'] == line[SUBS_QUANTITY]
|
169
|
subscription['csv_matched'] = true
|
170
|
return true
|
171
|
end
|
172
|
end
|
173
|
false
|
174
|
end
|
175
|
|
176
|
def get_or_create_manifest(line)
|
177
|
manifest = get_existing_manifest(line)
|
178
|
if manifest
|
179
|
if manifest['subscriptions'].nil?
|
180
|
api = rest_client("/subscription/consumers/#{manifest['uuid']}/entitlements")
|
181
|
results = JSON.parse(api.get)
|
182
|
manifest['subscriptions'] = results
|
183
|
end
|
184
|
else
|
185
|
api = rest_client("/subscription/consumers?owner=#{@manifests[line[ORGANIZATION]][:owner]}")
|
186
|
body = {
|
187
|
'name' => line[MANIFEST],
|
188
|
'type' => 'satellite',
|
189
|
'facts' => {
|
190
|
'distributor_version' => 'sat-6.0',
|
191
|
'system.certificate_version' => '3.2'
|
192
|
}
|
193
|
}
|
194
|
results = api.post(body.to_json,
|
195
|
{'accept' => 'json', 'content_type' => 'application/json'}
|
196
|
)
|
197
|
manifest = JSON.parse(results)
|
198
|
manifest['subscriptions'] = []
|
199
|
@manifests[line[ORGANIZATION]][:manifest] = manifest
|
200
|
end
|
201
|
manifest
|
202
|
end
|
203
|
|
204
|
def get_existing_manifest(line)
|
205
|
return @manifests[line[ORGANIZATION]][:manifest] if @manifests[line[ORGANIZATION]][:manifest]
|
206
|
|
207
|
unless @manifests[line[ORGANIZATION]][:owner]
|
208
|
api = rest_client("/subscription/users/#{option_portal_username}/owners")
|
209
|
@manifests[line[ORGANIZATION]][:owner] = JSON.parse(api.get)[0]['key']
|
210
|
end
|
211
|
|
212
|
api = rest_client("/subscription/owners/#{@manifests[line[ORGANIZATION]][:owner]}/consumers?type=satellite")
|
213
|
response = JSON.parse(api.get).each do |manifest|
|
214
|
if manifest['name'] == @manifests[line[ORGANIZATION]][:name]
|
215
|
@manifests[line[ORGANIZATION]][:manifest] = manifest
|
216
|
break
|
217
|
end
|
218
|
end
|
219
|
@manifests[line[ORGANIZATION]][:manifest]
|
220
|
end
|
221
|
|
222
|
def rest_client(path)
|
223
|
options = {
|
224
|
:headers => {
|
225
|
'accept' => 'application/json',
|
226
|
'accept-language' => HammerCLI::I18n.locale,
|
227
|
'content-type' => 'application/json'
|
228
|
},
|
229
|
:user => option_portal_username,
|
230
|
:password => option_portal_password,
|
231
|
:verify_ssl => OpenSSL::SSL::VERIFY_NONE
|
232
|
}
|
233
|
|
234
|
RestClient::Resource.new(option_portal + path, options)
|
235
|
end
|
236
|
end
|
237
|
end
|
238
|
end
|