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

data-source/aws_ami: Prevent panics with AMIs in failed image state #5968

Merged
merged 1 commit into from
Sep 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
36 changes: 14 additions & 22 deletions aws/data_source_aws_ami.go
Original file line number Diff line number Diff line change
Expand Up @@ -341,31 +341,23 @@ func amiBlockDeviceMappings(m []*ec2.BlockDeviceMapping) *schema.Set {
}
for _, v := range m {
mapping := map[string]interface{}{
"device_name": *v.DeviceName,
"device_name": aws.StringValue(v.DeviceName),
"virtual_name": aws.StringValue(v.VirtualName),
}

if v.Ebs != nil {
ebs := map[string]interface{}{
"delete_on_termination": fmt.Sprintf("%t", *v.Ebs.DeleteOnTermination),
"encrypted": fmt.Sprintf("%t", *v.Ebs.Encrypted),
"volume_size": fmt.Sprintf("%d", *v.Ebs.VolumeSize),
"volume_type": *v.Ebs.VolumeType,
}
// Iops is not always set
if v.Ebs.Iops != nil {
ebs["iops"] = fmt.Sprintf("%d", *v.Ebs.Iops)
} else {
ebs["iops"] = "0"
}
// snapshot id may not be set
if v.Ebs.SnapshotId != nil {
ebs["snapshot_id"] = *v.Ebs.SnapshotId
"delete_on_termination": fmt.Sprintf("%t", aws.BoolValue(v.Ebs.DeleteOnTermination)),
"encrypted": fmt.Sprintf("%t", aws.BoolValue(v.Ebs.Encrypted)),
"iops": fmt.Sprintf("%d", aws.Int64Value(v.Ebs.Iops)),
"volume_size": fmt.Sprintf("%d", aws.Int64Value(v.Ebs.VolumeSize)),
"snapshot_id": aws.StringValue(v.Ebs.SnapshotId),
"volume_type": aws.StringValue(v.Ebs.VolumeType),
}

mapping["ebs"] = ebs
}
if v.VirtualName != nil {
mapping["virtual_name"] = *v.VirtualName
}

log.Printf("[DEBUG] aws_ami - adding block device mapping: %v", mapping)
s.Add(mapping)
}
Expand All @@ -379,8 +371,8 @@ func amiProductCodes(m []*ec2.ProductCode) *schema.Set {
}
for _, v := range m {
code := map[string]interface{}{
"product_code_id": *v.ProductCodeId,
"product_code_type": *v.ProductCodeType,
"product_code_id": aws.StringValue(v.ProductCodeId),
"product_code_type": aws.StringValue(v.ProductCodeType),
}
s.Add(code)
}
Expand All @@ -407,8 +399,8 @@ func amiRootSnapshotId(image *ec2.Image) string {
func amiStateReason(m *ec2.StateReason) map[string]interface{} {
s := make(map[string]interface{})
if m != nil {
s["code"] = *m.Code
s["message"] = *m.Message
s["code"] = aws.StringValue(m.Code)
s["message"] = aws.StringValue(m.Message)
} else {
s["code"] = "UNSET"
s["message"] = "UNSET"
Expand Down
2 changes: 1 addition & 1 deletion aws/data_source_aws_ami_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func TestAccAWSAmiDataSource_windowsInstance(t *testing.T) {
resource.TestCheckResourceAttr("data.aws_ami.windows_ami", "architecture", "x86_64"),
resource.TestCheckResourceAttr("data.aws_ami.windows_ami", "block_device_mappings.#", "27"),
resource.TestMatchResourceAttr("data.aws_ami.windows_ami", "creation_date", regexp.MustCompile("^20[0-9]{2}-")),
resource.TestMatchResourceAttr("data.aws_ami.windows_ami", "description", regexp.MustCompile("^Microsoft Windows Server 2012")),
resource.TestMatchResourceAttr("data.aws_ami.windows_ami", "description", regexp.MustCompile("^Microsoft Windows Server")),
resource.TestCheckResourceAttr("data.aws_ami.windows_ami", "hypervisor", "xen"),
resource.TestMatchResourceAttr("data.aws_ami.windows_ami", "image_id", regexp.MustCompile("^ami-")),
resource.TestMatchResourceAttr("data.aws_ami.windows_ami", "image_location", regexp.MustCompile("^amazon/")),
Expand Down