generated from hashicorp/packer-plugin-scaffolding
-
Notifications
You must be signed in to change notification settings - Fork 104
/
interpolate_build_info.go
63 lines (55 loc) · 1.99 KB
/
interpolate_build_info.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package common
import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/hashicorp/packer-plugin-sdk/multistep"
"github.com/hashicorp/packer-plugin-sdk/packerbuilderdata"
)
type BuildInfoTemplate struct {
BuildRegion string
SourceAMI string
SourceAMICreationDate string
SourceAMIName string
SourceAMIOwner string
SourceAMIOwnerName string
SourceAMITags map[string]string
}
func extractBuildInfo(region string, state multistep.StateBag, generatedData *packerbuilderdata.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{
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("BuildRegion", buildInfoTemplate.BuildRegion)
generatedData.Put("SourceAMI", buildInfoTemplate.SourceAMI)
generatedData.Put("SourceAMICreationDate", buildInfoTemplate.SourceAMICreationDate)
generatedData.Put("SourceAMIName", buildInfoTemplate.SourceAMIName)
generatedData.Put("SourceAMIOwner", buildInfoTemplate.SourceAMIOwner)
generatedData.Put("SourceAMIOwnerName", buildInfoTemplate.SourceAMIOwnerName)
return buildInfoTemplate
}
func GetGeneratedDataList() []string {
return []string{
"SourceAMIName",
"BuildRegion",
"SourceAMI",
"SourceAMICreationDate",
"SourceAMIOwner",
"SourceAMIOwnerName",
}
}