Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

resource/aws_launch_configuration: Add support for ebs_block_device.*.no_device #4070

Merged
merged 2 commits into from
Apr 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 40 additions & 3 deletions aws/resource_aws_launch_configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,12 @@ func resourceAwsLaunchConfiguration() *schema.Resource {
ForceNew: true,
},

"no_device": {
Type: schema.TypeBool,
Optional: true,
ForceNew: true,
},

"iops": {
Type: schema.TypeInt,
Optional: true,
Expand Down Expand Up @@ -338,8 +344,13 @@ func resourceAwsLaunchConfigurationCreate(d *schema.ResourceData, meta interface
vL := v.(*schema.Set).List()
for _, v := range vL {
bd := v.(map[string]interface{})
ebs := &autoscaling.Ebs{
DeleteOnTermination: aws.Bool(bd["delete_on_termination"].(bool)),
ebs := &autoscaling.Ebs{}

var noDevice *bool
if v, ok := bd["no_device"].(bool); ok && v {
noDevice = aws.Bool(v)
} else {
ebs.DeleteOnTermination = aws.Bool(bd["delete_on_termination"].(bool))
}

if v, ok := bd["snapshot_id"].(string); ok && v != "" {
Expand Down Expand Up @@ -369,6 +380,7 @@ func resourceAwsLaunchConfigurationCreate(d *schema.ResourceData, meta interface
blockDevices = append(blockDevices, &autoscaling.BlockDeviceMapping{
DeviceName: aws.String(bd["device_name"].(string)),
Ebs: ebs,
NoDevice: noDevice,
})
}
}
Expand Down Expand Up @@ -580,11 +592,33 @@ func readBlockDevicesFromLaunchConfiguration(d *schema.ResourceData, lc *autosca
var blank string
rootDeviceName = &blank
}

// Collect existing configured devices, so we can check
// existing value of delete_on_termination below
existingEbsBlockDevices := make(map[string]map[string]interface{}, 0)
if v, ok := d.GetOk("ebs_block_device"); ok {
ebsBlocks := v.(*schema.Set)
for _, ebd := range ebsBlocks.List() {
m := ebd.(map[string]interface{})
deviceName := m["device_name"].(string)
existingEbsBlockDevices[deviceName] = m
}
}

for _, bdm := range lc.BlockDeviceMappings {
bd := make(map[string]interface{})
if bdm.Ebs != nil && bdm.Ebs.DeleteOnTermination != nil {

if bdm.NoDevice != nil {
// Keep existing value in place to avoid spurious diff
deleteOnTermination := true
if device, ok := existingEbsBlockDevices[*bdm.DeviceName]; ok {
deleteOnTermination = device["delete_on_termination"].(bool)
}
bd["delete_on_termination"] = deleteOnTermination
} else if bdm.Ebs != nil && bdm.Ebs.DeleteOnTermination != nil {
bd["delete_on_termination"] = *bdm.Ebs.DeleteOnTermination
}

if bdm.Ebs != nil && bdm.Ebs.VolumeSize != nil {
bd["volume_size"] = *bdm.Ebs.VolumeSize
}
Expand All @@ -611,6 +645,9 @@ func readBlockDevicesFromLaunchConfiguration(d *schema.ResourceData, lc *autosca
if bdm.Ebs != nil && bdm.Ebs.SnapshotId != nil {
bd["snapshot_id"] = *bdm.Ebs.SnapshotId
}
if bdm.NoDevice != nil {
bd["no_device"] = *bdm.NoDevice
}
blockDevices["ebs"] = append(blockDevices["ebs"].([]map[string]interface{}), bd)
}
}
Expand Down
49 changes: 47 additions & 2 deletions aws/resource_aws_launch_configuration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,15 +281,37 @@ func TestAccAWSLaunchConfiguration_updateEbsBlockDevices(t *testing.T) {
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSLaunchConfigurationExists("aws_launch_configuration.baz", &conf),
resource.TestCheckResourceAttr(
"aws_launch_configuration.baz", "ebs_block_device.2764618555.volume_size", "9"),
"aws_launch_configuration.baz", "ebs_block_device.1393547169.volume_size", "9"),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this okay that it changed?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It worried me too when I saw that, but I did verify that state migration is not necessary:

$ terraform init && terraform apply && terraform plan

Initializing provider plugins...

Terraform has been successfully initialized!

You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.

If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  + aws_launch_configuration.baz
      id:                                                <computed>
      associate_public_ip_address:                       "false"
      ebs_block_device.#:                                "1"
      ebs_block_device.2764618555.delete_on_termination: "true"
      ebs_block_device.2764618555.device_name:           "/dev/sdb"
      ebs_block_device.2764618555.encrypted:             "true"
      ebs_block_device.2764618555.iops:                  <computed>
      ebs_block_device.2764618555.snapshot_id:           <computed>
      ebs_block_device.2764618555.volume_size:           "9"
      ebs_block_device.2764618555.volume_type:           <computed>
      ebs_optimized:                                     <computed>
      enable_monitoring:                                 "true"
      image_id:                                          "ami-5189a661"
      instance_type:                                     "t2.micro"
      key_name:                                          <computed>
      name:                                              <computed>
      root_block_device.#:                               "1"
      root_block_device.0.delete_on_termination:         "true"
      root_block_device.0.iops:                          <computed>
      root_block_device.0.volume_size:                   "11"
      root_block_device.0.volume_type:                   "gp2"


Plan: 1 to add, 0 to change, 0 to destroy.

Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: yes

aws_launch_configuration.baz: Creating...
  associate_public_ip_address:                       "" => "false"
  ebs_block_device.#:                                "" => "1"
  ebs_block_device.2764618555.delete_on_termination: "" => "true"
  ebs_block_device.2764618555.device_name:           "" => "/dev/sdb"
  ebs_block_device.2764618555.encrypted:             "" => "true"
  ebs_block_device.2764618555.iops:                  "" => "<computed>"
  ebs_block_device.2764618555.snapshot_id:           "" => "<computed>"
  ebs_block_device.2764618555.volume_size:           "" => "9"
  ebs_block_device.2764618555.volume_type:           "" => "<computed>"
  ebs_optimized:                                     "" => "<computed>"
  enable_monitoring:                                 "" => "true"
  image_id:                                          "" => "ami-5189a661"
  instance_type:                                     "" => "t2.micro"
  key_name:                                          "" => "<computed>"
  name:                                              "" => "<computed>"
  root_block_device.#:                               "" => "1"
  root_block_device.0.delete_on_termination:         "" => "true"
  root_block_device.0.iops:                          "" => "<computed>"
  root_block_device.0.volume_size:                   "" => "11"
  root_block_device.0.volume_type:                   "" => "gp2"
aws_launch_configuration.baz: Creation complete after 5s (ID: terraform-20180405195506238300000001)

Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
Refreshing Terraform state in-memory prior to plan...
The refreshed state will be used to calculate this plan, but will not be
persisted to local or remote state storage.

aws_launch_configuration.baz: Refreshing state... (ID: terraform-20180405195506238300000001)

------------------------------------------------------------------------

No changes. Infrastructure is up-to-date.

This means that Terraform did not detect any differences between your
configuration and real physical resources that exist. As a result, no
actions need to be performed.

$ terraform init && terraform plan

Initializing provider plugins...

Terraform has been successfully initialized!

You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.

If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.
Refreshing Terraform state in-memory prior to plan...
The refreshed state will be used to calculate this plan, but will not be
persisted to local or remote state storage.

aws_launch_configuration.baz: Refreshing state... (ID: terraform-20180405195506238300000001)

------------------------------------------------------------------------

No changes. Infrastructure is up-to-date.

This means that Terraform did not detect any differences between your
configuration and real physical resources that exist. As a result, no
actions need to be performed.

$ terraform show
aws_launch_configuration.baz:
  id = terraform-20180405195506238300000001
  associate_public_ip_address = false
  ebs_block_device.# = 1
  ebs_block_device.2764618555.delete_on_termination = true
  ebs_block_device.2764618555.device_name = /dev/sdb
  ebs_block_device.2764618555.encrypted = true
  ebs_block_device.2764618555.iops = 0
  ebs_block_device.2764618555.snapshot_id =
  ebs_block_device.2764618555.volume_size = 9
  ebs_block_device.2764618555.volume_type =
  ebs_optimized = false
  enable_monitoring = true
  ephemeral_block_device.# = 0
  iam_instance_profile =
  image_id = ami-5189a661
  instance_type = t2.micro
  key_name =
  name = terraform-20180405195506238300000001
  root_block_device.# = 1
  root_block_device.0.delete_on_termination = true
  root_block_device.0.iops = 0
  root_block_device.0.volume_size = 11
  root_block_device.0.volume_type = gp2
  security_groups.# = 0
  spot_price =
  vpc_classic_link_id =
  vpc_classic_link_security_groups.# = 0


$ terraform refresh
aws_launch_configuration.baz: Refreshing state... (ID: terraform-20180405195506238300000001)

$ terraform show
aws_launch_configuration.baz:
  id = terraform-20180405195506238300000001
  associate_public_ip_address = false
  ebs_block_device.# = 1
  ebs_block_device.1393547169.delete_on_termination = true
  ebs_block_device.1393547169.device_name = /dev/sdb
  ebs_block_device.1393547169.encrypted = true
  ebs_block_device.1393547169.iops = 0
  ebs_block_device.1393547169.no_device = false
  ebs_block_device.1393547169.snapshot_id =
  ebs_block_device.1393547169.volume_size = 9
  ebs_block_device.1393547169.volume_type =
  ebs_optimized = false
  enable_monitoring = true
  ephemeral_block_device.# = 0
  iam_instance_profile =
  image_id = ami-5189a661
  instance_type = t2.micro
  key_name =
  name = terraform-20180405195506238300000001
  root_block_device.# = 1
  root_block_device.0.delete_on_termination = true
  root_block_device.0.iops = 0
  root_block_device.0.volume_size = 11
  root_block_device.0.volume_type = gp2
  security_groups.# = 0
  spot_price =
  vpc_classic_link_id =
  vpc_classic_link_security_groups.# = 0

You can see in the last step during refresh that index is automatically re-computed and plan does not show any diff in the previous step. I don't know the exact reason behind that, but I'm confident that we don't need state migration.

),
},
{
Config: testAccAWSLaunchConfigurationWithEncryptionUpdated,
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSLaunchConfigurationExists("aws_launch_configuration.baz", &conf),
resource.TestCheckResourceAttr(
"aws_launch_configuration.baz", "ebs_block_device.3859927736.volume_size", "10"),
"aws_launch_configuration.baz", "ebs_block_device.4131155854.volume_size", "10"),
),
},
},
})
}

func TestAccAWSLaunchConfiguration_ebs_noDevice(t *testing.T) {
var conf autoscaling.LaunchConfiguration
rInt := acctest.RandInt()

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSLaunchConfigurationDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSLaunchConfigurationConfigEbsNoDevice(rInt),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSLaunchConfigurationExists("aws_launch_configuration.bar", &conf),
resource.TestCheckResourceAttr("aws_launch_configuration.bar", "ebs_block_device.#", "1"),
resource.TestCheckResourceAttr("aws_launch_configuration.bar", "ebs_block_device.3099842682.device_name", "/dev/sda2"),
resource.TestCheckResourceAttr("aws_launch_configuration.bar", "ebs_block_device.3099842682.no_device", "true"),
),
},
},
Expand Down Expand Up @@ -608,3 +630,26 @@ resource "aws_launch_configuration" "bar" {
}
`, rInt)
}

func testAccAWSLaunchConfigurationConfigEbsNoDevice(rInt int) string {
return fmt.Sprintf(`
data "aws_ami" "ubuntu" {
most_recent = true
filter {
name = "name"
values = ["ubuntu/images/ebs/ubuntu-precise-12.04-i386-server-*"]
}
owners = ["099720109477"] # Canonical
}
resource "aws_launch_configuration" "bar" {
name_prefix = "tf-acc-test-%d"
image_id = "${data.aws_ami.ubuntu.id}"
instance_type = "m1.small"
ebs_block_device {
device_name = "/dev/sda2"
no_device = true
}
}
`, rInt)
}