Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update system from hybrid to payg if no more non free products active #1217

Merged
merged 9 commits into from
Aug 28, 2024
12 changes: 12 additions & 0 deletions engines/scc_proxy/lib/scc_proxy/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,9 @@ def scc_deactivate_product
# it is OK to remove it from SCC
response = SccProxy.deactivate_product_scc(auth, @product, @system.system_token)
handle_response(response)
# if the system does not have more non free products activated on SCC
# system should turn to hybrid
jesusbv marked this conversation as resolved.
Show resolved Hide resolved
@system.payg! if no_more_activations scc_systems_activations
jesusbv marked this conversation as resolved.
Show resolved Hide resolved
elsif result[:message].downcase.include?('unexpected error')
raise ActionController::TranslatedError.new(result[:message])
end
Expand All @@ -429,6 +432,15 @@ def handle_response(response)
raise ActionController::TranslatedError.new(error['error'])
end
end

def no_more_activations(scc_systems_activations)
jesusbv marked this conversation as resolved.
Show resolved Hide resolved
active_products_classes = scc_systems_activations.map do |act|
if act['status'].casecmp('active').zero? && act['service']['product']['product_class'] != @product.product_class
act['service']['product']['product_class']
end
end.flatten.compact
active_products_classes.empty?
end
end

Api::Connect::V3::Systems::SystemsController.class_eval do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,110 @@
end
end

context 'when SCC API suceeds for HYBRiD system' do
let(:product) do
FactoryBot.create(:product, :product_sles, :extension, :with_mirrored_repositories, :with_mirrored_extensions, :activated, system: system_hybrid)
end
let(:payload) do
{
identifier: product.identifier,
version: product.version,
arch: product.arch
}
end
let(:serialized_service_json) do
V3::ServiceSerializer.new(
product.service,
base_url: URI::HTTP.build({ scheme: response.request.scheme, host: response.request.host }).to_s
).to_json
end

let(:scc_systems_activations_url) { 'https://scc.suse.com/connect/systems/activations' }

before do
stub_request(:delete, scc_systems_products_url)
.to_return(
status: 200,
body: "{\"error\": \"Could not de-activate product \'#{product.friendly_name}\'\"}",
headers: {}
)
stub_request(:get, scc_systems_activations_url).to_return(status: 200, body: body_active, headers: {})
delete url, params: payload, headers: headers
end

context 'when only one product was active' do
let(:body_active) do
[{
id: 1,
regcode: '631dc51f',
name: 'Subscription 1',
type: 'FULL',
status: 'ACTIVE',
starts_at: 'null',
expires_at: DateTime.parse((Time.zone.today + 1).to_s),
system_limit: 6,
systems_count: 1,
service: {
product: {
id: system_hybrid.activations.first.product.id,
product_class: system_hybrid.activations.first.product.product_class
}
}
}].to_json
end

it 'makes the hybrid system payg' do
updated_system = System.find_by(login: system_hybrid.login)
expect(updated_system.payg?).to eq(true)
end
end

context 'when more activations are left' do
let(:body_active) do
[
{
id: 1,
regcode: '631dc51f',
name: 'Subscription 1',
type: 'FULL',
status: 'ACTIVE',
starts_at: 'null',
expires_at: DateTime.parse((Time.zone.today + 1).to_s),
system_limit: 6,
systems_count: 1,
service: {
product: {
id: system_hybrid.activations.first.product.id,
product_class: system_hybrid.activations.first.product.product_class
}
}
}, {
id: 2,
regcode: '631dc51f',
name: 'Subscription 1',
type: 'FULL',
status: 'ACTIVE',
starts_at: 'null',
expires_at: DateTime.parse((Time.zone.today + 1).to_s),
system_limit: 6,
systems_count: 1,
service: {
product: {
id: '30',
product_class: '23'
}
}
}
].to_json
end

it 'keeps the system as hybrid' do
updated_system = System.find_by(login: system_hybrid.login)
expect(updated_system.hybrid?).to eq(true)
end
end
end

context 'when SCC API returns an error' do
let(:product) do
FactoryBot.create(:product, :product_sles, :extension, :with_mirrored_repositories, :with_mirrored_extensions, :activated, system: system_hybrid)
Expand Down