1package handlers 2 3import ( 4 "context" 5 "reflect" 6 7 corev2 "github.com/sensu/sensu-go/api/core/v2" 8 "github.com/sensu/sensu-go/backend/apid/actions" 9 "github.com/sensu/sensu-go/backend/store" 10) 11 12// ListResources lists all resources for the resource type 13func (h Handlers) ListResources(ctx context.Context, pred *store.SelectionPredicate) ([]corev2.Resource, error) { 14 // Get the type of the resource and create a slice type of []type 15 typeOfResource := reflect.TypeOf(h.Resource) 16 sliceOfResource := reflect.SliceOf(typeOfResource) 17 // Create a pointer to our slice type and then set the slice value 18 ptr := reflect.New(sliceOfResource) 19 ptr.Elem().Set(reflect.MakeSlice(sliceOfResource, 0, 0)) 20 21 if err := h.Store.ListResources(ctx, h.Resource.StorePrefix(), ptr.Interface(), pred); err != nil { 22 return nil, actions.NewError(actions.InternalErr, err) 23 } 24 25 results := ptr.Elem() 26 resources := make([]corev2.Resource, results.Len()) 27 for i := 0; i < results.Len(); i++ { 28 r, ok := results.Index(i).Interface().(corev2.Resource) 29 if !ok { 30 logger.Errorf("%T is not core2.Resource", results.Index(i).Interface()) 31 continue 32 } 33 resources[i] = r 34 } 35 36 return resources, nil 37} 38