fix(kv): use urm filter to search endpoints
parent
81283b1ac2
commit
4615a01338
|
@ -264,7 +264,16 @@ func (h *NotificationEndpointHandler) handleGetNotificationEndpoint(w http.Respo
|
|||
}
|
||||
|
||||
func decodeNotificationEndpointFilter(ctx context.Context, r *http.Request) (*influxdb.NotificationEndpointFilter, *influxdb.FindOptions, error) {
|
||||
f := &influxdb.NotificationEndpointFilter{}
|
||||
auth, err := pctx.GetAuthorizer(ctx)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
f := &influxdb.NotificationEndpointFilter{
|
||||
UserResourceMappingFilter: influxdb.UserResourceMappingFilter{
|
||||
UserID: auth.GetUserID(),
|
||||
ResourceType: influxdb.NotificationEndpointResourceType,
|
||||
},
|
||||
}
|
||||
|
||||
opts, err := decodeFindOptions(ctx, r)
|
||||
if err != nil {
|
||||
|
|
|
@ -231,6 +231,7 @@ func TestService_handleGetNotificationEndpoints(t *testing.T) {
|
|||
}
|
||||
}
|
||||
r.URL.RawQuery = qp.Encode()
|
||||
r = r.WithContext(pcontext.SetAuthorizer(r.Context(), &influxdb.Session{UserID: user1ID}))
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
|
|
|
@ -392,6 +392,19 @@ func (s *Service) FindNotificationEndpoints(ctx context.Context, filter influxdb
|
|||
|
||||
func (s *Service) findNotificationEndpoints(ctx context.Context, tx Tx, filter influxdb.NotificationEndpointFilter, opt ...influxdb.FindOptions) ([]influxdb.NotificationEndpoint, int, error) {
|
||||
edps := make([]influxdb.NotificationEndpoint, 0)
|
||||
m, err := s.findUserResourceMappings(ctx, tx, filter.UserResourceMappingFilter)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
if len(m) == 0 {
|
||||
return edps, 0, nil
|
||||
}
|
||||
|
||||
idMap := make(map[influxdb.ID]bool)
|
||||
for _, item := range m {
|
||||
idMap[item.ResourceID] = true
|
||||
}
|
||||
|
||||
if filter.Org != nil {
|
||||
o, err := s.findOrganizationByName(ctx, tx, *filter.Org)
|
||||
|
@ -410,8 +423,8 @@ func (s *Service) findNotificationEndpoints(ctx context.Context, tx Tx, filter i
|
|||
limit = opt[0].Limit
|
||||
descending = opt[0].Descending
|
||||
}
|
||||
filterFn := filterNotificationEndpointsFn(filter)
|
||||
err := s.forEachNotificationEndpoint(ctx, tx, descending, func(edp influxdb.NotificationEndpoint) bool {
|
||||
filterFn := filterNotificationEndpointsFn(idMap, filter)
|
||||
err = s.forEachNotificationEndpoint(ctx, tx, descending, func(edp influxdb.NotificationEndpoint) bool {
|
||||
if filterFn(edp) {
|
||||
if count >= offset {
|
||||
edps = append(edps, edp)
|
||||
|
@ -468,7 +481,7 @@ func (s *Service) forEachNotificationEndpoint(ctx context.Context, tx Tx, descen
|
|||
return nil
|
||||
}
|
||||
|
||||
func filterNotificationEndpointsFn(filter influxdb.NotificationEndpointFilter) func(edp influxdb.NotificationEndpoint) bool {
|
||||
func filterNotificationEndpointsFn(idMap map[influxdb.ID]bool, filter influxdb.NotificationEndpointFilter) func(edp influxdb.NotificationEndpoint) bool {
|
||||
return func(edp influxdb.NotificationEndpoint) bool {
|
||||
if filter.ID != nil {
|
||||
if edp.GetID() != *filter.ID {
|
||||
|
@ -481,7 +494,10 @@ func filterNotificationEndpointsFn(filter influxdb.NotificationEndpointFilter) f
|
|||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
if idMap == nil {
|
||||
return true
|
||||
}
|
||||
return idMap[edp.GetID()]
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -52,6 +52,7 @@ type NotificationEndpointFilter struct {
|
|||
ID *ID
|
||||
OrgID *ID
|
||||
Org *string
|
||||
UserResourceMappingFilter
|
||||
}
|
||||
|
||||
// QueryParams Converts NotificationEndpointFilter fields to url query params.
|
||||
|
|
|
@ -483,6 +483,20 @@ func FindNotificationEndpoints(
|
|||
{
|
||||
name: "find all notification endpoints",
|
||||
fields: NotificationEndpointFields{
|
||||
UserResourceMappings: []*influxdb.UserResourceMapping{
|
||||
{
|
||||
ResourceID: MustIDBase16(oneID),
|
||||
UserID: MustIDBase16(sixID),
|
||||
UserType: influxdb.Member,
|
||||
ResourceType: influxdb.NotificationEndpointResourceType,
|
||||
},
|
||||
{
|
||||
ResourceID: MustIDBase16(twoID),
|
||||
UserID: MustIDBase16(sixID),
|
||||
UserType: influxdb.Member,
|
||||
ResourceType: influxdb.NotificationEndpointResourceType,
|
||||
},
|
||||
},
|
||||
NotificationEndpoints: []influxdb.NotificationEndpoint{
|
||||
&endpoint.Slack{
|
||||
Base: endpoint.Base{
|
||||
|
@ -515,7 +529,12 @@ func FindNotificationEndpoints(
|
|||
},
|
||||
},
|
||||
args: args{
|
||||
filter: influxdb.NotificationEndpointFilter{},
|
||||
filter: influxdb.NotificationEndpointFilter{
|
||||
UserResourceMappingFilter: influxdb.UserResourceMappingFilter{
|
||||
UserID: MustIDBase16(sixID),
|
||||
ResourceType: influxdb.NotificationEndpointResourceType,
|
||||
},
|
||||
},
|
||||
},
|
||||
wants: wants{
|
||||
notificationEndpoints: []influxdb.NotificationEndpoint{
|
||||
|
@ -563,6 +582,26 @@ func FindNotificationEndpoints(
|
|||
Name: "org4",
|
||||
},
|
||||
},
|
||||
UserResourceMappings: []*influxdb.UserResourceMapping{
|
||||
{
|
||||
ResourceID: MustIDBase16(oneID),
|
||||
UserID: MustIDBase16(sixID),
|
||||
UserType: influxdb.Member,
|
||||
ResourceType: influxdb.NotificationEndpointResourceType,
|
||||
},
|
||||
{
|
||||
ResourceID: MustIDBase16(twoID),
|
||||
UserID: MustIDBase16(sixID),
|
||||
UserType: influxdb.Member,
|
||||
ResourceType: influxdb.NotificationEndpointResourceType,
|
||||
},
|
||||
{
|
||||
ResourceID: MustIDBase16(fourID),
|
||||
UserID: MustIDBase16(sixID),
|
||||
UserType: influxdb.Member,
|
||||
ResourceType: influxdb.NotificationEndpointResourceType,
|
||||
},
|
||||
},
|
||||
NotificationEndpoints: []influxdb.NotificationEndpoint{
|
||||
&endpoint.Slack{
|
||||
Base: endpoint.Base{
|
||||
|
@ -599,6 +638,10 @@ func FindNotificationEndpoints(
|
|||
args: args{
|
||||
filter: influxdb.NotificationEndpointFilter{
|
||||
OrgID: idPtr(MustIDBase16(oneID)),
|
||||
UserResourceMappingFilter: influxdb.UserResourceMappingFilter{
|
||||
ResourceType: influxdb.NotificationEndpointResourceType,
|
||||
UserID: MustIDBase16(sixID),
|
||||
},
|
||||
},
|
||||
},
|
||||
wants: wants{
|
||||
|
@ -629,6 +672,20 @@ func FindNotificationEndpoints(
|
|||
Name: "org4",
|
||||
},
|
||||
},
|
||||
UserResourceMappings: []*influxdb.UserResourceMapping{
|
||||
{
|
||||
ResourceID: MustIDBase16(oneID),
|
||||
UserID: MustIDBase16(sixID),
|
||||
UserType: influxdb.Member,
|
||||
ResourceType: influxdb.NotificationEndpointResourceType,
|
||||
},
|
||||
{
|
||||
ResourceID: MustIDBase16(twoID),
|
||||
UserID: MustIDBase16(sixID),
|
||||
UserType: influxdb.Member,
|
||||
ResourceType: influxdb.NotificationEndpointResourceType,
|
||||
},
|
||||
},
|
||||
NotificationEndpoints: []influxdb.NotificationEndpoint{
|
||||
&endpoint.Slack{
|
||||
Base: endpoint.Base{
|
||||
|
@ -666,6 +723,10 @@ func FindNotificationEndpoints(
|
|||
args: args{
|
||||
filter: influxdb.NotificationEndpointFilter{
|
||||
Org: strPtr("org4"),
|
||||
UserResourceMappingFilter: influxdb.UserResourceMappingFilter{
|
||||
UserID: MustIDBase16(sixID),
|
||||
ResourceType: influxdb.NotificationEndpointResourceType,
|
||||
},
|
||||
},
|
||||
},
|
||||
wants: wants{
|
||||
|
@ -707,6 +768,20 @@ func FindNotificationEndpoints(
|
|||
Name: "org4",
|
||||
},
|
||||
},
|
||||
UserResourceMappings: []*influxdb.UserResourceMapping{
|
||||
{
|
||||
ResourceID: MustIDBase16(oneID),
|
||||
UserID: MustIDBase16(sixID),
|
||||
UserType: influxdb.Member,
|
||||
ResourceType: influxdb.NotificationEndpointResourceType,
|
||||
},
|
||||
{
|
||||
ResourceID: MustIDBase16(twoID),
|
||||
UserID: MustIDBase16(sixID),
|
||||
UserType: influxdb.Member,
|
||||
ResourceType: influxdb.NotificationEndpointResourceType,
|
||||
},
|
||||
},
|
||||
NotificationEndpoints: []influxdb.NotificationEndpoint{
|
||||
&endpoint.Slack{
|
||||
Base: endpoint.Base{
|
||||
|
@ -744,6 +819,10 @@ func FindNotificationEndpoints(
|
|||
args: args{
|
||||
filter: influxdb.NotificationEndpointFilter{
|
||||
Org: strPtr("org4"),
|
||||
UserResourceMappingFilter: influxdb.UserResourceMappingFilter{
|
||||
UserID: MustIDBase16(sixID),
|
||||
ResourceType: influxdb.NotificationEndpointResourceType,
|
||||
},
|
||||
},
|
||||
opts: influxdb.FindOptions{
|
||||
Limit: 2,
|
||||
|
@ -788,6 +867,27 @@ func FindNotificationEndpoints(
|
|||
Name: "org4",
|
||||
},
|
||||
},
|
||||
UserResourceMappings: []*influxdb.UserResourceMapping{
|
||||
{
|
||||
ResourceID: MustIDBase16(oneID),
|
||||
UserID: MustIDBase16(sixID),
|
||||
UserType: influxdb.Member,
|
||||
ResourceType: influxdb.NotificationEndpointResourceType,
|
||||
},
|
||||
{
|
||||
ResourceID: MustIDBase16(twoID),
|
||||
UserID: MustIDBase16(sixID),
|
||||
UserType: influxdb.Member,
|
||||
ResourceType: influxdb.NotificationEndpointResourceType,
|
||||
},
|
||||
|
||||
{
|
||||
ResourceID: MustIDBase16(fourID),
|
||||
UserID: MustIDBase16(sixID),
|
||||
UserType: influxdb.Member,
|
||||
ResourceType: influxdb.NotificationEndpointResourceType,
|
||||
},
|
||||
},
|
||||
NotificationEndpoints: []influxdb.NotificationEndpoint{
|
||||
&endpoint.Slack{
|
||||
Base: endpoint.Base{
|
||||
|
@ -825,6 +925,10 @@ func FindNotificationEndpoints(
|
|||
args: args{
|
||||
filter: influxdb.NotificationEndpointFilter{
|
||||
Org: strPtr("org4"),
|
||||
UserResourceMappingFilter: influxdb.UserResourceMappingFilter{
|
||||
UserID: MustIDBase16(sixID),
|
||||
ResourceType: influxdb.NotificationEndpointResourceType,
|
||||
},
|
||||
},
|
||||
opts: influxdb.FindOptions{
|
||||
Offset: 1,
|
||||
|
@ -869,6 +973,26 @@ func FindNotificationEndpoints(
|
|||
Name: "org4",
|
||||
},
|
||||
},
|
||||
UserResourceMappings: []*influxdb.UserResourceMapping{
|
||||
{
|
||||
ResourceID: MustIDBase16(oneID),
|
||||
UserID: MustIDBase16(sixID),
|
||||
UserType: influxdb.Member,
|
||||
ResourceType: influxdb.NotificationEndpointResourceType,
|
||||
},
|
||||
{
|
||||
ResourceID: MustIDBase16(twoID),
|
||||
UserID: MustIDBase16(sixID),
|
||||
UserType: influxdb.Member,
|
||||
ResourceType: influxdb.NotificationEndpointResourceType,
|
||||
},
|
||||
{
|
||||
ResourceID: MustIDBase16(fourID),
|
||||
UserID: MustIDBase16(sixID),
|
||||
UserType: influxdb.Member,
|
||||
ResourceType: influxdb.NotificationEndpointResourceType,
|
||||
},
|
||||
},
|
||||
NotificationEndpoints: []influxdb.NotificationEndpoint{
|
||||
&endpoint.Slack{
|
||||
Base: endpoint.Base{
|
||||
|
@ -906,6 +1030,10 @@ func FindNotificationEndpoints(
|
|||
args: args{
|
||||
filter: influxdb.NotificationEndpointFilter{
|
||||
ID: idPtr(MustIDBase16(fourID)),
|
||||
UserResourceMappingFilter: influxdb.UserResourceMappingFilter{
|
||||
UserID: MustIDBase16(sixID),
|
||||
ResourceType: influxdb.NotificationEndpointResourceType,
|
||||
},
|
||||
},
|
||||
},
|
||||
wants: wants{
|
||||
|
@ -936,6 +1064,26 @@ func FindNotificationEndpoints(
|
|||
Name: "org4",
|
||||
},
|
||||
},
|
||||
UserResourceMappings: []*influxdb.UserResourceMapping{
|
||||
{
|
||||
ResourceID: MustIDBase16(oneID),
|
||||
UserID: MustIDBase16(sixID),
|
||||
UserType: influxdb.Member,
|
||||
ResourceType: influxdb.NotificationEndpointResourceType,
|
||||
},
|
||||
{
|
||||
ResourceID: MustIDBase16(twoID),
|
||||
UserID: MustIDBase16(sixID),
|
||||
UserType: influxdb.Member,
|
||||
ResourceType: influxdb.NotificationEndpointResourceType,
|
||||
},
|
||||
{
|
||||
ResourceID: MustIDBase16(threeID),
|
||||
UserID: MustIDBase16(sixID),
|
||||
UserType: influxdb.Member,
|
||||
ResourceType: influxdb.NotificationEndpointResourceType,
|
||||
},
|
||||
},
|
||||
NotificationEndpoints: []influxdb.NotificationEndpoint{
|
||||
&endpoint.Slack{
|
||||
Base: endpoint.Base{
|
||||
|
@ -972,6 +1120,10 @@ func FindNotificationEndpoints(
|
|||
args: args{
|
||||
filter: influxdb.NotificationEndpointFilter{
|
||||
OrgID: idPtr(MustIDBase16(oneID)),
|
||||
UserResourceMappingFilter: influxdb.UserResourceMappingFilter{
|
||||
UserID: MustIDBase16(sixID),
|
||||
ResourceType: influxdb.NotificationEndpointResourceType,
|
||||
},
|
||||
},
|
||||
},
|
||||
wants: wants{
|
||||
|
@ -991,6 +1143,20 @@ func FindNotificationEndpoints(
|
|||
Name: "org4",
|
||||
},
|
||||
},
|
||||
UserResourceMappings: []*influxdb.UserResourceMapping{
|
||||
{
|
||||
ResourceID: MustIDBase16(oneID),
|
||||
UserID: MustIDBase16(sixID),
|
||||
UserType: influxdb.Member,
|
||||
ResourceType: influxdb.NotificationEndpointResourceType,
|
||||
},
|
||||
{
|
||||
ResourceID: MustIDBase16(twoID),
|
||||
UserID: MustIDBase16(sixID),
|
||||
UserType: influxdb.Member,
|
||||
ResourceType: influxdb.NotificationEndpointResourceType,
|
||||
},
|
||||
},
|
||||
NotificationEndpoints: []influxdb.NotificationEndpoint{
|
||||
&endpoint.Slack{
|
||||
Base: endpoint.Base{
|
||||
|
@ -1027,6 +1193,10 @@ func FindNotificationEndpoints(
|
|||
args: args{
|
||||
filter: influxdb.NotificationEndpointFilter{
|
||||
ID: idPtr(MustIDBase16(fiveID)),
|
||||
UserResourceMappingFilter: influxdb.UserResourceMappingFilter{
|
||||
UserID: MustIDBase16(sixID),
|
||||
ResourceType: influxdb.NotificationEndpointResourceType,
|
||||
},
|
||||
},
|
||||
},
|
||||
wants: wants{},
|
||||
|
|
Loading…
Reference in New Issue