Class: CycloneLariat::Resources::Topic

Inherits:
Object
  • Object
show all
Defined in:
lib/cyclone_lariat/resources/topic.rb

Constant Summary collapse

SNS_SUFFIX =
:fanout

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(instance:, kind:, region:, account_id:, publisher:, type:, fifo:, content_based_deduplication: nil, tags: nil, name: nil) ⇒ Topic

Returns a new instance of Topic.



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/cyclone_lariat/resources/topic.rb', line 10

def initialize(instance:, kind:, region:, account_id:, publisher:, type:, fifo:, content_based_deduplication: nil, tags: nil, name: nil)
  @instance  = instance
  @kind      = kind
  @region    = region
  @account_id = 
  @publisher = publisher
  @type      = type
  @fifo      = fifo
  @tags      = tags
  @name      = name
  @content_based_deduplication = content_based_deduplication
end

Instance Attribute Details

#account_idObject (readonly)

Returns the value of attribute account_id.



8
9
10
# File 'lib/cyclone_lariat/resources/topic.rb', line 8

def 
  @account_id
end

#content_based_deduplicationObject (readonly)

Returns the value of attribute content_based_deduplication.



8
9
10
# File 'lib/cyclone_lariat/resources/topic.rb', line 8

def content_based_deduplication
  @content_based_deduplication
end

#fifoObject (readonly)

Returns the value of attribute fifo.



8
9
10
# File 'lib/cyclone_lariat/resources/topic.rb', line 8

def fifo
  @fifo
end

#instanceObject (readonly)

Returns the value of attribute instance.



8
9
10
# File 'lib/cyclone_lariat/resources/topic.rb', line 8

def instance
  @instance
end

#kindObject (readonly)

Returns the value of attribute kind.



8
9
10
# File 'lib/cyclone_lariat/resources/topic.rb', line 8

def kind
  @kind
end

#publisherObject (readonly)

Returns the value of attribute publisher.



8
9
10
# File 'lib/cyclone_lariat/resources/topic.rb', line 8

def publisher
  @publisher
end

#regionObject (readonly)

Returns the value of attribute region.



8
9
10
# File 'lib/cyclone_lariat/resources/topic.rb', line 8

def region
  @region
end

#typeObject (readonly)

Returns the value of attribute type.



8
9
10
# File 'lib/cyclone_lariat/resources/topic.rb', line 8

def type
  @type
end

Class Method Details

.from_arn(arn) ⇒ Object

Raises:

  • (ArgumentError)


100
101
102
103
104
105
106
107
# File 'lib/cyclone_lariat/resources/topic.rb', line 100

def from_arn(arn)
  arn_array = arn.split(':')
  raise ArgumentError, 'Arn should consists `arn`' unless arn_array[0] == 'arn'
  raise ArgumentError, 'Arn should consists `aws`' unless arn_array[1] == 'aws'
  raise ArgumentError, 'Arn should consists `sns`' unless arn_array[2] == 'sns'

  from_name(arn_array[5], region: arn_array[3], account_id: arn_array[4])
end

.from_name(name, region:, account_id:) ⇒ Object

Raises:

  • (ArgumentError)


71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/cyclone_lariat/resources/topic.rb', line 71

def from_name(name, region:, account_id:)
  is_fifo_array  = name.split('.')
  full_name      = is_fifo_array[0]
  fifo_suffix    = is_fifo_array[-1]
  suffix_exists  = fifo_suffix != full_name

  if suffix_exists && fifo_suffix != 'fifo'
    raise ArgumentError, "Topic name #{name} consists unexpected suffix #{fifo_suffix}"
  end

  fifo = suffix_exists
  topic_array = full_name.split('-')

  raise ArgumentError, "Topic name should consists `#{SNS_SUFFIX}`" unless topic_array[2] != SNS_SUFFIX

  topic_array.clear if topic_array.size < 5

  new(
    instance: topic_array[0],
    kind: topic_array[1],
    publisher: topic_array[3],
    type: topic_array[4],
    region: region,
    account_id: ,
    fifo: fifo,
    name: name
  )
end

Instance Method Details

#==(other) ⇒ Object



66
67
68
# File 'lib/cyclone_lariat/resources/topic.rb', line 66

def ==(other)
  arn == other.arn
end

#arnObject



23
24
25
# File 'lib/cyclone_lariat/resources/topic.rb', line 23

def arn
  ['arn', 'aws', 'sns', region, , to_s].join ':'
end

#attributesObject



45
46
47
48
49
50
# File 'lib/cyclone_lariat/resources/topic.rb', line 45

def attributes
  attrs = {}
  attrs['FifoTopic']                 = 'true' if fifo
  attrs['ContentBasedDeduplication'] = 'true' if content_based_deduplication
  attrs
end

#custom?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/cyclone_lariat/resources/topic.rb', line 27

def custom?
  !standard?
end

#nameObject Also known as: to_s



37
38
39
40
41
42
43
# File 'lib/cyclone_lariat/resources/topic.rb', line 37

def name
  @name ||= begin
    name = [instance, kind, SNS_SUFFIX, publisher, type].compact.join '-'
    name += '.fifo' if fifo
    name
  end
end

#protocolObject



60
61
62
# File 'lib/cyclone_lariat/resources/topic.rb', line 60

def protocol
  'sns'
end

#queue?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/cyclone_lariat/resources/topic.rb', line 56

def queue?
  false
end

#standard?Boolean

Returns:

  • (Boolean)


31
32
33
34
35
# File 'lib/cyclone_lariat/resources/topic.rb', line 31

def standard?
  return false unless instance && kind && publisher && type

  true
end

#tagsObject



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/cyclone_lariat/resources/topic.rb', line 110

def tags
  @tags ||= begin
    if standard?
      [
        { key: 'standard',  value: 'true' },
        { key: 'instance',  value: String(instance) },
        { key: 'kind',      value: String(kind) },
        { key: 'publisher', value: String(publisher) },
        { key: 'type',      value: String(type) },
        { key: 'fifo',      value: fifo ? 'true' : 'false' }
      ]
    else
      [
        { key: 'standard',  value: 'false' },
        { key: 'name',      value: String(name) },
        { key: 'fifo',      value: fifo ? 'true' : 'false' }
      ]
    end
  end
end

#topic?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/cyclone_lariat/resources/topic.rb', line 52

def topic?
  true
end