Search in sources :

Example 1 with PublicEndpointBase

use of org.platformlayer.core.model.PublicEndpointBase in project platformlayer by platformlayer.

the class OwnedEndpoint method buildItemTemplate.

@Override
protected PublicEndpointBase buildItemTemplate() throws OpsException {
    InstanceBase instance = OpsContext.get().getInstance(InstanceBase.class);
    PlatformLayerKey instanceKey = instance.getKey();
    PublicEndpointBase publicEndpoint = platformLayerCloudHelpers.createPublicEndpoint(instance, parentItem);
    // publicEndpoint.network = network;
    publicEndpoint.publicPort = publicPort;
    publicEndpoint.publicPortCluster = publicPortCluster;
    publicEndpoint.backendPort = backendPort;
    publicEndpoint.instance = instanceKey;
    publicEndpoint.key = PlatformLayerKey.fromId(instance.getId() + "_" + publicPort);
    if (transport != null) {
        publicEndpoint.transport = transport.toString();
    }
    // publicEndpoint.getTags().add(OpsSystem.get().createParentTag(instance));
    Tag uniqueTag = UniqueTag.build(instance, String.valueOf(publicPort));
    publicEndpoint.getTags().add(uniqueTag);
    return publicEndpoint;
}
Also used : PlatformLayerKey(org.platformlayer.core.model.PlatformLayerKey) UniqueTag(org.platformlayer.ops.UniqueTag) Tag(org.platformlayer.core.model.Tag) PublicEndpointBase(org.platformlayer.core.model.PublicEndpointBase) InstanceBase(org.platformlayer.core.model.InstanceBase)

Example 2 with PublicEndpointBase

use of org.platformlayer.core.model.PublicEndpointBase in project platformlayer by platformlayer.

the class PlatformLayerCloudHelpers method createPublicEndpoint.

public PublicEndpointBase createPublicEndpoint(InstanceBase instance, PlatformLayerKey parent) throws OpsException {
    MachineProvider cloudController = getCloud(instance.cloud);
    PublicEndpointBase endpoint = cloudController.buildEndpointTemplate();
    if (parent != null) {
        endpoint.getTags().add(Tag.buildParentTag(parent));
    }
    return endpoint;
// throw new UnsupportedOperationException();
}
Also used : PublicEndpointBase(org.platformlayer.core.model.PublicEndpointBase)

Example 3 with PublicEndpointBase

use of org.platformlayer.core.model.PublicEndpointBase 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);
        }
    }
}
Also used : PlatformLayerClientException(org.platformlayer.PlatformLayerClientException) EndpointInfo(org.platformlayer.core.model.EndpointInfo) OpsException(org.platformlayer.ops.OpsException) Tag(org.platformlayer.core.model.Tag) PublicEndpointBase(org.platformlayer.core.model.PublicEndpointBase) DnsRecord(org.platformlayer.dns.model.DnsRecord) Handler(org.platformlayer.ops.Handler)

Example 4 with PublicEndpointBase

use of org.platformlayer.core.model.PublicEndpointBase 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

PublicEndpointBase (org.platformlayer.core.model.PublicEndpointBase)4 EndpointInfo (org.platformlayer.core.model.EndpointInfo)2 Tag (org.platformlayer.core.model.Tag)2 OpsException (org.platformlayer.ops.OpsException)2 PlatformLayerClientException (org.platformlayer.PlatformLayerClientException)1 InstanceBase (org.platformlayer.core.model.InstanceBase)1 PlatformLayerKey (org.platformlayer.core.model.PlatformLayerKey)1 TagChanges (org.platformlayer.core.model.TagChanges)1 DnsRecord (org.platformlayer.dns.model.DnsRecord)1 Handler (org.platformlayer.ops.Handler)1 OpsProvider (org.platformlayer.ops.OpsProvider)1 UniqueTag (org.platformlayer.ops.UniqueTag)1 EndpointDnsRecord (org.platformlayer.ops.dns.EndpointDnsRecord)1 Tagger (org.platformlayer.ops.tagger.Tagger)1