github.phpd.cn/hashicorp/packer@v1.3.2/builder/amazon/common/interpolate_build_info.go (about)

     1  package common
     2  
     3  import (
     4  	"github.com/aws/aws-sdk-go/aws"
     5  	"github.com/aws/aws-sdk-go/service/ec2"
     6  	"github.com/hashicorp/packer/helper/multistep"
     7  )
     8  
     9  type BuildInfoTemplate struct {
    10  	BuildRegion   string
    11  	SourceAMI     string
    12  	SourceAMIName string
    13  	SourceAMITags map[string]string
    14  }
    15  
    16  func extractBuildInfo(region string, state multistep.StateBag) *BuildInfoTemplate {
    17  	rawSourceAMI, hasSourceAMI := state.GetOk("source_image")
    18  	if !hasSourceAMI {
    19  		return &BuildInfoTemplate{
    20  			BuildRegion: region,
    21  		}
    22  	}
    23  
    24  	sourceAMI := rawSourceAMI.(*ec2.Image)
    25  	sourceAMITags := make(map[string]string, len(sourceAMI.Tags))
    26  	for _, tag := range sourceAMI.Tags {
    27  		sourceAMITags[aws.StringValue(tag.Key)] = aws.StringValue(tag.Value)
    28  	}
    29  
    30  	return &BuildInfoTemplate{
    31  		BuildRegion:   region,
    32  		SourceAMI:     aws.StringValue(sourceAMI.ImageId),
    33  		SourceAMIName: aws.StringValue(sourceAMI.Name),
    34  		SourceAMITags: sourceAMITags,
    35  	}
    36  }