2021-06-29 09:59:16 +00:00
|
|
|
package middlewares
|
|
|
|
|
|
|
|
import (
|
2021-12-06 13:29:39 +00:00
|
|
|
"github.com/snyk/driftctl/pkg/resource"
|
|
|
|
"github.com/snyk/driftctl/pkg/resource/aws"
|
2021-06-29 09:59:16 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
/**
|
|
|
|
Fetching eip association from remote return every association but some of them are embedded in eip.
|
|
|
|
This middleware will check for every eip_association that here is no corresponding association_id inside eip.
|
|
|
|
*/
|
|
|
|
|
|
|
|
type EipAssociationExpander struct {
|
|
|
|
resourceFactory resource.ResourceFactory
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewEipAssociationExpander(resourceFactory resource.ResourceFactory) EipAssociationExpander {
|
|
|
|
return EipAssociationExpander{resourceFactory}
|
|
|
|
}
|
|
|
|
|
2021-08-09 14:03:04 +00:00
|
|
|
func (m EipAssociationExpander) Execute(_, resourcesFromState *[]*resource.Resource) error {
|
|
|
|
var newResources []*resource.Resource
|
2021-06-29 09:59:16 +00:00
|
|
|
for _, res := range *resourcesFromState {
|
|
|
|
newResources = append(newResources, res)
|
|
|
|
|
2021-08-18 13:58:28 +00:00
|
|
|
if res.ResourceType() != aws.AwsEipResourceType {
|
2021-06-29 09:59:16 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
if m.haveMatchingEipAssociation(res, resourcesFromState) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// This EIP have no association, check if we need to create one
|
|
|
|
assocID := res.Attributes().GetString("association_id")
|
|
|
|
if assocID == nil || *assocID == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
attributes := *res.Attributes()
|
|
|
|
newAssoc := m.resourceFactory.CreateAbstractResource(
|
|
|
|
aws.AwsEipAssociationResourceType,
|
|
|
|
*assocID,
|
|
|
|
map[string]interface{}{
|
2021-08-18 13:58:28 +00:00
|
|
|
"allocation_id": res.ResourceId(),
|
2021-06-29 09:59:16 +00:00
|
|
|
"id": *assocID,
|
|
|
|
"instance_id": attributes["instance"],
|
|
|
|
"network_interface_id": attributes["network_interface"],
|
|
|
|
"private_ip_address": attributes["private_ip"],
|
|
|
|
"public_ip": attributes["public_ip"],
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
newResources = append(newResources, newAssoc)
|
|
|
|
}
|
|
|
|
*resourcesFromState = newResources
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-08-09 14:03:04 +00:00
|
|
|
func (m EipAssociationExpander) haveMatchingEipAssociation(cur *resource.Resource, stateRes *[]*resource.Resource) bool {
|
2021-06-29 09:59:16 +00:00
|
|
|
for _, res := range *stateRes {
|
2021-08-18 13:58:28 +00:00
|
|
|
if res.ResourceType() != aws.AwsEipAssociationResourceType {
|
2021-06-29 09:59:16 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
assocId := cur.Attributes().GetString("association_id")
|
2021-08-18 13:58:28 +00:00
|
|
|
if assocId != nil && res.ResourceId() == *assocId {
|
2021-06-29 09:59:16 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|