packer-cn/builder/amazon/common/interpolate_build_info.go

46 lines
1.4 KiB
Go
Raw Normal View History

package common
import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/hashicorp/packer/builder"
"github.com/hashicorp/packer/helper/multistep"
)
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
}
func extractBuildInfo(region string, state multistep.StateBag, generatedData *builder.GeneratedData) *BuildInfoTemplate {
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)
}
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,
}
generatedData.Put("SourceAMIName", buildInfoTemplate.SourceAMIName)
return buildInfoTemplate
}