public String returnNameOrIp(List<NetworkAddress> addresses) { Iterator<NetworkAddress> it = applyQuery(addresses.iterator()); if (it.hasNext()) { NetworkAddress address = it.next(); return address.getHostName() != null ? address.getHostName() : address.getIpAddress(); } return null; }
public NetworkAddress build() { return new NetworkAddress(label, protocolType, ipAddress, hostName); } }
@Override public boolean equals(Object o) { if (this == o) return true; if (!(o instanceof AwsDataCenterInfo)) return false; AwsDataCenterInfo that = (AwsDataCenterInfo) o; if (accountId != null ? !accountId.equals(that.accountId) : that.accountId != null) return false; if (amiId != null ? !amiId.equals(that.amiId) : that.amiId != null) return false; if (eth0mac != null ? !eth0mac.equals(that.eth0mac) : that.eth0mac != null) return false; if (instanceId != null ? !instanceId.equals(that.instanceId) : that.instanceId != null) return false; if (instanceType != null ? !instanceType.equals(that.instanceType) : that.instanceType != null) return false; if (name != null ? !name.equals(that.name) : that.name != null) return false; if (placementGroup != null ? !placementGroup.equals(that.placementGroup) : that.placementGroup != null) return false; if (privateAddress != null ? !privateAddress.equals(that.privateAddress) : that.privateAddress != null) return false; if (publicAddress != null ? !publicAddress.equals(that.publicAddress) : that.publicAddress != null) return false; if (region != null ? !region.equals(that.region) : that.region != null) return false; if (vpcId != null ? !vpcId.equals(that.vpcId) : that.vpcId != null) return false; if (zone != null ? !zone.equals(that.zone) : that.zone != null) return false; return true; }
@Override protected boolean matches(Boolean value, NetworkAddress endpoint) { String ipAddress = endpoint.getIpAddress(); if (ipAddress == null) { return false; } return SystemUtil.isPublic(ipAddress) == value; } }
public static String extractHostname(InstanceInfo instanceInfo) { if (AwsDataCenterInfo.class.isAssignableFrom(instanceInfo.getDataCenterInfo().getClass())) { final AwsDataCenterInfo dataCenterInfo = (AwsDataCenterInfo) instanceInfo.getDataCenterInfo(); return dataCenterInfo.getPublicAddress().getHostName(); } return ""; }
@Override protected boolean matches(String value, NetworkAddress endpoint) { return value.equals(endpoint.getLabel()); } }
@Override protected boolean matches(NetworkAddress.ProtocolType value, NetworkAddress endpoint) { return endpoint.getProtocolType() == value; } }
/** * The order of selection: first public, next private. * If there are multiple addresses within a group (for example multiple public IPs), the first in * the list is be taken. */ @Override public NetworkAddress getDefaultAddress() { if (defaultAddress != null) { return defaultAddress; } if (addresses == null || addresses.isEmpty()) { return null; } NetworkAddress best = null; for (NetworkAddress address : addresses) { if (best == null) { best = address; if (best.hasLabel(PUBLIC_ADDRESS)) { break; } } else if (address.hasLabel(PUBLIC_ADDRESS)) { best = address; break; } } defaultAddress = best; return defaultAddress; }
@Override protected boolean matches(Boolean value, ServiceEndpoint endpoint) { String ipAddress = endpoint.getAddress().getIpAddress(); if (ipAddress == null) { return false; } return SystemUtil.isPublic(ipAddress) == value; } }
@Override protected boolean matches(String value, ServiceEndpoint endpoint) { return value.equals(endpoint.getAddress().getLabel()); } }
@Override protected boolean matches(ProtocolType value, ServiceEndpoint endpoint) { return endpoint.getAddress().getProtocolType() == value; } }
public List<String> returnNameOrIpList(List<NetworkAddress> addresses) { Iterator<NetworkAddress> it = applyQuery(addresses.iterator()); List<String> addressList = new ArrayList<>(); while (it.hasNext()) { NetworkAddress address = it.next(); addressList.add(address.getHostName() != null ? address.getHostName() : address.getIpAddress()); } return addressList; }
public InetSocketAddress returnServiceAddress(InstanceInfo instanceInfo) { ServiceEndpoint endpoint = returnServiceEndpoint(instanceInfo); if (endpoint == null) { return null; } String address = endpoint.getAddress().getHostName(); if (address == null) { address = endpoint.getAddress().getIpAddress(); } return new InetSocketAddress(address, endpoint.getServicePort().getPort()); }
@Override public AwsDataCenterInfo next() { NetworkAddress publicAddress = publicAddresses.next(); NetworkAddress privateAddress = privateAddresses.next(); return new AwsDataCenterInfo.Builder() .withAwsDataCenter(template) .withInstanceId(String.format("id-%08d", ++instanceId)) .withPublicHostName(publicAddress.getHostName()) .withPublicIPv4(publicAddress.getIpAddress()) .withPrivateHostName(privateAddress.getHostName()) .withPrivateIPv4(privateAddress.getIpAddress()) .build(); }
@Override public JsonElement serialize(InstanceInfo instanceInfo, Type typeOfSrc, JsonSerializationContext context) { JsonObject result = new JsonObject(); if (AwsDataCenterInfo.class.isAssignableFrom(instanceInfo.getDataCenterInfo().getClass())) { final AwsDataCenterInfo dataCenterInfo = (AwsDataCenterInfo) instanceInfo.getDataCenterInfo(); result.addProperty("instId", dataCenterInfo.getInstanceId()); if(dataCenterInfo.getPublicAddress() != null) { result.addProperty("ip", dataCenterInfo.getPublicAddress().getIpAddress()); result.addProperty("hostname", dataCenterInfo.getPublicAddress().getHostName()); } result.addProperty("zone", dataCenterInfo.getZone()); result.addProperty("reg", dataCenterInfo.getRegion()); } result.addProperty("appId", instanceInfo.getApp()); result.addProperty("status", instanceInfo.getStatus().name()); result.addProperty("vip", instanceInfo.getVipAddress()); return result; } }
public Builder withAwsDataCenter(AwsDataCenterInfo dataCenter) { this.region = dataCenter.getRegion(); this.zone = dataCenter.getZone(); this.placementGroup = dataCenter.getPlacementGroup(); this.amiId = dataCenter.getAmiId(); this.instanceId = dataCenter.getInstanceId(); this.instanceType = dataCenter.getInstanceType(); this.privateIP = dataCenter.getPrivateAddress().getIpAddress(); this.privateHostName = dataCenter.getPrivateAddress().getHostName(); this.publicIP = dataCenter.getPublicAddress().getIpAddress(); this.publicHostName = dataCenter.getPublicAddress().getHostName(); this.eth0mac = dataCenter.getEth0mac(); this.vpcId = dataCenter.getVpcId(); this.accountId = dataCenter.getAccountId(); return this; }
private static Observable<Void> redirectTo(InstanceInfo readServerInfo, HttpServerRequest<ByteBuf> request, HttpServerResponse<ByteBuf> response) { ServiceEndpoint serviceEndpoint = HTTP_PUBLIC_SERVICE_SELECTOR.returnServiceEndpoint(readServerInfo); String redirectHost = serviceEndpoint == null ? null : serviceEndpoint.getAddress().getHostName(); if(redirectHost == null) { serviceEndpoint = HTTP_PRIVATE_SERVICE_SELECTOR.returnServiceEndpoint(readServerInfo); redirectHost = serviceEndpoint.getAddress().getIpAddress(); } StringBuilder redirectBuilder = new StringBuilder("http://") .append(redirectHost) .append(':') .append(serviceEndpoint.getServicePort().getPort()) .append(request.getPath()); if (request.getQueryString() != null) { redirectBuilder.append('?').append(request.getQueryString()); } response.getHeaders().add(Names.LOCATION, redirectBuilder.toString()); response.setStatus(HttpResponseStatus.FOUND); return Observable.empty(); } }
@Override public InstanceInfo next() { int cidx = idx.incrementAndGet(); String name = baseName + '_' + cidx; NetworkAddress publicAddress = publicAddresses.next(); NetworkAddress privateAddress = privateAddresses.next(); DataCenterInfo dataCenter = new AwsDataCenterInfo.Builder() .withAwsDataCenter(templateDataCenter) .withInstanceId(String.format("i-%s-%08d", baseName, cidx)) .withPublicHostName(publicAddress.getHostName()) .withPublicIPv4(publicAddress.getIpAddress()) .withPrivateHostName(privateAddress.getHostName()) .withPrivateIPv4(privateAddress.getIpAddress()) .build(); return new InstanceInfo.Builder() .withId("id#" + name) .withApp(template.getApp()) .withAppGroup(template.getAppGroup()) .withAsg(template.getAsg()) .withHealthCheckUrls(template.getHealthCheckUrls()) .withHomePageUrl(template.getHomePageUrl()) .withPorts(template.getPorts()) .withSecureVipAddress(template.getSecureVipAddress()) .withStatus(template.getStatus()) .withStatusPageUrl(template.getStatusPageUrl()) .withVipAddress(template.getVipAddress()) .withMetaData(template.getMetaData()) .withDataCenterInfo(dataCenter) .build(); }