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

[GAPRINDASHVILI] Add ArchivedMixin to ServiceTemplate #17481

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions app/models/mixins/archived_mixin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ def active?
deleted_on.nil?
end

def archive!
update_attributes!(:deleted_on => Time.now.utc)
end

def unarchive!
update_attributes!(:deleted_on => nil)
end

# Needed for metrics
def my_zone
if ext_management_system.present?
Expand Down
13 changes: 13 additions & 0 deletions app/models/service_template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,15 @@ class ServiceTemplate < ApplicationRecord
include OwnershipMixin
include NewWithTypeStiMixin
include TenancyMixin
include ArchivedMixin
include_concern 'Filter'

include ReservedMixin
reserve_attribute :deleted_on, :datetime

scope :archived, -> { includes(:reserved_rec).reject { |st| st.deleted_on.nil? } }
scope :active, -> { includes(:reserved_rec).select { |st| st.deleted_on.nil? } }

belongs_to :tenant
# # These relationships are used to specify children spawned from a parent service
# has_many :child_services, :class_name => "ServiceTemplate", :foreign_key => :service_template_id
Expand All @@ -56,6 +63,7 @@ class ServiceTemplate < ApplicationRecord
has_many :dialogs, -> { distinct }, :through => :resource_actions

has_many :miq_requests, :as => :source, :dependent => :nullify
has_many :active_requests, -> { where(:request_state => %w(active queued)) }, :as => :source, :class_name => "MiqRequest"

virtual_column :type_display, :type => :string
virtual_column :template_valid, :type => :boolean
Expand Down Expand Up @@ -151,6 +159,11 @@ def destroy
super
end

def archive
raise _("Cannot archive while in use") unless active_requests.empty?
archive!
end

def request_class
ServiceTemplateProvisionRequest
end
Expand Down
34 changes: 34 additions & 0 deletions spec/models/service_template_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -856,6 +856,40 @@
)
end
end

context "#archive" do
let(:service_template) { FactoryGirl.create(:service_template, :miq_requests => miq_requests) }
context "with no MiqRequests" do
let(:miq_requests) { [] }

it "archives the service_template" do
service_template.archive
expect(service_template.reload.archived?).to be_truthy
end
end

context "with no active MiqRequests" do
let(:miq_requests) { [FactoryGirl.create(:service_template_provision_request, :request_state => "finished")] }
it "archives the service_template" do
service_template.archive
expect(service_template.reload.archived?).to be_truthy
end
end

context "with an active MiqRequest" do
let(:miq_requests) do
[
FactoryGirl.create(:service_template_provision_request, :request_state => "finished"),
FactoryGirl.create(:service_template_provision_request, :request_state => "queued"),
]
end

it "archives the service_template" do
expect { service_template.archive }.to raise_error("Cannot archive while in use")
expect(service_template.reload.archived?).to be_falsy
end
end
end
end

def add_and_save_service(p, c)
Expand Down