mirror of
https://github.com/opentofu/opentofu.git
synced 2025-02-20 11:48:24 -06:00
Instance block devices are now managed by three distinct sub-resources: * `root_block_device` - introduced previously * `ebs_block_device` - all additional ebs-backed volumes * `ephemeral_block_device` - instance store / ephemeral devices The AWS API support around BlockDeviceMapping is pretty confusing. It's a single collection type that supports these three members each of which has different fields and different behavior. My biggest hiccup came from the fact that Instance Store volumes do not show up in any response BlockDeviceMapping for any EC2 `Describe*` API calls. They're only available from the instance meta-data service as queried from inside the node. This removes `block_device` altogether for a clean break from old configs. New configs will need to sort their `block_device` declarations into the three new types. The field has been marked `Removed` to indicate this to users. With the new block device format being introduced, we need to ensure Terraform is able to properly read statefiles written in the old format. So we use the new `helper/schema` facility of "state migrations" to transform statefiles in the old format to something that the current version of the schema can use. Fixes #858
136 lines
5.9 KiB
Go
136 lines
5.9 KiB
Go
package aws
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
)
|
|
|
|
func TestAWSInstanceMigrateState(t *testing.T) {
|
|
cases := map[string]struct {
|
|
StateVersion int
|
|
Attributes map[string]string
|
|
Expected map[string]string
|
|
Meta interface{}
|
|
}{
|
|
"v0.3.6 and earlier": {
|
|
StateVersion: 0,
|
|
Attributes: map[string]string{
|
|
// EBS
|
|
"block_device.#": "2",
|
|
"block_device.3851383343.delete_on_termination": "true",
|
|
"block_device.3851383343.device_name": "/dev/sdx",
|
|
"block_device.3851383343.encrypted": "false",
|
|
"block_device.3851383343.snapshot_id": "",
|
|
"block_device.3851383343.virtual_name": "",
|
|
"block_device.3851383343.volume_size": "5",
|
|
"block_device.3851383343.volume_type": "standard",
|
|
// Ephemeral
|
|
"block_device.3101711606.delete_on_termination": "false",
|
|
"block_device.3101711606.device_name": "/dev/sdy",
|
|
"block_device.3101711606.encrypted": "false",
|
|
"block_device.3101711606.snapshot_id": "",
|
|
"block_device.3101711606.virtual_name": "ephemeral0",
|
|
"block_device.3101711606.volume_size": "",
|
|
"block_device.3101711606.volume_type": "",
|
|
// Root
|
|
"block_device.56575650.delete_on_termination": "true",
|
|
"block_device.56575650.device_name": "/dev/sda1",
|
|
"block_device.56575650.encrypted": "false",
|
|
"block_device.56575650.snapshot_id": "",
|
|
"block_device.56575650.volume_size": "10",
|
|
"block_device.56575650.volume_type": "standard",
|
|
},
|
|
Expected: map[string]string{
|
|
"ebs_block_device.#": "1",
|
|
"ebs_block_device.3851383343.delete_on_termination": "true",
|
|
"ebs_block_device.3851383343.device_name": "/dev/sdx",
|
|
"ebs_block_device.3851383343.encrypted": "false",
|
|
"ebs_block_device.3851383343.snapshot_id": "",
|
|
"ebs_block_device.3851383343.volume_size": "5",
|
|
"ebs_block_device.3851383343.volume_type": "standard",
|
|
"ephemeral_block_device.#": "1",
|
|
"ephemeral_block_device.2458403513.device_name": "/dev/sdy",
|
|
"ephemeral_block_device.2458403513.virtual_name": "ephemeral0",
|
|
"root_block_device.#": "1",
|
|
"root_block_device.3018388612.delete_on_termination": "true",
|
|
"root_block_device.3018388612.device_name": "/dev/sda1",
|
|
"root_block_device.3018388612.snapshot_id": "",
|
|
"root_block_device.3018388612.volume_size": "10",
|
|
"root_block_device.3018388612.volume_type": "standard",
|
|
},
|
|
},
|
|
"v0.3.7": {
|
|
StateVersion: 0,
|
|
Attributes: map[string]string{
|
|
// EBS
|
|
"block_device.#": "2",
|
|
"block_device.3851383343.delete_on_termination": "true",
|
|
"block_device.3851383343.device_name": "/dev/sdx",
|
|
"block_device.3851383343.encrypted": "false",
|
|
"block_device.3851383343.snapshot_id": "",
|
|
"block_device.3851383343.virtual_name": "",
|
|
"block_device.3851383343.volume_size": "5",
|
|
"block_device.3851383343.volume_type": "standard",
|
|
"block_device.3851383343.iops": "",
|
|
// Ephemeral
|
|
"block_device.3101711606.delete_on_termination": "false",
|
|
"block_device.3101711606.device_name": "/dev/sdy",
|
|
"block_device.3101711606.encrypted": "false",
|
|
"block_device.3101711606.snapshot_id": "",
|
|
"block_device.3101711606.virtual_name": "ephemeral0",
|
|
"block_device.3101711606.volume_size": "",
|
|
"block_device.3101711606.volume_type": "",
|
|
"block_device.3101711606.iops": "",
|
|
// Root
|
|
"root_block_device.#": "1",
|
|
"root_block_device.3018388612.delete_on_termination": "true",
|
|
"root_block_device.3018388612.device_name": "/dev/sda1",
|
|
"root_block_device.3018388612.snapshot_id": "",
|
|
"root_block_device.3018388612.volume_size": "10",
|
|
"root_block_device.3018388612.volume_type": "io1",
|
|
"root_block_device.3018388612.iops": "1000",
|
|
},
|
|
Expected: map[string]string{
|
|
"ebs_block_device.#": "1",
|
|
"ebs_block_device.3851383343.delete_on_termination": "true",
|
|
"ebs_block_device.3851383343.device_name": "/dev/sdx",
|
|
"ebs_block_device.3851383343.encrypted": "false",
|
|
"ebs_block_device.3851383343.snapshot_id": "",
|
|
"ebs_block_device.3851383343.volume_size": "5",
|
|
"ebs_block_device.3851383343.volume_type": "standard",
|
|
"ephemeral_block_device.#": "1",
|
|
"ephemeral_block_device.2458403513.device_name": "/dev/sdy",
|
|
"ephemeral_block_device.2458403513.virtual_name": "ephemeral0",
|
|
"root_block_device.#": "1",
|
|
"root_block_device.3018388612.delete_on_termination": "true",
|
|
"root_block_device.3018388612.device_name": "/dev/sda1",
|
|
"root_block_device.3018388612.snapshot_id": "",
|
|
"root_block_device.3018388612.volume_size": "10",
|
|
"root_block_device.3018388612.volume_type": "io1",
|
|
"root_block_device.3018388612.iops": "1000",
|
|
},
|
|
},
|
|
}
|
|
|
|
for tn, tc := range cases {
|
|
is := &terraform.InstanceState{
|
|
Attributes: tc.Attributes,
|
|
}
|
|
is, err := resourceAwsInstanceMigrateState(
|
|
tc.StateVersion, is, tc.Meta)
|
|
|
|
if err != nil {
|
|
t.Fatalf("bad: %s, err: %#v", tn, err)
|
|
}
|
|
|
|
for k, v := range tc.Expected {
|
|
if is.Attributes[k] != v {
|
|
t.Fatalf(
|
|
"bad: %s\n\n expected: %#v -> %#v\n got: %#v -> %#v\n in: %#v",
|
|
tn, k, v, k, is.Attributes[k], is.Attributes)
|
|
}
|
|
}
|
|
}
|
|
}
|