Class: Simnos::Client

Inherits:
Object
  • Object
show all
Includes:
Filterable
Defined in:
lib/simnos/client.rb

Constant Summary collapse

MAGIC_COMMENT =
"# -*- mode: ruby -*-\n# vi: set ft=ruby :\n"

Instance Method Summary collapse

Methods included from Filterable

#target?

Constructor Details

#initialize(filepath, options = {}) ⇒ Client

Returns a new instance of Client.



17
18
19
20
21
# File 'lib/simnos/client.rb', line 17

def initialize(filepath, options = {})
  @filepath = filepath
  @options = options
  @options[:secret_expander] = SecretExpander.new(@options[:secret_provider]) if @options[:secret_provider]
end

Instance Method Details

#applyObject



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/simnos/client.rb', line 23

def apply
  Simnos.logger.info("Applying...#{@options[:dry_run] ? ' [dry-run]' : ''}")
  dsl = load_file(@filepath)

  dsl.snss.each do |region, sns|
    @options[:region] = region
    Simnos.logger.info("region: #{region}")
    aws_topics_by_name = client.topics
    traverse_topics(sns.topics, aws_topics_by_name)
  end
end

#exportObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/simnos/client.rb', line 35

def export
  Simnos.logger.info("Exporting...#{@options[:dry_run] ? ' [dry-run]' : ''}")

  topics_by_name = client.topics

  if @options[:with_subscriptions]
    topics_by_name.each do |name, topic|
      Simnos.logger.debug("exporting subscriptions of #{topic[:topic].topic_arn}")
      topic[:subscriptions] = client.subscriptions_by_topic(topic_arn: topic[:topic].topic_arn)
    end
  end
  region = client.region

  path = Pathname.new(@filepath)
  base_dir = path.parent
  if @options[:split]
    FileUtils.mkdir_p(base_dir)
    topics_by_name.each do |name, aws|
      Converter.new({name => aws}, region).convert do |dsl|
        sns_file = base_dir.join("#{name}.sns")
        Simnos.logger.info("Export #{sns_file}")
        open(sns_file, 'wb') do |f|
          f.puts MAGIC_COMMENT
          f.puts dsl
        end
      end
    end
  else
    Converter.new(topics_by_name, region).convert do |dsl|
      FileUtils.mkdir_p(base_dir)
      Simnos.logger.info("Export #{path}")
      open(path, 'wb') do |f|
        f.puts MAGIC_COMMENT
        f.puts dsl
      end
    end
  end
end