use of org.platformlayer.core.model.EndpointInfo in project platformlayer by platformlayer.
the class DnsHelpers method buildDnsFile.
public ZoneFile buildDnsFile(DnsZone dnsZone) throws OpsException {
ZoneFile dnsFile = new ZoneFile(dnsZone.dnsName);
Iterable<DnsServer> dnsServers = platformLayer.listItems(DnsServer.class);
for (DnsServer dnsServer : dnsServers) {
switch(dnsServer.getState()) {
case DELETE_REQUESTED:
case DELETED:
log.info("Skipping server (deleted/deleting): " + dnsServer);
continue;
case ACTIVE:
// Good
break;
default:
log.warn("Dns server not yet active: " + dnsServer);
// failed = true;
continue;
}
List<EndpointInfo> dnsServerEndpoints = EndpointInfo.findEndpoints(dnsServer.getTags(), 53);
if (dnsServerEndpoints.isEmpty()) {
throw new OpsException("Cannot find endpoint for: " + dnsServer);
}
// Use the ID to produce a stable identifier
// TODO: What if we shutdown nameservers? Should we do something like consistent hashing instead?
// i.e. always create ns1 ... ns16, and then dynamically repoint them as we add/remove nameservers?
// Does this really help?
// String serverId = dnsServer"ns" + dnsServer.getId();
String dnsName = dnsServer.dnsName;
if (dnsName == null) {
throw new OpsException("DnsName not set on " + dnsServer);
}
// TODO: This might not be the right address in complex networks
for (EndpointInfo dnsServerEndpoint : dnsServerEndpoints) {
String address = dnsServerEndpoint.publicIp;
dnsFile.addNS(dnsZone.dnsName, address, dnsName);
}
}
Iterable<DnsRecord> dnsRecords = platformLayer.listItems(DnsRecord.class);
for (DnsRecord record : dnsRecords) {
switch(record.getState()) {
case DELETE_REQUESTED:
case DELETED:
log.info("Skipping record (deleted/deleting): " + record);
continue;
default:
break;
}
if (!isInZone(record, dnsZone)) {
continue;
}
dnsFile.addAddress(record.dnsName, record.address);
}
return dnsFile;
}
use of org.platformlayer.core.model.EndpointInfo in project platformlayer by platformlayer.
the class GetEndpoint method runCommand.
@Override
public Object runCommand() throws PlatformLayerClientException {
// Should this be a tag?
PlatformLayerClient client = getPlatformLayerClient();
PlatformLayerKey key = path.resolve(getContext());
UntypedItem untypedItem = client.getItemUntyped(key, Format.XML);
List<EndpointInfo> endpoints = EndpointInfo.getEndpoints(untypedItem.getTags());
return endpoints;
}
use of org.platformlayer.core.model.EndpointInfo in project platformlayer by platformlayer.
the class TagFromChildren method handler.
@Handler
public void handler() throws OpsException {
if (OpsContext.isConfigure()) {
for (OwnedItem<?> childServer : parentController.getChildren(OwnedItem.class)) {
ItemBase server = childServer.getItem();
if (server == null) {
// Right now, we have to go through a retry cycle
throw new OpsException("Child server not ready");
}
List<EndpointInfo> endpoints = EndpointInfo.findEndpoints(server.getTags(), port);
if (endpoints.isEmpty()) {
// TODO: Cope in future e.g. if we only need one of two in a cluster
throw new OpsException("Child server not ready");
}
for (EndpointInfo endpoint : endpoints) {
platformLayer.addTag(parentItem.getKey(), endpoint.toTag());
}
}
}
}
use of org.platformlayer.core.model.EndpointInfo in project platformlayer by platformlayer.
the class DirectPublicPorts method addChildren.
@Override
protected void addChildren() throws OpsException {
final CloudInstanceMapper cloudHost;
{
cloudHost = injected(CloudInstanceMapper.class);
cloudHost.createInstance = false;
cloudHost.instance = backendItem;
addChild(cloudHost);
}
final SocketAddressPoolAssignment assignPublicAddress;
{
assignPublicAddress = cloudHost.addChild(SocketAddressPoolAssignment.class);
assignPublicAddress.holder = backendItem.getKey();
if (Objects.equal(transport, Transport.Ipv6)) {
assignPublicAddress.poolProvider = new OpsProvider<ResourcePool<InetSocketAddress>>() {
@Override
public ResourcePool<InetSocketAddress> get() throws OpsException {
final ResourcePool<AddressModel> pool = directCloudHelpers.getAddressPool6().get();
return new AssignPortToAddressPool(pool, publicPort);
}
};
} else {
List<Integer> publicPortCluster = this.publicPortCluster;
if (publicPortCluster == null) {
publicPortCluster = Lists.newArrayList();
publicPortCluster.add(publicPort);
}
if (!publicPortCluster.contains(publicPort)) {
throw new OpsException("Port set specified, but public port not in the set");
}
assignPublicAddress.poolProvider = directCloudHelpers.getPublicAddressPool4(publicPort, publicPortCluster);
}
}
if (Objects.equal(transport, Transport.Ipv6)) {
// TODO: Do we need separate frontend / backend ports really?
if (this.publicPort != this.backendPort) {
throw new UnsupportedOperationException();
}
} else {
for (Protocol protocol : Protocol.TcpAndUdp()) {
IptablesForwardPort forward = injected(IptablesForwardPort.class);
forward.publicAddress = assignPublicAddress;
forward.ruleKey = protocol.name() + "-" + uuid;
forward.protocol = protocol;
forward.privateAddress = new OpsProvider<String>() {
@Override
public String get() throws OpsException {
// Refresh item to pick up new tags
backendItem = platformLayerClient.getItem(backendItem.getKey(), DirectInstance.class);
PlatformLayerCloudMachine instanceMachine = (PlatformLayerCloudMachine) instanceHelpers.getMachine(backendItem);
DirectInstance instance = (DirectInstance) instanceMachine.getInstance();
List<InetAddress> addresses = Tag.NETWORK_ADDRESS.find(instance);
InetAddress address = InetAddressChooser.preferIpv4().choose(addresses);
if (address == null) {
throw new IllegalStateException();
}
if (InetAddressUtils.isIpv6(address)) {
// We can't NAT IPV4 -> IPV6 (I think)
throw new IllegalStateException();
}
return address.getHostAddress();
}
};
forward.privatePort = backendPort;
cloudHost.addChild(forward);
}
}
{
OpsProvider<TagChanges> tagChanges = new OpsProvider<TagChanges>() {
@Override
public TagChanges get() {
TagChanges tagChanges = new TagChanges();
InetSocketAddress socketAddress = assignPublicAddress.get();
if (socketAddress == null) {
return null;
}
if (socketAddress.getPort() != publicPort) {
throw new IllegalStateException();
}
EndpointInfo endpoint = new EndpointInfo(socketAddress);
tagChanges.addTags.add(endpoint.toTag());
return tagChanges;
}
};
for (ItemBase tagItem : tagItems) {
Tagger tagger = addChild(Tagger.class);
tagger.platformLayerKey = tagItem.getKey();
tagger.tagChangesProvider = tagChanges;
}
}
}
use of org.platformlayer.core.model.EndpointInfo in project platformlayer by platformlayer.
the class EndpointDnsRecord method handler.
@Handler
public void handler() throws OpsException {
PublicEndpointBase endpoint = endpointProvider.get();
if (OpsContext.isConfigure()) {
// Create a DNS record
Tag parentTag = Tag.buildParentTag(endpoint.getKey());
List<EndpointInfo> endpoints = EndpointInfo.findEndpoints(endpoint.getTags(), destinationPort);
if (endpoints.isEmpty()) {
throw new OpsException("Cannot find endpoint for port: " + destinationPort);
}
DnsRecord record = new DnsRecord();
record.setDnsName(dnsName);
for (EndpointInfo endpointInfo : endpoints) {
record.getAddress().add(endpointInfo.publicIp);
}
record.getTags().add(parentTag);
record.setKey(PlatformLayerKey.fromId(dnsName));
try {
platformLayerClient.putItemByTag((ItemBase) record, parentTag);
} catch (PlatformLayerClientException e) {
throw new OpsException("Error registering persistent instance", e);
}
}
}
Aggregations