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-12-17 16:29:25 -05:00
|
|
|
"github.com/hashicorp/packer-plugin-sdk/multistep"
|
|
|
|
"github.com/hashicorp/packer-plugin-sdk/packerbuilderdata"
|
2018-03-30 04:47:11 -04:00
|
|
|
)
|
|
|
|
|
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-11-04 18:44:05 -05:00
|
|
|
func extractBuildInfo(region string, state multistep.StateBag, generatedData *packerbuilderdata.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-08-12 04:43:15 -04:00
|
|
|
|
|
|
|
generatedData.Put("BuildRegion", buildInfoTemplate.BuildRegion)
|
|
|
|
generatedData.Put("SourceAMI", buildInfoTemplate.SourceAMI)
|
|
|
|
generatedData.Put("SourceAMICreationDate", buildInfoTemplate.SourceAMICreationDate)
|
2020-01-20 10:29:38 -05:00
|
|
|
generatedData.Put("SourceAMIName", buildInfoTemplate.SourceAMIName)
|
2020-08-12 04:43:15 -04:00
|
|
|
generatedData.Put("SourceAMIOwner", buildInfoTemplate.SourceAMIOwner)
|
|
|
|
generatedData.Put("SourceAMIOwnerName", buildInfoTemplate.SourceAMIOwnerName)
|
|
|
|
|
2020-01-16 06:04:03 -05:00
|
|
|
return buildInfoTemplate
|
2017-01-10 05:41:28 -05:00
|
|
|
}
|
2020-08-12 04:43:15 -04:00
|
|
|
|
|
|
|
func GetGeneratedDataList() []string {
|
|
|
|
return []string{
|
|
|
|
"SourceAMIName",
|
|
|
|
"BuildRegion",
|
|
|
|
"SourceAMI",
|
|
|
|
"SourceAMICreationDate",
|
|
|
|
"SourceAMIOwner",
|
|
|
|
"SourceAMIOwnerName",
|
|
|
|
}
|
|
|
|
}
|