Skip to content
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

feat: allow resource to hook the parent item for modification #57

Merged
merged 5 commits into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions pkg/resource/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,17 @@ type SettingsGetter interface {
Settings(setting *settings.Setting)
}

// HandleWaitHook is an interface that allows a resource to handle waiting for a resource to be deleted.
// This is useful for resources that may take a while to delete, typically where the delete operation happens
// asynchronously from the initial delete command. This allows libnuke to not block during the delete operation.
type HandleWaitHook interface {
Resource
HandleWait(context.Context) error
}

// QueueItemHook is an interface that allows a resource to modify the queue item to which it belongs to.
// For advanced use only, please use with caution!
type QueueItemHook interface {
Resource
BeforeEnqueue(interface{})
}
6 changes: 6 additions & 0 deletions pkg/scanner/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,12 @@ func (s *Scanner) list(ctx context.Context, owner, resourceType string, opts int
Owner: owner,
Opts: opts,
}

itemHook, ok := r.(resource.QueueItemHook)
if ok {
itemHook.BeforeEnqueue(i)
}

s.Items <- i
}
}
1 change: 1 addition & 0 deletions pkg/scanner/scanner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ func Test_NewScannerWithMorphOpts(t *testing.T) {
for item := range scanner.Items {
assert.Equal(t, "testing", item.Opts.(TestOpts).SessionOne)
assert.Equal(t, "testing-testResourceType", item.Opts.(TestOpts).SessionTwo)
assert.Equal(t, "OwnerModded", item.Owner)
}
}

Expand Down
6 changes: 6 additions & 0 deletions pkg/scanner/testsuite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/stretchr/testify/assert"

"github.com/ekristen/libnuke/pkg/errors"
"github.com/ekristen/libnuke/pkg/queue"
"github.com/ekristen/libnuke/pkg/registry"
"github.com/ekristen/libnuke/pkg/resource"
"github.com/ekristen/libnuke/pkg/settings"
Expand Down Expand Up @@ -59,6 +60,11 @@ func (r *TestResource) Settings(setting *settings.Setting) {

}

func (r *TestResource) BeforeEnqueue(item interface{}) {
i := item.(*queue.Item)
i.Owner = "OwnerModded"
}

type TestResource2 struct {
Filtered bool
RemoveError bool
Expand Down
12 changes: 12 additions & 0 deletions pkg/types/properties.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,11 +193,16 @@ func (p Properties) SetFromStruct(data interface{}) Properties { //nolint:funlen
name := field.Name
prefix := ""
tagPrefix := ""
inline := false

if options[0] == "-" {
continue
}

if len(options) == 2 && options[1] == "inline" {
inline = true
}

for _, option := range options {
parts := strings.Split(option, "=")
if len(parts) != 2 {
Expand All @@ -213,6 +218,11 @@ func (p Properties) SetFromStruct(data interface{}) Properties { //nolint:funlen
}
}

if inline {
p.SetFromStruct(value.Interface())
continue
}

if tagPrefix != "" {
p.SetTagPrefix(tagPrefix)
}
Expand All @@ -222,6 +232,8 @@ func (p Properties) SetFromStruct(data interface{}) Properties { //nolint:funlen
}

switch value.Kind() {
case reflect.Struct:
// do nothing
case reflect.Map:
for _, key := range value.MapKeys() {
val := value.MapIndex(key)
Expand Down
32 changes: 32 additions & 0 deletions pkg/types/properties_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,22 @@ func TestPropertiesSetFromStruct(t *testing.T) {
Labels map[string]string `property:"tagPrefix=label"`
}

type TestStruct8 struct {
Name string
}

type testStruct9 struct {
*TestStruct8 `property:",inline"`

Region string
}

type testStruct10 struct {
*TestStruct8

Region string
}

cases := []struct {
name string
s interface{}
Expand Down Expand Up @@ -493,6 +509,22 @@ func TestPropertiesSetFromStruct(t *testing.T) {
},
want: types.NewProperties().SetTagPrefix("label").Set("Name", "Bob").SetTag(ptr.String("key"), "value"),
},
{
name: "struct-with-inline",
s: testStruct9{
TestStruct8: &TestStruct8{Name: "Alice"},
Region: "us-west-2",
},
want: types.NewProperties().Set("Name", "Alice").Set("Region", "us-west-2"),
},
{
name: "struct-without-inline",
s: testStruct10{
TestStruct8: &TestStruct8{Name: "Alice"},
Region: "us-west-2",
},
want: types.NewProperties().Set("Region", "us-west-2"),
},
}

for _, tc := range cases {
Expand Down