|
| 1 | +/* |
| 2 | + * Copyright 2017 Google Inc. All Rights Reserved. |
| 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.google.cloud.compute.testing; |
| 18 | + |
| 19 | +import com.google.cloud.compute.AddressId; |
| 20 | +import com.google.cloud.compute.Compute; |
| 21 | +import com.google.cloud.compute.DiskId; |
| 22 | +import com.google.cloud.compute.ImageId; |
| 23 | +import com.google.cloud.compute.InstanceId; |
| 24 | +import com.google.cloud.compute.NetworkId; |
| 25 | +import com.google.cloud.compute.SnapshotId; |
| 26 | +import com.google.cloud.compute.SubnetworkId; |
| 27 | +import com.google.common.base.Function; |
| 28 | +import java.util.HashMap; |
| 29 | +import java.util.Map; |
| 30 | +import java.util.NoSuchElementException; |
| 31 | + |
| 32 | +public class ResourceCleaner { |
| 33 | + private final Compute compute; |
| 34 | + private final Map<Object, ManagedResource<?>> resources; |
| 35 | + |
| 36 | + private ResourceCleaner(Compute compute) { |
| 37 | + this.compute = compute; |
| 38 | + this.resources = new HashMap<>(); |
| 39 | + } |
| 40 | + |
| 41 | + public static ResourceCleaner create(Compute compute) { |
| 42 | + return new ResourceCleaner(compute); |
| 43 | + } |
| 44 | + |
| 45 | + private class ManagedResource<T> { |
| 46 | + final T resourceId; |
| 47 | + Function<T, Void> deleteFunc; |
| 48 | + |
| 49 | + private ManagedResource(T resourceId, Function<T, Void> deleteFunc) { |
| 50 | + this.resourceId = resourceId; |
| 51 | + this.deleteFunc = deleteFunc; |
| 52 | + } |
| 53 | + |
| 54 | + public void delete() { |
| 55 | + deleteFunc.apply(resourceId); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + public void cleanUp() { |
| 60 | + for (Map.Entry<Object, ManagedResource<?>> r : resources.entrySet()) { |
| 61 | + r.getValue().delete(); |
| 62 | + } |
| 63 | + resources.clear(); |
| 64 | + } |
| 65 | + |
| 66 | + public void add(AddressId addressId) { |
| 67 | + ManagedResource<AddressId> r = |
| 68 | + new ManagedResource<>( |
| 69 | + addressId, |
| 70 | + new Function<AddressId, Void>() { |
| 71 | + @Override |
| 72 | + public Void apply(AddressId addressId) { |
| 73 | + compute.deleteAddress(addressId); |
| 74 | + return null; |
| 75 | + } |
| 76 | + }); |
| 77 | + resources.put(addressId, r); |
| 78 | + } |
| 79 | + |
| 80 | + public void add(DiskId diskId) { |
| 81 | + ManagedResource<DiskId> r = |
| 82 | + new ManagedResource<>( |
| 83 | + diskId, |
| 84 | + new Function<DiskId, Void>() { |
| 85 | + @Override |
| 86 | + public Void apply(DiskId diskId) { |
| 87 | + compute.deleteDisk(diskId); |
| 88 | + return null; |
| 89 | + } |
| 90 | + }); |
| 91 | + resources.put(diskId, r); |
| 92 | + } |
| 93 | + |
| 94 | + public void add(SnapshotId snapshotId) { |
| 95 | + ManagedResource<SnapshotId> r = |
| 96 | + new ManagedResource<>( |
| 97 | + snapshotId, |
| 98 | + new Function<SnapshotId, Void>() { |
| 99 | + @Override |
| 100 | + public Void apply(SnapshotId snapshotId) { |
| 101 | + compute.deleteSnapshot(snapshotId); |
| 102 | + return null; |
| 103 | + } |
| 104 | + }); |
| 105 | + resources.put(snapshotId, r); |
| 106 | + } |
| 107 | + |
| 108 | + public void add(NetworkId networkId) { |
| 109 | + ManagedResource<NetworkId> r = |
| 110 | + new ManagedResource<>( |
| 111 | + networkId, |
| 112 | + new Function<NetworkId, Void>() { |
| 113 | + @Override |
| 114 | + public Void apply(NetworkId networkId) { |
| 115 | + compute.deleteNetwork(networkId); |
| 116 | + return null; |
| 117 | + } |
| 118 | + }); |
| 119 | + resources.put(networkId, r); |
| 120 | + } |
| 121 | + |
| 122 | + public void add(SubnetworkId subnetworkId) { |
| 123 | + ManagedResource<SubnetworkId> r = |
| 124 | + new ManagedResource<>( |
| 125 | + subnetworkId, |
| 126 | + new Function<SubnetworkId, Void>() { |
| 127 | + @Override |
| 128 | + public Void apply(SubnetworkId subnetworkId) { |
| 129 | + compute.deleteSubnetwork(subnetworkId); |
| 130 | + return null; |
| 131 | + } |
| 132 | + }); |
| 133 | + resources.put(subnetworkId, r); |
| 134 | + } |
| 135 | + |
| 136 | + public void add(ImageId imageId) { |
| 137 | + ManagedResource<ImageId> r = |
| 138 | + new ManagedResource<>( |
| 139 | + imageId, |
| 140 | + new Function<ImageId, Void>() { |
| 141 | + @Override |
| 142 | + public Void apply(ImageId imageId) { |
| 143 | + compute.deleteImage(imageId); |
| 144 | + return null; |
| 145 | + } |
| 146 | + }); |
| 147 | + resources.put(imageId, r); |
| 148 | + } |
| 149 | + |
| 150 | + public void add(InstanceId instanceId) { |
| 151 | + ManagedResource<InstanceId> r = |
| 152 | + new ManagedResource<>( |
| 153 | + instanceId, |
| 154 | + new Function<InstanceId, Void>() { |
| 155 | + @Override |
| 156 | + public Void apply(InstanceId instanceId) { |
| 157 | + compute.deleteInstance(instanceId); |
| 158 | + return null; |
| 159 | + } |
| 160 | + }); |
| 161 | + resources.put(instanceId, r); |
| 162 | + } |
| 163 | + |
| 164 | + public ResourceCleaner remove(Object resourceId) { |
| 165 | + if (!resources.containsKey(resourceId)) { |
| 166 | + throw new NoSuchElementException(resourceId + " has not been added to managed resources"); |
| 167 | + } |
| 168 | + resources.remove(resourceId); |
| 169 | + return this; |
| 170 | + } |
| 171 | +} |
0 commit comments