Module: TFW::AwsSgWorkaround

Defined in:
lib/tfw/aws_sg_workaround.rb

Overview

This module is a workaround the issue that when using json some fields that are optional become required github.com/hashicorp/terraform/issues/23347

Class Method Summary collapse

Class Method Details

.fill_fields(obj) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/tfw/aws_sg_workaround.rb', line 30

def fill_fields(obj)
  obj['description'] = '' unless obj.key? 'description'
  obj['security_groups'] = [] unless obj.key? 'security_groups'
  obj['self'] = false unless obj.key? 'self'
  obj['ipv6_cidr_blocks'] = [] unless obj.key? 'ipv6_cidr_blocks'
  obj['prefix_list_ids'] = [] unless obj.key? 'prefix_list_ids'
end

.fix(stack_json) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/tfw/aws_sg_workaround.rb', line 9

def fix(stack_json)
  stack = JSON.parse stack_json
  return stack.to_json unless stack.key? 'resource'

  r = stack['resource']
  return stack.to_json unless r.key? 'aws_security_group'

  asg = r['aws_security_group']
  asg.each do |_, sg|
    replace_and_fill sg, 'egress' if sg.key? 'egress'
    replace_and_fill sg, 'ingress' if sg.key? 'ingress'
  end
  stack.to_json
end

.replace_and_fill(obj, key) ⇒ Object



24
25
26
27
28
# File 'lib/tfw/aws_sg_workaround.rb', line 24

def replace_and_fill(obj, key)
  backup = obj[key]
  fill_fields(backup)
  obj[key] = [backup]
end