-
Notifications
You must be signed in to change notification settings - Fork 1.1k
adding functions to manage life cycle of resources in ITComputeTest #1768
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e127447
WIP: adding functions to ITComputeTest, in order to make sure resources
neozwu 40b1cd4
refactor and incorporate feedbacks
neozwu c047cd0
fix copyright header
neozwu f04997e
rename class name
neozwu c05b45b
modify remove function, pass delete function to each add method
neozwu 8cf9f8a
address comments
neozwu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
210 changes: 210 additions & 0 deletions
210
...-cloud-compute/src/main/java/com/google/cloud/compute/testing/ManagedResourceCleaner.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,210 @@ | ||
| /* | ||
| * Copyright 2017 Google Inc. All Rights Reserved. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.google.cloud.compute.testing; | ||
|
|
||
| import com.google.cloud.compute.AddressId; | ||
| import com.google.cloud.compute.Compute; | ||
| import com.google.cloud.compute.DiskId; | ||
| import com.google.cloud.compute.ImageId; | ||
| import com.google.cloud.compute.InstanceId; | ||
| import com.google.cloud.compute.NetworkId; | ||
| import com.google.cloud.compute.SnapshotId; | ||
| import com.google.cloud.compute.SubnetworkId; | ||
| import com.google.common.base.Function; | ||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
| import sun.reflect.generics.reflectiveObjects.NotImplementedException; | ||
|
|
||
| public class ManagedResourceCleaner { | ||
| private final Compute compute; | ||
| private final Map<Object, ManagedResource<?>> resources; | ||
|
|
||
| private ManagedResourceCleaner(Compute compute) { | ||
| this.compute = compute; | ||
| this.resources = new HashMap<>(); | ||
| } | ||
|
|
||
| public static ManagedResourceCleaner create(Compute compute) { | ||
| return new ManagedResourceCleaner(compute); | ||
| } | ||
|
|
||
| private class ManagedResource<T> { | ||
| final T res; | ||
| Function<T, Void> func; | ||
|
|
||
| private ManagedResource(T res) { | ||
| this.res = res; | ||
|
|
||
| @SuppressWarnings("unchecked") | ||
| Function<T, Void> localFunc = (Function<T, Void>) getDeleteMethod(res); | ||
|
|
||
| this.func = localFunc; | ||
| } | ||
|
|
||
| public void delete() { | ||
| func.apply(res); | ||
| } | ||
| } | ||
|
|
||
| public void cleanUp() { | ||
| for (Map.Entry<Object, ManagedResource<?>> r : resources.entrySet()) { | ||
| System.out.println("Clean up compute engine resource: " + r.getKey().toString()); | ||
| r.getValue().delete(); | ||
| } | ||
| resources.clear(); | ||
| } | ||
|
|
||
| public ManagedResourceCleaner add(AddressId addressId) { | ||
| ManagedResource<AddressId> r = new ManagedResource<>(addressId); | ||
| resources.put(addressId, r); | ||
| return this; | ||
| } | ||
|
|
||
| public ManagedResourceCleaner remove(AddressId addressId) { | ||
| resources.remove(addressId); | ||
| return this; | ||
| } | ||
|
|
||
| public ManagedResourceCleaner add(DiskId diskId) { | ||
| ManagedResource<DiskId> r = new ManagedResource<>(diskId); | ||
| resources.put(diskId, r); | ||
| return this; | ||
| } | ||
|
|
||
| public ManagedResourceCleaner remove(DiskId diskId) { | ||
| resources.remove(diskId); | ||
| return this; | ||
| } | ||
|
|
||
| public ManagedResourceCleaner add(SnapshotId snapshotId) { | ||
| ManagedResource<SnapshotId> r = new ManagedResource<>(snapshotId); | ||
| resources.put(snapshotId, r); | ||
| return this; | ||
| } | ||
|
|
||
| public ManagedResourceCleaner remove(SnapshotId snapshotId) { | ||
| resources.remove(snapshotId); | ||
| return this; | ||
| } | ||
|
|
||
| public ManagedResourceCleaner add(NetworkId networkId) { | ||
| ManagedResource<NetworkId> r = new ManagedResource<>(networkId); | ||
| resources.put(networkId, r); | ||
| return this; | ||
| } | ||
|
|
||
| public ManagedResourceCleaner remove(NetworkId networkId) { | ||
| resources.remove(networkId); | ||
| return this; | ||
| } | ||
|
|
||
| public ManagedResourceCleaner add(SubnetworkId subnetworkId) { | ||
| ManagedResource<SubnetworkId> r = new ManagedResource<>(subnetworkId); | ||
| resources.put(subnetworkId, r); | ||
| return this; | ||
| } | ||
|
|
||
| public ManagedResourceCleaner remove(SubnetworkId subnetworkId) { | ||
| resources.remove(subnetworkId); | ||
| return this; | ||
| } | ||
|
|
||
| public ManagedResourceCleaner add(ImageId imageId) { | ||
| ManagedResource<ImageId> r = new ManagedResource<>(imageId); | ||
| resources.put(imageId, r); | ||
| return this; | ||
| } | ||
|
|
||
| public ManagedResourceCleaner remove(ImageId imageId) { | ||
| resources.remove(imageId); | ||
| return this; | ||
| } | ||
|
|
||
| public ManagedResourceCleaner add(InstanceId instanceId) { | ||
| ManagedResource<InstanceId> r = new ManagedResource<>(instanceId); | ||
| resources.put(instanceId, r); | ||
| return this; | ||
| } | ||
|
|
||
| public ManagedResourceCleaner remove(InstanceId instanceId) { | ||
| resources.remove(instanceId); | ||
| return this; | ||
| } | ||
|
|
||
| private <T> Function<?, Void> getDeleteMethod(T res) { | ||
|
|
||
| if (res instanceof AddressId) { | ||
| return new Function<AddressId, Void>() { | ||
| @Override | ||
| public Void apply(AddressId addressId) { | ||
| compute.deleteAddress(addressId); | ||
| return null; | ||
| } | ||
| }; | ||
|
||
| } else if (res instanceof DiskId) { | ||
| return new Function<DiskId, Void>() { | ||
| @Override | ||
| public Void apply(DiskId diskId) { | ||
| compute.deleteDisk(diskId); | ||
| return null; | ||
| } | ||
| }; | ||
| } else if (res instanceof SnapshotId) { | ||
| return new Function<SnapshotId, Void>() { | ||
| @Override | ||
| public Void apply(SnapshotId snapshotId) { | ||
| compute.deleteSnapshot(snapshotId); | ||
| return null; | ||
| } | ||
| }; | ||
| } else if (res instanceof NetworkId) { | ||
| return new Function<NetworkId, Void>() { | ||
| @Override | ||
| public Void apply(NetworkId networkId) { | ||
| compute.deleteNetwork(networkId); | ||
| return null; | ||
| } | ||
| }; | ||
| } else if (res instanceof SubnetworkId) { | ||
| return new Function<SubnetworkId, Void>() { | ||
| @Override | ||
| public Void apply(SubnetworkId subnetworkId) { | ||
| compute.deleteSubnetwork(subnetworkId); | ||
| return null; | ||
| } | ||
| }; | ||
| } else if (res instanceof ImageId) { | ||
| return new Function<ImageId, Void>() { | ||
| @Override | ||
| public Void apply(ImageId imageId) { | ||
| compute.deleteImage(imageId); | ||
| return null; | ||
| } | ||
| }; | ||
| } else if (res instanceof InstanceId) { | ||
| return new Function<InstanceId, Void>() { | ||
| @Override | ||
| public Void apply(InstanceId instanceId) { | ||
| compute.deleteInstance(instanceId); | ||
| return null; | ||
| } | ||
| }; | ||
| } else { | ||
| throw new NotImplementedException(); | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.