Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup Cloud IoT region tags. #1374

Merged
merged 1 commit into from
Feb 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions iot/api-client/http_example/cloudiot_http_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
README in the parent folder.
"""

# [START iot_http_includes]
import argparse
import base64
import datetime
Expand All @@ -30,11 +31,13 @@
from google.api_core import retry
import jwt
import requests
# [END iot_http_includes]

_BASE_URL = 'https://cloudiotdevice.googleapis.com/v1'
_BACKOFF_DURATION = 60


# [START iot_http_jwt]
def create_jwt(project_id, private_key_file, algorithm):
token = {
# The time the token was issued.
Expand All @@ -53,11 +56,13 @@ def create_jwt(project_id, private_key_file, algorithm):
algorithm, private_key_file))

return jwt.encode(token, private_key, algorithm=algorithm).decode('ascii')
# [END iot_http_jwt]


@retry.Retry(
predicate=retry.if_exception_type(AssertionError),
deadline=_BACKOFF_DURATION)
# [START iot_http_publish]
def publish_message(
message, message_type, base_url, project_id, cloud_region, registry_id,
device_id, jwt_token):
Expand Down Expand Up @@ -92,6 +97,7 @@ def publish_message(
raise AssertionError('Not OK response: {}'.format(resp.status_code))

return resp
# [END iot_http_publish]


@retry.Retry(
Expand Down Expand Up @@ -172,6 +178,7 @@ def parse_command_line_args():
return parser.parse_args()


# [START iot_http_run]
def main():
args = parse_command_line_args()

Expand Down Expand Up @@ -208,6 +215,7 @@ def main():
# Send events every second. State should not be updated as often
time.sleep(1 if args.message_type == 'event' else 5)
print('Finished.')
# [END iot_http_run]


if __name__ == '__main__':
Expand Down
34 changes: 34 additions & 0 deletions iot/api-client/manager/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ def get_client(service_account_json):
credentials=scoped_credentials)


# [START iot_create_rsa_device]
def create_rs256_device(
service_account_json, project_id, cloud_region, registry_id, device_id,
certificate_file):
Expand All @@ -107,8 +108,10 @@ def create_rs256_device(

devices = client.projects().locations().registries().devices()
return devices.create(parent=registry_name, body=device_template).execute()
# [END iot_create_rsa_device]


# [START iot_create_es_device]
def create_es256_device(
service_account_json, project_id, cloud_region, registry_id,
device_id, public_key_file):
Expand All @@ -134,8 +137,10 @@ def create_es256_device(

devices = client.projects().locations().registries().devices()
return devices.create(parent=registry_name, body=device_template).execute()
# [END iot_create_es_device]


# [START iot_create_unauth_device]
def create_unauth_device(
service_account_json, project_id, cloud_region, registry_id,
device_id):
Expand All @@ -150,8 +155,10 @@ def create_unauth_device(

devices = client.projects().locations().registries().devices()
return devices.create(parent=registry_name, body=device_template).execute()
# [END iot_create_unauth_device]


# [START iot_delete_device]
def delete_device(
service_account_json, project_id, cloud_region, registry_id,
device_id):
Expand All @@ -165,8 +172,10 @@ def delete_device(

devices = client.projects().locations().registries().devices()
return devices.delete(name=device_name).execute()
# [END iot_delete_device]


# [START iot_delete_registry]
def delete_registry(
service_account_json, project_id, cloud_region, registry_id):
"""Deletes the specified registry."""
Expand All @@ -177,8 +186,10 @@ def delete_registry(

registries = client.projects().locations().registries()
return registries.delete(name=registry_name).execute()
# [END iot_delete_registry]


# [START iot_get_device]
def get_device(
service_account_json, project_id, cloud_region, registry_id,
device_id):
Expand Down Expand Up @@ -209,8 +220,10 @@ def get_device(
'cloudUpdateTime')))

return device
# [END iot_get_device]


# [START iot_get_device_state]
def get_state(
service_account_json, project_id, cloud_region, registry_id,
device_id):
Expand All @@ -226,8 +239,10 @@ def get_state(
print('State: {}\n'.format(state))

return state
# [END iot_get_device_state]


# [START iot_list_devices]
def list_devices(
service_account_json, project_id, cloud_region, registry_id):
"""List all devices in the registry."""
Expand All @@ -244,8 +259,10 @@ def list_devices(
device.get('id')))

return devices
# [END iot_list_devices]


# [START iot_list_registries]
def list_registries(service_account_json, project_id, cloud_region):
"""List all registries in the project."""
print('Listing Registries')
Expand All @@ -261,8 +278,10 @@ def list_registries(service_account_json, project_id, cloud_region):
registry.get('name')))

return registries
# [END iot_list_devices]


# [START iot_create_registry]
def create_registry(
service_account_json, project_id, cloud_region, pubsub_topic,
registry_id):
Expand All @@ -288,8 +307,10 @@ def create_registry(
except HttpError:
print('Error, registry not created')
return ""
# [END iot_create_registry]


# [START iot_get_registry]
def get_registry(
service_account_json, project_id, cloud_region, registry_id):
""" Retrieves a device registry."""
Expand All @@ -300,6 +321,7 @@ def get_registry(
topic_name = '{}/registries/{}'.format(registry_parent, registry_id)
request = client.projects().locations().registries().get(name=topic_name)
return request.execute()
# [END iot_get_registry]


def open_registry(
Expand All @@ -325,6 +347,7 @@ def open_registry(
print(response)


# [START iot_patch_es]
def patch_es256_auth(
service_account_json, project_id, cloud_region, registry_id,
device_id, public_key_file):
Expand All @@ -350,8 +373,10 @@ def patch_es256_auth(

return client.projects().locations().registries().devices().patch(
name=device_name, updateMask='credentials', body=patch).execute()
# [END iot_patch_es]


# [START iot_patch_rsa]
def patch_rsa256_auth(
service_account_json, project_id, cloud_region, registry_id, device_id,
public_key_file):
Expand All @@ -377,8 +402,10 @@ def patch_rsa256_auth(

return client.projects().locations().registries().devices().patch(
name=device_name, updateMask='credentials', body=patch).execute()
# [END iot_patch_rsa]


# [START iot_set_device_config]
def set_config(
service_account_json, project_id, cloud_region, registry_id, device_id,
version, config):
Expand All @@ -397,8 +424,10 @@ def set_config(
).locations().registries(
).devices().modifyCloudToDeviceConfig(
name=device_path, body=config_body).execute()
# [END iot_set_device_config]


# [START iot_get_device_configs]
def get_config_versions(
service_account_json, project_id, cloud_region, registry_id,
device_id):
Expand All @@ -420,8 +449,10 @@ def get_config_versions(
config.get('binaryData')))

return configs
# [END iot_get_device_configs]


# [START iot_get_iam_policy]
def get_iam_permissions(
service_account_json, project_id, cloud_region, registry_id):
"""Retrieves IAM permissions for the given registry."""
Expand All @@ -433,8 +464,10 @@ def get_iam_permissions(
resource=registry_path, body={}).execute()

return policy
# [END iot_get_iam_policy]


# [START iot_set_iam_policy]
def set_iam_permissions(
service_account_json, project_id, cloud_region, registry_id, role,
member):
Expand All @@ -456,6 +489,7 @@ def set_iam_permissions(

return client.projects().locations().registries().setIamPolicy(
resource=registry_path, body=body).execute()
# [END iot_set_iam_policy]


def parse_command_line_args():
Expand Down
3 changes: 2 additions & 1 deletion iot/api-client/mqtt_example/cloudiot_mqtt_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
for this sample.
"""

# [START iot_mqtt_includes]
import argparse
import datetime
import os
Expand All @@ -30,7 +31,7 @@

import jwt
import paho.mqtt.client as mqtt

# [END iot_mqtt_includes]

# The initial backoff time after a disconnection occurs, in seconds.
minimum_backoff_time = 1
Expand Down