2020import com .amazonaws .services .ec2 .AmazonEC2 ;
2121import com .amazonaws .services .ec2 .AmazonEC2ClientBuilder ;
2222import com .amazonaws .services .ec2 .model .DescribeInstancesRequest ;
23+ import com .amazonaws .services .ec2 .model .Reservation ;
2324import com .amazonaws .services .elasticloadbalancing .AmazonElasticLoadBalancing ;
2425import com .amazonaws .services .elasticloadbalancing .AmazonElasticLoadBalancingClientBuilder ;
2526import com .amazonaws .services .elasticloadbalancing .model .DescribeLoadBalancersRequest ;
2627import com .amazonaws .services .elasticloadbalancing .model .Instance ;
2728import com .amazonaws .services .elasticloadbalancing .model .LoadBalancerDescription ;
29+ import java .util .ArrayList ;
2830import org .apache .ignite .spi .IgniteSpiConfiguration ;
2931import org .apache .ignite .spi .IgniteSpiException ;
3032import org .apache .ignite .spi .discovery .tcp .ipfinder .TcpDiscoveryIpFinderAdapter ;
3436import java .util .List ;
3537
3638import static com .amazonaws .util .StringUtils .isNullOrEmpty ;
37- import static java .util .stream .Collectors .toList ;
3839
3940/**
4041 * AWS ELB-based IP finder.
@@ -67,7 +68,7 @@ public class TcpDiscoveryElbIpFinder extends TcpDiscoveryIpFinderAdapter {
6768 private AmazonEC2 amazonEC2Client ;
6869
6970 /** */
70- private AWSCredentialsProvider credentialsProvider ;
71+ private AWSCredentialsProvider credsProvider ;
7172
7273 /** */
7374 private String region ;
@@ -82,47 +83,53 @@ public TcpDiscoveryElbIpFinder() {
8283 setShared (true );
8384 }
8485
85- /**
86- * {@inheritDoc}
87- */
88- @ Override
89- public Collection <InetSocketAddress > getRegisteredAddresses () throws IgniteSpiException {
86+ /** {@inheritDoc} */
87+ @ Override public Collection <InetSocketAddress > getRegisteredAddresses () throws IgniteSpiException {
9088 initClients ();
9189
90+ List <String > instanceIds = new ArrayList <>();
91+
9292 DescribeLoadBalancersRequest req = new DescribeLoadBalancersRequest ().withLoadBalancerNames (loadBalancerName );
9393
94- List <String > instanceIds = amazonELBClient .describeLoadBalancers (req )
95- .getLoadBalancerDescriptions ()
96- .stream ()
97- .map (LoadBalancerDescription ::getInstances )
98- .flatMap (instances -> instances .stream ())
99- .map (Instance ::getInstanceId )
100- .collect (toList ());
101-
102- return amazonEC2Client .describeInstances (new DescribeInstancesRequest ().withInstanceIds (instanceIds ))
103- .getReservations ()
104- .stream ()
105- .flatMap (reservation -> reservation .getInstances ().stream ())
106- .map (instance -> new InetSocketAddress (instance .getPrivateIpAddress (), 0 ))
107- .collect (toList ());
94+ List <LoadBalancerDescription > descs = amazonELBClient .describeLoadBalancers (req ).getLoadBalancerDescriptions ();
95+
96+ for (LoadBalancerDescription desc : descs ) {
97+ for (Instance instance : desc .getInstances ())
98+ instanceIds .add (instance .getInstanceId ());
99+ }
100+
101+ DescribeInstancesRequest instReq = new DescribeInstancesRequest ().withInstanceIds (instanceIds );
102+
103+ List <Reservation > reservations = amazonEC2Client .describeInstances (instReq ).getReservations ();
104+
105+ List <InetSocketAddress > addrs = new ArrayList <>();
106+
107+ for (Reservation reservation : reservations ) {
108+ List <com .amazonaws .services .ec2 .model .Instance > instances = reservation .getInstances ();
109+
110+ for (com .amazonaws .services .ec2 .model .Instance instance : instances )
111+ addrs .add (new InetSocketAddress (instance .getPrivateIpAddress (), 0 ));
112+ }
113+
114+ return addrs ;
108115 }
109116
110117 /**
111118 * Initializing the IP finder.
112119 */
113120 private void initClients () {
114- if (credentialsProvider == null || isNullOrEmpty (loadBalancerName ) || isNullOrEmpty (region ))
121+ if (credsProvider == null || isNullOrEmpty (loadBalancerName ) || isNullOrEmpty (region ))
115122 throw new IgniteSpiException ("One or more configuration parameters are invalid [setCredentialsProvider=" +
116- credentialsProvider + ", setRegion=" + region + ", setLoadBalancerName=" +
123+ credsProvider + ", setRegion=" + region + ", setLoadBalancerName=" +
117124 loadBalancerName + "]" );
118125
119126 if (amazonEC2Client == null )
120- amazonEC2Client = AmazonEC2ClientBuilder .standard ().withRegion (region ).withCredentials (credentialsProvider )
127+ amazonEC2Client = AmazonEC2ClientBuilder .standard ().withRegion (region ).withCredentials (credsProvider )
121128 .build ();
122129
123130 if (amazonELBClient == null )
124131 amazonELBClient = AmazonElasticLoadBalancingClientBuilder .standard ().withRegion (region )
125- .withCredentials (credentialsProvider ).build ();
132+ .withCredentials (credsProvider ).build ();
126133 }
127134
128135 /**
@@ -153,27 +160,20 @@ public void setRegion(String region) {
153160 *
154161 * For details refer to Amazon API reference.
155162 *
156- * @param credentialsProvider AWS credentials provider.
157- * @return {@code this} for chaining.
163+ * @param credsProvider AWS credentials provider.
158164 */
159165 @ IgniteSpiConfiguration (optional = false )
160- public void setCredentialsProvider (AWSCredentialsProvider credentialsProvider ) {
161- this .credentialsProvider = credentialsProvider ;
166+ public void setCredentialsProvider (AWSCredentialsProvider credsProvider ) {
167+ this .credsProvider = credsProvider ;
162168 }
163169
164- /**
165- * {@inheritDoc}
166- */
167- @ Override
168- public void registerAddresses (Collection <InetSocketAddress > addrs ) throws IgniteSpiException {
170+ /** {@inheritDoc} */
171+ @ Override public void registerAddresses (Collection <InetSocketAddress > addrs ) throws IgniteSpiException {
169172 //No-op, ELB will take care of registration.
170173 }
171174
172- /**
173- * {@inheritDoc}
174- */
175- @ Override
176- public void unregisterAddresses (Collection <InetSocketAddress > addrs ) throws IgniteSpiException {
175+ /** {@inheritDoc} */
176+ @ Override public void unregisterAddresses (Collection <InetSocketAddress > addrs ) throws IgniteSpiException {
177177 // No-op, ELB will take care of this process.
178178 }
179179}
0 commit comments