@@ -22,22 +22,25 @@ import (
22
22
"github.com/np-guard/vpc-network-config-analyzer/pkg/vpcmodel"
23
23
)
24
24
25
+ type AWSresourcesContainer struct {
26
+ aws.ResourcesContainer
27
+ }
28
+
25
29
// parseResourcesFromFile returns aws.ResourcesContainer object, containing the configured resources structs
26
30
// from the input JSON file
27
- func parseResourcesFromFile ( fileName string ) ( * aws. ResourcesContainer , error ) {
31
+ func ( rc * AWSresourcesContainer ) ParseResourcesFromFile ( fileName string ) error {
28
32
inputConfigContent , err := os .ReadFile (fileName )
29
33
if err != nil {
30
- return nil , err
34
+ return err
31
35
}
32
- config := aws.ResourcesContainer {}
33
- err = json .Unmarshal (inputConfigContent , & config )
36
+ err = json .Unmarshal (inputConfigContent , & rc )
34
37
if err != nil {
35
- return nil , err
38
+ return err
36
39
}
37
- return & config , nil
40
+ return nil
38
41
}
39
42
40
- func mergeResourcesContainers (rc1 , rc2 * aws. ResourcesContainer ) (* aws. ResourcesContainer , error ) {
43
+ func mergeResourcesContainers (rc1 , rc2 * AWSresourcesContainer ) (* AWSresourcesContainer , error ) {
41
44
if rc2 == nil && rc1 != nil {
42
45
return rc1 , nil
43
46
}
@@ -57,27 +60,27 @@ func mergeResourcesContainers(rc1, rc2 *aws.ResourcesContainer) (*aws.ResourcesC
57
60
return rc1 , nil
58
61
}
59
62
60
- func VpcConfigsFromFiles (fileNames []string , vpcID , resourceGroup string , regions []string ) (
63
+ func ( rc * AWSresourcesContainer ) VpcConfigsFromFiles (fileNames []string , vpcID , resourceGroup string , regions []string ) (
61
64
* vpcmodel.MultipleVPCConfigs , error ) {
62
- var mergedRC * aws.ResourcesContainer
63
65
for _ , file := range fileNames {
64
- rc , err1 := parseResourcesFromFile (file )
66
+ mergedRC := & AWSresourcesContainer {}
67
+ err1 := mergedRC .ParseResourcesFromFile (file )
65
68
if err1 != nil {
66
69
return nil , fmt .Errorf ("error parsing input vpc resources file: %w" , err1 )
67
70
}
68
- mergedRC , err1 = mergeResourcesContainers (mergedRC , rc )
71
+ rc , err1 = mergeResourcesContainers (mergedRC , rc )
69
72
if err1 != nil {
70
73
return nil , err1
71
74
}
72
75
}
73
- vpcConfigs , err2 := VPCConfigsFromResources (mergedRC , vpcID , resourceGroup , regions )
76
+ vpcConfigs , err2 := rc . VPCConfigsFromResources (vpcID , resourceGroup , regions )
74
77
if err2 != nil {
75
78
return nil , fmt .Errorf ("error generating cloud config from input vpc resources file: %w" , err2 )
76
79
}
77
80
return vpcConfigs , nil
78
81
}
79
82
80
- func filterByVpc (rc * aws. ResourcesContainer , vpcID string ) map [string ]bool {
83
+ func filterByVpc (rc * AWSresourcesContainer , vpcID string ) map [string ]bool {
81
84
shouldSkipVpcIds := make (map [string ]bool )
82
85
for _ , vpc := range rc .VpcsList {
83
86
if vpcID != "" && * vpc .VpcId != vpcID {
@@ -89,7 +92,7 @@ func filterByVpc(rc *aws.ResourcesContainer, vpcID string) map[string]bool {
89
92
90
93
// VPCConfigsFromResources returns a map from VPC UID (string) to its corresponding VPCConfig object,
91
94
// containing the parsed resources in the relevant model objects
92
- func VPCConfigsFromResources (rc * aws. ResourcesContainer , vpcID , resourceGroup string , regions []string ) (
95
+ func (rc * AWSresourcesContainer ) VPCConfigsFromResources ( vpcID , resourceGroup string , regions []string ) (
93
96
* vpcmodel.MultipleVPCConfigs , error ) {
94
97
res := vpcmodel .NewMultipleVPCConfigs ("AWS Cloud" ) // map from VPC UID to its config
95
98
var err error
@@ -131,7 +134,7 @@ func VPCConfigsFromResources(rc *aws.ResourcesContainer, vpcID, resourceGroup st
131
134
return res , nil
132
135
}
133
136
134
- func getVPCconfig (rc * aws. ResourcesContainer ,
137
+ func getVPCconfig (rc * AWSresourcesContainer ,
135
138
res * vpcmodel.MultipleVPCConfigs ,
136
139
skipByVPC map [string ]bool ) error {
137
140
for _ , vpc := range rc .VpcsList {
@@ -178,7 +181,7 @@ func newNetworkInterface(uid, zone, address, vsi string, vpc vpcmodel.VPCResourc
178
181
}
179
182
180
183
func getInstancesConfig (
181
- rc * aws. ResourcesContainer ,
184
+ rc * AWSresourcesContainer ,
182
185
subnetIDToNetIntf map [string ][]* commonvpc.NetworkInterface ,
183
186
netIntfToSGs map [string ][]types.GroupIdentifier ,
184
187
res * vpcmodel.MultipleVPCConfigs ,
@@ -236,7 +239,7 @@ func getInstancesConfig(
236
239
func getSubnetsConfig (
237
240
res * vpcmodel.MultipleVPCConfigs ,
238
241
subnetNameToNetIntf map [string ][]* commonvpc.NetworkInterface ,
239
- rc * aws. ResourcesContainer ,
242
+ rc * AWSresourcesContainer ,
240
243
skipByVPC map [string ]bool ,
241
244
) (vpcInternalAddressRange map [string ]* ipblock.IPBlock , err error ) {
242
245
vpcInternalAddressRange = map [string ]* ipblock.IPBlock {}
@@ -301,7 +304,7 @@ func parseSGTargets(sgResources map[string]map[string]*commonvpc.SecurityGroup,
301
304
}
302
305
}
303
306
304
- func getSGconfig (rc * aws. ResourcesContainer ,
307
+ func getSGconfig (rc * AWSresourcesContainer ,
305
308
res * vpcmodel.MultipleVPCConfigs ,
306
309
skipByVPC map [string ]bool ,
307
310
netIntfToSGs map [string ][]types.GroupIdentifier ,
0 commit comments