2020-12-09 15:31:34 +00:00
|
|
|
package middlewares
|
|
|
|
|
|
|
|
import (
|
2021-03-29 16:10:50 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
|
2020-12-09 15:31:34 +00:00
|
|
|
"github.com/cloudskiff/driftctl/pkg/resource"
|
|
|
|
"github.com/cloudskiff/driftctl/pkg/resource/aws"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Remove root_block_device from aws_instance resources and create dedicated aws_ebs_volume resources
|
2021-03-29 16:10:50 +00:00
|
|
|
type AwsInstanceBlockDeviceResourceMapper struct {
|
2021-04-29 15:17:55 +00:00
|
|
|
resourceFactory resource.ResourceFactory
|
2021-03-29 16:10:50 +00:00
|
|
|
}
|
2020-12-09 15:31:34 +00:00
|
|
|
|
2021-04-29 15:17:55 +00:00
|
|
|
func NewAwsInstanceBlockDeviceResourceMapper(resourceFactory resource.ResourceFactory) AwsInstanceBlockDeviceResourceMapper {
|
2021-04-29 10:32:02 +00:00
|
|
|
return AwsInstanceBlockDeviceResourceMapper{
|
2021-04-29 15:17:55 +00:00
|
|
|
resourceFactory: resourceFactory,
|
2021-04-29 10:32:02 +00:00
|
|
|
}
|
2020-12-09 15:31:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a AwsInstanceBlockDeviceResourceMapper) Execute(remoteResources, resourcesFromState *[]resource.Resource) error {
|
|
|
|
|
|
|
|
newStateResources := make([]resource.Resource, 0)
|
|
|
|
for _, stateRes := range *resourcesFromState {
|
|
|
|
|
|
|
|
// Ignore all resources other than aws_instance
|
|
|
|
if stateRes.TerraformType() != aws.AwsInstanceResourceType {
|
|
|
|
newStateResources = append(newStateResources, stateRes)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-04-29 15:17:55 +00:00
|
|
|
instance, _ := stateRes.(*resource.AbstractResource)
|
2021-04-29 10:32:02 +00:00
|
|
|
|
2021-04-29 15:17:55 +00:00
|
|
|
if rootBlockDevice, exist := instance.Attrs.Get("root_block_device"); exist {
|
2021-05-06 10:55:19 +00:00
|
|
|
for _, rootBlock := range rootBlockDevice.([]interface{}) {
|
|
|
|
rootBlock := rootBlock.(map[string]interface{})
|
2020-12-09 15:31:34 +00:00
|
|
|
logrus.WithFields(logrus.Fields{
|
2021-04-29 15:17:55 +00:00
|
|
|
"volume": rootBlock["volume_id"],
|
2020-12-09 15:31:34 +00:00
|
|
|
"instance": instance.TerraformId(),
|
|
|
|
}).Debug("Creating aws_ebs_volume from aws_instance.root_block_device")
|
2021-03-29 16:10:50 +00:00
|
|
|
data := map[string]interface{}{
|
2021-04-29 15:17:55 +00:00
|
|
|
"availability_zone": (*instance.Attrs)["availability_zone"],
|
|
|
|
"encrypted": rootBlock["encrypted"],
|
|
|
|
"id": rootBlock["volume_id"],
|
|
|
|
"iops": rootBlock["iops"],
|
|
|
|
"kms_key_id": rootBlock["kms_key_id"],
|
|
|
|
"size": rootBlock["volume_size"],
|
|
|
|
"type": rootBlock["volume_type"],
|
2021-03-29 16:10:50 +00:00
|
|
|
"multi_attach_enabled": false,
|
2021-06-22 09:26:39 +00:00
|
|
|
"tags": a.volumeTags(instance, rootBlock),
|
2020-12-09 15:31:34 +00:00
|
|
|
}
|
2021-06-22 08:52:44 +00:00
|
|
|
if throughput, exist := rootBlock["throughput"]; exist {
|
|
|
|
data["throughput"] = throughput
|
|
|
|
}
|
2021-05-04 13:03:17 +00:00
|
|
|
newRes := a.resourceFactory.CreateAbstractResource("aws_ebs_volume", rootBlock["volume_id"].(string), data)
|
|
|
|
newStateResources = append(newStateResources, newRes)
|
2020-12-09 15:31:34 +00:00
|
|
|
}
|
2021-04-29 15:17:55 +00:00
|
|
|
instance.Attrs.SafeDelete([]string{"root_block_device"})
|
2021-05-04 13:03:17 +00:00
|
|
|
instance.Attrs.SafeDelete([]string{"volume_tags"})
|
2020-12-09 15:31:34 +00:00
|
|
|
}
|
2021-04-29 15:17:55 +00:00
|
|
|
|
|
|
|
if ebsBlockDevice, exist := instance.Attrs.Get("ebs_block_device"); exist {
|
2021-05-06 10:55:19 +00:00
|
|
|
for _, blockDevice := range ebsBlockDevice.([]interface{}) {
|
|
|
|
blockDevice := blockDevice.(map[string]interface{})
|
2021-06-22 13:40:33 +00:00
|
|
|
if a.hasBlockDevice(blockDevice, resourcesFromState) {
|
|
|
|
continue
|
|
|
|
}
|
2020-12-09 15:31:34 +00:00
|
|
|
logrus.WithFields(logrus.Fields{
|
2021-04-29 15:17:55 +00:00
|
|
|
"volume": blockDevice["volume_id"],
|
2020-12-09 15:31:34 +00:00
|
|
|
"instance": instance.TerraformId(),
|
|
|
|
}).Debug("Creating aws_ebs_volume from aws_instance.ebs_block_device")
|
2021-03-29 16:10:50 +00:00
|
|
|
data := map[string]interface{}{
|
2021-04-29 15:17:55 +00:00
|
|
|
"availability_zone": (*instance.Attrs)["availability_zone"],
|
|
|
|
"encrypted": blockDevice["encrypted"],
|
|
|
|
"id": blockDevice["volume_id"],
|
|
|
|
"iops": blockDevice["iops"],
|
|
|
|
"kms_key_id": blockDevice["kms_key_id"],
|
|
|
|
"size": blockDevice["volume_size"],
|
|
|
|
"type": blockDevice["volume_type"],
|
2021-03-29 16:10:50 +00:00
|
|
|
"multi_attach_enabled": false,
|
2021-06-22 09:26:39 +00:00
|
|
|
"tags": a.volumeTags(instance, blockDevice),
|
2021-03-29 16:10:50 +00:00
|
|
|
}
|
2021-06-22 08:52:44 +00:00
|
|
|
if throughput, exist := blockDevice["throughput"]; exist {
|
|
|
|
data["throughput"] = throughput
|
|
|
|
}
|
2021-05-04 13:03:17 +00:00
|
|
|
newRes := a.resourceFactory.CreateAbstractResource("aws_ebs_volume", blockDevice["volume_id"].(string), data)
|
|
|
|
newStateResources = append(newStateResources, newRes)
|
2020-12-09 15:31:34 +00:00
|
|
|
}
|
2021-04-29 15:17:55 +00:00
|
|
|
instance.Attrs.SafeDelete([]string{"ebs_block_device"})
|
2021-05-04 13:03:17 +00:00
|
|
|
instance.Attrs.SafeDelete([]string{"volume_tags"})
|
2020-12-09 15:31:34 +00:00
|
|
|
}
|
|
|
|
newStateResources = append(newStateResources, instance)
|
|
|
|
}
|
|
|
|
|
2021-04-29 15:17:55 +00:00
|
|
|
newRemoteResources := make([]resource.Resource, 0)
|
|
|
|
for _, remoteRes := range *remoteResources {
|
|
|
|
if remoteRes.TerraformType() != aws.AwsInstanceResourceType {
|
|
|
|
newRemoteResources = append(newRemoteResources, remoteRes)
|
|
|
|
continue
|
2021-04-29 10:32:02 +00:00
|
|
|
}
|
2021-04-29 15:17:55 +00:00
|
|
|
instance, _ := remoteRes.(*resource.AbstractResource)
|
|
|
|
instance.Attrs.SafeDelete([]string{"root_block_device"})
|
|
|
|
instance.Attrs.SafeDelete([]string{"ebs_block_device"})
|
2021-05-21 14:09:45 +00:00
|
|
|
instance.Attrs.SafeDelete([]string{"volume_tags"})
|
2021-04-29 15:17:55 +00:00
|
|
|
newRemoteResources = append(newRemoteResources, instance)
|
2021-04-29 10:32:02 +00:00
|
|
|
}
|
|
|
|
|
2020-12-09 15:31:34 +00:00
|
|
|
*resourcesFromState = newStateResources
|
2021-04-29 15:17:55 +00:00
|
|
|
*remoteResources = newRemoteResources
|
2020-12-09 15:31:34 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2021-06-22 09:26:39 +00:00
|
|
|
|
|
|
|
func (a AwsInstanceBlockDeviceResourceMapper) volumeTags(instance *resource.AbstractResource, blockDevice map[string]interface{}) interface{} {
|
|
|
|
if tags, exist := instance.Attrs.Get("volume_tags"); exist {
|
|
|
|
return tags
|
|
|
|
}
|
|
|
|
return blockDevice["tags"]
|
|
|
|
}
|
2021-06-22 13:40:33 +00:00
|
|
|
|
|
|
|
func (a AwsInstanceBlockDeviceResourceMapper) hasBlockDevice(blockDevice map[string]interface{}, resourcesFromState *[]resource.Resource) bool {
|
|
|
|
for _, stateRes := range *resourcesFromState {
|
|
|
|
if stateRes.TerraformType() == aws.AwsEbsVolumeResourceType &&
|
|
|
|
stateRes.TerraformId() == blockDevice["volume_id"] {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|