Skip to content

Commit

Permalink
feat: allow resource to hook the parent item for modification (#57)
Browse files Browse the repository at this point in the history
* feat: allow resource to hook the parent item for modification

* feat(types): support inline structs on properties

* refactor(resource): rename function to be more descriptive

* refactor(resource): rename function to be more descriptive

* chore: fix goimports order
  • Loading branch information
ekristen committed Jun 11, 2024
1 parent 649088d commit 4362964
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 0 deletions.
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

0 comments on commit 4362964

Please sign in to comment.