Skip to content

Commit fcaf37c

Browse files
gguussJon Wayne Parrott
authored and
Jon Wayne Parrott
committed
Changes for snippets (#1105)
* Removes START / END blocks for snippets. * Fixes style * Fixes IAM section for Python3 compatibility.
1 parent b95874a commit fcaf37c

File tree

1 file changed

+77
-34
lines changed

1 file changed

+77
-34
lines changed

iot/api-client/manager/manager.py

+77-34
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,12 @@ def create_iot_topic(topic_name):
5252
topic = pubsub_client.topic(topic_name)
5353
policy = topic.get_iam_policy()
5454
publishers = policy.get('roles/pubsub.publisher', [])
55-
publishers.add(policy.service_account(
56-
'cloud-iot@system.gserviceaccount.com'))
55+
if hasattr(publishers, "append"):
56+
publishers.append(policy.service_account(
57+
'cloud-iot@system.gserviceaccount.com'))
58+
else:
59+
publishers.add(policy.service_account(
60+
'cloud-iot@system.gserviceaccount.com'))
5761
policy['roles/pubsub.publisher'] = publishers
5862
topic.set_iam_policy(policy)
5963

@@ -64,7 +68,6 @@ def get_client(service_account_json, api_key):
6468
"""Returns an authorized API client by discovering the IoT API using the
6569
provided API key and creating a service object using the service account
6670
credentials JSON."""
67-
# [START authorize]
6871
api_scopes = ['https://www.googleapis.com/auth/cloud-platform']
6972
api_version = 'v1beta1'
7073
discovery_api = 'https://cloudiot.googleapis.com/$discovery/rest'
@@ -82,15 +85,13 @@ def get_client(service_account_json, api_key):
8285
api_version,
8386
discoveryServiceUrl=discovery_url,
8487
credentials=scoped_credentials)
85-
# [END authorize]
8688

8789

8890
def create_rs256_device(
8991
service_account_json, api_key, project_id, cloud_region, registry_id,
9092
device_id, certificate_file):
9193
"""Create a new device with the given id, using RS256 for
9294
authentication."""
93-
# [START create_rs256_device]
9495
registry_name = 'projects/{}/locations/{}/registries/{}'.format(
9596
project_id, cloud_region, registry_id)
9697

@@ -111,15 +112,13 @@ def create_rs256_device(
111112

112113
devices = client.projects().locations().registries().devices()
113114
return devices.create(parent=registry_name, body=device_template).execute()
114-
# [END create_rs256_device]
115115

116116

117117
def create_es256_device(
118118
service_account_json, api_key, project_id, cloud_region, registry_id,
119119
device_id, public_key_file):
120120
"""Create a new device with the given id, using ES256 for
121121
authentication."""
122-
# [START create_rs256_device]
123122
registry_name = 'projects/{}/locations/{}/registries/{}'.format(
124123
project_id, cloud_region, registry_id)
125124

@@ -140,14 +139,12 @@ def create_es256_device(
140139

141140
devices = client.projects().locations().registries().devices()
142141
return devices.create(parent=registry_name, body=device_template).execute()
143-
# [END create_rs256_device]
144142

145143

146144
def create_unauth_device(
147145
service_account_json, api_key, project_id, cloud_region, registry_id,
148146
device_id):
149147
"""Create a new device without authentication."""
150-
# [START create_noauth_device]
151148
registry_name = 'projects/{}/locations/{}/registries/{}'.format(
152149
project_id, cloud_region, registry_id)
153150

@@ -158,14 +155,12 @@ def create_unauth_device(
158155

159156
devices = client.projects().locations().registries().devices()
160157
return devices.create(parent=registry_name, body=device_template).execute()
161-
# [END create_noauth_device]
162158

163159

164160
def delete_device(
165161
service_account_json, api_key, project_id, cloud_region, registry_id,
166162
device_id):
167163
"""Delete the device with the given id."""
168-
# [START delete_device]
169164
print('Delete device')
170165
client = get_client(service_account_json, api_key)
171166
registry_name = 'projects/{}/locations/{}/registries/{}'.format(
@@ -175,28 +170,24 @@ def delete_device(
175170

176171
devices = client.projects().locations().registries().devices()
177172
return devices.delete(name=device_name).execute()
178-
# [END delete_device]
179173

180174

181175
def delete_registry(
182176
service_account_json, api_key, project_id, cloud_region, registry_id):
183177
"""Deletes the specified registry."""
184-
# [START delete_registry]
185178
print('Delete registry')
186179
client = get_client(service_account_json, api_key)
187180
registry_name = 'projects/{}/locations/{}/registries/{}'.format(
188181
project_id, cloud_region, registry_id)
189182

190183
registries = client.projects().locations().registries()
191184
return registries.delete(name=registry_name).execute()
192-
# [END delete_registry]
193185

194186

195187
def get_device(
196188
service_account_json, api_key, project_id, cloud_region, registry_id,
197189
device_id):
198190
"""Retrieve the device with the given id."""
199-
# [START delete_device]
200191
print('Getting device')
201192
client = get_client(service_account_json, api_key)
202193
registry_name = 'projects/{}/locations/{}/registries/{}'.format(
@@ -223,13 +214,11 @@ def get_device(
223214
'cloudUpdateTime')))
224215

225216
return device
226-
# [END delete_device]
227217

228218

229219
def list_devices(
230220
service_account_json, api_key, project_id, cloud_region, registry_id):
231221
"""List all devices in the registry."""
232-
# [START list_devices]
233222
print('Listing devices')
234223
registry_path = 'projects/{}/locations/{}/registries/{}'.format(
235224
project_id, cloud_region, registry_id)
@@ -243,14 +232,30 @@ def list_devices(
243232
device.get('id')))
244233

245234
return devices
246-
# [list_devices]
247235

248236

249-
def open_registry(
237+
def list_registries(service_account_json, api_key, project_id, cloud_region):
238+
"""List all registries in the project."""
239+
print('Listing Registries')
240+
registry_path = 'projects/{}/locations/{}'.format(
241+
project_id, cloud_region)
242+
client = get_client(service_account_json, api_key)
243+
registries = client.projects().locations().registries().list(
244+
parent=registry_path).execute().get('deviceRegistries', [])
245+
246+
for registry in registries:
247+
print('id: {}\n\tname: {}'.format(
248+
registry.get('id'),
249+
registry.get('name')))
250+
251+
return registries
252+
253+
254+
def create_registry(
250255
service_account_json, api_key, project_id, cloud_region, pubsub_topic,
251256
registry_id):
252-
"""Gets or creates a device registry."""
253-
print('Creating registry')
257+
""" Creates a registry and returns the result. Returns an empty result if
258+
the registry already exists."""
254259
client = get_client(service_account_json, api_key)
255260
registry_parent = 'projects/{}/locations/{}'.format(
256261
project_id,
@@ -266,19 +271,45 @@ def open_registry(
266271

267272
try:
268273
response = request.execute()
269-
print('Created registry', registry_id)
270-
print(response)
271-
except HttpError as e:
272-
if e.resp.status == 409:
273-
# Device registry already exists
274-
print(
275-
'Registry', registry_id,
276-
'already exists - looking it up instead.')
277-
topic_name = '{}/registries/{}'.format(
278-
registry_parent, registry_id)
279-
request = client.projects().locations().registries(
280-
).get(name=topic_name)
281-
request.execute()
274+
print('Created registry')
275+
return response
276+
except HttpError:
277+
return ""
278+
279+
280+
def get_registry(
281+
service_account_json, api_key, project_id, cloud_region, registry_id):
282+
""" Retrieves a device registry."""
283+
client = get_client(service_account_json, api_key)
284+
registry_parent = 'projects/{}/locations/{}'.format(
285+
project_id,
286+
cloud_region)
287+
topic_name = '{}/registries/{}'.format(registry_parent, registry_id)
288+
request = client.projects().locations().registries().get(name=topic_name)
289+
return request.execute()
290+
291+
292+
def open_registry(
293+
service_account_json, api_key, project_id, cloud_region, pubsub_topic,
294+
registry_id):
295+
"""Gets or creates a device registry."""
296+
print('Creating registry')
297+
298+
response = create_registry(
299+
service_account_json, api_key, project_id, cloud_region,
300+
pubsub_topic, registry_id)
301+
302+
if (response is ""):
303+
# Device registry already exists
304+
print(
305+
'Registry {} already exists - looking it up instead.'.format(
306+
registry_id))
307+
response = get_registry(
308+
service_account_json, api_key, project_id, cloud_region,
309+
registry_id)
310+
311+
print('Registry {} opened: '.format(response.get('name')))
312+
print(response)
282313

283314

284315
def patch_es256_auth(
@@ -394,7 +425,9 @@ def parse_command_line_args():
394425
command.add_parser('delete-device', help=delete_device.__doc__)
395426
command.add_parser('delete-registry', help=delete_registry.__doc__)
396427
command.add_parser('get', help=get_device.__doc__)
428+
command.add_parser('get-registry', help=get_device.__doc__)
397429
command.add_parser('list', help=list_devices.__doc__)
430+
command.add_parser('list-registries', help=list_registries.__doc__)
398431
command.add_parser('patch-es256', help=patch_es256_auth.__doc__)
399432
command.add_parser('patch-rs256', help=patch_rsa256_auth.__doc__)
400433

@@ -448,6 +481,16 @@ def run_command(args):
448481
args.service_account_json, args.api_key, args.project_id,
449482
args.cloud_region, args.registry_id)
450483

484+
elif args.command == 'get-registry':
485+
print(get_registry(
486+
args.service_account_json, args.api_key, args.project_id,
487+
args.cloud_region, args.registry_id))
488+
489+
elif args.command == 'list-registries':
490+
list_registries(
491+
args.service_account_json, args.api_key, args.project_id,
492+
args.cloud_region)
493+
451494
elif args.command == 'patch-es256':
452495
if (args.ec_public_key_file is None):
453496
sys.exit('Error: specify --ec_public_key_file')

0 commit comments

Comments
 (0)