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