1// Copyright 2020 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 protoc-gen-go_gapic. DO NOT EDIT. 16 17package iot 18 19import ( 20 "context" 21 "fmt" 22 "math" 23 "net/url" 24 "time" 25 26 "github.com/golang/protobuf/proto" 27 gax "github.com/googleapis/gax-go/v2" 28 "google.golang.org/api/iterator" 29 "google.golang.org/api/option" 30 gtransport "google.golang.org/api/transport/grpc" 31 iotpb "google.golang.org/genproto/googleapis/cloud/iot/v1" 32 iampb "google.golang.org/genproto/googleapis/iam/v1" 33 "google.golang.org/grpc" 34 "google.golang.org/grpc/codes" 35 "google.golang.org/grpc/metadata" 36) 37 38var newDeviceManagerClientHook clientHook 39 40// DeviceManagerCallOptions contains the retry settings for each method of DeviceManagerClient. 41type DeviceManagerCallOptions struct { 42 CreateDeviceRegistry []gax.CallOption 43 GetDeviceRegistry []gax.CallOption 44 UpdateDeviceRegistry []gax.CallOption 45 DeleteDeviceRegistry []gax.CallOption 46 ListDeviceRegistries []gax.CallOption 47 CreateDevice []gax.CallOption 48 GetDevice []gax.CallOption 49 UpdateDevice []gax.CallOption 50 DeleteDevice []gax.CallOption 51 ListDevices []gax.CallOption 52 ModifyCloudToDeviceConfig []gax.CallOption 53 ListDeviceConfigVersions []gax.CallOption 54 ListDeviceStates []gax.CallOption 55 SetIamPolicy []gax.CallOption 56 GetIamPolicy []gax.CallOption 57 TestIamPermissions []gax.CallOption 58 SendCommandToDevice []gax.CallOption 59 BindDeviceToGateway []gax.CallOption 60 UnbindDeviceFromGateway []gax.CallOption 61} 62 63func defaultDeviceManagerClientOptions() []option.ClientOption { 64 return []option.ClientOption{ 65 option.WithEndpoint("cloudiot.googleapis.com:443"), 66 option.WithGRPCDialOption(grpc.WithDisableServiceConfig()), 67 option.WithScopes(DefaultAuthScopes()...), 68 option.WithGRPCDialOption(grpc.WithDefaultCallOptions( 69 grpc.MaxCallRecvMsgSize(math.MaxInt32))), 70 } 71} 72 73func defaultDeviceManagerCallOptions() *DeviceManagerCallOptions { 74 return &DeviceManagerCallOptions{ 75 CreateDeviceRegistry: []gax.CallOption{}, 76 GetDeviceRegistry: []gax.CallOption{ 77 gax.WithRetry(func() gax.Retryer { 78 return gax.OnCodes([]codes.Code{ 79 codes.Unavailable, 80 codes.DeadlineExceeded, 81 }, gax.Backoff{ 82 Initial: 100 * time.Millisecond, 83 Max: 60000 * time.Millisecond, 84 Multiplier: 1.30, 85 }) 86 }), 87 }, 88 UpdateDeviceRegistry: []gax.CallOption{}, 89 DeleteDeviceRegistry: []gax.CallOption{ 90 gax.WithRetry(func() gax.Retryer { 91 return gax.OnCodes([]codes.Code{ 92 codes.Unavailable, 93 codes.DeadlineExceeded, 94 }, gax.Backoff{ 95 Initial: 100 * time.Millisecond, 96 Max: 60000 * time.Millisecond, 97 Multiplier: 1.30, 98 }) 99 }), 100 }, 101 ListDeviceRegistries: []gax.CallOption{ 102 gax.WithRetry(func() gax.Retryer { 103 return gax.OnCodes([]codes.Code{ 104 codes.Unavailable, 105 codes.DeadlineExceeded, 106 }, gax.Backoff{ 107 Initial: 100 * time.Millisecond, 108 Max: 60000 * time.Millisecond, 109 Multiplier: 1.30, 110 }) 111 }), 112 }, 113 CreateDevice: []gax.CallOption{}, 114 GetDevice: []gax.CallOption{ 115 gax.WithRetry(func() gax.Retryer { 116 return gax.OnCodes([]codes.Code{ 117 codes.Unavailable, 118 codes.DeadlineExceeded, 119 }, gax.Backoff{ 120 Initial: 100 * time.Millisecond, 121 Max: 60000 * time.Millisecond, 122 Multiplier: 1.30, 123 }) 124 }), 125 }, 126 UpdateDevice: []gax.CallOption{}, 127 DeleteDevice: []gax.CallOption{ 128 gax.WithRetry(func() gax.Retryer { 129 return gax.OnCodes([]codes.Code{ 130 codes.Unavailable, 131 codes.DeadlineExceeded, 132 }, gax.Backoff{ 133 Initial: 100 * time.Millisecond, 134 Max: 60000 * time.Millisecond, 135 Multiplier: 1.30, 136 }) 137 }), 138 }, 139 ListDevices: []gax.CallOption{ 140 gax.WithRetry(func() gax.Retryer { 141 return gax.OnCodes([]codes.Code{ 142 codes.Unavailable, 143 codes.DeadlineExceeded, 144 }, gax.Backoff{ 145 Initial: 100 * time.Millisecond, 146 Max: 60000 * time.Millisecond, 147 Multiplier: 1.30, 148 }) 149 }), 150 }, 151 ModifyCloudToDeviceConfig: []gax.CallOption{ 152 gax.WithRetry(func() gax.Retryer { 153 return gax.OnCodes([]codes.Code{ 154 codes.Unavailable, 155 codes.DeadlineExceeded, 156 codes.ResourceExhausted, 157 }, gax.Backoff{ 158 Initial: 1000 * time.Millisecond, 159 Max: 60000 * time.Millisecond, 160 Multiplier: 1.30, 161 }) 162 }), 163 }, 164 ListDeviceConfigVersions: []gax.CallOption{ 165 gax.WithRetry(func() gax.Retryer { 166 return gax.OnCodes([]codes.Code{ 167 codes.Unavailable, 168 codes.DeadlineExceeded, 169 }, gax.Backoff{ 170 Initial: 100 * time.Millisecond, 171 Max: 60000 * time.Millisecond, 172 Multiplier: 1.30, 173 }) 174 }), 175 }, 176 ListDeviceStates: []gax.CallOption{ 177 gax.WithRetry(func() gax.Retryer { 178 return gax.OnCodes([]codes.Code{ 179 codes.Unavailable, 180 codes.DeadlineExceeded, 181 }, gax.Backoff{ 182 Initial: 100 * time.Millisecond, 183 Max: 60000 * time.Millisecond, 184 Multiplier: 1.30, 185 }) 186 }), 187 }, 188 SetIamPolicy: []gax.CallOption{}, 189 GetIamPolicy: []gax.CallOption{}, 190 TestIamPermissions: []gax.CallOption{}, 191 SendCommandToDevice: []gax.CallOption{ 192 gax.WithRetry(func() gax.Retryer { 193 return gax.OnCodes([]codes.Code{ 194 codes.Unavailable, 195 codes.DeadlineExceeded, 196 codes.ResourceExhausted, 197 }, gax.Backoff{ 198 Initial: 1000 * time.Millisecond, 199 Max: 60000 * time.Millisecond, 200 Multiplier: 1.30, 201 }) 202 }), 203 }, 204 BindDeviceToGateway: []gax.CallOption{}, 205 UnbindDeviceFromGateway: []gax.CallOption{}, 206 } 207} 208 209// DeviceManagerClient is a client for interacting with Cloud IoT API. 210// 211// Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls. 212type DeviceManagerClient struct { 213 // Connection pool of gRPC connections to the service. 214 connPool gtransport.ConnPool 215 216 // The gRPC API client. 217 deviceManagerClient iotpb.DeviceManagerClient 218 219 // The call options for this service. 220 CallOptions *DeviceManagerCallOptions 221 222 // The x-goog-* metadata to be sent with each request. 223 xGoogMetadata metadata.MD 224} 225 226// NewDeviceManagerClient creates a new device manager client. 227// 228// Internet of Things (IoT) service. Securely connect and manage IoT devices. 229func NewDeviceManagerClient(ctx context.Context, opts ...option.ClientOption) (*DeviceManagerClient, error) { 230 clientOpts := defaultDeviceManagerClientOptions() 231 232 if newDeviceManagerClientHook != nil { 233 hookOpts, err := newDeviceManagerClientHook(ctx, clientHookParams{}) 234 if err != nil { 235 return nil, err 236 } 237 clientOpts = append(clientOpts, hookOpts...) 238 } 239 240 connPool, err := gtransport.DialPool(ctx, append(clientOpts, opts...)...) 241 if err != nil { 242 return nil, err 243 } 244 c := &DeviceManagerClient{ 245 connPool: connPool, 246 CallOptions: defaultDeviceManagerCallOptions(), 247 248 deviceManagerClient: iotpb.NewDeviceManagerClient(connPool), 249 } 250 c.setGoogleClientInfo() 251 252 return c, nil 253} 254 255// Connection returns a connection to the API service. 256// 257// Deprecated. 258func (c *DeviceManagerClient) Connection() *grpc.ClientConn { 259 return c.connPool.Conn() 260} 261 262// Close closes the connection to the API service. The user should invoke this when 263// the client is no longer required. 264func (c *DeviceManagerClient) Close() error { 265 return c.connPool.Close() 266} 267 268// setGoogleClientInfo sets the name and version of the application in 269// the `x-goog-api-client` header passed on each request. Intended for 270// use by Google-written clients. 271func (c *DeviceManagerClient) setGoogleClientInfo(keyval ...string) { 272 kv := append([]string{"gl-go", versionGo()}, keyval...) 273 kv = append(kv, "gapic", versionClient, "gax", gax.Version, "grpc", grpc.Version) 274 c.xGoogMetadata = metadata.Pairs("x-goog-api-client", gax.XGoogHeader(kv...)) 275} 276 277// CreateDeviceRegistry creates a device registry that contains devices. 278func (c *DeviceManagerClient) CreateDeviceRegistry(ctx context.Context, req *iotpb.CreateDeviceRegistryRequest, opts ...gax.CallOption) (*iotpb.DeviceRegistry, error) { 279 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent()))) 280 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 281 opts = append(c.CallOptions.CreateDeviceRegistry[0:len(c.CallOptions.CreateDeviceRegistry):len(c.CallOptions.CreateDeviceRegistry)], opts...) 282 var resp *iotpb.DeviceRegistry 283 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 284 var err error 285 resp, err = c.deviceManagerClient.CreateDeviceRegistry(ctx, req, settings.GRPC...) 286 return err 287 }, opts...) 288 if err != nil { 289 return nil, err 290 } 291 return resp, nil 292} 293 294// GetDeviceRegistry gets a device registry configuration. 295func (c *DeviceManagerClient) GetDeviceRegistry(ctx context.Context, req *iotpb.GetDeviceRegistryRequest, opts ...gax.CallOption) (*iotpb.DeviceRegistry, error) { 296 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))) 297 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 298 opts = append(c.CallOptions.GetDeviceRegistry[0:len(c.CallOptions.GetDeviceRegistry):len(c.CallOptions.GetDeviceRegistry)], opts...) 299 var resp *iotpb.DeviceRegistry 300 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 301 var err error 302 resp, err = c.deviceManagerClient.GetDeviceRegistry(ctx, req, settings.GRPC...) 303 return err 304 }, opts...) 305 if err != nil { 306 return nil, err 307 } 308 return resp, nil 309} 310 311// UpdateDeviceRegistry updates a device registry configuration. 312func (c *DeviceManagerClient) UpdateDeviceRegistry(ctx context.Context, req *iotpb.UpdateDeviceRegistryRequest, opts ...gax.CallOption) (*iotpb.DeviceRegistry, error) { 313 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "device_registry.name", url.QueryEscape(req.GetDeviceRegistry().GetName()))) 314 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 315 opts = append(c.CallOptions.UpdateDeviceRegistry[0:len(c.CallOptions.UpdateDeviceRegistry):len(c.CallOptions.UpdateDeviceRegistry)], opts...) 316 var resp *iotpb.DeviceRegistry 317 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 318 var err error 319 resp, err = c.deviceManagerClient.UpdateDeviceRegistry(ctx, req, settings.GRPC...) 320 return err 321 }, opts...) 322 if err != nil { 323 return nil, err 324 } 325 return resp, nil 326} 327 328// DeleteDeviceRegistry deletes a device registry configuration. 329func (c *DeviceManagerClient) DeleteDeviceRegistry(ctx context.Context, req *iotpb.DeleteDeviceRegistryRequest, opts ...gax.CallOption) error { 330 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))) 331 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 332 opts = append(c.CallOptions.DeleteDeviceRegistry[0:len(c.CallOptions.DeleteDeviceRegistry):len(c.CallOptions.DeleteDeviceRegistry)], opts...) 333 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 334 var err error 335 _, err = c.deviceManagerClient.DeleteDeviceRegistry(ctx, req, settings.GRPC...) 336 return err 337 }, opts...) 338 return err 339} 340 341// ListDeviceRegistries lists device registries. 342func (c *DeviceManagerClient) ListDeviceRegistries(ctx context.Context, req *iotpb.ListDeviceRegistriesRequest, opts ...gax.CallOption) *DeviceRegistryIterator { 343 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent()))) 344 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 345 opts = append(c.CallOptions.ListDeviceRegistries[0:len(c.CallOptions.ListDeviceRegistries):len(c.CallOptions.ListDeviceRegistries)], opts...) 346 it := &DeviceRegistryIterator{} 347 req = proto.Clone(req).(*iotpb.ListDeviceRegistriesRequest) 348 it.InternalFetch = func(pageSize int, pageToken string) ([]*iotpb.DeviceRegistry, string, error) { 349 var resp *iotpb.ListDeviceRegistriesResponse 350 req.PageToken = pageToken 351 if pageSize > math.MaxInt32 { 352 req.PageSize = math.MaxInt32 353 } else { 354 req.PageSize = int32(pageSize) 355 } 356 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 357 var err error 358 resp, err = c.deviceManagerClient.ListDeviceRegistries(ctx, req, settings.GRPC...) 359 return err 360 }, opts...) 361 if err != nil { 362 return nil, "", err 363 } 364 365 it.Response = resp 366 return resp.DeviceRegistries, resp.NextPageToken, nil 367 } 368 fetch := func(pageSize int, pageToken string) (string, error) { 369 items, nextPageToken, err := it.InternalFetch(pageSize, pageToken) 370 if err != nil { 371 return "", err 372 } 373 it.items = append(it.items, items...) 374 return nextPageToken, nil 375 } 376 it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf) 377 it.pageInfo.MaxSize = int(req.PageSize) 378 it.pageInfo.Token = req.PageToken 379 return it 380} 381 382// CreateDevice creates a device in a device registry. 383func (c *DeviceManagerClient) CreateDevice(ctx context.Context, req *iotpb.CreateDeviceRequest, opts ...gax.CallOption) (*iotpb.Device, error) { 384 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent()))) 385 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 386 opts = append(c.CallOptions.CreateDevice[0:len(c.CallOptions.CreateDevice):len(c.CallOptions.CreateDevice)], opts...) 387 var resp *iotpb.Device 388 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 389 var err error 390 resp, err = c.deviceManagerClient.CreateDevice(ctx, req, settings.GRPC...) 391 return err 392 }, opts...) 393 if err != nil { 394 return nil, err 395 } 396 return resp, nil 397} 398 399// GetDevice gets details about a device. 400func (c *DeviceManagerClient) GetDevice(ctx context.Context, req *iotpb.GetDeviceRequest, opts ...gax.CallOption) (*iotpb.Device, error) { 401 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))) 402 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 403 opts = append(c.CallOptions.GetDevice[0:len(c.CallOptions.GetDevice):len(c.CallOptions.GetDevice)], opts...) 404 var resp *iotpb.Device 405 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 406 var err error 407 resp, err = c.deviceManagerClient.GetDevice(ctx, req, settings.GRPC...) 408 return err 409 }, opts...) 410 if err != nil { 411 return nil, err 412 } 413 return resp, nil 414} 415 416// UpdateDevice updates a device. 417func (c *DeviceManagerClient) UpdateDevice(ctx context.Context, req *iotpb.UpdateDeviceRequest, opts ...gax.CallOption) (*iotpb.Device, error) { 418 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "device.name", url.QueryEscape(req.GetDevice().GetName()))) 419 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 420 opts = append(c.CallOptions.UpdateDevice[0:len(c.CallOptions.UpdateDevice):len(c.CallOptions.UpdateDevice)], opts...) 421 var resp *iotpb.Device 422 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 423 var err error 424 resp, err = c.deviceManagerClient.UpdateDevice(ctx, req, settings.GRPC...) 425 return err 426 }, opts...) 427 if err != nil { 428 return nil, err 429 } 430 return resp, nil 431} 432 433// DeleteDevice deletes a device. 434func (c *DeviceManagerClient) DeleteDevice(ctx context.Context, req *iotpb.DeleteDeviceRequest, opts ...gax.CallOption) error { 435 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))) 436 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 437 opts = append(c.CallOptions.DeleteDevice[0:len(c.CallOptions.DeleteDevice):len(c.CallOptions.DeleteDevice)], opts...) 438 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 439 var err error 440 _, err = c.deviceManagerClient.DeleteDevice(ctx, req, settings.GRPC...) 441 return err 442 }, opts...) 443 return err 444} 445 446// ListDevices list devices in a device registry. 447func (c *DeviceManagerClient) ListDevices(ctx context.Context, req *iotpb.ListDevicesRequest, opts ...gax.CallOption) *DeviceIterator { 448 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent()))) 449 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 450 opts = append(c.CallOptions.ListDevices[0:len(c.CallOptions.ListDevices):len(c.CallOptions.ListDevices)], opts...) 451 it := &DeviceIterator{} 452 req = proto.Clone(req).(*iotpb.ListDevicesRequest) 453 it.InternalFetch = func(pageSize int, pageToken string) ([]*iotpb.Device, string, error) { 454 var resp *iotpb.ListDevicesResponse 455 req.PageToken = pageToken 456 if pageSize > math.MaxInt32 { 457 req.PageSize = math.MaxInt32 458 } else { 459 req.PageSize = int32(pageSize) 460 } 461 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 462 var err error 463 resp, err = c.deviceManagerClient.ListDevices(ctx, req, settings.GRPC...) 464 return err 465 }, opts...) 466 if err != nil { 467 return nil, "", err 468 } 469 470 it.Response = resp 471 return resp.Devices, resp.NextPageToken, nil 472 } 473 fetch := func(pageSize int, pageToken string) (string, error) { 474 items, nextPageToken, err := it.InternalFetch(pageSize, pageToken) 475 if err != nil { 476 return "", err 477 } 478 it.items = append(it.items, items...) 479 return nextPageToken, nil 480 } 481 it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf) 482 it.pageInfo.MaxSize = int(req.PageSize) 483 it.pageInfo.Token = req.PageToken 484 return it 485} 486 487// ModifyCloudToDeviceConfig modifies the configuration for the device, which is eventually sent from 488// the Cloud IoT Core servers. Returns the modified configuration version and 489// its metadata. 490func (c *DeviceManagerClient) ModifyCloudToDeviceConfig(ctx context.Context, req *iotpb.ModifyCloudToDeviceConfigRequest, opts ...gax.CallOption) (*iotpb.DeviceConfig, error) { 491 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))) 492 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 493 opts = append(c.CallOptions.ModifyCloudToDeviceConfig[0:len(c.CallOptions.ModifyCloudToDeviceConfig):len(c.CallOptions.ModifyCloudToDeviceConfig)], opts...) 494 var resp *iotpb.DeviceConfig 495 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 496 var err error 497 resp, err = c.deviceManagerClient.ModifyCloudToDeviceConfig(ctx, req, settings.GRPC...) 498 return err 499 }, opts...) 500 if err != nil { 501 return nil, err 502 } 503 return resp, nil 504} 505 506// ListDeviceConfigVersions lists the last few versions of the device configuration in descending 507// order (i.e.: newest first). 508func (c *DeviceManagerClient) ListDeviceConfigVersions(ctx context.Context, req *iotpb.ListDeviceConfigVersionsRequest, opts ...gax.CallOption) (*iotpb.ListDeviceConfigVersionsResponse, error) { 509 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))) 510 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 511 opts = append(c.CallOptions.ListDeviceConfigVersions[0:len(c.CallOptions.ListDeviceConfigVersions):len(c.CallOptions.ListDeviceConfigVersions)], opts...) 512 var resp *iotpb.ListDeviceConfigVersionsResponse 513 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 514 var err error 515 resp, err = c.deviceManagerClient.ListDeviceConfigVersions(ctx, req, settings.GRPC...) 516 return err 517 }, opts...) 518 if err != nil { 519 return nil, err 520 } 521 return resp, nil 522} 523 524// ListDeviceStates lists the last few versions of the device state in descending order (i.e.: 525// newest first). 526func (c *DeviceManagerClient) ListDeviceStates(ctx context.Context, req *iotpb.ListDeviceStatesRequest, opts ...gax.CallOption) (*iotpb.ListDeviceStatesResponse, error) { 527 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))) 528 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 529 opts = append(c.CallOptions.ListDeviceStates[0:len(c.CallOptions.ListDeviceStates):len(c.CallOptions.ListDeviceStates)], opts...) 530 var resp *iotpb.ListDeviceStatesResponse 531 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 532 var err error 533 resp, err = c.deviceManagerClient.ListDeviceStates(ctx, req, settings.GRPC...) 534 return err 535 }, opts...) 536 if err != nil { 537 return nil, err 538 } 539 return resp, nil 540} 541 542// SetIamPolicy sets the access control policy on the specified resource. Replaces any 543// existing policy. 544func (c *DeviceManagerClient) SetIamPolicy(ctx context.Context, req *iampb.SetIamPolicyRequest, opts ...gax.CallOption) (*iampb.Policy, error) { 545 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "resource", url.QueryEscape(req.GetResource()))) 546 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 547 opts = append(c.CallOptions.SetIamPolicy[0:len(c.CallOptions.SetIamPolicy):len(c.CallOptions.SetIamPolicy)], opts...) 548 var resp *iampb.Policy 549 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 550 var err error 551 resp, err = c.deviceManagerClient.SetIamPolicy(ctx, req, settings.GRPC...) 552 return err 553 }, opts...) 554 if err != nil { 555 return nil, err 556 } 557 return resp, nil 558} 559 560// GetIamPolicy gets the access control policy for a resource. 561// Returns an empty policy if the resource exists and does not have a policy 562// set. 563func (c *DeviceManagerClient) GetIamPolicy(ctx context.Context, req *iampb.GetIamPolicyRequest, opts ...gax.CallOption) (*iampb.Policy, error) { 564 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "resource", url.QueryEscape(req.GetResource()))) 565 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 566 opts = append(c.CallOptions.GetIamPolicy[0:len(c.CallOptions.GetIamPolicy):len(c.CallOptions.GetIamPolicy)], opts...) 567 var resp *iampb.Policy 568 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 569 var err error 570 resp, err = c.deviceManagerClient.GetIamPolicy(ctx, req, settings.GRPC...) 571 return err 572 }, opts...) 573 if err != nil { 574 return nil, err 575 } 576 return resp, nil 577} 578 579// TestIamPermissions returns permissions that a caller has on the specified resource. 580// If the resource does not exist, this will return an empty set of 581// permissions, not a NOT_FOUND error. 582func (c *DeviceManagerClient) TestIamPermissions(ctx context.Context, req *iampb.TestIamPermissionsRequest, opts ...gax.CallOption) (*iampb.TestIamPermissionsResponse, error) { 583 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "resource", url.QueryEscape(req.GetResource()))) 584 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 585 opts = append(c.CallOptions.TestIamPermissions[0:len(c.CallOptions.TestIamPermissions):len(c.CallOptions.TestIamPermissions)], opts...) 586 var resp *iampb.TestIamPermissionsResponse 587 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 588 var err error 589 resp, err = c.deviceManagerClient.TestIamPermissions(ctx, req, settings.GRPC...) 590 return err 591 }, opts...) 592 if err != nil { 593 return nil, err 594 } 595 return resp, nil 596} 597 598// SendCommandToDevice sends a command to the specified device. In order for a device to be able 599// to receive commands, it must: 600// 601// be connected to Cloud IoT Core using the MQTT protocol, and 602// 603// be subscribed to the group of MQTT topics specified by 604// /devices/{device-id}/commands/#. This subscription will receive commands 605// at the top-level topic /devices/{device-id}/commands as well as commands 606// for subfolders, like /devices/{device-id}/commands/subfolder. 607// Note that subscribing to specific subfolders is not supported. 608// If the command could not be delivered to the device, this method will 609// return an error; in particular, if the device is not subscribed, this 610// method will return FAILED_PRECONDITION. Otherwise, this method will 611// return OK. If the subscription is QoS 1, at least once delivery will be 612// guaranteed; for QoS 0, no acknowledgment will be expected from the device. 613func (c *DeviceManagerClient) SendCommandToDevice(ctx context.Context, req *iotpb.SendCommandToDeviceRequest, opts ...gax.CallOption) (*iotpb.SendCommandToDeviceResponse, error) { 614 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))) 615 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 616 opts = append(c.CallOptions.SendCommandToDevice[0:len(c.CallOptions.SendCommandToDevice):len(c.CallOptions.SendCommandToDevice)], opts...) 617 var resp *iotpb.SendCommandToDeviceResponse 618 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 619 var err error 620 resp, err = c.deviceManagerClient.SendCommandToDevice(ctx, req, settings.GRPC...) 621 return err 622 }, opts...) 623 if err != nil { 624 return nil, err 625 } 626 return resp, nil 627} 628 629// BindDeviceToGateway associates the device with the gateway. 630func (c *DeviceManagerClient) BindDeviceToGateway(ctx context.Context, req *iotpb.BindDeviceToGatewayRequest, opts ...gax.CallOption) (*iotpb.BindDeviceToGatewayResponse, error) { 631 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent()))) 632 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 633 opts = append(c.CallOptions.BindDeviceToGateway[0:len(c.CallOptions.BindDeviceToGateway):len(c.CallOptions.BindDeviceToGateway)], opts...) 634 var resp *iotpb.BindDeviceToGatewayResponse 635 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 636 var err error 637 resp, err = c.deviceManagerClient.BindDeviceToGateway(ctx, req, settings.GRPC...) 638 return err 639 }, opts...) 640 if err != nil { 641 return nil, err 642 } 643 return resp, nil 644} 645 646// UnbindDeviceFromGateway deletes the association between the device and the gateway. 647func (c *DeviceManagerClient) UnbindDeviceFromGateway(ctx context.Context, req *iotpb.UnbindDeviceFromGatewayRequest, opts ...gax.CallOption) (*iotpb.UnbindDeviceFromGatewayResponse, error) { 648 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent()))) 649 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 650 opts = append(c.CallOptions.UnbindDeviceFromGateway[0:len(c.CallOptions.UnbindDeviceFromGateway):len(c.CallOptions.UnbindDeviceFromGateway)], opts...) 651 var resp *iotpb.UnbindDeviceFromGatewayResponse 652 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 653 var err error 654 resp, err = c.deviceManagerClient.UnbindDeviceFromGateway(ctx, req, settings.GRPC...) 655 return err 656 }, opts...) 657 if err != nil { 658 return nil, err 659 } 660 return resp, nil 661} 662 663// DeviceIterator manages a stream of *iotpb.Device. 664type DeviceIterator struct { 665 items []*iotpb.Device 666 pageInfo *iterator.PageInfo 667 nextFunc func() error 668 669 // Response is the raw response for the current page. 670 // It must be cast to the RPC response type. 671 // Calling Next() or InternalFetch() updates this value. 672 Response interface{} 673 674 // InternalFetch is for use by the Google Cloud Libraries only. 675 // It is not part of the stable interface of this package. 676 // 677 // InternalFetch returns results from a single call to the underlying RPC. 678 // The number of results is no greater than pageSize. 679 // If there are no more results, nextPageToken is empty and err is nil. 680 InternalFetch func(pageSize int, pageToken string) (results []*iotpb.Device, nextPageToken string, err error) 681} 682 683// PageInfo supports pagination. See the google.golang.org/api/iterator package for details. 684func (it *DeviceIterator) PageInfo() *iterator.PageInfo { 685 return it.pageInfo 686} 687 688// Next returns the next result. Its second return value is iterator.Done if there are no more 689// results. Once Next returns Done, all subsequent calls will return Done. 690func (it *DeviceIterator) Next() (*iotpb.Device, error) { 691 var item *iotpb.Device 692 if err := it.nextFunc(); err != nil { 693 return item, err 694 } 695 item = it.items[0] 696 it.items = it.items[1:] 697 return item, nil 698} 699 700func (it *DeviceIterator) bufLen() int { 701 return len(it.items) 702} 703 704func (it *DeviceIterator) takeBuf() interface{} { 705 b := it.items 706 it.items = nil 707 return b 708} 709 710// DeviceRegistryIterator manages a stream of *iotpb.DeviceRegistry. 711type DeviceRegistryIterator struct { 712 items []*iotpb.DeviceRegistry 713 pageInfo *iterator.PageInfo 714 nextFunc func() error 715 716 // Response is the raw response for the current page. 717 // It must be cast to the RPC response type. 718 // Calling Next() or InternalFetch() updates this value. 719 Response interface{} 720 721 // InternalFetch is for use by the Google Cloud Libraries only. 722 // It is not part of the stable interface of this package. 723 // 724 // InternalFetch returns results from a single call to the underlying RPC. 725 // The number of results is no greater than pageSize. 726 // If there are no more results, nextPageToken is empty and err is nil. 727 InternalFetch func(pageSize int, pageToken string) (results []*iotpb.DeviceRegistry, nextPageToken string, err error) 728} 729 730// PageInfo supports pagination. See the google.golang.org/api/iterator package for details. 731func (it *DeviceRegistryIterator) PageInfo() *iterator.PageInfo { 732 return it.pageInfo 733} 734 735// Next returns the next result. Its second return value is iterator.Done if there are no more 736// results. Once Next returns Done, all subsequent calls will return Done. 737func (it *DeviceRegistryIterator) Next() (*iotpb.DeviceRegistry, error) { 738 var item *iotpb.DeviceRegistry 739 if err := it.nextFunc(); err != nil { 740 return item, err 741 } 742 item = it.items[0] 743 it.items = it.items[1:] 744 return item, nil 745} 746 747func (it *DeviceRegistryIterator) bufLen() int { 748 return len(it.items) 749} 750 751func (it *DeviceRegistryIterator) takeBuf() interface{} { 752 b := it.items 753 it.items = nil 754 return b 755} 756