Class: Demeter::Aws::ManageSecurityGroups

Inherits:
Object
  • Object
show all
Defined in:
lib/demeter/aws/manage_security_groups.rb

Instance Method Summary collapse

Constructor Details

#initialize(ec2:, project_path: File.join(Demeter::root, "/configs/**/*.yml"), options: {}) ⇒ ManageSecurityGroups

Returns a new instance of ManageSecurityGroups.



9
10
11
12
13
14
# File 'lib/demeter/aws/manage_security_groups.rb', line 9

def initialize(ec2:, project_path: File.join(Demeter::root, "/configs/**/*.yml"), options:{})
  @ec2 = ec2
  @sgs = {}
  @project_path = project_path  
  @options = options
end

Instance Method Details

#applyObject



43
44
45
46
# File 'lib/demeter/aws/manage_security_groups.rb', line 43

def apply
  create_all
  modify_all
end

#create_allObject



29
30
31
32
33
34
# File 'lib/demeter/aws/manage_security_groups.rb', line 29

def create_all
  describe
  @sgs.each do |key, sg|
    sg.create
  end
end

#describeObject



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/demeter/aws/manage_security_groups.rb', line 87

def describe()
  Dir.glob(@project_path).each do |path|
    project_config = YAML::load_file(path)
    
    next if !project_config
    next if project_config['environments'] && @options['environment'] && !project_config['environments'].include?(@options['environment'])

    if project_config && project_config['security_groups']
      project_config['security_groups'].each do |local_sg|
        sg = Demeter::Aws::SecurityGroup.new(@ec2)
        sg.load_local(local_sg)
        @sgs[sg.hash] = sg
      end
    end
  end
  
  res = @ec2.describe_security_groups
  res[:security_groups].each do |object|
    name_tag =  object['tags'].detect{|tag| tag['key'].downcase == 'name'}
    if name_tag && @sgs.include?(name_tag['value'])
      @sgs[name_tag['value']].load_aws(object)
    end
  end
end

#diff_allObject

Returns array of diffs



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/demeter/aws/manage_security_groups.rb', line 17

def diff_all
  describe
  all_diffs = {}
  @sgs.each do |key, sg|
    diff = sg.diff
    if diff.any?
      all_diffs[key] = diff
    end
  end
  all_diffs
end

#modify_allObject



36
37
38
39
40
41
# File 'lib/demeter/aws/manage_security_groups.rb', line 36

def modify_all
  describe
  @sgs.each do |key, sg|
    sg.modify
  end
end

#statusObject



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
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/demeter/aws/manage_security_groups.rb', line 48

def status
  status = {managed: [], unmanaged: []}
  local_sgs = []

  Dir.glob(@project_path).each do |path|
    project_config = YAML::load_file(path)
    
    next if !project_config
    next if project_config['environments'] && @options['environment'] && !project_config['environments'].include?(@options['environment'])
    
    if project_config && project_config['security_groups']
      project_config['security_groups'].each do |local_sg|
        local_sgs << local_sg['name']
      end
    end
  end

  res = @ec2.describe_security_groups
  res[:security_groups].each do |object|
    name_tag = object['tags'].detect{|tag| tag['key'].downcase == 'name'}
    if name_tag && local_sgs.include?(name_tag['value'])
      status[:managed] << {
        name: name_tag['value'],
        group_id: object.group_id,
        group_name: object.group_name
      }
    else
      status[:unmanaged] << {
        name: (name_tag ? name_tag['value'] : ''),
        group_id: object.group_id,
        group_name: object.group_name
      }
    end
  end
  status[:managed].sort_by!{|x| x[:name]}
  status[:unmanaged].sort_by!{|x| x[:name]}
  status
end