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.



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

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

Instance Method Details

#attrs_updated?Boolean

Returns:

  • (Boolean)


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

def attrs_updated?
  to_h
end

#aws(aws_topic) ⇒ Object



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

def aws(aws_topic)
  @aws_topic = aws_topic
  self
end

#clientObject



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

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

#createObject



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

def create
  Simnos.logger.info("Create Topic #{name}.#{@options[:dry_run] ? ' [dry-run]' : ''}".colorize(:green))
  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



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

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

#modifyObject



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

def modify
  modify_attrs
  modify_attrs_hash
end

#modify_attr(dsl_val, attr_name) ⇒ Object



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

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]' : ''}".colorize(:blue))
  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



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
118
# File 'lib/simnos/dsl/topic.rb', line 93

def modify_attr_hash(dsl_val, attr_name)
  aws_val = JSON.parse(@aws_topic[:attrs].attributes[attr_name])
  return if Simnos::Utils.normalize_hash(dsl_val) == Simnos::Utils.normalize_hash(aws_val)
  Simnos.logger.info("Modify Topic `#{name}` #{attr_name} attributes.#{@options[:dry_run] ? ' [dry-run]' : ''}".colorize(:blue))
  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’,



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

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

#modify_attrs_hashObject



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

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



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

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