cloud/aws: move maintenance calls to separate file
This commit is contained in:
parent
66c2c31a1c
commit
8fc91d1c6d
4 changed files with 95 additions and 75 deletions
|
|
@ -534,59 +534,6 @@ func (a *AWS) shareSnapshot(snapshotId string, userIds []string) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *AWS) RemoveSnapshotAndDeregisterImage(image *ec2types.Image) error {
|
|
||||||
if image == nil {
|
|
||||||
return fmt.Errorf("image is nil")
|
|
||||||
}
|
|
||||||
|
|
||||||
var snapshots []*string
|
|
||||||
for _, bdm := range image.BlockDeviceMappings {
|
|
||||||
snapshots = append(snapshots, bdm.Ebs.SnapshotId)
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err := a.ec2.DeregisterImage(
|
|
||||||
context.Background(),
|
|
||||||
&ec2.DeregisterImageInput{
|
|
||||||
ImageId: image.ImageId,
|
|
||||||
},
|
|
||||||
)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, s := range snapshots {
|
|
||||||
_, err = a.ec2.DeleteSnapshot(
|
|
||||||
context.Background(),
|
|
||||||
&ec2.DeleteSnapshotInput{
|
|
||||||
SnapshotId: s,
|
|
||||||
},
|
|
||||||
)
|
|
||||||
if err != nil {
|
|
||||||
// TODO return err?
|
|
||||||
logrus.Warn("Unable to remove snapshot", s)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// For service maintenance images are discovered by the "Name:composer-api-*" tag filter. Currently
|
|
||||||
// all image names in the service are generated, so they're guaranteed to be unique as well. If
|
|
||||||
// users are ever allowed to name their images, an extra tag should be added.
|
|
||||||
func (a *AWS) DescribeImagesByTag(tagKey, tagValue string) ([]ec2types.Image, error) {
|
|
||||||
imgs, err := a.ec2.DescribeImages(
|
|
||||||
context.Background(),
|
|
||||||
&ec2.DescribeImagesInput{
|
|
||||||
Filters: []ec2types.Filter{
|
|
||||||
{
|
|
||||||
Name: aws.String(fmt.Sprintf("tag:%s", tagKey)),
|
|
||||||
Values: []string{tagValue},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
)
|
|
||||||
return imgs.Images, err
|
|
||||||
}
|
|
||||||
|
|
||||||
func (a *AWS) S3ObjectPresignedURL(bucket, objectKey string) (string, error) {
|
func (a *AWS) S3ObjectPresignedURL(bucket, objectKey string) (string, error) {
|
||||||
logrus.Infof("[AWS] 📋 Generating Presigned URL for S3 object %s/%s", bucket, objectKey)
|
logrus.Infof("[AWS] 📋 Generating Presigned URL for S3 object %s/%s", bucket, objectKey)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,6 @@ import (
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
ec2types "github.com/aws/aws-sdk-go-v2/service/ec2/types"
|
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
"github.com/osbuild/osbuild-composer/internal/cloud/awscloud"
|
"github.com/osbuild/osbuild-composer/internal/cloud/awscloud"
|
||||||
|
|
@ -78,27 +77,6 @@ func TestEC2CopyImage(t *testing.T) {
|
||||||
require.Equal(t, 2, m.calledFn["CreateTags"])
|
require.Equal(t, 2, m.calledFn["CreateTags"])
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestEC2RemoveSnapshotAndDeregisterImage(t *testing.T) {
|
|
||||||
m := newEc2Mock(t)
|
|
||||||
aws := awscloud.NewForTest(m, nil, &s3mock{t, "bucket", "object-key"}, nil, nil)
|
|
||||||
require.NotNil(t, aws)
|
|
||||||
|
|
||||||
err := aws.RemoveSnapshotAndDeregisterImage(&ec2types.Image{
|
|
||||||
ImageId: &m.imageId,
|
|
||||||
State: ec2types.ImageStateAvailable,
|
|
||||||
BlockDeviceMappings: []ec2types.BlockDeviceMapping{
|
|
||||||
{
|
|
||||||
Ebs: &ec2types.EbsBlockDevice{
|
|
||||||
SnapshotId: &m.snapshotId,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
})
|
|
||||||
require.NoError(t, err)
|
|
||||||
require.Equal(t, 1, m.calledFn["DeleteSnapshot"])
|
|
||||||
require.Equal(t, 1, m.calledFn["DeregisterImage"])
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestEC2Regions(t *testing.T) {
|
func TestEC2Regions(t *testing.T) {
|
||||||
m := newEc2Mock(t)
|
m := newEc2Mock(t)
|
||||||
aws := awscloud.NewForTest(m, nil, &s3mock{t, "bucket", "object-key"}, nil, nil)
|
aws := awscloud.NewForTest(m, nil, &s3mock{t, "bucket", "object-key"}, nil, nil)
|
||||||
|
|
|
||||||
64
internal/cloud/awscloud/maintenance.go
Normal file
64
internal/cloud/awscloud/maintenance.go
Normal file
|
|
@ -0,0 +1,64 @@
|
||||||
|
package awscloud
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/aws/aws-sdk-go-v2/aws"
|
||||||
|
"github.com/aws/aws-sdk-go-v2/service/ec2"
|
||||||
|
ec2types "github.com/aws/aws-sdk-go-v2/service/ec2/types"
|
||||||
|
"github.com/sirupsen/logrus"
|
||||||
|
)
|
||||||
|
|
||||||
|
// For service maintenance images are discovered by the "Name:composer-api-*" tag filter. Currently
|
||||||
|
// all image names in the service are generated, so they're guaranteed to be unique as well. If
|
||||||
|
// users are ever allowed to name their images, an extra tag should be added.
|
||||||
|
func (a *AWS) DescribeImagesByTag(tagKey, tagValue string) ([]ec2types.Image, error) {
|
||||||
|
imgs, err := a.ec2.DescribeImages(
|
||||||
|
context.Background(),
|
||||||
|
&ec2.DescribeImagesInput{
|
||||||
|
Filters: []ec2types.Filter{
|
||||||
|
{
|
||||||
|
Name: aws.String(fmt.Sprintf("tag:%s", tagKey)),
|
||||||
|
Values: []string{tagValue},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
)
|
||||||
|
return imgs.Images, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *AWS) RemoveSnapshotAndDeregisterImage(image *ec2types.Image) error {
|
||||||
|
if image == nil {
|
||||||
|
return fmt.Errorf("image is nil")
|
||||||
|
}
|
||||||
|
|
||||||
|
var snapshots []*string
|
||||||
|
for _, bdm := range image.BlockDeviceMappings {
|
||||||
|
snapshots = append(snapshots, bdm.Ebs.SnapshotId)
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err := a.ec2.DeregisterImage(
|
||||||
|
context.Background(),
|
||||||
|
&ec2.DeregisterImageInput{
|
||||||
|
ImageId: image.ImageId,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, s := range snapshots {
|
||||||
|
_, err = a.ec2.DeleteSnapshot(
|
||||||
|
context.Background(),
|
||||||
|
&ec2.DeleteSnapshotInput{
|
||||||
|
SnapshotId: s,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
// TODO return err?
|
||||||
|
logrus.Warn("Unable to remove snapshot", s)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
31
internal/cloud/awscloud/maintenance_test.go
Normal file
31
internal/cloud/awscloud/maintenance_test.go
Normal file
|
|
@ -0,0 +1,31 @@
|
||||||
|
package awscloud_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
ec2types "github.com/aws/aws-sdk-go-v2/service/ec2/types"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
|
"github.com/osbuild/osbuild-composer/internal/cloud/awscloud"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestEC2RemoveSnapshotAndDeregisterImage(t *testing.T) {
|
||||||
|
m := newEc2Mock(t)
|
||||||
|
aws := awscloud.NewForTest(m, nil, &s3mock{t, "bucket", "object-key"}, nil, nil)
|
||||||
|
require.NotNil(t, aws)
|
||||||
|
|
||||||
|
err := aws.RemoveSnapshotAndDeregisterImage(&ec2types.Image{
|
||||||
|
ImageId: &m.imageId,
|
||||||
|
State: ec2types.ImageStateAvailable,
|
||||||
|
BlockDeviceMappings: []ec2types.BlockDeviceMapping{
|
||||||
|
{
|
||||||
|
Ebs: &ec2types.EbsBlockDevice{
|
||||||
|
SnapshotId: &m.snapshotId,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Equal(t, 1, m.calledFn["DeleteSnapshot"])
|
||||||
|
require.Equal(t, 1, m.calledFn["DeregisterImage"])
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue