-
-
Notifications
You must be signed in to change notification settings - Fork 353
/
base_resource.go
103 lines (89 loc) · 2.95 KB
/
base_resource.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
package resources
import (
"context"
"errors"
"fmt"
"time"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/gruntwork-io/cloud-nuke/config"
"github.com/gruntwork-io/cloud-nuke/util"
)
// BaseAwsResource struct and its associated methods to serve as a placeholder or template for a resource that is not
// yet fully implemented within a system or framework. Its purpose is to provide a skeleton structure that adheres to a
// specific interface or contract expected by the system without containing the actual implementation details.
type BaseAwsResource struct {
// A key-value of identifiers and nukable status
Nukables map[string]error
Timeout time.Duration
Context context.Context
cancel context.CancelFunc
}
func (br *BaseAwsResource) Init(_ *session.Session) {
br.Nukables = make(map[string]error)
}
func (br *BaseAwsResource) ResourceName() string {
return "not implemented: ResourceName"
}
func (br *BaseAwsResource) ResourceIdentifiers() []string {
return nil
}
func (br *BaseAwsResource) MaxBatchSize() int {
return 0
}
func (br *BaseAwsResource) Nuke(_ []string) error {
return errors.New("not implemented: Nuke")
}
func (br *BaseAwsResource) GetAndSetIdentifiers(_ context.Context, _ config.Config) ([]string, error) {
return nil, errors.New("not implemented: GetAndSetIdentifiers")
}
func (br *BaseAwsResource) GetNukableStatus(identifier string) (error, bool) {
val, ok := br.Nukables[identifier]
return val, ok
}
func (br *BaseAwsResource) SetNukableStatus(identifier string, err error) {
br.Nukables[identifier] = err
}
func (br *BaseAwsResource) GetAndSetResourceConfig(_ config.Config) config.ResourceType {
return config.ResourceType{
Timeout: "",
}
}
func (br *BaseAwsResource) PrepareContext(parentContext context.Context, resourceConfig config.ResourceType) error {
if resourceConfig.Timeout == "" {
br.Context = parentContext
return nil
}
duration, err := time.ParseDuration(resourceConfig.Timeout)
if err != nil {
return err
}
br.Context, _ = context.WithTimeout(parentContext, duration)
return nil
}
// VerifyNukablePermissions performs nukable permission verification for each ID. For each ID, the function is
// executed, and the result (error or success) is recorded using the SetNukableStatus method, indicating whether
// the specified action is nukable
func (br *BaseAwsResource) VerifyNukablePermissions(ids []*string, nukableCheckfn func(id *string) error) {
// check if the 'Nukables' map is initialized, and if it's not, initialize it
if br.Nukables == nil {
br.Nukables = make(map[string]error)
}
for _, id := range ids {
// skip if the id is already exists
if _, ok := br.GetNukableStatus(*id); ok {
continue
}
err := nukableCheckfn(id)
br.SetNukableStatus(*id, util.TransformAWSError(err))
}
}
func (br *BaseAwsResource) IsNukable(identifier string) (bool, error) {
err, ok := br.Nukables[identifier]
if !ok {
return false, fmt.Errorf("-")
}
if err != nil {
return false, err
}
return true, nil
}