Skip to content

Commit be46be1

Browse files
committed
Removes START / END blocks for snippets.
1 parent cfa07e8 commit be46be1

File tree

1 file changed

+72
-32
lines changed

1 file changed

+72
-32
lines changed

iot/api-client/manager/manager.py

Lines changed: 72 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ 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(
55+
publishers.append(policy.service_account(
5656
'cloud-iot@system.gserviceaccount.com'))
5757
policy['roles/pubsub.publisher'] = publishers
5858
topic.set_iam_policy(policy)
@@ -64,7 +64,6 @@ def get_client(service_account_json, api_key):
6464
"""Returns an authorized API client by discovering the IoT API using the
6565
provided API key and creating a service object using the service account
6666
credentials JSON."""
67-
# [START authorize]
6867
api_scopes = ['https://www.googleapis.com/auth/cloud-platform']
6968
api_version = 'v1beta1'
7069
discovery_api = 'https://cloudiot.googleapis.com/$discovery/rest'
@@ -82,15 +81,13 @@ def get_client(service_account_json, api_key):
8281
api_version,
8382
discoveryServiceUrl=discovery_url,
8483
credentials=scoped_credentials)
85-
# [END authorize]
8684

8785

8886
def create_rs256_device(
8987
service_account_json, api_key, project_id, cloud_region, registry_id,
9088
device_id, certificate_file):
9189
"""Create a new device with the given id, using RS256 for
9290
authentication."""
93-
# [START create_rs256_device]
9491
registry_name = 'projects/{}/locations/{}/registries/{}'.format(
9592
project_id, cloud_region, registry_id)
9693

@@ -111,15 +108,13 @@ def create_rs256_device(
111108

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

116112

117113
def create_es256_device(
118114
service_account_json, api_key, project_id, cloud_region, registry_id,
119115
device_id, public_key_file):
120116
"""Create a new device with the given id, using ES256 for
121117
authentication."""
122-
# [START create_rs256_device]
123118
registry_name = 'projects/{}/locations/{}/registries/{}'.format(
124119
project_id, cloud_region, registry_id)
125120

@@ -140,14 +135,12 @@ def create_es256_device(
140135

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

145139

146140
def create_unauth_device(
147141
service_account_json, api_key, project_id, cloud_region, registry_id,
148142
device_id):
149143
"""Create a new device without authentication."""
150-
# [START create_noauth_device]
151144
registry_name = 'projects/{}/locations/{}/registries/{}'.format(
152145
project_id, cloud_region, registry_id)
153146

@@ -158,14 +151,12 @@ def create_unauth_device(
158151

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

163155

164156
def delete_device(
165157
service_account_json, api_key, project_id, cloud_region, registry_id,
166158
device_id):
167159
"""Delete the device with the given id."""
168-
# [START delete_device]
169160
print('Delete device')
170161
client = get_client(service_account_json, api_key)
171162
registry_name = 'projects/{}/locations/{}/registries/{}'.format(
@@ -175,28 +166,24 @@ def delete_device(
175166

176167
devices = client.projects().locations().registries().devices()
177168
return devices.delete(name=device_name).execute()
178-
# [END delete_device]
179169

180170

181171
def delete_registry(
182172
service_account_json, api_key, project_id, cloud_region, registry_id):
183173
"""Deletes the specified registry."""
184-
# [START delete_registry]
185174
print('Delete registry')
186175
client = get_client(service_account_json, api_key)
187176
registry_name = 'projects/{}/locations/{}/registries/{}'.format(
188177
project_id, cloud_region, registry_id)
189178

190179
registries = client.projects().locations().registries()
191180
return registries.delete(name=registry_name).execute()
192-
# [END delete_registry]
193181

194182

195183
def get_device(
196184
service_account_json, api_key, project_id, cloud_region, registry_id,
197185
device_id):
198186
"""Retrieve the device with the given id."""
199-
# [START delete_device]
200187
print('Getting device')
201188
client = get_client(service_account_json, api_key)
202189
registry_name = 'projects/{}/locations/{}/registries/{}'.format(
@@ -223,13 +210,11 @@ def get_device(
223210
'cloudUpdateTime')))
224211

225212
return device
226-
# [END delete_device]
227213

228214

229215
def list_devices(
230216
service_account_json, api_key, project_id, cloud_region, registry_id):
231217
"""List all devices in the registry."""
232-
# [START list_devices]
233218
print('Listing devices')
234219
registry_path = 'projects/{}/locations/{}/registries/{}'.format(
235220
project_id, cloud_region, registry_id)
@@ -243,14 +228,30 @@ def list_devices(
243228
device.get('id')))
244229

245230
return devices
246-
# [list_devices]
247231

248232

249-
def open_registry(
233+
def list_registries(service_account_json, api_key, project_id, cloud_region):
234+
"""List all registries in the project."""
235+
print('Listing Registries')
236+
registry_path = 'projects/{}/locations/{}'.format(
237+
project_id, cloud_region)
238+
client = get_client(service_account_json, api_key)
239+
registries= client.projects().locations().registries().list(
240+
parent=registry_path).execute().get('deviceRegistries', [])
241+
242+
for registry in registries:
243+
print('id: {}\n\tname: {}'.format(
244+
registry.get('id'),
245+
registry.get('name')))
246+
247+
return registries
248+
249+
250+
def create_registry(
250251
service_account_json, api_key, project_id, cloud_region, pubsub_topic,
251252
registry_id):
252-
"""Gets or creates a device registry."""
253-
print('Creating registry')
253+
""" Creates a registry and returns the result. Returns an empty result if
254+
the registry already exists."""
254255
client = get_client(service_account_json, api_key)
255256
registry_parent = 'projects/{}/locations/{}'.format(
256257
project_id,
@@ -264,21 +265,48 @@ def open_registry(
264265
request = client.projects().locations().registries().create(
265266
parent=registry_parent, body=body)
266267

268+
print (request)
269+
267270
try:
268271
response = request.execute()
269-
print('Created registry', registry_id)
270-
print(response)
272+
print('Created registry')
273+
return response
271274
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()
275+
return ""
276+
277+
def get_registry(
278+
service_account_json, api_key, project_id, cloud_region, registry_id):
279+
""" Retrieves a device registry."""
280+
client = get_client(service_account_json, api_key)
281+
registry_parent = 'projects/{}/locations/{}'.format(
282+
project_id,
283+
cloud_region)
284+
topic_name = '{}/registries/{}'.format(registry_parent, registry_id)
285+
request = client.projects().locations().registries().get(name=topic_name)
286+
return request.execute()
287+
288+
def open_registry(
289+
service_account_json, api_key, project_id, cloud_region, pubsub_topic,
290+
registry_id):
291+
"""Gets or creates a device registry."""
292+
print('Creating registry')
293+
294+
response = create_registry(
295+
service_account_json, api_key, project_id, cloud_region,
296+
pubsub_topic, registry_id)
297+
298+
if (response is ""):
299+
# Device registry already exists
300+
print(
301+
'Registry {} already exists - looking it up instead.'.format(
302+
registry_id))
303+
response = get_registry(
304+
service_account_json, api_key, project_id, cloud_region,
305+
registry_id)
306+
307+
print('Registry {} opened: '.format(response.get('name')))
308+
print(response)
309+
282310

283311

284312
def patch_es256_auth(
@@ -394,7 +422,9 @@ def parse_command_line_args():
394422
command.add_parser('delete-device', help=delete_device.__doc__)
395423
command.add_parser('delete-registry', help=delete_registry.__doc__)
396424
command.add_parser('get', help=get_device.__doc__)
425+
command.add_parser('get-registry', help=get_device.__doc__)
397426
command.add_parser('list', help=list_devices.__doc__)
427+
command.add_parser('list-registries', help=list_registries.__doc__)
398428
command.add_parser('patch-es256', help=patch_es256_auth.__doc__)
399429
command.add_parser('patch-rs256', help=patch_rsa256_auth.__doc__)
400430

@@ -448,6 +478,16 @@ def run_command(args):
448478
args.service_account_json, args.api_key, args.project_id,
449479
args.cloud_region, args.registry_id)
450480

481+
elif args.command == 'get-registry':
482+
print(get_registry(
483+
args.service_account_json, args.api_key, args.project_id,
484+
args.cloud_region, args.registry_id))
485+
486+
elif args.command == 'list-registries':
487+
list_registries(
488+
args.service_account_json, args.api_key, args.project_id,
489+
args.cloud_region)
490+
451491
elif args.command == 'patch-es256':
452492
if (args.ec_public_key_file is None):
453493
sys.exit('Error: specify --ec_public_key_file')

0 commit comments

Comments
 (0)