diff --git a/logging/logadmin/logadmin.go b/logging/logadmin/logadmin.go index 1ec2b4c22e43..c32e831e4a7f 100644 --- a/logging/logadmin/logadmin.go +++ b/logging/logadmin/logadmin.go @@ -223,6 +223,13 @@ type newestFirst struct{} func (newestFirst) set(r *logpb.ListLogEntriesRequest) { r.OrderBy = "timestamp desc" } +// PageSize provide a way to override number of results to return from each request. +func PageSize(p int32) EntriesOption { return pageSize(p) } + +type pageSize int32 + +func (p pageSize) set(r *logpb.ListLogEntriesRequest) { r.PageSize = int32(p) } + // Entries returns an EntryIterator for iterating over log entries. By default, // the log entries will be restricted to those from the project passed to // NewClient. This may be overridden by passing a ProjectIDs option. Requires ReadScope or AdminScope. diff --git a/logging/logadmin/logadmin_test.go b/logging/logadmin/logadmin_test.go index 2ca4a6c691ea..d63b2600fe49 100644 --- a/logging/logadmin/logadmin_test.go +++ b/logging/logadmin/logadmin_test.go @@ -288,31 +288,89 @@ func TestListLogEntriesRequest(t *testing.T) { resourceNames []string filterPrefix string orderBy string + pageSize int32 }{ // Timestamp default does not override user's filter - {[]EntriesOption{NewestFirst(), Filter(`timestamp > "2020-10-30T15:39:09Z"`)}, - []string{"projects/PROJECT_ID"}, `timestamp > "2020-10-30T15:39:09Z"`, "timestamp desc"}, - {[]EntriesOption{NewestFirst(), Filter("f")}, - []string{"projects/PROJECT_ID"}, "f AND timestamp >= \"", "timestamp desc"}, - {[]EntriesOption{ProjectIDs([]string{"foo"})}, - []string{"projects/foo"}, "timestamp >= \"", ""}, - {[]EntriesOption{ResourceNames([]string{"folders/F", "organizations/O"})}, - []string{"folders/F", "organizations/O"}, "timestamp >= \"", ""}, - {[]EntriesOption{NewestFirst(), Filter("f"), ProjectIDs([]string{"foo"})}, - []string{"projects/foo"}, "f AND timestamp >= \"", "timestamp desc"}, - {[]EntriesOption{NewestFirst(), Filter("f"), ProjectIDs([]string{"foo"})}, - []string{"projects/foo"}, "f AND timestamp >= \"", "timestamp desc"}, - // If there are repeats, last one wins. - {[]EntriesOption{NewestFirst(), Filter("no"), ProjectIDs([]string{"foo"}), Filter("f")}, - []string{"projects/foo"}, "f AND timestamp >= \"", "timestamp desc"}, + { + // default resource name and timestamp filter + opts: []EntriesOption{ + NewestFirst(), + Filter(`timestamp > "2020-10-30T15:39:09Z"`), + }, + resourceNames: []string{"projects/PROJECT_ID"}, + filterPrefix: `timestamp > "2020-10-30T15:39:09Z"`, + orderBy: "timestamp desc", + }, + { + // default resource name and user's filter + opts: []EntriesOption{ + NewestFirst(), + Filter("f"), + }, + resourceNames: []string{"projects/PROJECT_ID"}, + filterPrefix: "f AND timestamp >= \"", + orderBy: "timestamp desc", + }, + { + // user's project id and default timestamp filter + opts: []EntriesOption{ + ProjectIDs([]string{"foo"}), + }, + resourceNames: []string{"projects/foo"}, + filterPrefix: "timestamp >= \"", + orderBy: "", + }, + { + // user's resource name and default timestamp filter + opts: []EntriesOption{ + ResourceNames([]string{"folders/F", "organizations/O"}), + }, + resourceNames: []string{"folders/F", "organizations/O"}, + filterPrefix: "timestamp >= \"", + orderBy: "", + }, + { + // user's project id and user's options + opts: []EntriesOption{ + NewestFirst(), + Filter("f"), + ProjectIDs([]string{"foo"}), + }, + resourceNames: []string{"projects/foo"}, + filterPrefix: "f AND timestamp >= \"", + orderBy: "timestamp desc", + }, + { + // user's project id with multiple filter options + opts: []EntriesOption{ + NewestFirst(), + Filter("no"), + ProjectIDs([]string{"foo"}), + Filter("f"), + }, + resourceNames: []string{"projects/foo"}, + filterPrefix: "f AND timestamp >= \"", + orderBy: "timestamp desc", + }, + { + // user's project id and custom page size + opts: []EntriesOption{ + ProjectIDs([]string{"foo"}), + PageSize(100), + }, + resourceNames: []string{"projects/foo"}, + filterPrefix: "timestamp >= \"", + pageSize: 100, + }, } { got := listLogEntriesRequest("projects/PROJECT_ID", test.opts) want := &logpb.ListLogEntriesRequest{ ResourceNames: test.resourceNames, Filter: test.filterPrefix, OrderBy: test.orderBy, + PageSize: test.pageSize, } - if !testutil.Equal(got.ResourceNames, want.ResourceNames) || !strings.HasPrefix(got.Filter, want.Filter) || got.OrderBy != want.OrderBy { + if !testutil.Equal(got.ResourceNames, want.ResourceNames) || !strings.HasPrefix(got.Filter, want.Filter) || got.OrderBy != want.OrderBy || got.PageSize != want.PageSize { t.Errorf("got: %v; want %v (mind wanted Filter is prefix)", got, want) } }