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

r/aws_launch_template: Fix various bugs #4321

Merged
merged 5 commits into from
Apr 24, 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
26 changes: 13 additions & 13 deletions aws/resource_aws_launch_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,7 @@ func getBlockDeviceMappings(m []*ec2.LaunchTemplateBlockDeviceMapping) []interfa
ebs["snapshot_id"] = aws.StringValue(v.Ebs.SnapshotId)
}

mapping["ebs"] = ebs
mapping["ebs"] = []interface{}{ebs}
Copy link
Contributor

Choose a reason for hiding this comment

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

An acceptance test implementing this part of the resource would've caught this. I'll add one.

}
s = append(s, mapping)
}
Expand Down Expand Up @@ -882,16 +882,16 @@ func buildLaunchTemplateData(d *schema.ResourceData, meta interface{}) (*ec2.Req
func readBlockDeviceMappingFromConfig(bdm map[string]interface{}) *ec2.LaunchTemplateBlockDeviceMappingRequest {
blockDeviceMapping := &ec2.LaunchTemplateBlockDeviceMappingRequest{}

if v := bdm["device_name"]; v != nil {
blockDeviceMapping.DeviceName = aws.String(v.(string))
if v := bdm["device_name"].(string); v != "" {
blockDeviceMapping.DeviceName = aws.String(v)
}

if v := bdm["no_device"]; v != nil {
blockDeviceMapping.NoDevice = aws.String(v.(string))
if v := bdm["no_device"].(string); v != "" {
blockDeviceMapping.NoDevice = aws.String(v)
}

if v := bdm["virtual_name"]; v != nil {
blockDeviceMapping.VirtualName = aws.String(v.(string))
if v := bdm["virtual_name"].(string); v != "" {
blockDeviceMapping.VirtualName = aws.String(v)
}

if v := bdm["ebs"]; len(v.([]interface{})) > 0 {
Expand Down Expand Up @@ -920,20 +920,20 @@ func readEbsBlockDeviceFromConfig(ebs map[string]interface{}) *ec2.LaunchTemplat
ebsDevice.Iops = aws.Int64(int64(v.(int)))
}

if v := ebs["kms_key_id"]; v != nil {
ebsDevice.KmsKeyId = aws.String(v.(string))
if v := ebs["kms_key_id"].(string); v != "" {
ebsDevice.KmsKeyId = aws.String(v)
}

if v := ebs["snapshot_id"]; v != nil {
ebsDevice.SnapshotId = aws.String(v.(string))
if v := ebs["snapshot_id"].(string); v != "" {
ebsDevice.SnapshotId = aws.String(v)
}

if v := ebs["volume_size"]; v != nil {
ebsDevice.VolumeSize = aws.Int64(int64(v.(int)))
}

if v := ebs["volume_type"]; v != nil {
ebsDevice.VolumeType = aws.String(v.(string))
if v := ebs["volume_type"].(string); v != "" {
ebsDevice.VolumeType = aws.String(v)
}

return ebsDevice
Expand Down
4 changes: 2 additions & 2 deletions aws/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -1751,8 +1751,8 @@ func validateLaunchTemplateName(v interface{}, k string) (ws []string, errors []
errors = append(errors, fmt.Errorf("%q cannot be longer than 99 characters, name is limited to 125", k))
} else if !strings.HasSuffix(k, "prefix") && len(value) > 125 {
errors = append(errors, fmt.Errorf("%q cannot be longer than 125 characters", k))
} else if !regexp.MustCompile(`^[0-9a-zA-Z()./_]+$`).MatchString(value) {
errors = append(errors, fmt.Errorf("%q can only alphanumeric characters and ()./_ symbols", k))
} else if !regexp.MustCompile(`^[0-9a-zA-Z()./_\-]+$`).MatchString(value) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Adding a dash here looks good according to the EC2 API documentation: https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_CreateLaunchTemplate.html

LaunchTemplateName
A name for the launch template.

Type: String

Length Constraints: Minimum length of 3. Maximum length of 128.

Pattern: [a-zA-Z0-9\(\)\.-/_]+

Required: Yes

errors = append(errors, fmt.Errorf("%q can only alphanumeric characters and ()./_- symbols", k))
}
return
}
1 change: 0 additions & 1 deletion aws/validators_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2537,7 +2537,6 @@ func TestValidateLaunchTemplateName(t *testing.T) {
invalidNames := []string{
"tf",
strings.Repeat("W", 126), // > 125
"invalid-",
"invalid*",
"invalid\name",
"inavalid&",
Expand Down
14 changes: 11 additions & 3 deletions website/docs/r/launch_template.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,24 @@ description: |-

Provides an EC2 launch template resource. Can be used to create instances or auto scaling groups.

-> **Note:** All arguments are optional except for either `name`, or `name_prefix`.
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm curious why this was removed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As noted in the commit message, terraform will generate a name if the name is missing. This is also noted in the description of the attribute:

The name of the launch template. If you leave this blank, Terraform will auto-generate a unique name.


## Example Usage

```hcl
resource "aws_launch_template" "foo" {
name = "foo"

block_device_mappings {
device_name = "test"
# to change the type or size of the root volume, override the ami's root device name
Copy link
Contributor

Choose a reason for hiding this comment

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

We should add this helper information to the block_device_mappings documentation below. I'll adjust on merge 👍 More importantly its better to get the updated device_name information in the example.

# you can figure this out based on the ami id by running:
# aws ec2 describe-images --region us-west-2 --image-id ami-4e79ed36
device_name = "/dev/sda1"
ebs {
volume_size = 20
}
}

block_device_mappings {
device_name = "/dev/xvdb"
}

credit_specification {
Expand Down