...

Source file src/cloud.google.com/go/logging/logadmin/resources.go

Documentation: cloud.google.com/go/logging/logadmin

     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  
    15  package logadmin
    16  
    17  import (
    18  	"context"
    19  
    20  	vkit "cloud.google.com/go/logging/apiv2"
    21  	logpb "cloud.google.com/go/logging/apiv2/loggingpb"
    22  	"google.golang.org/api/iterator"
    23  	mrpb "google.golang.org/genproto/googleapis/api/monitoredres"
    24  )
    25  
    26  // ResourceDescriptors returns a ResourceDescriptorIterator
    27  // for iterating over MonitoredResourceDescriptors. Requires ReadScope or AdminScope.
    28  // See https://cloud.google.com/logging/docs/api/v2/#monitored-resources for an explanation of
    29  // monitored resources.
    30  // See https://cloud.google.com/logging/docs/api/v2/resource-list for a list of monitored resources.
    31  func (c *Client) ResourceDescriptors(ctx context.Context) *ResourceDescriptorIterator {
    32  	it := &ResourceDescriptorIterator{
    33  		it: c.lClient.ListMonitoredResourceDescriptors(ctx,
    34  			&logpb.ListMonitoredResourceDescriptorsRequest{}),
    35  	}
    36  	it.pageInfo, it.nextFunc = iterator.NewPageInfo(
    37  		it.fetch,
    38  		func() int { return len(it.items) },
    39  		func() interface{} { b := it.items; it.items = nil; return b })
    40  	return it
    41  }
    42  
    43  // ResourceDescriptorIterator is an iterator over MonitoredResourceDescriptors.
    44  type ResourceDescriptorIterator struct {
    45  	it       *vkit.MonitoredResourceDescriptorIterator
    46  	pageInfo *iterator.PageInfo
    47  	nextFunc func() error
    48  	items    []*mrpb.MonitoredResourceDescriptor
    49  }
    50  
    51  // PageInfo supports pagination. See the google.golang.org/api/iterator package for details.
    52  func (it *ResourceDescriptorIterator) PageInfo() *iterator.PageInfo { return it.pageInfo }
    53  
    54  // Next returns the next result. Its second return value is Done if there are
    55  // no more results. Once Next returns Done, all subsequent calls will return
    56  // Done.
    57  func (it *ResourceDescriptorIterator) Next() (*mrpb.MonitoredResourceDescriptor, error) {
    58  	if err := it.nextFunc(); err != nil {
    59  		return nil, err
    60  	}
    61  	item := it.items[0]
    62  	it.items = it.items[1:]
    63  	return item, nil
    64  }
    65  
    66  func (it *ResourceDescriptorIterator) fetch(pageSize int, pageToken string) (string, error) {
    67  	return iterFetch(pageSize, pageToken, it.it.PageInfo(), func() error {
    68  		item, err := it.it.Next()
    69  		if err != nil {
    70  			return err
    71  		}
    72  		it.items = append(it.items, item)
    73  		return nil
    74  	})
    75  }
    76  

View as plain text