Class: Simnos::DSL::Topic::Result

Inherits:
Object
  • Object
show all
Defined in:
lib/simnos/dsl/topic.rb

Constant Summary collapse

ATTRIBUTES =
%i/name display_name subscriptions_pending subscriptions_confirmed subscriptions_deleted effective_delivery_policy policy topic_arn subscriptions aws_topic opt_out_subscriptions/
CREATE_KEYS =
%i/name/
MODIFY_ATTRS =
{
  display_name: 'DisplayName',
  #subscriptions_pending: 'SubscriptionsPending',
  #subscriptions_confirmed: 'SubscriptionsConfirmed',
  #subscriptions_deleted: 'SubscriptionsDeleted',
}
MODIFY_ATTRS_HASH =
{
  effective_delivery_policy: 'EffectiveDeliveryPolicy',
  policy: 'Policy',
}

Instance Method Summary collapse

Constructor Details

#initialize(context) ⇒ Result

Returns a new instance of Result.



13
14
15
16
17
# File 'lib/simnos/dsl/topic.rb', line 13

def initialize(context)
  @context = context
  @options = context.options
  @subscriptions = []
end

Instance Method Details

#attrs_updated?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/simnos/dsl/topic.rb', line 46

def attrs_updated?
  to_h
end

#aws(aws_topic) ⇒ Object



41
42
43
44
# File 'lib/simnos/dsl/topic.rb', line 41

def aws(aws_topic)
  @aws_topic = aws_topic
  self
end

#clientObject



129
130
131
# File 'lib/simnos/dsl/topic.rb', line 129

def client
  @client ||= Simnos::ClientWrapper.new(@context)
end

#createObject



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/simnos/dsl/topic.rb', line 28

def create
  Simnos.logger.info("Create Topic #{name}.#{@options[:dry_run] ? ' [dry-run]' : ''}")
  return { topic: Hashie::Mash.new(topic_arn: 'not yet created') } if @options[:dry_run]

  resp = client.create_topic(name: name)
  # save topic_arn
  topic_arn = resp.topic_arn
  {
    topic: resp,
    attrs: client.topic_attrs(topic_arn: resp.topic_arn)
  }
end

#create_optionObject



24
25
26
# File 'lib/simnos/dsl/topic.rb', line 24

def create_option
  to_h.select { |k, _| CREATE_KEYS.include?(k) }
end

#modifyObject



50
51
52
53
# File 'lib/simnos/dsl/topic.rb', line 50

def modify
  modify_attrs
  modify_attrs_hash
end

#modify_attr(dsl_val, attr_name) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/simnos/dsl/topic.rb', line 67

def modify_attr(dsl_val, attr_name)
  aws_val = @aws_topic[:attrs].attributes[attr_name]
  return if dsl_val == aws_val

  Simnos.logger.debug('--- aws ---')
  Simnos.logger.debug(@aws_topic[:attrs].attributes.pretty_inspect)
  Simnos.logger.debug('--- dsl ---')
  Simnos.logger.debug(dsl_val)
  Simnos.logger.info("Modify Topic `#{name}` #{attr_name} attributes.#{@options[:dry_run] ? ' [dry-run]' : ''}")
  dsl_attrs = {
    attribute_name: attr_name,
    attribute_value: dsl_val,
  }
  diff = Simnos::Utils.diff({
    attribute_name: attr_name,
    attribute_value: aws_val,
  }, dsl_attrs,
    color: @options[:color],
  )
  Simnos.logger.info("<diff>\n#{diff}")
  return if @options[:dry_run]

  client.set_topic_attributes(dsl_attrs.merge(topic_arn: @aws_topic[:topic].topic_arn))
end

#modify_attr_hash(dsl_val, attr_name) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/simnos/dsl/topic.rb', line 92

def modify_attr_hash(dsl_val, attr_name)
  aws_val = JSON.parse(@aws_topic[:attrs].attributes[attr_name])
  return if dsl_val == aws_val
  Simnos.logger.info("Modify Topic `#{name}` #{attr_name} attributes.#{@options[:dry_run] ? ' [dry-run]' : ''}")
  dsl_attrs = {
    attribute_name: attr_name,
    attribute_value: dsl_val,
  }
  diff = Simnos::Utils.diff({
    attribute_name: attr_name,
    attribute_value: aws_val,
  }, dsl_attrs,
    color: @options[:color],
  )
  Simnos.logger.info("<diff>\n#{diff}")
  return if @options[:dry_run]

  dsl_attrs.merge!(topic_arn: @aws_topic[:topic].topic_arn)
  dsl_attrs[:attribute_value] = dsl_attrs[:attribute_value].to_json
  if attr_name == 'Policy'
    client.set_topic_attributes(dsl_attrs)
  elsif attr_name == 'EffectiveDeliveryPolicy'
    dsl_attrs[:attribute_name] = 'DeliveryPolicy'
    client.set_topic_attributes(dsl_attrs)
  end
end

#modify_attrsObject

subscriptions_pending: ‘SubscriptionsPending’, subscriptions_confirmed: ‘SubscriptionsConfirmed’, subscriptions_deleted: ‘SubscriptionsDeleted’,



61
62
63
64
65
# File 'lib/simnos/dsl/topic.rb', line 61

def modify_attrs
  MODIFY_ATTRS.each do |prop_name, attr_name|
    modify_attr(send(prop_name), attr_name)
  end
end

#modify_attrs_hashObject



123
124
125
126
127
# File 'lib/simnos/dsl/topic.rb', line 123

def modify_attrs_hash
  MODIFY_ATTRS_HASH.each do |prop_name, attr_name|
    modify_attr_hash(send(prop_name), attr_name)
  end
end

#to_hObject



19
20
21
# File 'lib/simnos/dsl/topic.rb', line 19

def to_h
  Hash[ATTRIBUTES.sort.map { |name| [name, public_send(name)] }]
end