Search in sources :

Example 1 with EndpointDnsRecord

use of org.platformlayer.ops.dns.EndpointDnsRecord in project platformlayer by platformlayer.

the class PublicEndpoint method addChildren.

@Override
protected void addChildren() throws OpsException {
    final OwnedEndpoint endpoint;
    {
        endpoint = addChild(OwnedEndpoint.class);
        endpoint.publicPort = publicPort;
        endpoint.publicPortCluster = publicPortCluster;
        endpoint.backendPort = backendPort;
        endpoint.parentItem = parentItem;
        endpoint.transport = transport;
    }
    if (tagItem != null) {
        Tagger tagger = injected(Tagger.class);
        OpsProvider<TagChanges> tagChanges = new OpsProvider<TagChanges>() {

            @Override
            public TagChanges get() throws OpsException {
                int maxAttempts = 5;
                int attempt = 0;
                PublicEndpointBase item = endpoint.getItem();
                while (true) {
                    attempt++;
                    if (item == null) {
                        if (!OpsContext.isDelete()) {
                            throw new OpsException("Endpoint not created");
                        } else {
                            log.warn("No endpoint => no tagging to be done");
                            return null;
                        }
                    }
                    List<EndpointInfo> endpointInfos = EndpointInfo.findEndpoints(item.getTags(), publicPort);
                    if (!endpointInfos.isEmpty()) {
                        TagChanges tagChanges = new TagChanges();
                        for (EndpointInfo endpointInfo : endpointInfos) {
                            tagChanges.addTags.add(endpointInfo.toTag());
                        }
                        return tagChanges;
                    }
                    if (attempt != maxAttempts) {
                        log.info("Endpoint not yet found; sleeping and retrying");
                        TimeSpan.FIVE_SECONDS.doSafeSleep();
                        item = platformLayerClient.getItem(item.getKey());
                        continue;
                    } else {
                        throw new OpsException("Cannot find endpoint for port: " + publicPort);
                    }
                }
            }
        };
        tagger.platformLayerKey = tagItem;
        tagger.tagChangesProvider = tagChanges;
        addChild(tagger);
    }
    if (defaultBlocked) {
        // Block on machine's firewall
        log.warn("Not adding firewall block; relying on default block");
    // addChild(IptablesFirewallEntry.build(FirewallRecord.buildBlockPort(protocol, backendPort)));
    }
    if (!Strings.isNullOrEmpty(dnsName)) {
        EndpointDnsRecord dns = injected(EndpointDnsRecord.class);
        dns.destinationPort = publicPort;
        dns.endpointProvider = new Provider<PublicEndpointBase>() {

            @Override
            public PublicEndpointBase get() {
                return endpoint.getItem();
            }
        };
        dns.dnsName = dnsName;
        addChild(dns);
    }
}
Also used : OpsException(org.platformlayer.ops.OpsException) Tagger(org.platformlayer.ops.tagger.Tagger) TagChanges(org.platformlayer.core.model.TagChanges) EndpointInfo(org.platformlayer.core.model.EndpointInfo) OpsProvider(org.platformlayer.ops.OpsProvider) EndpointDnsRecord(org.platformlayer.ops.dns.EndpointDnsRecord) PublicEndpointBase(org.platformlayer.core.model.PublicEndpointBase)

Aggregations

EndpointInfo (org.platformlayer.core.model.EndpointInfo)1 PublicEndpointBase (org.platformlayer.core.model.PublicEndpointBase)1 TagChanges (org.platformlayer.core.model.TagChanges)1 OpsException (org.platformlayer.ops.OpsException)1 OpsProvider (org.platformlayer.ops.OpsProvider)1 EndpointDnsRecord (org.platformlayer.ops.dns.EndpointDnsRecord)1 Tagger (org.platformlayer.ops.tagger.Tagger)1