osbuild-service-maintenance: implement removal of launch templates

Launch templates of instances that are terminated should be removed.
HMS-3632
This commit is contained in:
Florian Schüller 2024-12-04 12:32:13 +01:00 committed by Florian Schüller
parent a96ea533c0
commit 65b7ee65b2
2 changed files with 74 additions and 0 deletions

View file

@ -93,6 +93,10 @@ func (a *AWS) DescribeInstancesBySecurityGroupID(securityGroupID string) ([]ec2t
return a.describeInstancesByKeyValue("instance.group-id", securityGroupID)
}
func (a *AWS) DescribeInstancesByLaunchTemplateID(launchTemplateID string) ([]ec2types.Reservation, error) {
return a.describeInstancesByKeyValue("tag:aws:ec2launchtemplate:id", launchTemplateID)
}
func (a *AWS) DescribeInstancesByInstanceID(instanceID string) ([]ec2types.Reservation, error) {
res, err := a.ec2.DescribeInstances(
context.Background(),
@ -132,6 +136,22 @@ func (a *AWS) DescribeSecurityGroupsByPrefix(ctx context.Context, prefix string)
return securityGroups, nil
}
func (a *AWS) DescribeLaunchTemplatesByPrefix(ctx context.Context, prefix string) ([]ec2types.LaunchTemplate, error) {
var launchTemplates []ec2types.LaunchTemplate
ltOutput, err := a.ec2.DescribeLaunchTemplates(ctx, &ec2.DescribeLaunchTemplatesInput{})
if err != nil {
return launchTemplates, fmt.Errorf("failed to describe security groups: %w", err)
}
for _, lt := range ltOutput.LaunchTemplates {
if lt.LaunchTemplateName != nil && strings.HasPrefix(*lt.LaunchTemplateName, prefix) {
launchTemplates = append(launchTemplates, lt)
}
}
return launchTemplates, nil
}
func (a *AWS) DeleteSecurityGroupById(ctx context.Context, sgID *string) error {
_, err := a.ec2.DeleteSecurityGroup(
ctx,
@ -141,3 +161,13 @@ func (a *AWS) DeleteSecurityGroupById(ctx context.Context, sgID *string) error {
)
return err
}
func (a *AWS) DeleteLaunchTemplateById(ctx context.Context, ltID *string) error {
_, err := a.ec2.DeleteLaunchTemplate(
ctx,
&ec2.DeleteLaunchTemplateInput{
LaunchTemplateId: ltID,
},
)
return err
}