Class: Sfn::StateMachine

Inherits:
Object
  • Object
show all
Defined in:
lib/sfn/state_machine.rb

Constant Summary collapse

ROLE =
'arn:aws:iam::123456789012:role/DummyRole'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, variables = {}, arn = nil) ⇒ StateMachine

Returns a new instance of StateMachine.



30
31
32
33
34
35
36
# File 'lib/sfn/state_machine.rb', line 30

def initialize(name, variables = {}, arn = nil)
  self.path = "#{Sfn.configuration.definition_path}/#{name}.json"
  self.name = name.gsub('/','-')
  self.variables = (variables || {}).stringify_keys
  self.arn = arn || self.class.find_by_name(self.name)&.arn || create_state_machine
  self.executions = {}
end

Instance Attribute Details

#arnObject

Returns the value of attribute arn.



12
13
14
# File 'lib/sfn/state_machine.rb', line 12

def arn
  @arn
end

#definitionObject

Returns the value of attribute definition.



12
13
14
# File 'lib/sfn/state_machine.rb', line 12

def definition
  @definition
end

#execution_arnObject

Returns the value of attribute execution_arn.



12
13
14
# File 'lib/sfn/state_machine.rb', line 12

def execution_arn
  @execution_arn
end

#executionsObject

Returns the value of attribute executions.



12
13
14
# File 'lib/sfn/state_machine.rb', line 12

def executions
  @executions
end

#nameObject

Returns the value of attribute name.



12
13
14
# File 'lib/sfn/state_machine.rb', line 12

def name
  @name
end

#pathObject

Returns the value of attribute path.



12
13
14
# File 'lib/sfn/state_machine.rb', line 12

def path
  @path
end

#variablesObject

Returns the value of attribute variables.



12
13
14
# File 'lib/sfn/state_machine.rb', line 12

def variables
  @variables
end

Class Method Details

.allObject



14
15
16
# File 'lib/sfn/state_machine.rb', line 14

def self.all
  Collection.instance.all.map { |sf| new(sf['name'], nil, sf['stateMachineArn']) }
end

.destroy_allObject



18
19
20
# File 'lib/sfn/state_machine.rb', line 18

def self.destroy_all
  all.each(&:destroy)
end

.find_by_arn(arn) ⇒ Object



26
27
28
# File 'lib/sfn/state_machine.rb', line 26

def self.find_by_arn(arn)
  all.find { |sf| sf.arn == arn }
end

.find_by_name(name) ⇒ Object



22
23
24
# File 'lib/sfn/state_machine.rb', line 22

def self.find_by_name(name)
  all.find { |sf| sf.name == name }
end

Instance Method Details

#destroyObject



38
39
40
41
42
# File 'lib/sfn/state_machine.rb', line 38

def destroy
  AwsCli.run('stepfunctions', 'delete-state-machine',
             { 'state-machine-arn': arn })
  Collection.instance.delete_by_arn(arn)
end

#dry_run(mock_data = {}, input = {}, test_name = nil) ⇒ Object



44
45
46
# File 'lib/sfn/state_machine.rb', line 44

def dry_run(mock_data = {}, input = {}, test_name = nil)
  execution = run(mock_data, input, test_name, true)
end

#run(mock_data = {}, input = {}, test_name = nil, dry_run = false) ⇒ Object



48
49
50
51
52
# File 'lib/sfn/state_machine.rb', line 48

def run(mock_data = {}, input = {}, test_name = nil, dry_run = false)
  test_name ||= OpenSSL::Digest::SHA512.digest(mock_data.merge({ input: input }).to_json)
  executions[test_name] ||= Execution.call(self, test_name, mock_data, input, dry_run)
  executions[test_name]
end

#to_hashObject



54
55
56
# File 'lib/sfn/state_machine.rb', line 54

def to_hash
  { 'stateMachineArn' => arn, 'name' => name }
end