diff --git a/src/bosh-director/lib/bosh/director/api/controllers/deployments_controller.rb b/src/bosh-director/lib/bosh/director/api/controllers/deployments_controller.rb index 9718542b7b0..ef6bf195592 100644 --- a/src/bosh-director/lib/bosh/director/api/controllers/deployments_controller.rb +++ b/src/bosh-director/lib/bosh/director/api/controllers/deployments_controller.rb @@ -198,9 +198,9 @@ def initialize(config) deployments = @deployment_manager.all_by_name_asc .select { |deployment| @permission_authorizer.is_granted?(deployment, :read, token_scopes) } .map do |deployment| - cloud_config = if deployment.cloud_config.nil? + cloud_config = if deployment.cloud_config_id.nil? 'none' - elsif deployment.cloud_config == latest_cloud_config + elsif deployment.cloud_config_id == latest_cloud_config.id 'latest' else 'outdated' @@ -208,7 +208,7 @@ def initialize(config) { 'name' => deployment.name, - 'releases' => deployment.release_versions.map do |rv| + 'releases' => deployment.release_versions.sort { |a,b| [a.release.name, a.version] <=> [b.release.name, b.version] } .map do |rv| { 'name' => rv.release.name, 'version' => rv.version.to_s diff --git a/src/bosh-director/lib/bosh/director/api/deployment_manager.rb b/src/bosh-director/lib/bosh/director/api/deployment_manager.rb index 08035b3d79b..5973483b1d8 100644 --- a/src/bosh-director/lib/bosh/director/api/deployment_manager.rb +++ b/src/bosh-director/lib/bosh/director/api/deployment_manager.rb @@ -12,7 +12,10 @@ def find_by_name(name) end def all_by_name_asc - Bosh::Director::Models::Deployment.order_by(Sequel.asc(:name)).all + Bosh::Director::Models::Deployment + .eager(:teams, :stemcells, release_versions: :release) + .order_by(Sequel.asc(:name)) + .all end def create_deployment(username, manifest_text, cloud_config, runtime_configs, deployment, options = {}, context_id = '') diff --git a/src/bosh-director/lib/bosh/director/models/deployment.rb b/src/bosh-director/lib/bosh/director/models/deployment.rb index 485ba3f1199..b6b646d9728 100644 --- a/src/bosh-director/lib/bosh/director/models/deployment.rb +++ b/src/bosh-director/lib/bosh/director/models/deployment.rb @@ -1,6 +1,6 @@ module Bosh::Director::Models class Deployment < Sequel::Model(Bosh::Director::Config.db) - many_to_many :stemcells + many_to_many :stemcells, order: [Sequel.asc(:name), Sequel.asc(:version)] many_to_many :release_versions one_to_many :job_instances, :class => "Bosh::Director::Models::Instance" one_to_many :instances @@ -8,7 +8,7 @@ class Deployment < Sequel::Model(Bosh::Director::Config.db) one_to_many :problems, :class => "Bosh::Director::Models::DeploymentProblem" many_to_one :cloud_config many_to_many :runtime_configs - many_to_many :teams + many_to_many :teams, order: Sequel.asc(:name) one_to_many :variable_sets, :class => 'Bosh::Director::Models::VariableSet' def validate diff --git a/src/bosh-director/spec/unit/api/controllers/deployments_controller_spec.rb b/src/bosh-director/spec/unit/api/controllers/deployments_controller_spec.rb index 72ed54b1cd4..6c1dd4796f8 100644 --- a/src/bosh-director/spec/unit/api/controllers/deployments_controller_spec.rb +++ b/src/bosh-director/spec/unit/api/controllers/deployments_controller_spec.rb @@ -575,6 +575,9 @@ def manifest_with_errand(deployment_name='errand') end describe 'listing deployments' do + let(:cloud_config) { Models::CloudConfig.make } + let(:deployment) { Models::Deployment.make(name: 'b', cloud_config_id: cloud_config.id) } + before { basic_authorize 'reader', 'reader' } it 'lists deployment info in deployment name order' do @@ -662,6 +665,37 @@ def manifest_with_errand(deployment_name='errand') } ]) end + + it 'orders the associations' do + release2 = Models::Release.make(name: 'r2') + release1 = Models::Release.make(name: 'r1') + + deployment.add_release_version(Models::ReleaseVersion.make(version: '3', release_id: release1.id)) + deployment.add_release_version(Models::ReleaseVersion.make(version: '2', release_id: release1.id)) + deployment.add_release_version(Models::ReleaseVersion.make(version: '1', release_id: release2.id)) + + deployment.add_team(Models::Team.make(name: 'team2')) + deployment.add_team(Models::Team.make(name: 'team3')) + deployment.add_team(Models::Team.make(name: 'team1')) + + deployment.add_stemcell(Models::Stemcell.make(name: 'stemcell2', version: '4')) + deployment.add_stemcell(Models::Stemcell.make(name: 'stemcell1', version: '1')) + deployment.add_stemcell(Models::Stemcell.make(name: 'stemcell2', version: '3')) + deployment.add_stemcell(Models::Stemcell.make(name: 'stemcell3', version: '2')) + + get '/', {}, {} + expect(last_response.status).to eq(200) + + body = JSON.parse(last_response.body) + + expect(body.first['releases']).to eq([{'name' => 'r1', 'version' => '2'}, {'name' => 'r1', 'version' => '3'}, {'name' => 'r2', 'version' => '1'}]) + expect(body.first['stemcells']).to eq([ + {'name' => 'stemcell1', 'version' => '1'}, + {'name' => 'stemcell2', 'version' => '3'}, + {'name' => 'stemcell2', 'version' => '4'}, + {'name' => 'stemcell3', 'version' => '2'}]) + expect(body.first['teams']).to eq(%w(team1 team2 team3)) + end end describe 'getting deployment info' do diff --git a/src/bosh-director/spec/unit/api/deployment_manager_spec.rb b/src/bosh-director/spec/unit/api/deployment_manager_spec.rb index 97cf10ca630..632c8b1961a 100644 --- a/src/bosh-director/spec/unit/api/deployment_manager_spec.rb +++ b/src/bosh-director/spec/unit/api/deployment_manager_spec.rb @@ -63,5 +63,35 @@ module Bosh::Director expect(subject.find_by_name(deployment.name)).to eq deployment end end + + describe '#all_by_name_asc' do + + before do + cloud_config = Models::CloudConfig.make + release = Models::Release.make + deployment = Models::Deployment.make(name: 'b', cloud_config_id: cloud_config.id) + release_version = Models::ReleaseVersion.make(release_id: release.id) + deployment.add_release_version(release_version) + end + + it 'eagerly loads :stemcells, :release_versions, :teams' do + allow(Bosh::Director::Config.db).to receive(:execute).and_call_original + + deployments = subject.all_by_name_asc + + deployments.first.stemcells + deployments.first.release_versions.map(&:release) + deployments.first.teams + + expect(Bosh::Director::Config.db).to have_received(:execute).exactly(5).times + end + + it 'lists all deployments in alphabetic order' do + Models::Deployment.make(name: 'c') + Models::Deployment.make(name: 'a') + + expect(subject.all_by_name_asc.map(&:name)).to eq(['a', 'b', 'c']) + end + end end end