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

Updates create / delete device methods to use Cloud client library #2420

Merged
merged 2 commits into from
Oct 7, 2019
Merged
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
116 changes: 62 additions & 54 deletions iot/api-client/manager/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,26 +87,25 @@ def create_rs256_device(
"""Create a new device with the given id, using RS256 for
authentication."""
# [START iot_create_rsa_device]
registry_name = 'projects/{}/locations/{}/registries/{}'.format(
project_id, cloud_region, registry_id)
client = iot_v1.DeviceManagerClient()

parent = client.registry_path(project_id, cloud_region, registry_id)

client = get_client(service_account_json)
with io.open(certificate_file) as f:
certificate = f.read()

# Note: You can have multiple credentials associated with a device.
device_template = {
'id': device_id,
'credentials': [{
'publicKey': {
'public_key': {
'format': 'RSA_X509_PEM',
'key': certificate
}
}]
}

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


Expand All @@ -116,26 +115,25 @@ def create_es256_device(
"""Create a new device with the given id, using ES256 for
authentication."""
# [START iot_create_es_device]
registry_name = 'projects/{}/locations/{}/registries/{}'.format(
project_id, cloud_region, registry_id)
client = iot_v1.DeviceManagerClient()

parent = client.registry_path(project_id, cloud_region, registry_id)

client = get_client(service_account_json)
with io.open(public_key_file) as f:
public_key = f.read()

# Note: You can have multiple credentials associated with a device.
device_template = {
'id': device_id,
'credentials': [{
'publicKey': {
'public_key': {
'format': 'ES256_PEM',
'key': public_key
}
}]
}

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


Expand Down Expand Up @@ -187,16 +185,15 @@ def create_unauth_device(
device_id):
"""Create a new device without authentication."""
# [START iot_create_unauth_device]
registry_name = 'projects/{}/locations/{}/registries/{}'.format(
project_id, cloud_region, registry_id)
client = iot_v1.DeviceManagerClient()

parent = client.registry_path(project_id, cloud_region, registry_id)

client = get_client(service_account_json)
device_template = {
'id': device_id,
}

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


Expand All @@ -206,14 +203,12 @@ def delete_device(
"""Delete the device with the given id."""
# [START iot_delete_device]
print('Delete device')
client = get_client(service_account_json)
registry_name = 'projects/{}/locations/{}/registries/{}'.format(
project_id, cloud_region, registry_id)
client = iot_v1.DeviceManagerClient()

device_name = '{}/devices/{}'.format(registry_name, device_id)
device_path = client.device_path(
project_id, cloud_region, registry_id, device_id)

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


Expand Down Expand Up @@ -401,26 +396,32 @@ def patch_es256_auth(
"""Patch the device to add an ES256 public key to the device."""
# [START iot_patch_es]
print('Patch device with ES256 certificate')
client = get_client(service_account_json)
registry_path = 'projects/{}/locations/{}/registries/{}'.format(
project_id, cloud_region, registry_id)

client = iot_v1.DeviceManagerClient()
device_path = client.device_path(
project_id, cloud_region, registry_id, device_id)

public_key_bytes = ''
with io.open(public_key_file) as f:
public_key = f.read()
public_key_bytes = f.read()

patch = {
'credentials': [{
'publicKey': {
'format': 'ES256_PEM',
'key': public_key
}
}]
}
key = iot_v1.types.PublicKeyCredential(
format='ES256_PEM',
key=public_key_bytes)

cred = iot_v1.types.DeviceCredential(public_key=key)
device = client.get_device(device_path)

device.id = b''
device.num_id = 0
device.credentials.append(cred)

device_name = '{}/devices/{}'.format(registry_path, device_id)
mask = iot_v1.types.FieldMask()
mask.paths.append('credentials')

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


Expand All @@ -430,26 +431,33 @@ def patch_rsa256_auth(
"""Patch the device to add an RSA256 public key to the device."""
# [START iot_patch_rsa]
print('Patch device with RSA256 certificate')
client = get_client(service_account_json)
registry_path = 'projects/{}/locations/{}/registries/{}'.format(
project_id, cloud_region, registry_id)

client = iot_v1.DeviceManagerClient()
device_path = client.device_path(
project_id, cloud_region, registry_id, device_id)

public_key_bytes = ''
with io.open(public_key_file) as f:
public_key = f.read()
public_key_bytes = f.read()

patch = {
'credentials': [{
'publicKey': {
'format': 'RSA_X509_PEM',
'key': public_key
}
}]
}
key = iot_v1.types.PublicKeyCredential(
format='RSA_X509_PEM',
key=public_key_bytes)

cred = iot_v1.types.DeviceCredential(public_key=key)
device = client.get_device(device_path)

device.id = b''
device.num_id = 0
device.credentials.append(cred)

mask = iot_v1.types.FieldMask()
mask.paths.append('credentials')

device_name = '{}/devices/{}'.format(registry_path, device_id)
return client.update_device(
device=device,
update_mask=mask)

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


Expand Down Expand Up @@ -604,7 +612,7 @@ def create_gateway(
device_template = {
'id': gateway_id,
'credentials': [{
'publicKey': {
'public_key': {
'format': certificate_format,
'key': certificate
}
Expand Down