1// Copyright 2019 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// https://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// Code generated by gapic-generator. DO NOT EDIT. 16 17package monitoring 18 19import ( 20 "context" 21 "fmt" 22 "math" 23 "time" 24 25 "github.com/golang/protobuf/proto" 26 gax "github.com/googleapis/gax-go/v2" 27 "google.golang.org/api/iterator" 28 "google.golang.org/api/option" 29 "google.golang.org/api/transport" 30 monitoringpb "google.golang.org/genproto/googleapis/monitoring/v3" 31 "google.golang.org/grpc" 32 "google.golang.org/grpc/codes" 33 "google.golang.org/grpc/metadata" 34) 35 36// UptimeCheckCallOptions contains the retry settings for each method of UptimeCheckClient. 37type UptimeCheckCallOptions struct { 38 ListUptimeCheckConfigs []gax.CallOption 39 GetUptimeCheckConfig []gax.CallOption 40 CreateUptimeCheckConfig []gax.CallOption 41 UpdateUptimeCheckConfig []gax.CallOption 42 DeleteUptimeCheckConfig []gax.CallOption 43 ListUptimeCheckIps []gax.CallOption 44} 45 46func defaultUptimeCheckClientOptions() []option.ClientOption { 47 return []option.ClientOption{ 48 option.WithEndpoint("monitoring.googleapis.com:443"), 49 option.WithScopes(DefaultAuthScopes()...), 50 } 51} 52 53func defaultUptimeCheckCallOptions() *UptimeCheckCallOptions { 54 retry := map[[2]string][]gax.CallOption{ 55 {"default", "idempotent"}: { 56 gax.WithRetry(func() gax.Retryer { 57 return gax.OnCodes([]codes.Code{ 58 codes.DeadlineExceeded, 59 codes.Unavailable, 60 }, gax.Backoff{ 61 Initial: 100 * time.Millisecond, 62 Max: 60000 * time.Millisecond, 63 Multiplier: 1.3, 64 }) 65 }), 66 }, 67 } 68 return &UptimeCheckCallOptions{ 69 ListUptimeCheckConfigs: retry[[2]string{"default", "idempotent"}], 70 GetUptimeCheckConfig: retry[[2]string{"default", "idempotent"}], 71 CreateUptimeCheckConfig: retry[[2]string{"default", "non_idempotent"}], 72 UpdateUptimeCheckConfig: retry[[2]string{"default", "non_idempotent"}], 73 DeleteUptimeCheckConfig: retry[[2]string{"default", "idempotent"}], 74 ListUptimeCheckIps: retry[[2]string{"default", "idempotent"}], 75 } 76} 77 78// UptimeCheckClient is a client for interacting with Stackdriver Monitoring API. 79// 80// Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls. 81type UptimeCheckClient struct { 82 // The connection to the service. 83 conn *grpc.ClientConn 84 85 // The gRPC API client. 86 uptimeCheckClient monitoringpb.UptimeCheckServiceClient 87 88 // The call options for this service. 89 CallOptions *UptimeCheckCallOptions 90 91 // The x-goog-* metadata to be sent with each request. 92 xGoogMetadata metadata.MD 93} 94 95// NewUptimeCheckClient creates a new uptime check service client. 96// 97// The UptimeCheckService API is used to manage (list, create, delete, edit) 98// uptime check configurations in the Stackdriver Monitoring product. An uptime 99// check is a piece of configuration that determines which resources and 100// services to monitor for availability. These configurations can also be 101// configured interactively by navigating to the [Cloud Console] 102// (http://console.cloud.google.com), selecting the appropriate project, 103// clicking on "Monitoring" on the left-hand side to navigate to Stackdriver, 104// and then clicking on "Uptime". 105func NewUptimeCheckClient(ctx context.Context, opts ...option.ClientOption) (*UptimeCheckClient, error) { 106 conn, err := transport.DialGRPC(ctx, append(defaultUptimeCheckClientOptions(), opts...)...) 107 if err != nil { 108 return nil, err 109 } 110 c := &UptimeCheckClient{ 111 conn: conn, 112 CallOptions: defaultUptimeCheckCallOptions(), 113 114 uptimeCheckClient: monitoringpb.NewUptimeCheckServiceClient(conn), 115 } 116 c.setGoogleClientInfo() 117 return c, nil 118} 119 120// Connection returns the client's connection to the API service. 121func (c *UptimeCheckClient) Connection() *grpc.ClientConn { 122 return c.conn 123} 124 125// Close closes the connection to the API service. The user should invoke this when 126// the client is no longer required. 127func (c *UptimeCheckClient) Close() error { 128 return c.conn.Close() 129} 130 131// setGoogleClientInfo sets the name and version of the application in 132// the `x-goog-api-client` header passed on each request. Intended for 133// use by Google-written clients. 134func (c *UptimeCheckClient) setGoogleClientInfo(keyval ...string) { 135 kv := append([]string{"gl-go", versionGo()}, keyval...) 136 kv = append(kv, "gapic", versionClient, "gax", gax.Version, "grpc", grpc.Version) 137 c.xGoogMetadata = metadata.Pairs("x-goog-api-client", gax.XGoogHeader(kv...)) 138} 139 140// ListUptimeCheckConfigs lists the existing valid uptime check configurations for the project, 141// leaving out any invalid configurations. 142func (c *UptimeCheckClient) ListUptimeCheckConfigs(ctx context.Context, req *monitoringpb.ListUptimeCheckConfigsRequest, opts ...gax.CallOption) *UptimeCheckConfigIterator { 143 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "parent", req.GetParent())) 144 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 145 opts = append(c.CallOptions.ListUptimeCheckConfigs[0:len(c.CallOptions.ListUptimeCheckConfigs):len(c.CallOptions.ListUptimeCheckConfigs)], opts...) 146 it := &UptimeCheckConfigIterator{} 147 req = proto.Clone(req).(*monitoringpb.ListUptimeCheckConfigsRequest) 148 it.InternalFetch = func(pageSize int, pageToken string) ([]*monitoringpb.UptimeCheckConfig, string, error) { 149 var resp *monitoringpb.ListUptimeCheckConfigsResponse 150 req.PageToken = pageToken 151 if pageSize > math.MaxInt32 { 152 req.PageSize = math.MaxInt32 153 } else { 154 req.PageSize = int32(pageSize) 155 } 156 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 157 var err error 158 resp, err = c.uptimeCheckClient.ListUptimeCheckConfigs(ctx, req, settings.GRPC...) 159 return err 160 }, opts...) 161 if err != nil { 162 return nil, "", err 163 } 164 return resp.UptimeCheckConfigs, resp.NextPageToken, nil 165 } 166 fetch := func(pageSize int, pageToken string) (string, error) { 167 items, nextPageToken, err := it.InternalFetch(pageSize, pageToken) 168 if err != nil { 169 return "", err 170 } 171 it.items = append(it.items, items...) 172 return nextPageToken, nil 173 } 174 it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf) 175 it.pageInfo.MaxSize = int(req.PageSize) 176 it.pageInfo.Token = req.PageToken 177 return it 178} 179 180// GetUptimeCheckConfig gets a single uptime check configuration. 181func (c *UptimeCheckClient) GetUptimeCheckConfig(ctx context.Context, req *monitoringpb.GetUptimeCheckConfigRequest, opts ...gax.CallOption) (*monitoringpb.UptimeCheckConfig, error) { 182 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", req.GetName())) 183 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 184 opts = append(c.CallOptions.GetUptimeCheckConfig[0:len(c.CallOptions.GetUptimeCheckConfig):len(c.CallOptions.GetUptimeCheckConfig)], opts...) 185 var resp *monitoringpb.UptimeCheckConfig 186 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 187 var err error 188 resp, err = c.uptimeCheckClient.GetUptimeCheckConfig(ctx, req, settings.GRPC...) 189 return err 190 }, opts...) 191 if err != nil { 192 return nil, err 193 } 194 return resp, nil 195} 196 197// CreateUptimeCheckConfig creates a new uptime check configuration. 198func (c *UptimeCheckClient) CreateUptimeCheckConfig(ctx context.Context, req *monitoringpb.CreateUptimeCheckConfigRequest, opts ...gax.CallOption) (*monitoringpb.UptimeCheckConfig, error) { 199 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "parent", req.GetParent())) 200 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 201 opts = append(c.CallOptions.CreateUptimeCheckConfig[0:len(c.CallOptions.CreateUptimeCheckConfig):len(c.CallOptions.CreateUptimeCheckConfig)], opts...) 202 var resp *monitoringpb.UptimeCheckConfig 203 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 204 var err error 205 resp, err = c.uptimeCheckClient.CreateUptimeCheckConfig(ctx, req, settings.GRPC...) 206 return err 207 }, opts...) 208 if err != nil { 209 return nil, err 210 } 211 return resp, nil 212} 213 214// UpdateUptimeCheckConfig updates an uptime check configuration. You can either replace the entire 215// configuration with a new one or replace only certain fields in the current 216// configuration by specifying the fields to be updated via "updateMask". 217// Returns the updated configuration. 218func (c *UptimeCheckClient) UpdateUptimeCheckConfig(ctx context.Context, req *monitoringpb.UpdateUptimeCheckConfigRequest, opts ...gax.CallOption) (*monitoringpb.UptimeCheckConfig, error) { 219 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "uptime_check_config.name", req.GetUptimeCheckConfig().GetName())) 220 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 221 opts = append(c.CallOptions.UpdateUptimeCheckConfig[0:len(c.CallOptions.UpdateUptimeCheckConfig):len(c.CallOptions.UpdateUptimeCheckConfig)], opts...) 222 var resp *monitoringpb.UptimeCheckConfig 223 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 224 var err error 225 resp, err = c.uptimeCheckClient.UpdateUptimeCheckConfig(ctx, req, settings.GRPC...) 226 return err 227 }, opts...) 228 if err != nil { 229 return nil, err 230 } 231 return resp, nil 232} 233 234// DeleteUptimeCheckConfig deletes an uptime check configuration. Note that this method will fail 235// if the uptime check configuration is referenced by an alert policy or 236// other dependent configs that would be rendered invalid by the deletion. 237func (c *UptimeCheckClient) DeleteUptimeCheckConfig(ctx context.Context, req *monitoringpb.DeleteUptimeCheckConfigRequest, opts ...gax.CallOption) error { 238 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", req.GetName())) 239 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 240 opts = append(c.CallOptions.DeleteUptimeCheckConfig[0:len(c.CallOptions.DeleteUptimeCheckConfig):len(c.CallOptions.DeleteUptimeCheckConfig)], opts...) 241 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 242 var err error 243 _, err = c.uptimeCheckClient.DeleteUptimeCheckConfig(ctx, req, settings.GRPC...) 244 return err 245 }, opts...) 246 return err 247} 248 249// ListUptimeCheckIps returns the list of IPs that checkers run from 250func (c *UptimeCheckClient) ListUptimeCheckIps(ctx context.Context, req *monitoringpb.ListUptimeCheckIpsRequest, opts ...gax.CallOption) *UptimeCheckIpIterator { 251 ctx = insertMetadata(ctx, c.xGoogMetadata) 252 opts = append(c.CallOptions.ListUptimeCheckIps[0:len(c.CallOptions.ListUptimeCheckIps):len(c.CallOptions.ListUptimeCheckIps)], opts...) 253 it := &UptimeCheckIpIterator{} 254 req = proto.Clone(req).(*monitoringpb.ListUptimeCheckIpsRequest) 255 it.InternalFetch = func(pageSize int, pageToken string) ([]*monitoringpb.UptimeCheckIp, string, error) { 256 var resp *monitoringpb.ListUptimeCheckIpsResponse 257 req.PageToken = pageToken 258 if pageSize > math.MaxInt32 { 259 req.PageSize = math.MaxInt32 260 } else { 261 req.PageSize = int32(pageSize) 262 } 263 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 264 var err error 265 resp, err = c.uptimeCheckClient.ListUptimeCheckIps(ctx, req, settings.GRPC...) 266 return err 267 }, opts...) 268 if err != nil { 269 return nil, "", err 270 } 271 return resp.UptimeCheckIps, resp.NextPageToken, nil 272 } 273 fetch := func(pageSize int, pageToken string) (string, error) { 274 items, nextPageToken, err := it.InternalFetch(pageSize, pageToken) 275 if err != nil { 276 return "", err 277 } 278 it.items = append(it.items, items...) 279 return nextPageToken, nil 280 } 281 it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf) 282 it.pageInfo.MaxSize = int(req.PageSize) 283 it.pageInfo.Token = req.PageToken 284 return it 285} 286 287// UptimeCheckConfigIterator manages a stream of *monitoringpb.UptimeCheckConfig. 288type UptimeCheckConfigIterator struct { 289 items []*monitoringpb.UptimeCheckConfig 290 pageInfo *iterator.PageInfo 291 nextFunc func() error 292 293 // InternalFetch is for use by the Google Cloud Libraries only. 294 // It is not part of the stable interface of this package. 295 // 296 // InternalFetch returns results from a single call to the underlying RPC. 297 // The number of results is no greater than pageSize. 298 // If there are no more results, nextPageToken is empty and err is nil. 299 InternalFetch func(pageSize int, pageToken string) (results []*monitoringpb.UptimeCheckConfig, nextPageToken string, err error) 300} 301 302// PageInfo supports pagination. See the google.golang.org/api/iterator package for details. 303func (it *UptimeCheckConfigIterator) PageInfo() *iterator.PageInfo { 304 return it.pageInfo 305} 306 307// Next returns the next result. Its second return value is iterator.Done if there are no more 308// results. Once Next returns Done, all subsequent calls will return Done. 309func (it *UptimeCheckConfigIterator) Next() (*monitoringpb.UptimeCheckConfig, error) { 310 var item *monitoringpb.UptimeCheckConfig 311 if err := it.nextFunc(); err != nil { 312 return item, err 313 } 314 item = it.items[0] 315 it.items = it.items[1:] 316 return item, nil 317} 318 319func (it *UptimeCheckConfigIterator) bufLen() int { 320 return len(it.items) 321} 322 323func (it *UptimeCheckConfigIterator) takeBuf() interface{} { 324 b := it.items 325 it.items = nil 326 return b 327} 328 329// UptimeCheckIpIterator manages a stream of *monitoringpb.UptimeCheckIp. 330type UptimeCheckIpIterator struct { 331 items []*monitoringpb.UptimeCheckIp 332 pageInfo *iterator.PageInfo 333 nextFunc func() error 334 335 // InternalFetch is for use by the Google Cloud Libraries only. 336 // It is not part of the stable interface of this package. 337 // 338 // InternalFetch returns results from a single call to the underlying RPC. 339 // The number of results is no greater than pageSize. 340 // If there are no more results, nextPageToken is empty and err is nil. 341 InternalFetch func(pageSize int, pageToken string) (results []*monitoringpb.UptimeCheckIp, nextPageToken string, err error) 342} 343 344// PageInfo supports pagination. See the google.golang.org/api/iterator package for details. 345func (it *UptimeCheckIpIterator) PageInfo() *iterator.PageInfo { 346 return it.pageInfo 347} 348 349// Next returns the next result. Its second return value is iterator.Done if there are no more 350// results. Once Next returns Done, all subsequent calls will return Done. 351func (it *UptimeCheckIpIterator) Next() (*monitoringpb.UptimeCheckIp, error) { 352 var item *monitoringpb.UptimeCheckIp 353 if err := it.nextFunc(); err != nil { 354 return item, err 355 } 356 item = it.items[0] 357 it.items = it.items[1:] 358 return item, nil 359} 360 361func (it *UptimeCheckIpIterator) bufLen() int { 362 return len(it.items) 363} 364 365func (it *UptimeCheckIpIterator) takeBuf() interface{} { 366 b := it.items 367 it.items = nil 368 return b 369} 370