@@ -36,7 +36,7 @@ import javax.annotation.PostConstruct
36
36
class SecurityGroupConverter (
37
37
private val clouddriverCache : CloudDriverCache ,
38
38
private val objectMapper : ObjectMapper
39
- ) : SpecConverter<SecurityGroupSpec, Set< SecurityGroup> > {
39
+ ) : SpecConverter<SecurityGroupSpec, SecurityGroup> {
40
40
41
41
private val log = LoggerFactory .getLogger(javaClass)
42
42
@@ -46,70 +46,61 @@ class SecurityGroupConverter(
46
46
)
47
47
}
48
48
49
- override fun convertToState (spec : SecurityGroupSpec ): Set < SecurityGroup > {
49
+ override fun convertToState (spec : SecurityGroupSpec ): SecurityGroup {
50
50
if (spec is AmazonSecurityGroupSpec ) {
51
- return spec.regions.map { region ->
52
- SecurityGroup (
53
- type = " aws" ,
54
- name = spec.name,
55
- description = spec.description,
56
- accountName = spec.accountName,
57
- region = region,
58
- // TODO rz - do we even want to mess with EC2-classic support?
59
- vpcId = clouddriverCache.networkBy(spec.vpcName!! , spec.accountName, region).id,
60
- inboundRules = spec.inboundRules.map {
61
- when (it) {
62
- is ReferenceSecurityGroupRule -> SecurityGroup .SecurityGroupRule (
63
- protocol = it.protocol,
64
- portRanges = it.portRanges.map { SecurityGroup .SecurityGroupRulePortRange (it.startPort, it.endPort) },
65
- securityGroup = SecurityGroup .SecurityGroupRuleReference (
66
- name = it.name,
67
- accountName = spec.accountName,
68
- region = region
69
- )
51
+ return SecurityGroup (
52
+ type = " aws" ,
53
+ name = spec.name,
54
+ description = spec.description,
55
+ accountName = spec.accountName,
56
+ region = spec.region,
57
+ // TODO rz - do we even want to mess with EC2-classic support?
58
+ vpcId = clouddriverCache.networkBy(spec.vpcName!! , spec.accountName, spec.region).id,
59
+ inboundRules = spec.inboundRules.map {
60
+ when (it) {
61
+ is ReferenceSecurityGroupRule -> SecurityGroup .SecurityGroupRule (
62
+ protocol = it.protocol,
63
+ portRanges = it.portRanges.map { SecurityGroup .SecurityGroupRulePortRange (it.startPort, it.endPort) },
64
+ securityGroup = SecurityGroup .SecurityGroupRuleReference (
65
+ name = it.name,
66
+ accountName = spec.accountName,
67
+ region = spec.region
70
68
)
71
- is CrossAccountReferenceSecurityGroupRule -> SecurityGroup . SecurityGroupRule (
72
- protocol = it.protocol,
73
- portRanges = it.portRanges.map { SecurityGroup . SecurityGroupRulePortRange (it.startPort, it.endPort) } ,
74
- securityGroup = SecurityGroup .SecurityGroupRuleReference (
75
- name = it.name,
76
- accountName = it.account ,
77
- region = it.region
78
- )
69
+ )
70
+ is CrossAccountReferenceSecurityGroupRule -> SecurityGroup . SecurityGroupRule (
71
+ protocol = it.protocol ,
72
+ portRanges = it.portRanges.map { SecurityGroup .SecurityGroupRulePortRange (it.startPort, it.endPort) },
73
+ securityGroup = SecurityGroup . SecurityGroupRuleReference (
74
+ name = it.name ,
75
+ accountName = it.account,
76
+ region = spec.region
79
77
)
80
- else -> TODO (reason = " ${it.javaClass.simpleName} has not been implemented yet" )
81
- }
82
- }.toSet(),
83
- id = null ,
84
- // TODO rz - fix so not bad
85
- moniker = Moniker (spec.name)
86
- )
87
- }.toSet()
78
+ )
79
+ else -> TODO (reason = " ${it.javaClass.simpleName} has not been implemented yet" )
80
+ }
81
+ }.toSet(),
82
+ id = null ,
83
+ moniker = Moniker (spec.name)
84
+ )
88
85
}
89
86
throw NotImplementedError (" Only AWS security groups are supported at the moment" )
90
87
}
91
88
92
- override fun convertFromState (state : Set <SecurityGroup >): SecurityGroupSpec ? {
93
- if (state.isEmpty()) {
94
- return null
95
- }
96
-
97
- state.first().let {
98
- if (it.type == " aws" ) {
99
- return AmazonSecurityGroupSpec (
100
- cloudProvider = " aws" ,
101
- application = it.moniker.app,
102
- name = it.name,
103
- description = it.description!! ,
104
- accountName = it.accountName,
105
- regions = state.map { s -> s.region }.toSet(),
106
- vpcName = clouddriverCache.networkBy(it.vpcId!! ).name,
107
- inboundRules = it.inboundRules.map {
108
- objectMapper.convertValue(it, SecurityGroupRule ::class .java)
109
- }.toSet(),
110
- outboundRules = setOf ()
111
- )
112
- }
89
+ override fun convertFromState (state : SecurityGroup ): SecurityGroupSpec ? {
90
+ if (state.type == " aws" ) {
91
+ return AmazonSecurityGroupSpec (
92
+ cloudProvider = " aws" ,
93
+ application = state.moniker.app,
94
+ name = state.name,
95
+ description = state.description!! ,
96
+ accountName = state.accountName,
97
+ region = state.region,
98
+ vpcName = clouddriverCache.networkBy(state.vpcId!! ).name,
99
+ inboundRules = state.inboundRules.map {
100
+ objectMapper.convertValue(state, SecurityGroupRule ::class .java)
101
+ }.toSet(),
102
+ outboundRules = setOf ()
103
+ )
113
104
}
114
105
throw NotImplementedError (" Only AWS security groups are supported at the moment" )
115
106
}
@@ -125,7 +116,7 @@ class SecurityGroupConverter(
125
116
" credentials" to spec.accountName,
126
117
" cloudProvider" to " aws" ,
127
118
" name" to spec.name,
128
- " regions" to spec.regions ,
119
+ " regions" to listOf ( spec.region) ,
129
120
" vpcId" to spec.vpcName,
130
121
" description" to spec.description,
131
122
" securityGroupIngress" to spec.inboundRules.flatMap {
@@ -142,7 +133,7 @@ class SecurityGroupConverter(
142
133
changeSummary.addMessage(" Adding cross account reference support account ${it.account} " )
143
134
m[" accountName" ] = it.account
144
135
m[" crossAccountEnabled" ] = true
145
- m[" vpcId" ] = clouddriverCache.networkBy(it.vpcName, spec.accountName, it .region)
136
+ m[" vpcId" ] = clouddriverCache.networkBy(it.vpcName, spec.accountName, spec .region)
146
137
}
147
138
m
148
139
}
0 commit comments