@@ -201,14 +201,19 @@ def parse_error(error_message, token = nil, email = nil)
201
201
error_message
202
202
end
203
203
204
- def get_scc_activations ( headers , system_token , mode )
204
+ def get_scc_activations ( headers , system )
205
205
auth = headers [ 'HTTP_AUTHORIZATION' ] if headers && headers . include? ( 'HTTP_AUTHORIZATION' )
206
206
uri = URI . parse ( SYSTEMS_ACTIVATIONS_URL )
207
207
http = Net ::HTTP . new ( uri . host , uri . port )
208
208
http . use_ssl = true
209
- uri . query = URI . encode_www_form ( { byos_mode : mode } )
210
- scc_request = Net ::HTTP ::Get . new ( uri . path , headers ( auth , system_token ) )
211
- http . request ( scc_request )
209
+ uri . query = URI . encode_www_form ( { byos_mode : system . proxy_byos_mode } )
210
+ scc_request = Net ::HTTP ::Get . new ( uri . path , headers ( auth , system . system_token ) )
211
+ response = http . request ( scc_request )
212
+ unless response . code_type == Net ::HTTPOK
213
+ Rails . logger . info "Could not get the system (#{ system . login } ) activations, error: #{ response . message } #{ response . code } "
214
+ raise ActionController ::TranslatedError . new ( response . body )
215
+ end
216
+ JSON . parse ( response . body )
212
217
end
213
218
214
219
def product_path_access ( x_original_uri , products_ids )
@@ -236,11 +241,17 @@ def product_class_access(scc_systems_activations, product)
236
241
end
237
242
end
238
243
244
+ # rubocop:disable Metrics/PerceivedComplexity
239
245
def activations_fail_state ( scc_systems_activations , headers , product = nil )
240
246
return SccProxy . product_class_access ( scc_systems_activations , product ) unless product . nil?
241
247
242
248
active_products_ids = scc_systems_activations . map { |act | act [ 'service' ] [ 'product' ] [ 'id' ] if act [ 'status' ] . casecmp ( 'active' ) . zero? } . flatten
243
249
x_original_uri = headers . fetch ( 'X-Original-URI' , '' )
250
+ # if there is no product info to compare the activations with
251
+ # probably means the query is to refresh credentials
252
+ # in any case, verification is true if ALL activations are ACTIVE
253
+ return { is_active : ( scc_systems_activations . length == active_products_ids . length ) } if x_original_uri . empty?
254
+
244
255
if SccProxy . product_path_access ( x_original_uri , active_products_ids )
245
256
{ is_active : true }
246
257
else
@@ -265,15 +276,10 @@ def activations_fail_state(scc_systems_activations, headers, product = nil)
265
276
end
266
277
end
267
278
end
279
+ # rubocop:enable Metrics/PerceivedComplexity
268
280
269
- def scc_check_subscription_expiration ( headers , login , system_token , logger , mode , product = nil ) # rubocop:disable Metrics/ParameterLists
270
- response = SccProxy . get_scc_activations ( headers , system_token , mode )
271
- unless response . code_type == Net ::HTTPOK
272
- logger . info "Could not get the system (#{ login } ) activations, error: #{ response . message } #{ response . code } "
273
- response . message = SccProxy . parse_error ( response . message ) if response . message . include? 'json'
274
- return { is_active : false , message : response . message }
275
- end
276
- scc_systems_activations = JSON . parse ( response . body )
281
+ def scc_check_subscription_expiration ( headers , system , product = nil )
282
+ scc_systems_activations = SccProxy . get_scc_activations ( headers , system )
277
283
return { is_active : false , message : 'No activations.' } if scc_systems_activations . empty?
278
284
279
285
no_status_products_ids = scc_systems_activations . map do |act |
@@ -282,6 +288,8 @@ def scc_check_subscription_expiration(headers, login, system_token, logger, mode
282
288
return { is_active : true } unless no_status_products_ids . all? ( &:nil? )
283
289
284
290
SccProxy . activations_fail_state ( scc_systems_activations , headers , product )
291
+ rescue StandardError
292
+ { is_active : false , message : 'Could not check the activations from SCC' }
285
293
end
286
294
287
295
def scc_upgrade ( auth , product , system_login , mode , logger )
@@ -299,7 +307,6 @@ def scc_upgrade(auth, product, system_login, mode, logger)
299
307
end
300
308
end
301
309
302
- # rubocop:disable Metrics/ClassLength
303
310
class Engine < ::Rails ::Engine
304
311
isolate_namespace SccProxy
305
312
config . generators . api_only = true
@@ -440,26 +447,17 @@ def scc_deactivate_product
440
447
elsif @system . hybrid? && @product . extension?
441
448
# check if product is on SCC and
442
449
# if it is -> de-activate it
443
- scc_systems_activations = find_hybrid_activations_on_scc ( request . headers )
444
- if scc_systems_activations . map { |act | act [ 'service' ] [ 'product' ] [ 'id' ] == @product . id } . present?
450
+ scc_hybrid_system_activations = SccProxy . get_scc_activations ( headers , @system )
451
+ if scc_hybrid_system_activations . map { |act | act [ 'service' ] [ 'product' ] [ 'id' ] == @product . id } . present?
445
452
# if product is found on SCC, regardless of the state
446
453
# it is OK to remove it from SCC
447
454
SccProxy . deactivate_product_scc ( auth , @product , @system . system_token , logger )
448
- make_system_payg ( auth ) if scc_systems_activations . reject { |act | act [ 'service' ] [ 'product' ] [ 'id' ] == @product . id } . blank?
455
+ make_system_payg ( auth ) if scc_hybrid_system_activations . reject { |act | act [ 'service' ] [ 'product' ] [ 'id' ] == @product . id } . blank?
449
456
end
450
457
end
451
458
logger . info "Product '#{ @product . friendly_name } ' successfully deactivated from SCC"
452
459
end
453
460
454
- def find_hybrid_activations_on_scc ( headers )
455
- response = SccProxy . get_scc_activations ( headers , @system . system_token , @system . proxy_byos_mode )
456
- unless response . code_type == Net ::HTTPOK
457
- logger . info "Could not get the system (#{ @system . login } ) activations, error: #{ response . message } #{ response . code } "
458
- raise ActionController ::TranslatedError . new ( response . body )
459
- end
460
- JSON . parse ( response . body )
461
- end
462
-
463
461
def make_system_payg ( auth )
464
462
# if the system does not have more products activated on SCC
465
463
# switch it back to payg
@@ -542,6 +540,5 @@ def get_system(systems)
542
540
end
543
541
end
544
542
end
545
- # rubocop:enable Metrics/ClassLength
546
543
end
547
544
# rubocop:enable Metrics/ModuleLength
0 commit comments