|
35 | 35 | import com.google.api.services.cloudiot.v1.model.GatewayConfig; |
36 | 36 | import com.google.api.services.cloudiot.v1.model.GetIamPolicyRequest; |
37 | 37 | import com.google.api.services.cloudiot.v1.model.ListDeviceStatesResponse; |
| 38 | +import com.google.api.services.cloudiot.v1.model.ListDevicesResponse; |
38 | 39 | import com.google.api.services.cloudiot.v1.model.ModifyCloudToDeviceConfigRequest; |
39 | 40 | import com.google.api.services.cloudiot.v1.model.PublicKeyCredential; |
40 | 41 | import com.google.api.services.cloudiot.v1.model.SendCommandToDeviceRequest; |
@@ -166,13 +167,128 @@ public static void deleteRegistry(String cloudRegion, String projectId, String r |
166 | 167 |
|
167 | 168 | final String registryPath = |
168 | 169 | String.format( |
169 | | - "projects/%s/locations/%s/registries/%s", projectId, cloudRegion, registryName); |
| 170 | + "projects/%s/locations/%s/registries/%s", projectId, cloudRegion, registryName); |
170 | 171 |
|
171 | 172 | System.out.println("Deleting: " + registryPath); |
172 | 173 | service.projects().locations().registries().delete(registryPath).execute(); |
173 | 174 | } |
174 | 175 | // [END iot_delete_registry] |
175 | 176 |
|
| 177 | + /** |
| 178 | + * clearRegistry |
| 179 | + * <ul> |
| 180 | + * <li>Registries can't be deleted if they contain devices,</li> |
| 181 | + * <li>Gateways (a type of device) can't be deleted if they have bound devices</li> |
| 182 | + * <li>Devices can't be deleted if bound to gateways...</li> |
| 183 | + * </ul> |
| 184 | + * To completely remove a registry, you must unbind all devices from gateways, |
| 185 | + * then remove all devices in a registry before removing the registry. |
| 186 | + * As pseudocode: |
| 187 | + * <code> |
| 188 | + * ForAll gateways |
| 189 | + * ForAll devicesBoundToGateway |
| 190 | + * unbindDeviceFromGateway |
| 191 | + * ForAll devices |
| 192 | + * Delete device by ID |
| 193 | + * Delete registry |
| 194 | + * </code> |
| 195 | + */ |
| 196 | + // [START iot_clear_registry] |
| 197 | + public static void clearRegistry(String cloudRegion, String projectId, String registryName) |
| 198 | + throws GeneralSecurityException, IOException { |
| 199 | + GoogleCredential credential = |
| 200 | + GoogleCredential.getApplicationDefault().createScoped(CloudIotScopes.all()); |
| 201 | + JsonFactory jsonFactory = JacksonFactory.getDefaultInstance(); |
| 202 | + HttpRequestInitializer init = new RetryHttpInitializerWrapper(credential); |
| 203 | + final CloudIot service = |
| 204 | + new CloudIot.Builder(GoogleNetHttpTransport.newTrustedTransport(), jsonFactory, init) |
| 205 | + .setApplicationName(APP_NAME) |
| 206 | + .build(); |
| 207 | + final String registryPath = |
| 208 | + String.format( |
| 209 | + "projects/%s/locations/%s/registries/%s", projectId, cloudRegion, registryName); |
| 210 | + |
| 211 | + ListDevicesResponse listGatewaysRes = |
| 212 | + service |
| 213 | + .projects() |
| 214 | + .locations() |
| 215 | + .registries() |
| 216 | + .devices() |
| 217 | + .list(registryPath) |
| 218 | + .setGatewayListOptionsGatewayType("GATEWAY") |
| 219 | + .execute(); |
| 220 | + List<Device> gateways = listGatewaysRes.getDevices(); |
| 221 | + |
| 222 | + // Unbind all devices from all gateways |
| 223 | + if (gateways != null) { |
| 224 | + System.out.println("Found " + gateways.size() + " devices"); |
| 225 | + for (Device g : gateways) { |
| 226 | + String gatewayId = g.getId(); |
| 227 | + System.out.println("Id: " + gatewayId); |
| 228 | + |
| 229 | + ListDevicesResponse res = |
| 230 | + service |
| 231 | + .projects() |
| 232 | + .locations() |
| 233 | + .registries() |
| 234 | + .devices() |
| 235 | + .list(registryPath) |
| 236 | + .setGatewayListOptionsAssociationsGatewayId(gatewayId) |
| 237 | + .execute(); |
| 238 | + List<Device> deviceNumIds = res.getDevices(); |
| 239 | + |
| 240 | + if (deviceNumIds != null) { |
| 241 | + System.out.println("Found " + deviceNumIds.size() + " devices"); |
| 242 | + for (Device device : deviceNumIds) { |
| 243 | + String deviceId = device.getId(); |
| 244 | + System.out.println(String.format("ID: %s", deviceId)); |
| 245 | + |
| 246 | + // Remove any bindings from the device |
| 247 | + UnbindDeviceFromGatewayRequest request = new UnbindDeviceFromGatewayRequest(); |
| 248 | + request.setDeviceId(deviceId); |
| 249 | + request.setGatewayId(gatewayId); |
| 250 | + UnbindDeviceFromGatewayResponse response = |
| 251 | + service |
| 252 | + .projects() |
| 253 | + .locations() |
| 254 | + .registries() |
| 255 | + .unbindDeviceFromGateway(registryPath, request) |
| 256 | + .execute(); |
| 257 | + } |
| 258 | + } else { |
| 259 | + System.out.println("Gateway has no bound devices."); |
| 260 | + } |
| 261 | + } |
| 262 | + } |
| 263 | + |
| 264 | + // Remove all devices from the regsitry |
| 265 | + List<Device> devices = |
| 266 | + service |
| 267 | + .projects() |
| 268 | + .locations() |
| 269 | + .registries() |
| 270 | + .devices() |
| 271 | + .list(registryPath) |
| 272 | + .execute() |
| 273 | + .getDevices(); |
| 274 | + |
| 275 | + if (devices != null) { |
| 276 | + System.out.println("Found " + devices.size() + " devices"); |
| 277 | + for (Device d : devices) { |
| 278 | + String deviceId = d.getId(); |
| 279 | + String devicePath = String.format( |
| 280 | + "%s/devices/%s", registryPath, deviceId); |
| 281 | + service.projects().locations().registries().devices().delete(devicePath).execute(); |
| 282 | + } |
| 283 | + } |
| 284 | + |
| 285 | + // Delete the registry |
| 286 | + service.projects().locations().registries().delete(registryPath).execute(); |
| 287 | + |
| 288 | + } |
| 289 | + // [END iot_clear_registry] |
| 290 | + |
| 291 | + |
176 | 292 | // [START iot_list_devices] |
177 | 293 | /** Print all of the devices in this registry to standard out. */ |
178 | 294 | public static void listDevices(String projectId, String cloudRegion, String registryName) |
@@ -1099,6 +1215,10 @@ public static void main(String[] args) throws Exception { |
1099 | 1215 | } |
1100 | 1216 |
|
1101 | 1217 | switch (options.command) { |
| 1218 | + case "clear-registry": |
| 1219 | + System.out.println("Clear registry"); |
| 1220 | + clearRegistry(options.cloudRegion, options.projectId, options.registryName); |
| 1221 | + break; |
1102 | 1222 | case "create-iot-topic": |
1103 | 1223 | System.out.println("Create IoT Topic:"); |
1104 | 1224 | createIotTopic(options.projectId, options.pubsubTopic); |
|
0 commit comments