This repository has been archived by the owner on Sep 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 659
/
db_security_group.rb
57 lines (47 loc) · 1.54 KB
/
db_security_group.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
module Terraforming
module Resource
class DBSecurityGroup
include Terraforming::Util
def self.tf(client: Aws::RDS::Client.new)
self.new(client).tf
end
def self.tfstate(client: Aws::RDS::Client.new)
self.new(client).tfstate
end
def initialize(client)
@client = client
end
def tf
apply_template(@client, "tf/db_security_group")
end
def tfstate
db_security_groups.inject({}) do |resources, security_group|
attributes = {
"db_subnet_group_name" => security_group.db_security_group_name,
"id" => security_group.db_security_group_name,
"ingress.#" => ingresses_of(security_group).length.to_s,
"name" => security_group.db_security_group_name,
}
resources["aws_db_security_group.#{module_name_of(security_group)}"] = {
"type" => "aws_db_security_group",
"primary" => {
"id" => security_group.db_security_group_name,
"attributes" => attributes
}
}
resources
end
end
private
def ingresses_of(security_group)
security_group.ec2_security_groups + security_group.ip_ranges
end
def db_security_groups
@client.describe_db_security_groups.map(&:db_security_groups).flatten.select { |sg| !ingresses_of(sg).empty? }
end
def module_name_of(security_group)
normalize_module_name(security_group.db_security_group_name)
end
end
end
end