|
| 1 | +/** |
| 2 | + * Copyright 2019 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +// [START apps_script_iot_list_registries] |
| 17 | +/** |
| 18 | + * Lists the registries for the configured project and region. |
| 19 | + */ |
| 20 | +function listRegistries() { |
| 21 | + Logger.log(response); |
| 22 | + var projectId = 'your-project-id'; |
| 23 | + var cloudRegion = 'us-central1'; |
| 24 | + var parent = 'projects/' + projectId + '/locations/' + cloudRegion; |
| 25 | + |
| 26 | + var response = CloudIoT.Projects.Locations.Registries.list(parent); |
| 27 | + if (response.deviceRegistries){ |
| 28 | + response.deviceRegistries.forEach( |
| 29 | + function(registry){ |
| 30 | + Logger.log(registry.id); |
| 31 | + }); |
| 32 | + } |
| 33 | +} |
| 34 | +// [END apps_script_iot_list_registries] |
| 35 | + |
| 36 | +// [START apps_script_iot_create_registry] |
| 37 | +/** |
| 38 | + * Creates a registry. |
| 39 | + */ |
| 40 | +function createRegistry() { |
| 41 | + var cloudRegion = 'us-central1'; |
| 42 | + var name = 'your-registry-name'; |
| 43 | + var projectId = 'your-project-id'; |
| 44 | + var topic = 'your-pubsub-topic'; |
| 45 | + |
| 46 | + var pubsubTopic = 'projects/' + projectId + '/topics/' + topic; |
| 47 | + |
| 48 | + var registry = { |
| 49 | + eventNotificationConfigs: [{ |
| 50 | + // From - https://console.cloud.google.com/cloudpubsub |
| 51 | + pubsubTopicName : pubsubTopic |
| 52 | + }], |
| 53 | + 'id': name |
| 54 | + }; |
| 55 | + var parent = 'projects/' + projectId + '/locations/' + cloudRegion; |
| 56 | + |
| 57 | + var response = CloudIoT.Projects.Locations.Registries.create(registry, parent) |
| 58 | + Logger.log('Created registry: ' + response.id); |
| 59 | +} |
| 60 | +// [END apps_script_iot_create_registry] |
| 61 | + |
| 62 | +// [START apps_script_iot_get_registry] |
| 63 | +/** |
| 64 | + * Describes a registry. |
| 65 | + */ |
| 66 | +function getRegistry() { |
| 67 | + var cloudRegion = 'us-central1'; |
| 68 | + var name = 'your-registry-name'; |
| 69 | + var projectId = 'your-project-id'; |
| 70 | + |
| 71 | + var parent = 'projects/' + projectId + '/locations/' + cloudRegion; |
| 72 | + var registryName = parent + '/registries/' + name; |
| 73 | + |
| 74 | + var response = CloudIoT.Projects.Locations.Registries.get(registryName) |
| 75 | + Logger.log('Retrieved registry: ' + response.id); |
| 76 | +} |
| 77 | +// [END apps_script_iot_get_registry] |
| 78 | + |
| 79 | +// [START apps_script_iot_delete_registry] |
| 80 | +/** |
| 81 | + * Deletes a registry. |
| 82 | + */ |
| 83 | +function deleteRegistry() { |
| 84 | + var cloudRegion = 'us-central1'; |
| 85 | + var name = 'your-registry-name'; |
| 86 | + var projectId = 'your-project-id'; |
| 87 | + |
| 88 | + var parent = 'projects/' + projectId + '/locations/' + cloudRegion; |
| 89 | + var registryName = parent + '/registries/' + name; |
| 90 | + |
| 91 | + var response = CloudIoT.Projects.Locations.Registries.remove(registryName) |
| 92 | + // Successfully removed registry if exception was not thrown. |
| 93 | + Logger.log('Deleted registry: ' + name); |
| 94 | +} |
| 95 | +// [END apps_script_iot_delete_registry] |
| 96 | + |
| 97 | +// [START apps_script_iot_list_devices] |
| 98 | +/** |
| 99 | + * Lists the devices in the given registry. |
| 100 | + */ |
| 101 | +function listDevicesForRegistry() { |
| 102 | + var cloudRegion = 'us-central1'; |
| 103 | + var name = 'your-registry-name'; |
| 104 | + var projectId = 'your-project-id'; |
| 105 | + |
| 106 | + var parent = 'projects/' + projectId + '/locations/' + cloudRegion; |
| 107 | + var registryName = parent + '/registries/' + name; |
| 108 | + |
| 109 | + var response = CloudIoT.Projects.Locations.Registries.Devices.list(registryName); |
| 110 | + |
| 111 | + Logger.log('Registry contains the following devices: '); |
| 112 | + if (response.devices) { |
| 113 | + response.devices.forEach( |
| 114 | + function(device){ |
| 115 | + Logger.log('\t' + device.id); |
| 116 | + }); |
| 117 | + } |
| 118 | +} |
| 119 | +// [END apps_script_iot_list_devices] |
| 120 | + |
| 121 | +// [START apps_script_iot_create_unauth_device] |
| 122 | +/** |
| 123 | + * Creates a device without credentials. |
| 124 | + */ |
| 125 | +function createDevice() { |
| 126 | + var cloudRegion = 'us-central1'; |
| 127 | + var name = 'your-device-name'; |
| 128 | + var projectId = 'your-project-id'; |
| 129 | + var registry = 'your-registry-name'; |
| 130 | + |
| 131 | + Logger.log('Creating device: ' + name + ' in Registry: ' + registry); |
| 132 | + var parent = 'projects/' + projectId + '/locations/' + cloudRegion + '/registries/' + registry; |
| 133 | + |
| 134 | + var device = { |
| 135 | + id: name, |
| 136 | + gatewayConfig: { |
| 137 | + gatewayType: 'NON_GATEWAY', |
| 138 | + gatewayAuthMethod: 'ASSOCIATION_ONLY' |
| 139 | + } |
| 140 | + }; |
| 141 | + |
| 142 | + var response = CloudIoT.Projects.Locations.Registries.Devices.create(device, parent) |
| 143 | + Logger.log('Created device:' + response.name); |
| 144 | +} |
| 145 | +// [END apps_script_iot_create_unauth_device] |
| 146 | + |
| 147 | +// [START apps_script_iot_create_rsa_device] |
| 148 | +/** |
| 149 | + * Creates a device with RSA credentials. |
| 150 | + */ |
| 151 | +function createRsaDevice() { |
| 152 | + // Create the RSA public/private keypair with the following OpenSSL command: |
| 153 | + // openssl req -x509 -newkey rsa:2048 -days 3650 -keyout rsa_private.pem \ |
| 154 | + // -nodes -out rsa_cert.pem -subj "/CN=unused" |
| 155 | + // |
| 156 | + // **NOTE** Be sure to insert the newline charaters in the string varant. |
| 157 | + var cert = |
| 158 | + '-----BEGIN CERTIFICATE-----\n' + |
| 159 | + 'your-PUBLIC-certificate-b64-bytes\n' + |
| 160 | + '...\n' + |
| 161 | + 'more-PUBLIC-certificate-b64-bytes==\n' + |
| 162 | + '-----END CERTIFICATE-----\n'; |
| 163 | + |
| 164 | + var cloudRegion = 'us-central1'; |
| 165 | + var name = 'your-device-name'; |
| 166 | + var projectId = 'your-project-id'; |
| 167 | + var registry = 'your-registry-name'; |
| 168 | + |
| 169 | + var parent = 'projects/' + projectId + '/locations/' + cloudRegion + '/registries/' + registry; |
| 170 | + var device = { |
| 171 | + id: name, |
| 172 | + gatewayConfig: { |
| 173 | + gatewayType: 'NON_GATEWAY', |
| 174 | + gatewayAuthMethod: 'ASSOCIATION_ONLY' |
| 175 | + }, |
| 176 | + credentials: [{ |
| 177 | + publicKey: { |
| 178 | + format: 'RSA_X509_PEM', |
| 179 | + key: cert |
| 180 | + } |
| 181 | + }], |
| 182 | + }; |
| 183 | + |
| 184 | + var response = CloudIoT.Projects.Locations.Registries.Devices.create(device, parent); |
| 185 | + Logger.log('Created device:' + response.name); |
| 186 | +} |
| 187 | +// [END apps_script_iot_create_rsa_device] |
| 188 | + |
| 189 | +// [START apps_script_iot_delete_device] |
| 190 | +/** |
| 191 | + * Deletes a device from the given registry. |
| 192 | + */ |
| 193 | +function deleteDevice() { |
| 194 | + var cloudRegion = 'us-central1'; |
| 195 | + var name = 'your-device-name'; |
| 196 | + var projectId = 'your-project-id'; |
| 197 | + var registry = 'your-registry-name'; |
| 198 | + |
| 199 | + var parent = 'projects/' + projectId + '/locations/' + cloudRegion + '/registries/' + registry; |
| 200 | + var deviceName = parent + '/devices/' + name; |
| 201 | + |
| 202 | + var response = CloudIoT.Projects.Locations.Registries.Devices.remove(deviceName) |
| 203 | + // If no exception thrown, device was successfully removed |
| 204 | + Logger.log('Successfully deleted device: ' + deviceName); |
| 205 | +} |
| 206 | +// [END apps_script_iot_delete_device] |
| 207 | + |
0 commit comments