Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 14b893c

Browse filesBrowse files
authored
Remove stale test registries to avoid reaching limits (GoogleCloudPlatform#2159)
* Add cleanup step to tests to avoid hitting 100 registry limit * Cleans up older test registries in case tests were cancelled and resources leaked * Fixes broken style * Renames includecode blocks to be ^iot(.*)
1 parent 1bddafd commit 14b893c
Copy full SHA for 14b893c

File tree

Expand file treeCollapse file tree

3 files changed

+113
-39
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+113
-39
lines changed

‎iot/api-client/manager/manager.py

Copy file name to clipboardExpand all lines: iot/api-client/manager/manager.py
+33-33Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -142,22 +142,22 @@ def create_device(
142142
service_account_json, project_id, cloud_region, registry_id,
143143
device_id):
144144
"""Create a device to bind to a gateway if it does not exist."""
145-
# [START create_device]
145+
# [START iot_create_device]
146146
# Check that the device doesn't already exist
147147
exists = False
148148

149149
client = get_client(service_account_json)
150150
registry_path = 'projects/{}/locations/{}/registries/{}'.format(
151-
project_id, cloud_region, registry_id)
151+
project_id, cloud_region, registry_id)
152152

153153
devices = client.projects().locations().registries().devices(
154-
).list(
155-
parent=registry_path, fieldMask='config,gatewayConfig'
156-
).execute().get('devices', [])
154+
).list(
155+
parent=registry_path, fieldMask='config,gatewayConfig'
156+
).execute().get('devices', [])
157157

158158
for device in devices:
159-
if device.get('id') == device_id:
160-
exists = True
159+
if device.get('id') == device_id:
160+
exists = True
161161

162162
# Create the device
163163
registry_name = 'projects/{}/locations/{}/registries/{}'.format(
@@ -178,7 +178,7 @@ def create_device(
178178
print('Created Device {}'.format(res))
179179
else:
180180
print('Device exists, skipping')
181-
# [END create_device]
181+
# [END iot_create_device]
182182

183183

184184
def create_unauth_device(
@@ -295,9 +295,9 @@ def list_devices(
295295
).list(parent=registry_path).execute().get('devices', [])
296296

297297
for device in devices:
298-
print('Device: {} : {}'.format(
299-
device.get('numId'),
300-
device.get('id')))
298+
print('Device: {} : {}'.format(
299+
device.get('numId'),
300+
device.get('id')))
301301

302302
return devices
303303
# [END iot_list_devices]
@@ -314,9 +314,9 @@ def list_registries(service_account_json, project_id, cloud_region):
314314
parent=registry_path).execute().get('deviceRegistries', [])
315315

316316
for registry in registries:
317-
print('id: {}\n\tname: {}'.format(
318-
registry.get('id'),
319-
registry.get('name')))
317+
print('id: {}\n\tname: {}'.format(
318+
registry.get('id'),
319+
registry.get('name')))
320320

321321
return registries
322322
# [END iot_list_registries]
@@ -375,7 +375,7 @@ def open_registry(
375375
service_account_json, project_id, cloud_region,
376376
pubsub_topic, registry_id)
377377

378-
if (response is ""):
378+
if response == "":
379379
# Device registry already exists
380380
print(
381381
'Registry {} already exists - looking it up instead.'.format(
@@ -559,7 +559,7 @@ def create_gateway(
559559
service_account_json, project_id, cloud_region, registry_id, device_id,
560560
gateway_id, certificate_file, algorithm):
561561
"""Create a gateway to bind devices to."""
562-
# [START create_gateway]
562+
# [START iot_create_gateway]
563563
# Check that the gateway doesn't already exist
564564
exists = False
565565
client = get_client(service_account_json)
@@ -572,14 +572,14 @@ def create_gateway(
572572
).execute().get('devices', [])
573573

574574
for device in devices:
575-
if device.get('id') == gateway_id:
576-
exists = True
577-
print('Device: {} : {} : {} : {}'.format(
578-
device.get('id'),
579-
device.get('numId'),
580-
device.get('config'),
581-
device.get('gatewayConfig')
582-
))
575+
if device.get('id') == gateway_id:
576+
exists = True
577+
print('Device: {} : {} : {} : {}'.format(
578+
device.get('id'),
579+
device.get('numId'),
580+
device.get('config'),
581+
device.get('gatewayConfig')
582+
))
583583

584584
# Create the gateway
585585
registry_name = 'projects/{}/locations/{}/registries/{}'.format(
@@ -615,14 +615,14 @@ def create_gateway(
615615
print('Created gateway {}'.format(res))
616616
else:
617617
print('Gateway exists, skipping')
618-
# [END create_gateway]
618+
# [END iot_create_gateway]
619619

620620

621621
def bind_device_to_gateway(
622622
service_account_json, project_id, cloud_region, registry_id, device_id,
623623
gateway_id):
624624
"""Binds a device to a gateway."""
625-
# [START bind_device_to_gateway]
625+
# [START iot_bind_device_to_gateway]
626626
client = get_client(service_account_json)
627627

628628
create_device(
@@ -638,14 +638,14 @@ def bind_device_to_gateway(
638638
client.projects().locations().registries().bindDeviceToGateway(
639639
parent=registry_name, body=bind_request).execute()
640640
print('Device Bound!')
641-
# [END bind_device_to_gateway]
641+
# [END iot_bind_device_to_gateway]
642642

643643

644644
def unbind_device_from_gateway(
645645
service_account_json, project_id, cloud_region, registry_id, device_id,
646646
gateway_id):
647647
"""Unbinds a device to a gateway."""
648-
# [START unbind_device_from_gateway]
648+
# [START iot_unbind_device_from_gateway]
649649
client = get_client(service_account_json)
650650

651651
registry_name = 'projects/{}/locations/{}/registries/{}'.format(
@@ -658,13 +658,13 @@ def unbind_device_from_gateway(
658658
res = client.projects().locations().registries().unbindDeviceFromGateway(
659659
parent=registry_name, body=bind_request).execute()
660660
print('Device unbound: {}'.format(res))
661-
# [END unbind_device_from_gateway]
661+
# [END iot_unbind_device_from_gateway]
662662

663663

664664
def list_gateways(
665665
service_account_json, project_id, cloud_region, registry_id):
666666
"""Lists gateways in a registry"""
667-
# [START list_gateways]
667+
# [START iot_list_gateways]
668668
client = get_client(service_account_json)
669669
registry_path = 'projects/{}/locations/{}/registries/{}'.format(
670670
project_id, cloud_region, registry_id)
@@ -678,14 +678,14 @@ def list_gateways(
678678
if device.get('gatewayConfig') is not None:
679679
if device.get('gatewayConfig').get('gatewayType') == 'GATEWAY':
680680
print('Gateway ID: {}\n\t{}'.format(device.get('id'), device))
681-
# [END list_gateways]
681+
# [END iot_list_gateways]
682682

683683

684684
def list_devices_for_gateway(
685685
service_account_json, project_id, cloud_region, registry_id,
686686
gateway_id):
687687
"""List devices bound to a gateway"""
688-
# [START list_devices_for_gateway]
688+
# [START iot_list_devices_for_gateway]
689689
client = get_client(service_account_json)
690690

691691
registry_name = 'projects/{}/locations/{}/registries/{}'.format(
@@ -715,7 +715,7 @@ def list_devices_for_gateway(
715715
else:
716716
if not found:
717717
print('No devices bound to gateway {}'.format(gateway_id))
718-
# [END list_devices_for_gateway]
718+
# [END iot_list_devices_for_gateway]
719719

720720

721721
def parse_command_line_args():

‎iot/api-client/manager/manager_test.py

Copy file name to clipboardExpand all lines: iot/api-client/manager/manager_test.py
+74Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
import datetime
1516
import os
1617
import sys
1718
import time
@@ -40,6 +41,79 @@
4041
registry_id = 'test-registry-{}'.format(int(time.time()))
4142

4243

44+
@pytest.fixture(scope="session", autouse=True)
45+
def clean_up_registries():
46+
all_registries = manager.list_registries(
47+
service_account_json, project_id, cloud_region)
48+
49+
for registry in all_registries:
50+
registry_id = registry.get('id')
51+
if registry_id.find('test-registry-') == 0:
52+
time_str = registry_id[
53+
registry_id.rfind('-') + 1: len(registry_id)]
54+
test_date = datetime.datetime.utcfromtimestamp(int(time_str))
55+
now_date = datetime.datetime.utcfromtimestamp(int(time.time()))
56+
difftime = now_date - test_date
57+
58+
# *NOTE* Restrict to registries used in the tests older than 30
59+
# days to prevent thrashing in the case of async tests
60+
if (difftime.days > 30):
61+
client = manager.get_client(service_account_json)
62+
gateways = client.projects().locations().registries().devices(
63+
).list(
64+
parent=registry.get('name'),
65+
fieldMask='config,gatewayConfig'
66+
).execute().get('devices', [])
67+
devices = client.projects().locations().registries().devices(
68+
).list(parent=registry.get('name')).execute().get(
69+
'devices', [])
70+
71+
# Unbind devices from each gateway and delete
72+
for gateway in gateways:
73+
gateway_id = gateway.get('id')
74+
bound = client.projects().locations().registries().devices(
75+
).list(
76+
parent=registry.get('name'),
77+
gatewayListOptions_associationsGatewayId=gateway_id
78+
).execute()
79+
if 'devices' in bound:
80+
for device in bound['devices']:
81+
bind_request = {
82+
'deviceId': device.get('id'),
83+
'gatewayId': gateway_id
84+
}
85+
client.projects().locations().registries(
86+
).unbindDeviceFromGateway(
87+
parent=registry.get('name'),
88+
body=bind_request).execute()
89+
gateway_name = '{}/devices/{}'.format(
90+
registry.get('name'), gateway_id)
91+
client.projects().locations().registries().devices(
92+
).delete(name=gateway_name).execute()
93+
94+
# Delete the devices
95+
# Assumption is that the devices are not bound to gateways
96+
for device in devices:
97+
device_name = '{}/devices/{}'.format(
98+
registry.get('name'), device.get('id'))
99+
print(device_name)
100+
remove_device = True
101+
try:
102+
client.projects().locations().registries().devices(
103+
).get(name=device_name).execute()
104+
except Exception:
105+
remove_device = False
106+
107+
if remove_device:
108+
print('removing {}'.format(device_name))
109+
client.projects().locations().registries().devices(
110+
).delete(name=device_name).execute()
111+
112+
# Delete the old test registry
113+
client.projects().locations().registries().delete(
114+
name=registry.get('name')).execute()
115+
116+
43117
@pytest.fixture(scope='module')
44118
def test_topic():
45119
topic = manager.create_iot_topic(project_id, topic_id)

‎iot/api-client/mqtt_example/cloudiot_mqtt_example.py

Copy file name to clipboardExpand all lines: iot/api-client/mqtt_example/cloudiot_mqtt_example.py
+6-6Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -172,20 +172,20 @@ def get_client(
172172

173173
def detach_device(client, device_id):
174174
"""Detach the device from the gateway."""
175-
# [START detach_device]
175+
# [START iot_detach_device]
176176
detach_topic = '/devices/{}/detach'.format(device_id)
177177
print('Detaching: {}'.format(detach_topic))
178178
client.publish(detach_topic, '{}', qos=1)
179-
# [END detach_device]
179+
# [END iot_detach_device]
180180

181181

182182
def attach_device(client, device_id, auth):
183183
"""Attach the device to the gateway."""
184-
# [START attach_device]
184+
# [START iot_attach_device]
185185
attach_topic = '/devices/{}/attach'.format(device_id)
186186
attach_payload = '{{"authorization" : "{}"}}'.format(auth)
187187
client.publish(attach_topic, attach_payload, qos=1)
188-
# [END attach_device]
188+
# [END iot_attach_device]
189189

190190

191191
def listen_for_messages(
@@ -194,7 +194,7 @@ def listen_for_messages(
194194
mqtt_bridge_hostname, mqtt_bridge_port, jwt_expires_minutes, duration,
195195
cb=None):
196196
"""Listens for messages sent to the gateway and bound devices."""
197-
# [START listen_for_messages]
197+
# [START iot_listen_for_messages]
198198
global minimum_backoff_time
199199

200200
jwt_iat = datetime.datetime.utcnow()
@@ -252,7 +252,7 @@ def listen_for_messages(
252252
detach_device(client, device_id)
253253

254254
print('Finished.')
255-
# [END listen_for_messages]
255+
# [END iot_listen_for_messages]
256256

257257

258258
def send_data_from_bound_device(

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.