Skip to content
This repository has been archived by the owner on Jan 22, 2025. It is now read-only.

Commit

Permalink
find launch template by id
Browse files Browse the repository at this point in the history
  • Loading branch information
nikpivkin committed Dec 19, 2023
1 parent 93e2cea commit 4d90647
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 7 deletions.
48 changes: 48 additions & 0 deletions internal/adapters/cloudformation/aws/ec2/adapt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,54 @@ Resources:
ImageId: "ami-79fd7eee"
LaunchTemplate:
LaunchTemplateName: MyTemplate
`,
expected: ec2.EC2{
LaunchTemplates: []ec2.LaunchTemplate{
{
Metadata: types.NewTestMetadata(),
Name: types.String("MyTemplate", types.NewTestMetadata()),
Instance: ec2.Instance{
Metadata: types.NewTestMetadata(),
MetadataOptions: ec2.MetadataOptions{
HttpEndpoint: types.String("enabled", types.NewTestMetadata()),
HttpTokens: types.String("required", types.NewTestMetadata()),
},
},
},
},
Instances: []ec2.Instance{
{
Metadata: types.NewTestMetadata(),
MetadataOptions: ec2.MetadataOptions{
HttpEndpoint: types.String("enabled", types.NewTestMetadata()),
HttpTokens: types.String("required", types.NewTestMetadata()),
},
RootBlockDevice: &ec2.BlockDevice{
Metadata: types.NewTestMetadata(),
Encrypted: types.Bool(false, types.NewTestMetadata()),
},
},
},
},
},
{
name: "ec2 instance with launch template, ref to id",
source: `AWSTemplateFormatVersion: 2010-09-09
Resources:
MyLaunchTemplate:
Type: AWS::EC2::LaunchTemplate
Properties:
LaunchTemplateName: MyTemplate
LaunchTemplateData:
MetadataOptions:
HttpEndpoint: enabled
HttpTokens: required
MyEC2Instance:
Type: AWS::EC2::Instance
Properties:
ImageId: "ami-79fd7eee"
LaunchTemplate:
LaunchTemplateId: !Ref MyLaunchTemplate
`,
expected: ec2.EC2{
LaunchTemplates: []ec2.LaunchTemplate{
Expand Down
28 changes: 21 additions & 7 deletions internal/adapters/cloudformation/aws/ec2/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func getInstances(ctx parser.FileContext) (instances []ec2.Instance) {
UserData: r.GetStringProperty("UserData"),
}

if launchTemplate := findRelatedLaunchTemplate(ctx, r); launchTemplate != nil {
if launchTemplate, ok := findRelatedLaunchTemplate(ctx, r); ok {
instance = launchTemplate.Instance
}

Expand All @@ -48,22 +48,36 @@ func getInstances(ctx parser.FileContext) (instances []ec2.Instance) {
return instances
}

func findRelatedLaunchTemplate(fctx parser.FileContext, r *parser.Resource) *ec2.LaunchTemplate {
func findRelatedLaunchTemplate(fctx parser.FileContext, r *parser.Resource) (ec2.LaunchTemplate, bool) {
launchTemplateRef := r.GetProperty("LaunchTemplate.LaunchTemplateName")
if launchTemplateRef.IsString() {
res := findLaunchTemplateByName(fctx, launchTemplateRef)
if res != nil {
return adaptLaunchTemplate(res), true
}
}

if !launchTemplateRef.IsString() || launchTemplateRef.IsEmpty() {
return nil
launchTemplateRef = r.GetProperty("LaunchTemplate.LaunchTemplateId")
if !launchTemplateRef.IsString() {
return ec2.LaunchTemplate{}, false
}

resource := fctx.GetResourceByLogicalID(launchTemplateRef.AsString())
if resource == nil {
return ec2.LaunchTemplate{}, false
}
return adaptLaunchTemplate(resource), true
}

func findLaunchTemplateByName(fctx parser.FileContext, prop *parser.Property) *parser.Resource {
for _, res := range fctx.GetResourcesByType("AWS::EC2::LaunchTemplate") {
templateName := res.GetProperty("LaunchTemplateName")
if templateName.IsNotString() {
continue
}

if launchTemplateRef.EqualTo(templateName.AsString()) {
launchTemplate := adaptLaunchTemplate(res)
return &launchTemplate
if prop.EqualTo(templateName.AsString()) {
return res
}
}

Expand Down

0 comments on commit 4d90647

Please sign in to comment.