mirror of https://github.com/apache/jclouds.git
Added allocationId to PublicIpInstanceIdPair so that IP addresses can be tracked by that AWS concept.
Added a releaseAddressInRegionByAllocationId so that an IP address can be released by that addresses AllocationId Modified the DescribeAddressesResponseHandler so that it can optionally take allocationId into account on a Describe Added a use case to test allocationId
This commit is contained in:
parent
6851ac9459
commit
b979e15529
|
@ -0,0 +1,55 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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 org.jclouds.ec2.compute.loaders;
|
||||
|
||||
import com.google.common.base.Predicate;
|
||||
import com.google.common.cache.CacheLoader;
|
||||
import com.google.common.collect.Iterables;
|
||||
import jakarta.inject.Inject;
|
||||
import jakarta.inject.Singleton;
|
||||
import org.jclouds.ec2.EC2Api;
|
||||
import org.jclouds.ec2.compute.domain.RegionAndName;
|
||||
import org.jclouds.ec2.domain.PublicIpInstanceIdPair;
|
||||
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
@Singleton
|
||||
public class LoadAllocationIdForInstanceOrNull extends CacheLoader<RegionAndName, String> {
|
||||
private final EC2Api client;
|
||||
|
||||
@Inject
|
||||
public LoadAllocationIdForInstanceOrNull(EC2Api client) {
|
||||
this.client = client;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String load(final RegionAndName key) throws Exception {
|
||||
try {
|
||||
return Iterables.find(client.getElasticIPAddressApi().get().describeAddressesInRegion(key.getRegion()),
|
||||
new Predicate<PublicIpInstanceIdPair>() {
|
||||
|
||||
@Override
|
||||
public boolean apply(PublicIpInstanceIdPair input) {
|
||||
return key.getName().equals(input.getInstanceId());
|
||||
}
|
||||
|
||||
}).getAllocationId();
|
||||
} catch (NoSuchElementException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -34,13 +34,15 @@ public class PublicIpInstanceIdPair implements Comparable<PublicIpInstanceIdPair
|
|||
private final String region;
|
||||
@Nullable
|
||||
private final String instanceId;
|
||||
private final String allocationId;
|
||||
private final String publicIp;
|
||||
private final Map<String, String> tags;
|
||||
|
||||
public PublicIpInstanceIdPair(final String region, final String publicIp, @Nullable final String instanceId,
|
||||
@Nullable final Map<String, String> tags) {
|
||||
@Nullable final String allocationId, @Nullable final Map<String, String> tags) {
|
||||
this.region = checkNotNull(region, "region");
|
||||
this.instanceId = instanceId;
|
||||
this.allocationId = allocationId;
|
||||
this.publicIp = checkNotNull(publicIp, "publicIp");
|
||||
this.tags = tags == null ? ImmutableMap.<String, String> of() : ImmutableMap.copyOf(tags);
|
||||
}
|
||||
|
@ -71,6 +73,13 @@ public class PublicIpInstanceIdPair implements Comparable<PublicIpInstanceIdPair
|
|||
return instanceId;
|
||||
}
|
||||
|
||||
/**
|
||||
* The ID of the IP allocation (e.g., eipalloc-0ca038968f2a2c986).
|
||||
*/
|
||||
public String getAllocationId() {
|
||||
return allocationId;
|
||||
}
|
||||
|
||||
/**
|
||||
* The public IP address.
|
||||
*/
|
||||
|
@ -87,6 +96,7 @@ public class PublicIpInstanceIdPair implements Comparable<PublicIpInstanceIdPair
|
|||
final int prime = 31;
|
||||
int result = 1;
|
||||
result = prime * result + ((instanceId == null) ? 0 : instanceId.hashCode());
|
||||
result = prime * result + ((allocationId == null) ? 0 : allocationId.hashCode());
|
||||
result = prime * result + ((publicIp == null) ? 0 : publicIp.hashCode());
|
||||
result = prime * result + ((region == null) ? 0 : region.hashCode());
|
||||
result = prime * result + ((tags == null) ? 0 : tags.hashCode());
|
||||
|
@ -107,6 +117,11 @@ public class PublicIpInstanceIdPair implements Comparable<PublicIpInstanceIdPair
|
|||
return false;
|
||||
} else if (!instanceId.equals(other.instanceId))
|
||||
return false;
|
||||
if (allocationId == null) {
|
||||
if (other.allocationId != null)
|
||||
return false;
|
||||
} else if (!allocationId.equals(other.allocationId))
|
||||
return false;
|
||||
if (publicIp == null) {
|
||||
if (other.publicIp != null)
|
||||
return false;
|
||||
|
|
|
@ -144,6 +144,28 @@ public interface ElasticIPAddressApi {
|
|||
@EndpointParam(parser = RegionToEndpointOrProviderIfNull.class) @Nullable String region,
|
||||
@FormParam("PublicIp") String publicIp);
|
||||
|
||||
/**
|
||||
* Releases an elastic IP address associated with your identity.
|
||||
*
|
||||
* @param region
|
||||
* Elastic IP addresses are tied to a Region and cannot be mapped across Regions.
|
||||
* @param allocationId
|
||||
* The Allocation ID (e.g., eipalloc-0ca038968f2a2c986) of the IP address that you are releasing from your identity.
|
||||
*
|
||||
* @see #allocateAddress
|
||||
* @see #describeAddresses
|
||||
* @see #associateAddress
|
||||
* @see #disassociateAddress
|
||||
* @see <a href="http://docs.amazonwebservices.com/AWSEC2/latest/APIReference/index.html?ApiReference-query-ReleaseAddress.html"
|
||||
*/
|
||||
@Named("ReleaseAddress")
|
||||
@POST
|
||||
@Path("/")
|
||||
@FormParams(keys = ACTION, values = "ReleaseAddress")
|
||||
void releaseAddressInRegionByAllocationId(
|
||||
@EndpointParam(parser = RegionToEndpointOrProviderIfNull.class) @Nullable String region,
|
||||
@FormParam("AllocationId") String allocationId);
|
||||
|
||||
/**
|
||||
* Lists elastic IP addresses assigned to your identity or provides information about a specific
|
||||
* address.
|
||||
|
|
|
@ -44,6 +44,7 @@ public class DescribeAddressesResponseHandler extends HandlerForGeneratedRequest
|
|||
@Inject
|
||||
@Region
|
||||
Supplier<String> defaultRegion;
|
||||
private String allocationId;
|
||||
private String instanceId;
|
||||
private final TagSetHandler tagSetHandler;
|
||||
private boolean inTagSet;
|
||||
|
@ -80,14 +81,17 @@ public class DescribeAddressesResponseHandler extends HandlerForGeneratedRequest
|
|||
ipAddress = currentOrNull();
|
||||
} else if (qName.equals("instanceId")) {
|
||||
instanceId = currentOrNull();
|
||||
} else if (qName.equals("allocationId")) {
|
||||
allocationId = currentOrNull();
|
||||
} else if (qName.equals("item")) {
|
||||
String region = AWSUtils.findRegionInArgsOrNull(getRequest());
|
||||
if (region == null)
|
||||
region = defaultRegion.get();
|
||||
|
||||
pairs.add(new PublicIpInstanceIdPair(region, ipAddress, instanceId, tagResults));
|
||||
pairs.add(new PublicIpInstanceIdPair(region, ipAddress, instanceId, allocationId, tagResults));
|
||||
ipAddress = null;
|
||||
instanceId = null;
|
||||
allocationId = null;
|
||||
tagResults = null;
|
||||
}
|
||||
|
||||
|
|
|
@ -41,7 +41,7 @@ public class LoadPublicIpForInstanceOrNullTest {
|
|||
|
||||
expect(client.getElasticIPAddressApi()).andReturn((Optional) Optional.of(ipClient)).atLeastOnce();
|
||||
expect(ipClient.describeAddressesInRegion("region")).andReturn(
|
||||
ImmutableSet.<PublicIpInstanceIdPair> of(new PublicIpInstanceIdPair("region", "1.1.1.1", "i-blah", null)))
|
||||
ImmutableSet.<PublicIpInstanceIdPair> of(new PublicIpInstanceIdPair("region", "1.1.1.1", "i-blah", null, null)))
|
||||
.atLeastOnce();
|
||||
|
||||
replay(client);
|
||||
|
@ -55,6 +55,27 @@ public class LoadPublicIpForInstanceOrNullTest {
|
|||
verify(ipClient);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReturnsPublicIpAndAllocIdOnMatch() throws Exception {
|
||||
EC2Api client = createMock(EC2Api.class);
|
||||
ElasticIPAddressApi ipClient = createMock(ElasticIPAddressApi.class);
|
||||
|
||||
expect(client.getElasticIPAddressApi()).andReturn((Optional) Optional.of(ipClient)).atLeastOnce();
|
||||
expect(ipClient.describeAddressesInRegion("region")).andReturn(
|
||||
ImmutableSet.<PublicIpInstanceIdPair> of(new PublicIpInstanceIdPair("region", "1.1.1.1", "i-blah", "a-foobar", null)))
|
||||
.atLeastOnce();
|
||||
|
||||
replay(client);
|
||||
replay(ipClient);
|
||||
|
||||
LoadAllocationIdForInstanceOrNull parser = new LoadAllocationIdForInstanceOrNull(client);
|
||||
|
||||
assertEquals(parser.load(new RegionAndName("region", "i-blah")), "a-foobar");
|
||||
|
||||
verify(client);
|
||||
verify(ipClient);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReturnsNullWhenNotFound() throws Exception {
|
||||
EC2Api client = createMock(EC2Api.class);
|
||||
|
@ -85,7 +106,7 @@ public class LoadPublicIpForInstanceOrNullTest {
|
|||
expect(client.getElasticIPAddressApi()).andReturn((Optional) Optional.of(ipClient)).atLeastOnce();
|
||||
|
||||
expect(ipClient.describeAddressesInRegion("region")).andReturn(
|
||||
ImmutableSet.<PublicIpInstanceIdPair> of(new PublicIpInstanceIdPair("region", "1.1.1.1", null, null)))
|
||||
ImmutableSet.<PublicIpInstanceIdPair> of(new PublicIpInstanceIdPair("region", "1.1.1.1", null, null, null)))
|
||||
.atLeastOnce();
|
||||
|
||||
replay(client);
|
||||
|
|
|
@ -47,7 +47,7 @@ public class DescribeAddressesResponseHandlerTest extends BaseEC2HandlerTest {
|
|||
Set<PublicIpInstanceIdPair> result = factory.create(handler).parse(is);
|
||||
|
||||
assertEquals(result, ImmutableSet.of(new PublicIpInstanceIdPair(defaultRegion, "67.202.55.255", "i-f15ebb98",
|
||||
Collections.<String, String> emptyMap()), new PublicIpInstanceIdPair(defaultRegion, "67.202.55.233", null,
|
||||
null, Collections.<String, String> emptyMap()), new PublicIpInstanceIdPair(defaultRegion, "67.202.55.233", null, null,
|
||||
Collections.<String, String> emptyMap())));
|
||||
}
|
||||
|
||||
|
@ -62,9 +62,9 @@ public class DescribeAddressesResponseHandlerTest extends BaseEC2HandlerTest {
|
|||
|
||||
assertEquals(result.size(), 3);
|
||||
assertEquals(result, ImmutableSet.of(new PublicIpInstanceIdPair(defaultRegion, "67.202.55.255", "i-f15ebb98",
|
||||
Collections.<String, String> emptyMap()), new PublicIpInstanceIdPair(defaultRegion, "67.202.55.233", null,
|
||||
Collections.<String, String> emptyMap()), new PublicIpInstanceIdPair(defaultRegion, "54.76.27.192", null,
|
||||
ImmutableMap.of("Name", "value-fa97d19c", "Empty", ""))));
|
||||
null, Collections.<String, String> emptyMap()), new PublicIpInstanceIdPair(defaultRegion, "67.202.55.233", null,
|
||||
null, Collections.<String, String> emptyMap()), new PublicIpInstanceIdPair(defaultRegion, "54.76.27.192", null,
|
||||
null, ImmutableMap.of("Name", "value-fa97d19c", "Empty", ""))));
|
||||
}
|
||||
|
||||
private void addDefaultRegionToHandler(final ParseSax.HandlerWithResult<?> handler) {
|
||||
|
|
Loading…
Reference in New Issue