|
| 1 | +/* |
| 2 | + * Copyright 2020 Google Inc. |
| 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 | + * http://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 | + |
| 17 | +package com.example.cloud.iot.endtoend; |
| 18 | + |
| 19 | +import static com.example.cloud.iot.endtoend.CloudiotPubsubExampleServer.APP_NAME; |
| 20 | + |
| 21 | +import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; |
| 22 | +import com.google.api.client.http.HttpRequestInitializer; |
| 23 | +import com.google.api.client.json.JsonFactory; |
| 24 | +import com.google.api.client.json.jackson2.JacksonFactory; |
| 25 | +import com.google.api.services.cloudiot.v1.CloudIot; |
| 26 | +import com.google.api.services.cloudiot.v1.CloudIotScopes; |
| 27 | +import com.google.api.services.cloudiot.v1.model.Device; |
| 28 | +import com.google.api.services.cloudiot.v1.model.DeviceRegistry; |
| 29 | +import com.google.auth.http.HttpCredentialsAdapter; |
| 30 | +import com.google.auth.oauth2.GoogleCredentials; |
| 31 | +import java.io.IOException; |
| 32 | +import java.security.GeneralSecurityException; |
| 33 | +import java.util.List; |
| 34 | + |
| 35 | +public class CleanUpHelper { |
| 36 | + protected static List<DeviceRegistry> getRegisteries(String project, String cloudRegion) |
| 37 | + throws IOException, GeneralSecurityException { |
| 38 | + GoogleCredentials credential = |
| 39 | + GoogleCredentials.getApplicationDefault().createScoped(CloudIotScopes.all()); |
| 40 | + JsonFactory jsonFactory = JacksonFactory.getDefaultInstance(); |
| 41 | + HttpRequestInitializer init = new HttpCredentialsAdapter(credential); |
| 42 | + final CloudIot service = |
| 43 | + new CloudIot.Builder(GoogleNetHttpTransport.newTrustedTransport(), jsonFactory, init) |
| 44 | + .setApplicationName(APP_NAME) |
| 45 | + .build(); |
| 46 | + final String projectPath = String.format("projects/%s/locations/%s", project, cloudRegion); |
| 47 | + |
| 48 | + List<DeviceRegistry> registries = |
| 49 | + service |
| 50 | + .projects() |
| 51 | + .locations() |
| 52 | + .registries() |
| 53 | + .list(projectPath) |
| 54 | + .execute() |
| 55 | + .getDeviceRegistries(); |
| 56 | + return registries; |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * clearRegistry |
| 61 | + * |
| 62 | + * <ul> |
| 63 | + * <li>Registries can't be deleted if they contain devices, |
| 64 | + * <li>Gateways (a type of device) can't be deleted if they have bound devices |
| 65 | + * <li>Devices can't be deleted if bound to gateways... |
| 66 | + * </ul> |
| 67 | + * |
| 68 | + * <p>To completely remove a registry, you must unbind all devices from gateways, then remove all |
| 69 | + * devices in a registry before removing the registry. As pseudocode: <code> |
| 70 | + * ForAll gateways |
| 71 | + * ForAll devicesBoundToGateway |
| 72 | + * unbindDeviceFromGateway |
| 73 | + * ForAll devices |
| 74 | + * Delete device by ID |
| 75 | + * Delete registry |
| 76 | + * </code> |
| 77 | + */ |
| 78 | + protected static void clearRegistry(String cloudRegion, String projectId, String registryName) |
| 79 | + throws GeneralSecurityException, IOException { |
| 80 | + GoogleCredentials credential = |
| 81 | + GoogleCredentials.getApplicationDefault().createScoped(CloudIotScopes.all()); |
| 82 | + JsonFactory jsonFactory = JacksonFactory.getDefaultInstance(); |
| 83 | + HttpRequestInitializer init = new HttpCredentialsAdapter(credential); |
| 84 | + final CloudIot service = |
| 85 | + new CloudIot.Builder(GoogleNetHttpTransport.newTrustedTransport(), jsonFactory, init) |
| 86 | + .setApplicationName(APP_NAME) |
| 87 | + .build(); |
| 88 | + final String registryPath = |
| 89 | + String.format( |
| 90 | + "projects/%s/locations/%s/registries/%s", projectId, cloudRegion, registryName); |
| 91 | + |
| 92 | + CloudIot.Projects.Locations.Registries regAlias = service.projects().locations().registries(); |
| 93 | + CloudIot.Projects.Locations.Registries.Devices devAlias = regAlias.devices(); |
| 94 | + |
| 95 | + // Remove all devices from the regsitry |
| 96 | + List<Device> devices = devAlias.list(registryPath).execute().getDevices(); |
| 97 | + |
| 98 | + if (devices != null) { |
| 99 | + System.out.println("Found " + devices.size() + " devices"); |
| 100 | + for (Device d : devices) { |
| 101 | + String deviceId = d.getId(); |
| 102 | + String devicePath = String.format("%s/devices/%s", registryPath, deviceId); |
| 103 | + service.projects().locations().registries().devices().delete(devicePath).execute(); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + // Delete the registry |
| 108 | + service.projects().locations().registries().delete(registryPath).execute(); |
| 109 | + } |
| 110 | +} |
0 commit comments