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

Add owner to orch stack model provisioning #18209

Merged
merged 1 commit into from
Nov 16, 2018
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/orchestration_stack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class OrchestrationStack < ApplicationRecord
include NewWithTypeStiMixin
include AsyncDeleteMixin
include ProcessTasksMixin
include OwnershipMixin
include RetirementMixin
include TenantIdentityMixin
include CustomActionsMixin
Expand All @@ -18,6 +19,7 @@ class OrchestrationStack < ApplicationRecord
has_ancestry

belongs_to :ext_management_system, :foreign_key => :ems_id
belongs_to :tenant

has_many :authentication_orchestration_stacks
has_many :authentications, :through => :authentication_orchestration_stacks
Expand Down Expand Up @@ -48,6 +50,8 @@ class OrchestrationStack < ApplicationRecord

virtual_column :stdout, :type => :string

before_validation :set_tenant_from_group

scope :without_type, ->(type) { where.not(:type => type) }

alias_method :orchestration_stack_parameters, :parameters
Expand Down Expand Up @@ -93,6 +97,10 @@ def stdout(format = nil)
format.nil? ? try(:raw_stdout) : try(:raw_stdout, format)
end

def set_tenant_from_group
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does the OwnershipMixin already do this for you?
(will this code work if you remove this validation and method?)

The logic behind the scenes for set_tenant_from_group is quite complex. Otherwise we end up running quite a few queries for code that is not altering the group.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope. It won't work if you remove the validation and method.

Also, yeah, they are beautiful tests, aren't they? Stolen directly from you.

self.tenant_id = miq_group.tenant_id if miq_group
end

private :directs_and_indirects

def self.create_stack(orchestration_manager, stack_name, template, options = {})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,32 @@
end
end

describe 'set_tenant_from_group' do
before { Tenant.seed }
let(:tenant1) { FactoryGirl.create(:tenant) }
let(:tenant2) { FactoryGirl.create(:tenant) }
let(:group1) { FactoryGirl.create(:miq_group, :tenant => tenant1) }
let(:group2) { FactoryGirl.create(:miq_group, :tenant => tenant2) }

it "assigns the tenant from the group" do
expect(FactoryGirl.create(:orchestration_stack, :miq_group => group1).tenant).to eq(tenant1)
end

it "assigns the tenant from the group_id" do
expect(FactoryGirl.create(:orchestration_stack, :miq_group_id => group1.id).tenant).to eq(tenant1)
end

it "assigns the tenant from the group over the tenant" do
expect(FactoryGirl.create(:orchestration_stack, :miq_group => group1, :tenant_id => tenant2).tenant).to eq(tenant1)
end

it "changes the tenant after changing the group" do
stack = FactoryGirl.create(:orchestration_stack, :miq_group => group1)
stack.update_attributes(:miq_group_id => group2.id)
expect(stack.tenant).to eq(tenant2)
end
end

describe 'direct_<resource> methods' do
it 'defines a set of methods for vms' do
expect(root_stack.direct_vms.size).to eq(1)
Expand Down