Skip to content
This repository was archived by the owner on Aug 6, 2025. It is now read-only.

Commit b979e15

Browse files
qpointsystemsgaul
authored andcommitted
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
1 parent 6851ac9 commit b979e15

File tree

6 files changed

+125
-8
lines changed

6 files changed

+125
-8
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.jclouds.ec2.compute.loaders;
18+
19+
import com.google.common.base.Predicate;
20+
import com.google.common.cache.CacheLoader;
21+
import com.google.common.collect.Iterables;
22+
import jakarta.inject.Inject;
23+
import jakarta.inject.Singleton;
24+
import org.jclouds.ec2.EC2Api;
25+
import org.jclouds.ec2.compute.domain.RegionAndName;
26+
import org.jclouds.ec2.domain.PublicIpInstanceIdPair;
27+
28+
import java.util.NoSuchElementException;
29+
30+
@Singleton
31+
public class LoadAllocationIdForInstanceOrNull extends CacheLoader<RegionAndName, String> {
32+
private final EC2Api client;
33+
34+
@Inject
35+
public LoadAllocationIdForInstanceOrNull(EC2Api client) {
36+
this.client = client;
37+
}
38+
39+
@Override
40+
public String load(final RegionAndName key) throws Exception {
41+
try {
42+
return Iterables.find(client.getElasticIPAddressApi().get().describeAddressesInRegion(key.getRegion()),
43+
new Predicate<PublicIpInstanceIdPair>() {
44+
45+
@Override
46+
public boolean apply(PublicIpInstanceIdPair input) {
47+
return key.getName().equals(input.getInstanceId());
48+
}
49+
50+
}).getAllocationId();
51+
} catch (NoSuchElementException e) {
52+
return null;
53+
}
54+
}
55+
}

apis/ec2/src/main/java/org/jclouds/ec2/domain/PublicIpInstanceIdPair.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,15 @@ public class PublicIpInstanceIdPair implements Comparable<PublicIpInstanceIdPair
3434
private final String region;
3535
@Nullable
3636
private final String instanceId;
37+
private final String allocationId;
3738
private final String publicIp;
3839
private final Map<String, String> tags;
3940

4041
public PublicIpInstanceIdPair(final String region, final String publicIp, @Nullable final String instanceId,
41-
@Nullable final Map<String, String> tags) {
42+
@Nullable final String allocationId, @Nullable final Map<String, String> tags) {
4243
this.region = checkNotNull(region, "region");
4344
this.instanceId = instanceId;
45+
this.allocationId = allocationId;
4446
this.publicIp = checkNotNull(publicIp, "publicIp");
4547
this.tags = tags == null ? ImmutableMap.<String, String> of() : ImmutableMap.copyOf(tags);
4648
}
@@ -71,6 +73,13 @@ public String getInstanceId() {
7173
return instanceId;
7274
}
7375

76+
/**
77+
* The ID of the IP allocation (e.g., eipalloc-0ca038968f2a2c986).
78+
*/
79+
public String getAllocationId() {
80+
return allocationId;
81+
}
82+
7483
/**
7584
* The public IP address.
7685
*/
@@ -87,6 +96,7 @@ public int hashCode() {
8796
final int prime = 31;
8897
int result = 1;
8998
result = prime * result + ((instanceId == null) ? 0 : instanceId.hashCode());
99+
result = prime * result + ((allocationId == null) ? 0 : allocationId.hashCode());
90100
result = prime * result + ((publicIp == null) ? 0 : publicIp.hashCode());
91101
result = prime * result + ((region == null) ? 0 : region.hashCode());
92102
result = prime * result + ((tags == null) ? 0 : tags.hashCode());
@@ -107,6 +117,11 @@ public boolean equals(Object obj) {
107117
return false;
108118
} else if (!instanceId.equals(other.instanceId))
109119
return false;
120+
if (allocationId == null) {
121+
if (other.allocationId != null)
122+
return false;
123+
} else if (!allocationId.equals(other.allocationId))
124+
return false;
110125
if (publicIp == null) {
111126
if (other.publicIp != null)
112127
return false;

apis/ec2/src/main/java/org/jclouds/ec2/features/ElasticIPAddressApi.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,28 @@ void releaseAddressInRegion(
144144
@EndpointParam(parser = RegionToEndpointOrProviderIfNull.class) @Nullable String region,
145145
@FormParam("PublicIp") String publicIp);
146146

147+
/**
148+
* Releases an elastic IP address associated with your identity.
149+
*
150+
* @param region
151+
* Elastic IP addresses are tied to a Region and cannot be mapped across Regions.
152+
* @param allocationId
153+
* The Allocation ID (e.g., eipalloc-0ca038968f2a2c986) of the IP address that you are releasing from your identity.
154+
*
155+
* @see #allocateAddress
156+
* @see #describeAddresses
157+
* @see #associateAddress
158+
* @see #disassociateAddress
159+
* @see <a href="http://docs.amazonwebservices.com/AWSEC2/latest/APIReference/index.html?ApiReference-query-ReleaseAddress.html"
160+
*/
161+
@Named("ReleaseAddress")
162+
@POST
163+
@Path("/")
164+
@FormParams(keys = ACTION, values = "ReleaseAddress")
165+
void releaseAddressInRegionByAllocationId(
166+
@EndpointParam(parser = RegionToEndpointOrProviderIfNull.class) @Nullable String region,
167+
@FormParam("AllocationId") String allocationId);
168+
147169
/**
148170
* Lists elastic IP addresses assigned to your identity or provides information about a specific
149171
* address.

apis/ec2/src/main/java/org/jclouds/ec2/xml/DescribeAddressesResponseHandler.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public class DescribeAddressesResponseHandler extends HandlerForGeneratedRequest
4444
@Inject
4545
@Region
4646
Supplier<String> defaultRegion;
47+
private String allocationId;
4748
private String instanceId;
4849
private final TagSetHandler tagSetHandler;
4950
private boolean inTagSet;
@@ -80,14 +81,17 @@ public void endElement(final String uri, final String name, final String qName)
8081
ipAddress = currentOrNull();
8182
} else if (qName.equals("instanceId")) {
8283
instanceId = currentOrNull();
84+
} else if (qName.equals("allocationId")) {
85+
allocationId = currentOrNull();
8386
} else if (qName.equals("item")) {
8487
String region = AWSUtils.findRegionInArgsOrNull(getRequest());
8588
if (region == null)
8689
region = defaultRegion.get();
8790

88-
pairs.add(new PublicIpInstanceIdPair(region, ipAddress, instanceId, tagResults));
91+
pairs.add(new PublicIpInstanceIdPair(region, ipAddress, instanceId, allocationId, tagResults));
8992
ipAddress = null;
9093
instanceId = null;
94+
allocationId = null;
9195
tagResults = null;
9296
}
9397

apis/ec2/src/test/java/org/jclouds/ec2/compute/loaders/LoadPublicIpForInstanceOrNullTest.java

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public void testReturnsPublicIpOnMatch() throws Exception {
4141

4242
expect(client.getElasticIPAddressApi()).andReturn((Optional) Optional.of(ipClient)).atLeastOnce();
4343
expect(ipClient.describeAddressesInRegion("region")).andReturn(
44-
ImmutableSet.<PublicIpInstanceIdPair> of(new PublicIpInstanceIdPair("region", "1.1.1.1", "i-blah", null)))
44+
ImmutableSet.<PublicIpInstanceIdPair> of(new PublicIpInstanceIdPair("region", "1.1.1.1", "i-blah", null, null)))
4545
.atLeastOnce();
4646

4747
replay(client);
@@ -55,6 +55,27 @@ ImmutableSet.<PublicIpInstanceIdPair> of(new PublicIpInstanceIdPair("region", "1
5555
verify(ipClient);
5656
}
5757

58+
@Test
59+
public void testReturnsPublicIpAndAllocIdOnMatch() throws Exception {
60+
EC2Api client = createMock(EC2Api.class);
61+
ElasticIPAddressApi ipClient = createMock(ElasticIPAddressApi.class);
62+
63+
expect(client.getElasticIPAddressApi()).andReturn((Optional) Optional.of(ipClient)).atLeastOnce();
64+
expect(ipClient.describeAddressesInRegion("region")).andReturn(
65+
ImmutableSet.<PublicIpInstanceIdPair> of(new PublicIpInstanceIdPair("region", "1.1.1.1", "i-blah", "a-foobar", null)))
66+
.atLeastOnce();
67+
68+
replay(client);
69+
replay(ipClient);
70+
71+
LoadAllocationIdForInstanceOrNull parser = new LoadAllocationIdForInstanceOrNull(client);
72+
73+
assertEquals(parser.load(new RegionAndName("region", "i-blah")), "a-foobar");
74+
75+
verify(client);
76+
verify(ipClient);
77+
}
78+
5879
@Test
5980
public void testReturnsNullWhenNotFound() throws Exception {
6081
EC2Api client = createMock(EC2Api.class);
@@ -85,7 +106,7 @@ public void testReturnsNullWhenNotAssigned() throws Exception {
85106
expect(client.getElasticIPAddressApi()).andReturn((Optional) Optional.of(ipClient)).atLeastOnce();
86107

87108
expect(ipClient.describeAddressesInRegion("region")).andReturn(
88-
ImmutableSet.<PublicIpInstanceIdPair> of(new PublicIpInstanceIdPair("region", "1.1.1.1", null, null)))
109+
ImmutableSet.<PublicIpInstanceIdPair> of(new PublicIpInstanceIdPair("region", "1.1.1.1", null, null, null)))
89110
.atLeastOnce();
90111

91112
replay(client);

apis/ec2/src/test/java/org/jclouds/ec2/xml/DescribeAddressesResponseHandlerTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public void testApplyInputStream() throws UnknownHostException {
4747
Set<PublicIpInstanceIdPair> result = factory.create(handler).parse(is);
4848

4949
assertEquals(result, ImmutableSet.of(new PublicIpInstanceIdPair(defaultRegion, "67.202.55.255", "i-f15ebb98",
50-
Collections.<String, String> emptyMap()), new PublicIpInstanceIdPair(defaultRegion, "67.202.55.233", null,
50+
null, Collections.<String, String> emptyMap()), new PublicIpInstanceIdPair(defaultRegion, "67.202.55.233", null, null,
5151
Collections.<String, String> emptyMap())));
5252
}
5353

@@ -62,9 +62,9 @@ public void testApplyInputStreamWithTags() throws UnknownHostException {
6262

6363
assertEquals(result.size(), 3);
6464
assertEquals(result, ImmutableSet.of(new PublicIpInstanceIdPair(defaultRegion, "67.202.55.255", "i-f15ebb98",
65-
Collections.<String, String> emptyMap()), new PublicIpInstanceIdPair(defaultRegion, "67.202.55.233", null,
66-
Collections.<String, String> emptyMap()), new PublicIpInstanceIdPair(defaultRegion, "54.76.27.192", null,
67-
ImmutableMap.of("Name", "value-fa97d19c", "Empty", ""))));
65+
null, Collections.<String, String> emptyMap()), new PublicIpInstanceIdPair(defaultRegion, "67.202.55.233", null,
66+
null, Collections.<String, String> emptyMap()), new PublicIpInstanceIdPair(defaultRegion, "54.76.27.192", null,
67+
null, ImmutableMap.of("Name", "value-fa97d19c", "Empty", ""))));
6868
}
6969

7070
private void addDefaultRegionToHandler(final ParseSax.HandlerWithResult<?> handler) {

0 commit comments

Comments
 (0)