From 5621fd78608fe7be6cce8fb1194b19e5efa08b61 Mon Sep 17 00:00:00 2001 From: d-m-u Date: Thu, 15 Nov 2018 08:45:39 -0500 Subject: [PATCH] Add owner to orch stack model provisioning --- app/models/orchestration_stack.rb | 8 ++++++ .../cloud_manager/orchestration_stack_spec.rb | 26 +++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/app/models/orchestration_stack.rb b/app/models/orchestration_stack.rb index eac4e8b16b7..2ca3092e12d 100644 --- a/app/models/orchestration_stack.rb +++ b/app/models/orchestration_stack.rb @@ -7,6 +7,7 @@ class OrchestrationStack < ApplicationRecord include NewWithTypeStiMixin include AsyncDeleteMixin include ProcessTasksMixin + include OwnershipMixin include RetirementMixin include TenantIdentityMixin include CustomActionsMixin @@ -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 @@ -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 @@ -93,6 +97,10 @@ def stdout(format = nil) format.nil? ? try(:raw_stdout) : try(:raw_stdout, format) end + def set_tenant_from_group + 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 = {}) diff --git a/spec/models/manageiq/providers/cloud_manager/orchestration_stack_spec.rb b/spec/models/manageiq/providers/cloud_manager/orchestration_stack_spec.rb index 34472efe9ac..34f944b91ca 100644 --- a/spec/models/manageiq/providers/cloud_manager/orchestration_stack_spec.rb +++ b/spec/models/manageiq/providers/cloud_manager/orchestration_stack_spec.rb @@ -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_ methods' do it 'defines a set of methods for vms' do expect(root_stack.direct_vms.size).to eq(1)