Class: ElasticWhenever::Task::Rule

Inherits:
Object
  • Object
show all
Defined in:
lib/elastic_whenever/task/rule.rb

Defined Under Namespace

Classes: UnsupportedOptionException

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(option, name:, expression:, description:, client: nil) ⇒ Rule

Returns a new instance of Rule.



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/elastic_whenever/task/rule.rb', line 41

def initialize(option, name:, expression:, description:, client: nil)
  @option = option
  @name = name
  @expression = expression
  @description = description
  if client != nil
    @client = client
  else
    @client = option.cloudwatch_events_client
  end
end

Instance Attribute Details

#descriptionObject (readonly)

Returns the value of attribute description.



7
8
9
# File 'lib/elastic_whenever/task/rule.rb', line 7

def description
  @description
end

#expressionObject (readonly)

Returns the value of attribute expression.



6
7
8
# File 'lib/elastic_whenever/task/rule.rb', line 6

def expression
  @expression
end

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/elastic_whenever/task/rule.rb', line 5

def name
  @name
end

#optionObject (readonly)

Returns the value of attribute option.



4
5
6
# File 'lib/elastic_whenever/task/rule.rb', line 4

def option
  @option
end

Class Method Details

.convert(option, expression, command) ⇒ Object



32
33
34
35
36
37
38
39
# File 'lib/elastic_whenever/task/rule.rb', line 32

def self.convert(option, expression, command)
  self.new(
    option,
    name: rule_name(option, expression, command),
    expression: expression,
    description: rule_description(option.identifier, expression, command)
  )
end

.fetch(option, rules: [], next_token: nil) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/elastic_whenever/task/rule.rb', line 11

def self.fetch(option, rules: [], next_token: nil)
  client = option.cloudwatch_events_client
  prefix = option.identifier

  response = client.list_rules(name_prefix: prefix, next_token: next_token)
  response.rules.each do |rule|
    rules << self.new(
      option,
      name: rule.name,
      expression: rule.schedule_expression,
      description: rule.description,
      client: client
    )
  end
  if response.next_token.nil?
    rules
  else
    fetch(option, rules: rules, next_token: response.next_token)
  end
end

Instance Method Details

#createObject



53
54
55
56
57
58
59
60
61
62
# File 'lib/elastic_whenever/task/rule.rb', line 53

def create
  # See https://docs.aws.amazon.com/eventbridge/latest/APIReference/API_PutRule.html#API_PutRule_RequestSyntax
  Logger.instance.message("Creating Rule: #{name} #{expression}")
  client.put_rule(
    name: name,
    schedule_expression: expression,
    description: truncate(description, 512),
    state: option.rule_state,
  )
end

#deleteObject



64
65
66
67
68
69
# File 'lib/elastic_whenever/task/rule.rb', line 64

def delete
  targets = client.list_targets_by_rule(rule: name).targets
  client.remove_targets(rule: name, ids: targets.map(&:id)) unless targets.empty?
  Logger.instance.message("Removing Rule: #{name}")
  client.delete_rule(name: name)
end