-
Notifications
You must be signed in to change notification settings - Fork 1
/
lineage.go
60 lines (47 loc) · 2.06 KB
/
lineage.go
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
58
59
60
package aws
import (
"fmt"
"github.com/luhring/reach/reach"
)
// GetENIFromLineage returns the ElasticNetworkInterface from the given lineage.
func GetENIFromLineage(lineage []reach.ResourceReference, collection *reach.ResourceCollection) (*ElasticNetworkInterface, error) {
const errPrefix = "unable to get ElasticNetworkInterface from lineage"
for _, ref := range lineage {
if ref.Domain == ResourceDomainAWS && ref.Kind == ResourceKindElasticNetworkInterface {
eniResource := collection.Get(ref)
if eniResource == nil {
return nil, fmt.Errorf("%s: no resource found in resource collection for reference: %s", errPrefix, ref)
}
eni := eniResource.Properties.(ElasticNetworkInterface)
return &eni, nil
}
}
return nil, fmt.Errorf("%s: lineage does not contain an ElasticNetworkInterface", errPrefix)
}
// GetENIsFromVector returns the ElasticNetworkInterfaces from the specified network vector.
func GetENIsFromVector(v reach.NetworkVector, collection *reach.ResourceCollection) (*ElasticNetworkInterface, *ElasticNetworkInterface, error) {
sourceENI, err := GetENIFromLineage(v.Source.Lineage, collection)
if err != nil {
return nil, nil, err
}
destinationENI, err := GetENIFromLineage(v.Destination.Lineage, collection)
if err != nil {
return nil, nil, err
}
return sourceENI, destinationENI, nil
}
// GetEC2InstanceFromLineage returns the EC2 instance from the given lineage.
func GetEC2InstanceFromLineage(lineage []reach.ResourceReference, collection *reach.ResourceCollection) (*EC2Instance, error) {
const errPrefix = "unable to get EC2Instance from lineage"
for _, ref := range lineage {
if ref.Domain == ResourceDomainAWS && ref.Kind == ResourceKindEC2Instance {
ec2InstanceResource := collection.Get(ref)
if ec2InstanceResource == nil {
return nil, fmt.Errorf("%s: no resource found in resource collection for reference: %s", errPrefix, ref)
}
ec2Instance := ec2InstanceResource.Properties.(EC2Instance)
return &ec2Instance, nil
}
}
return nil, fmt.Errorf("%s: lineage does not contain an EC2Instance", errPrefix)
}