2017-01-10 05:41:28 -05:00
|
|
|
package common
|
|
|
|
|
2018-03-30 04:47:11 -04:00
|
|
|
import (
|
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
|
|
|
"github.com/aws/aws-sdk-go/service/ec2"
|
2020-01-20 10:29:38 -05:00
|
|
|
"github.com/hashicorp/packer/builder"
|
2018-03-30 04:47:11 -04:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
|
|
|
)
|
|
|
|
|
2017-01-10 05:41:28 -05:00
|
|
|
type BuildInfoTemplate struct {
|
2020-05-22 07:17:38 -04:00
|
|
|
BuildRegion string
|
|
|
|
SourceAMI string
|
|
|
|
SourceAMICreationDate string
|
|
|
|
SourceAMIName string
|
|
|
|
SourceAMIOwner string
|
|
|
|
SourceAMIOwnerName string
|
|
|
|
SourceAMITags map[string]string
|
2018-03-30 04:47:11 -04:00
|
|
|
}
|
|
|
|
|
2020-01-20 10:29:38 -05:00
|
|
|
func extractBuildInfo(region string, state multistep.StateBag, generatedData *builder.GeneratedData) *BuildInfoTemplate {
|
2018-03-30 04:47:11 -04:00
|
|
|
rawSourceAMI, hasSourceAMI := state.GetOk("source_image")
|
|
|
|
if !hasSourceAMI {
|
|
|
|
return &BuildInfoTemplate{
|
|
|
|
BuildRegion: region,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sourceAMI := rawSourceAMI.(*ec2.Image)
|
|
|
|
sourceAMITags := make(map[string]string, len(sourceAMI.Tags))
|
|
|
|
for _, tag := range sourceAMI.Tags {
|
|
|
|
sourceAMITags[aws.StringValue(tag.Key)] = aws.StringValue(tag.Value)
|
|
|
|
}
|
|
|
|
|
2020-01-16 06:04:03 -05:00
|
|
|
buildInfoTemplate := &BuildInfoTemplate{
|
2020-05-22 07:17:38 -04:00
|
|
|
BuildRegion: region,
|
|
|
|
SourceAMI: aws.StringValue(sourceAMI.ImageId),
|
|
|
|
SourceAMICreationDate: aws.StringValue(sourceAMI.CreationDate),
|
|
|
|
SourceAMIName: aws.StringValue(sourceAMI.Name),
|
|
|
|
SourceAMIOwner: aws.StringValue(sourceAMI.OwnerId),
|
|
|
|
SourceAMIOwnerName: aws.StringValue(sourceAMI.ImageOwnerAlias),
|
|
|
|
SourceAMITags: sourceAMITags,
|
2018-03-30 04:47:11 -04:00
|
|
|
}
|
2020-01-20 10:29:38 -05:00
|
|
|
generatedData.Put("SourceAMIName", buildInfoTemplate.SourceAMIName)
|
2020-01-16 06:04:03 -05:00
|
|
|
return buildInfoTemplate
|
2017-01-10 05:41:28 -05:00
|
|
|
}
|