Class: Bosh::Director::DeploymentPlan::PlacementPlanner::IndexAssigner

Inherits:
Object
  • Object
show all
Defined in:
lib/bosh/director/deployment_plan/placement_planner/index_assigner.rb

Instance Method Summary collapse

Constructor Details

#initialize(deployment_model) ⇒ IndexAssigner

Returns a new instance of IndexAssigner.



6
7
8
# File 'lib/bosh/director/deployment_plan/placement_planner/index_assigner.rb', line 6

def initialize(deployment_model)
  @deployment_model = deployment_model
end

Instance Method Details

#assign_index(job_name, existing_instance_model = nil) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/bosh/director/deployment_plan/placement_planner/index_assigner.rb', line 10

def assign_index(job_name, existing_instance_model=nil)
  if existing_instance_model && existing_instance_model.job == job_name
    return existing_instance_model.index
  end

  sorted_indexes = Models::Instance.filter(job: job_name, deployment: @deployment_model).sort_by(&:index).map(&:index)
  if sorted_indexes.empty?
    0
  else
    find_unused_index(sorted_indexes)
  end
end

#find_unused_index(sorted_indexes) ⇒ Object



23
24
25
26
27
# File 'lib/bosh/director/deployment_plan/placement_planner/index_assigner.rb', line 23

def find_unused_index(sorted_indexes)
  sorted_indexes.unshift(-1)
  next_indexes = sorted_indexes.map { |i| i + 1 }
  (next_indexes - sorted_indexes).min
end