You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

76 rivejä
2.7 KiB

  1. // Copyright 2016 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package logadmin
  15. import (
  16. "context"
  17. vkit "cloud.google.com/go/logging/apiv2"
  18. "google.golang.org/api/iterator"
  19. mrpb "google.golang.org/genproto/googleapis/api/monitoredres"
  20. logpb "google.golang.org/genproto/googleapis/logging/v2"
  21. )
  22. // ResourceDescriptors returns a ResourceDescriptorIterator
  23. // for iterating over MonitoredResourceDescriptors. Requires ReadScope or AdminScope.
  24. // See https://cloud.google.com/logging/docs/api/v2/#monitored-resources for an explanation of
  25. // monitored resources.
  26. // See https://cloud.google.com/logging/docs/api/v2/resource-list for a list of monitored resources.
  27. func (c *Client) ResourceDescriptors(ctx context.Context) *ResourceDescriptorIterator {
  28. it := &ResourceDescriptorIterator{
  29. it: c.lClient.ListMonitoredResourceDescriptors(ctx,
  30. &logpb.ListMonitoredResourceDescriptorsRequest{}),
  31. }
  32. it.pageInfo, it.nextFunc = iterator.NewPageInfo(
  33. it.fetch,
  34. func() int { return len(it.items) },
  35. func() interface{} { b := it.items; it.items = nil; return b })
  36. return it
  37. }
  38. // ResourceDescriptorIterator is an iterator over MonitoredResourceDescriptors.
  39. type ResourceDescriptorIterator struct {
  40. it *vkit.MonitoredResourceDescriptorIterator
  41. pageInfo *iterator.PageInfo
  42. nextFunc func() error
  43. items []*mrpb.MonitoredResourceDescriptor
  44. }
  45. // PageInfo supports pagination. See the google.golang.org/api/iterator package for details.
  46. func (it *ResourceDescriptorIterator) PageInfo() *iterator.PageInfo { return it.pageInfo }
  47. // Next returns the next result. Its second return value is Done if there are
  48. // no more results. Once Next returns Done, all subsequent calls will return
  49. // Done.
  50. func (it *ResourceDescriptorIterator) Next() (*mrpb.MonitoredResourceDescriptor, error) {
  51. if err := it.nextFunc(); err != nil {
  52. return nil, err
  53. }
  54. item := it.items[0]
  55. it.items = it.items[1:]
  56. return item, nil
  57. }
  58. func (it *ResourceDescriptorIterator) fetch(pageSize int, pageToken string) (string, error) {
  59. return iterFetch(pageSize, pageToken, it.it.PageInfo(), func() error {
  60. item, err := it.it.Next()
  61. if err != nil {
  62. return err
  63. }
  64. it.items = append(it.items, item)
  65. return nil
  66. })
  67. }