Skip to content

Commit dfe8f3c

Browse files
committed
Replace "magic" strings with constants or StringInquirer
Rather than comparing various strings this commit shifts to using constants and/or ActiveSupport's `StringInquirer` Signed-off-by: Nishad Mathur <nishad.mathur@broadcom.com>
1 parent b85d560 commit dfe8f3c

6 files changed

Lines changed: 49 additions & 40 deletions

File tree

src/bosh-director/lib/bosh/director/deployment_plan/instance.rb

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,19 @@
11
require 'securerandom'
2+
require 'active_support/core_ext/string/inquiry'
23

34
module Bosh::Director
45
module DeploymentPlan
56
# Represents a single Instance Group instance.
67
class Instance
8+
RESTART = 'restart'.freeze
9+
RECREATE = 'recreate'.freeze
10+
STARTED = 'started'.freeze
11+
12+
VIRTUAL_STATE_TO_STATE = {
13+
RECREATE => STARTED,
14+
RESTART => STARTED
15+
}
16+
717
# @return [Integer] Instance index
818
attr_reader :index
919

@@ -25,9 +35,6 @@ class Instance
2535
attr_accessor :desired_variable_set
2636
attr_reader :previous_variable_set
2737

28-
# @return [String] job state
29-
attr_reader :virtual_state
30-
3138
attr_reader :availability_zone
3239

3340
attr_reader :existing_network_reservations
@@ -259,7 +266,7 @@ def current_packages
259266
end
260267

261268
def current_job_state
262-
@current_state['job_state']
269+
@current_state['job_state'].to_s.inquiry
263270
end
264271

265272
def current_networks
@@ -297,14 +304,11 @@ def update_variable_set
297304
end
298305

299306
def state
300-
case @virtual_state
301-
when 'recreate'
302-
'started'
303-
when 'restart'
304-
'started'
305-
else
306-
@virtual_state
307-
end
307+
VIRTUAL_STATE_TO_STATE.fetch(virtual_state, virtual_state)
308+
end
309+
310+
def virtual_state
311+
@virtual_state.to_s.inquiry
308312
end
309313

310314
##

src/bosh-director/lib/bosh/director/deployment_plan/instance_plan.rb

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def should_create_swap_delete?
5959
def unresponsive_agent?
6060
return false if @instance.nil?
6161

62-
@instance.current_job_state == 'unresponsive'
62+
@instance.current_job_state.unresponsive?
6363
end
6464

6565
##
@@ -118,7 +118,7 @@ def should_be_ignored?
118118
end
119119

120120
def restart_requested?
121-
@instance.virtual_state == 'restart'
121+
@instance.virtual_state.restart?
122122
end
123123

124124
def recreation_requested?
@@ -129,7 +129,7 @@ def recreation_requested?
129129
@logger.debug("#{__method__} instance should be recreated because of unresponsive agent")
130130
true
131131
else
132-
@instance.virtual_state == 'recreate'
132+
@instance.virtual_state.recreate?
133133
end
134134
end
135135

@@ -186,16 +186,15 @@ def network_settings_changed?
186186
end
187187

188188
def state_changed?
189-
if instance.state == 'detached' &&
190-
existing_instance.state != instance.state
189+
if instance.state.detached? && existing_instance.state != instance.state
191190
@logger.debug("Instance '#{instance}' needs to be detached")
192191
return true
193192
end
194193

195194
return true if unresponsive_agent?
196195

197-
if instance.state == 'stopped' && instance.current_job_state == 'running' ||
198-
instance.state == 'started' && instance.current_job_state != 'running'
196+
if instance.state.stopped? && instance.current_job_state.running? ||
197+
instance.state.started? && !instance.current_job_state.running?
199198
@logger.debug("Instance state is '#{instance.state}' and agent reports '#{instance.current_job_state}'")
200199
return true
201200
end
@@ -399,15 +398,11 @@ def packages_changed?
399398
end
400399

401400
def already_detached?
402-
return false if new?
403-
404-
@existing_instance.state == 'detached'
401+
new? ? false : @existing_instance.detached?
405402
end
406403

407404
def needs_disk?
408-
instance_group = @desired_instance.instance_group
409-
410-
instance_group.persistent_disk_collection.needs_disk?
405+
@desired_instance.instance_group.persistent_disk_collection.needs_disk?
411406
end
412407

413408
def persist_current_spec

src/bosh-director/lib/bosh/director/instance_updater/update_procedure.rb

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,22 @@
11
module Bosh::Director
22
class InstanceUpdater
33
class UpdateProcedure
4+
STARTED = 'started'.freeze
5+
STOPPED = 'stopped'.freeze
6+
DETACHED = 'detached'.freeze
7+
8+
RECREATE = 'recreate'.freeze
9+
CREATE = 'create'.freeze
10+
START = 'start'.freeze
11+
STOP = 'stop'.freeze
12+
UPDATE = 'update'.freeze
13+
14+
VIRTUAL_STATE_TO_ACTION_MAPPING = {
15+
STARTED => START,
16+
STOPPED => STOP,
17+
DETACHED => STOP,
18+
}
19+
420
attr_reader :instance, :instance_plan, :options, :instance_report, :action, :context
521

622
def initialize(instance,
@@ -178,21 +194,13 @@ def deleting_vm?
178194

179195
def calculate_action
180196
if restarting?
181-
names = {
182-
'started' => 'start',
183-
'stopped' => 'stop',
184-
'detached' => 'stop',
185-
}
186-
187-
raw_name = instance_plan.instance.virtual_state
188-
return names[raw_name] if names.key? raw_name
189-
190-
return raw_name
197+
return VIRTUAL_STATE_TO_ACTION_MAPPING.fetch(instance_plan.instance.virtual_state,
198+
instance_plan.instance.virtual_state)
191199
end
192200

193-
return 'create' if instance_plan.new?
201+
return CREATE if instance_plan.new?
194202

195-
@needs_recreate ? 'recreate' : 'update'
203+
@needs_recreate ? RECREATE : UPDATE
196204
end
197205

198206
def restarting?

src/bosh-director/spec/unit/bosh/director/errand/errand_provider_spec.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,11 @@ module Bosh::Director
2929
let(:template_blob_cache) { instance_double(Bosh::Director::Core::Templates::TemplateBlobCache) }
3030
let(:runner) { instance_double(Errand::Runner) }
3131
let(:errand_step) { instance_double(Errand::LifecycleErrandStep) }
32+
let(:current_job_state) { 'dummy'}
3233
let(:instance) do
3334
instance_double(
3435
DeploymentPlan::Instance,
35-
current_job_state: double(:current_job_state),
36+
current_job_state: current_job_state.inquiry,
3637
uuid: instance_model.uuid,
3738
model: instance_model,
3839
)

src/bosh-director/spec/unit/bosh/director/starter_spec.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require 'spec_helper'
2+
require 'active_support/core_ext/string/inquiry'
23

34
module Bosh::Director
45
describe Starter do
@@ -19,7 +20,7 @@ module Bosh::Director
1920
let(:instance) do
2021
instance_double(
2122
DeploymentPlan::Instance,
22-
current_job_state: current_job_state,
23+
current_job_state: current_job_state.inquiry,
2324
)
2425
end
2526

src/bosh-director/spec/unit/bosh/director/stopper_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ module Bosh::Director
3434
rendered_templates_archive: nil,
3535
configuration_hash: { 'fake-spec' => true },
3636
template_hashes: [],
37-
current_job_state: current_job_state,
37+
current_job_state: current_job_state.inquiry,
3838
deployment_model: deployment_model,
3939
)
4040
end

0 commit comments

Comments
 (0)