driftctl/pkg/middlewares/aws_instance_block_device.go

103 lines
4.0 KiB
Go
Raw Normal View History

package middlewares
import (
2021-03-29 16:10:50 +00:00
"github.com/sirupsen/logrus"
"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
}
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
}
}
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{})
logrus.WithFields(logrus.Fields{
2021-04-29 15:17:55 +00:00
"volume": rootBlock["volume_id"],
"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-04-29 15:17:55 +00:00
"tags": (*instance.Attrs)["volume_tags"],
}
2021-05-04 13:03:17 +00:00
newRes := a.resourceFactory.CreateAbstractResource("aws_ebs_volume", rootBlock["volume_id"].(string), data)
newStateResources = append(newStateResources, newRes)
}
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"})
}
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{})
logrus.WithFields(logrus.Fields{
2021-04-29 15:17:55 +00:00
"volume": blockDevice["volume_id"],
"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-04-29 15:17:55 +00:00
"tags": (*instance.Attrs)["volume_tags"],
2021-03-29 16:10:50 +00:00
}
2021-05-04 13:03:17 +00:00
newRes := a.resourceFactory.CreateAbstractResource("aws_ebs_volume", blockDevice["volume_id"].(string), data)
newStateResources = append(newStateResources, newRes)
}
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"})
}
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"})
newRemoteResources = append(newRemoteResources, instance)
2021-04-29 10:32:02 +00:00
}
*resourcesFromState = newStateResources
2021-04-29 15:17:55 +00:00
*remoteResources = newRemoteResources
return nil
}