-
Notifications
You must be signed in to change notification settings - Fork 4.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Get launch template versions after filtering templates #9909
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -301,68 +301,53 @@ func (t *LaunchTemplate) findAllLaunchTemplates(c *fi.Context) ([]*ec2.LaunchTem | |
} | ||
} | ||
|
||
// findAllLaunchTemplateVersions returns all the launch templates versions for us | ||
func (t *LaunchTemplate) findAllLaunchTemplatesVersions(c *fi.Context) ([]*ec2.LaunchTemplateVersion, error) { | ||
var list []*ec2.LaunchTemplateVersion | ||
|
||
// findLaunchTemplates returns a list of launch templates | ||
func (t *LaunchTemplate) findLaunchTemplates(c *fi.Context) ([]*ec2.LaunchTemplateVersion, error) { | ||
cloud, ok := c.Cloud.(awsup.AWSCloud) | ||
if !ok { | ||
return []*ec2.LaunchTemplateVersion{}, fmt.Errorf("invalid cloud provider: %v, expected: awsup.AWSCloud", c.Cloud) | ||
} | ||
|
||
// @step: get a list of the launch templates | ||
templates, err := t.findAllLaunchTemplates(c) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
prefix := fmt.Sprintf("%s-", fi.StringValue(t.Name)) | ||
|
||
// @step: get the launch template versions for the templates we are interested in | ||
var list []*ec2.LaunchTemplateVersion | ||
var next *string | ||
for _, x := range templates { | ||
err := func() error { | ||
for { | ||
resp, err := cloud.EC2().DescribeLaunchTemplateVersions(&ec2.DescribeLaunchTemplateVersionsInput{ | ||
LaunchTemplateName: x.LaunchTemplateName, | ||
NextToken: next, | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
list = append(list, resp.LaunchTemplateVersions...) | ||
if resp.NextToken == nil { | ||
return nil | ||
if strings.HasPrefix(aws.StringValue(x.LaunchTemplateName), prefix) { | ||
err := func() error { | ||
for { | ||
resp, err := cloud.EC2().DescribeLaunchTemplateVersions(&ec2.DescribeLaunchTemplateVersionsInput{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. FYI it looks like we could also use DescribeLaunchTemplateVersionsPages - that can be a separate PR though |
||
LaunchTemplateName: x.LaunchTemplateName, | ||
NextToken: next, | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
list = append(list, resp.LaunchTemplateVersions...) | ||
if resp.NextToken == nil { | ||
return nil | ||
} | ||
|
||
next = resp.NextToken | ||
} | ||
|
||
next = resp.NextToken | ||
}() | ||
if err != nil { | ||
return nil, err | ||
} | ||
}() | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
|
||
return list, nil | ||
} | ||
|
||
// findLaunchTemplates returns a list of launch templates | ||
func (t *LaunchTemplate) findLaunchTemplates(c *fi.Context) ([]*ec2.LaunchTemplateVersion, error) { | ||
// @step: get a list of the launch templates | ||
list, err := t.findAllLaunchTemplatesVersions(c) | ||
if err != nil { | ||
return nil, err | ||
} | ||
prefix := fmt.Sprintf("%s-", fi.StringValue(t.Name)) | ||
|
||
// @step: filter out the templates we are interested in | ||
var filtered []*ec2.LaunchTemplateVersion | ||
for _, x := range list { | ||
if strings.HasPrefix(aws.StringValue(x.LaunchTemplateName), prefix) { | ||
filtered = append(filtered, x) | ||
} | ||
} | ||
|
||
// @step: we can sort the configurations in chronological order | ||
sort.Slice(filtered, func(i, j int) bool { | ||
ti := filtered[i].CreateTime | ||
tj := filtered[j].CreateTime | ||
// @step: sort the configurations in chronological order | ||
sort.Slice(list, func(i, j int) bool { | ||
ti := list[i].CreateTime | ||
tj := list[j].CreateTime | ||
if tj == nil { | ||
return true | ||
} | ||
|
@@ -372,7 +357,7 @@ func (t *LaunchTemplate) findLaunchTemplates(c *fi.Context) ([]*ec2.LaunchTempla | |
return ti.UnixNano() < tj.UnixNano() | ||
}) | ||
|
||
return filtered, nil | ||
return list, nil | ||
} | ||
|
||
// findLatestLaunchTemplate returns the latest template | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Totally out of scope for this PR, but apparently filters can also support wildcards, e.g.
fmt.Sprintf("%s-*", name)
. I certainly didn't know that - so it wouldn't surprise me if we had multiple places where we could pre-filter by prefix.Source: https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/Using_Filtering.html#Filtering_Resources_CLI