Class: OpenStax::Aws::SecretsFactory

Inherits:
Object
  • Object
show all
Defined in:
lib/openstax/aws/secrets_factory.rb

Instance Method Summary collapse

Constructor Details

#initialize(region:, context:, namespace:, dry_run: true, for_create_or_update:, shared_substitutions_block: nil) ⇒ SecretsFactory

Returns a new instance of SecretsFactory.

Raises:

  • (ArgumentError)


3
4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/openstax/aws/secrets_factory.rb', line 3

def initialize(region:, context:, namespace:, dry_run: true,
               for_create_or_update:, shared_substitutions_block: nil)
  raise ArgumentError, "context cannot be nil" if context.nil?
  @context = context
  @specification_blocks = []
  @substitutions_block = nil
  @region = region
  @top_namespace = namespace
  @next_namespace = nil
  @dry_run = dry_run
  @for_create_or_update = for_create_or_update
  @shared_substitutions_block = shared_substitutions_block
end

Instance Method Details

#full_namespaceObject



74
75
76
# File 'lib/openstax/aws/secrets_factory.rb', line 74

def full_namespace
  [@top_namespace, @next_namespace].flatten.reject(&:blank?)
end

#instanceObject



78
79
80
81
82
83
84
# File 'lib/openstax/aws/secrets_factory.rb', line 78

def instance
  Secrets.new(region: @region, namespace: full_namespace, dry_run: @dry_run).tap do |secrets|
    if @for_create_or_update
      secrets.define(specifications: specification_instances, substitutions: substitutions_hash)
    end
  end
end

#namespace(*args, &block) ⇒ Object



17
18
19
# File 'lib/openstax/aws/secrets_factory.rb', line 17

def namespace(*args, &block)
  @next_namespace = args.any? ? args[0] : @context.instance_eval(&block)
end

#specification(&block) ⇒ Object



21
22
23
# File 'lib/openstax/aws/secrets_factory.rb', line 21

def specification(&block)
  @specification_blocks.push(block)
end

#specification_instancesObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/openstax/aws/secrets_factory.rb', line 29

def specification_instances
  raise "Must define secrets specification" if @specification_blocks.empty?

  @specification_blocks.map do |specification_block|
    factory = SecretsSpecificationFactory.new(@context)
    factory.instance_eval &specification_block
    attributes = factory.attributes

    if attributes.has_key?(:org_slash_repo)
      OpenStax::Aws::SecretsSpecification.from_git(
        org_slash_repo: attributes[:org_slash_repo],
        sha: attributes[:sha],
        path: attributes[:path],
        format: attributes[:format].to_sym,
        top_key: attributes[:top_key].to_sym,
        preparser: attributes[:preparser]
      )
    elsif attributes.has_key?(:content)
      OpenStax::Aws::SecretsSpecification.from_content(
        content: attributes[:content],
        format: attributes[:format],
        top_key: attributes[:top_key],
        preparser: attributes[:preparser]
      )
    else
      raise "Cannot build a secrets specification"
    end
  end
end

#substitutions(&block) ⇒ Object



25
26
27
# File 'lib/openstax/aws/secrets_factory.rb', line 25

def substitutions(&block)
  @substitutions_block = block
end

#substitutions_hashObject



59
60
61
62
63
64
# File 'lib/openstax/aws/secrets_factory.rb', line 59

def substitutions_hash
  shared_substitutions = substitutions_hash_from_block(@shared_substitutions_block)
  local_substitutions = substitutions_hash_from_block(@substitutions_block)

  shared_substitutions.merge(local_substitutions)
end

#substitutions_hash_from_block(block) ⇒ Object



66
67
68
69
70
71
72
# File 'lib/openstax/aws/secrets_factory.rb', line 66

def substitutions_hash_from_block(block)
  return {} if block.nil?

  factory = SecretsSubstitutionsFactory.new(@context)
  factory.instance_eval &block
  factory.attributes
end