Skip to content
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

Implement "live-instances" method #201

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions src/main/java/gyro/aws/elb/LoadBalancerInstance.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package gyro.aws.elb;

import gyro.aws.ec2.InstanceResource;
import gyro.core.GyroInstance;
import gyro.core.resource.Diffable;
import gyro.core.resource.Output;

public class LoadBalancerInstance extends Diffable {

private InstanceResource instance;
private String state;

public GyroInstance getInstance() {
return instance;
}

public void setInstance(InstanceResource instance) {
this.instance = instance;
}

@Output
public String getState() {
return state;
}

public void setState(String state) {
this.state = state;
}

@Override
public String primaryKey() {
if (instance != null) {
return instance.getId();
}

return "";
}

}
60 changes: 51 additions & 9 deletions src/main/java/gyro/aws/elb/LoadBalancerResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,12 @@

import gyro.core.resource.Updatable;
import gyro.core.scope.State;
import software.amazon.awssdk.services.ec2.Ec2Client;
import software.amazon.awssdk.services.ec2.model.DescribeInstancesResponse;
import software.amazon.awssdk.services.ec2.model.Reservation;
import software.amazon.awssdk.services.elasticloadbalancing.ElasticLoadBalancingClient;
import software.amazon.awssdk.services.elasticloadbalancing.model.CreateLoadBalancerResponse;
import software.amazon.awssdk.services.elasticloadbalancing.model.DescribeInstanceHealthResponse;
import software.amazon.awssdk.services.elasticloadbalancing.model.DescribeLoadBalancerAttributesResponse;
import software.amazon.awssdk.services.elasticloadbalancing.model.DescribeLoadBalancersResponse;
import software.amazon.awssdk.services.elasticloadbalancing.model.Instance;
Expand All @@ -44,7 +48,9 @@
import software.amazon.awssdk.services.elasticloadbalancing.model.TagKeyOnly;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -92,7 +98,7 @@ public class LoadBalancerResource extends AwsResource implements Copyable<LoadBa

private String dnsName;
private HealthCheckResource healthCheck;
private Set<InstanceResource> instances;
private Set<LoadBalancerInstance> instances;
private Set<ListenerResource> listener;
private String name;
private String scheme;
Expand Down Expand Up @@ -129,15 +135,15 @@ public void setHealthCheck(HealthCheckResource healthCheck) {
* The instances to associate with this load balancer.
*/
@Updatable
public Set<InstanceResource> getInstances() {
public Set<LoadBalancerInstance> getInstances() {
if (instances == null) {
instances = new LinkedHashSet<>();
}

return instances;
}

public void setInstances(Set<InstanceResource> instances) {
public void setInstances(Set<LoadBalancerInstance> instances) {
this.instances = instances;
}

Expand Down Expand Up @@ -421,8 +427,7 @@ public void copyFrom(LoadBalancerDescription description) {
setScheme(description.scheme());
setHostedZoneId(description.canonicalHostedZoneNameID());

getInstances().clear();
description.instances().forEach(i -> getInstances().add(findById(InstanceResource.class, i.instanceId())));
setInstances(liveInstances());

getSecurityGroups().clear();
description.securityGroups().forEach(r -> getSecurityGroups().add(findById(SecurityGroupResource.class, r)));
Expand Down Expand Up @@ -479,12 +484,12 @@ private LoadBalancerDescription getLoadBalancer(ElasticLoadBalancingClient clien
}

private Set<Instance> toInstances() {
Set<Instance> instance = new LinkedHashSet<>();
for (InstanceResource instanceResource : getInstances()) {
instance.add(Instance.builder().instanceId(instanceResource.getId()).build());
Set<Instance> instances = new LinkedHashSet<>();
for (LoadBalancerInstance instance : getInstances()) {
instances.add(Instance.builder().instanceId(instance.getInstance().getGyroInstanceId()).build());
}

return instance;
return instances;
}

private List<Listener> toListeners() {
Expand All @@ -503,4 +508,41 @@ private List<Listener> toListeners() {
return listeners;
}

public Set<LoadBalancerInstance> liveInstances() {
ElasticLoadBalancingClient client = createClient(ElasticLoadBalancingClient.class);

DescribeInstanceHealthResponse response = client.describeInstanceHealth(r -> r.loadBalancerName(getName()));
Map<String, InstanceState> instanceStates = new HashMap<>();
for (InstanceState instanceState : response.instanceStates()) {
instanceStates.put(instanceState.instanceId(), instanceState);
}

List<String> instanceIds = new ArrayList<>(instanceStates.keySet());
if (!instanceIds.isEmpty()) {
Ec2Client ec2Client = createClient(Ec2Client.class);
DescribeInstancesResponse instancesResponse = ec2Client.describeInstances(r -> r.instanceIds(instanceIds));

Set<LoadBalancerInstance> instances = new HashSet<>();
for (Reservation reservation : instancesResponse.reservations()) {
for (software.amazon.awssdk.services.ec2.model.Instance ec2Instance : reservation.instances()) {
LoadBalancerInstance instance = new LoadBalancerInstance();
instance.setInstance(getInstanceResource(ec2Instance));
instance.setState(instanceStates.get(ec2Instance.instanceId()).state());

instances.add(instance);
}
}

return instances;
}

return Collections.emptySet();
}

private InstanceResource getInstanceResource(software.amazon.awssdk.services.ec2.model.Instance instance) {
InstanceResource instanceResource = newSubresource(InstanceResource.class);
instanceResource.copyFrom(instance);
return instanceResource;
}

}