forked from ekristen/aws-nuke
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmgn-source-server.go
107 lines (83 loc) · 2.31 KB
/
mgn-source-server.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package resources
import (
"context"
"errors"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/mgn"
"github.com/ekristen/libnuke/pkg/registry"
"github.com/ekristen/libnuke/pkg/resource"
"github.com/ekristen/libnuke/pkg/types"
"github.com/ekristen/aws-nuke/v3/pkg/nuke"
)
const MGNSourceServerResource = "MGNSourceServer"
func init() {
registry.Register(®istry.Registration{
Name: MGNSourceServerResource,
Scope: nuke.Account,
Lister: &MGNSourceServerLister{},
})
}
type MGNSourceServerLister struct{}
func (l *MGNSourceServerLister) List(_ context.Context, o interface{}) ([]resource.Resource, error) {
opts := o.(*nuke.ListerOpts)
svc := mgn.New(opts.Session)
resources := make([]resource.Resource, 0)
params := &mgn.DescribeSourceServersInput{
MaxResults: aws.Int64(50),
}
for {
output, err := svc.DescribeSourceServers(params)
if err != nil {
var awsErr awserr.Error
ok := errors.As(err, &awsErr)
if ok && awsErr.Code() == "UninitializedAccountException" {
return nil, nil
}
return nil, err
}
for _, sourceServer := range output.Items {
resources = append(resources, &MGNSourceServer{
svc: svc,
sourceServerID: sourceServer.SourceServerID,
arn: sourceServer.Arn,
tags: sourceServer.Tags,
})
}
if output.NextToken == nil {
break
}
params.NextToken = output.NextToken
}
return resources, nil
}
type MGNSourceServer struct {
svc *mgn.Mgn
sourceServerID *string
arn *string
tags map[string]*string
}
func (f *MGNSourceServer) Remove(_ context.Context) error {
// Disconnect source server from service first before delete
if _, err := f.svc.DisconnectFromService(&mgn.DisconnectFromServiceInput{
SourceServerID: f.sourceServerID,
}); err != nil {
return err
}
_, err := f.svc.DeleteSourceServer(&mgn.DeleteSourceServerInput{
SourceServerID: f.sourceServerID,
})
return err
}
func (f *MGNSourceServer) Properties() types.Properties {
properties := types.NewProperties()
properties.Set("SourceServerID", f.sourceServerID)
properties.Set("ARN", f.arn)
for key, val := range f.tags {
properties.SetTag(&key, val)
}
return properties
}
func (f *MGNSourceServer) String() string {
return *f.sourceServerID
}