Search in sources :

Example 1 with Tagger

use of org.platformlayer.ops.tagger.Tagger in project platformlayer by platformlayer.

the class OpenstackPublicEndpointController method addChildren.

// @Inject
// ImageFactory imageFactory;
// 
@Override
protected void addChildren() throws OpsException {
    final OpenstackPublicEndpoint model = OpsContext.get().getInstance(OpenstackPublicEndpoint.class);
    OpenstackInstance instance = client.getItem(model.instance, OpenstackInstance.class);
    CloudInstanceMapper instanceMapper;
    {
        instanceMapper = injected(CloudInstanceMapper.class);
        instanceMapper.instance = instance;
        addChild(instanceMapper);
    }
    final EnsureFirewallIngress ingress;
    {
        ingress = injected(EnsureFirewallIngress.class);
        ingress.model = model;
        instanceMapper.addChild(ingress);
    }
    {
        OpsProvider<TagChanges> tagChanges = new OpsProvider<TagChanges>() {

            @Override
            public TagChanges get() {
                TagChanges tagChanges = new TagChanges();
                String address = ingress.getPublicAddress();
                if (Strings.isNullOrEmpty(address)) {
                    throw new IllegalStateException();
                }
                EndpointInfo endpoint = new EndpointInfo(address, model.publicPort);
                tagChanges.addTags.add(endpoint.toTag());
                return tagChanges;
            }
        };
        Tagger tagger = injected(Tagger.class);
        tagger.platformLayerKey = model.getKey();
        tagger.tagChangesProvider = tagChanges;
        instanceMapper.addChild(tagger);
        Tagger tagInstance = injected(Tagger.class);
        tagInstance.platformLayerKey = null;
        tagInstance.platformLayerKey = model.instance;
        tagInstance.tagChangesProvider = tagChanges;
        instanceMapper.addChild(tagInstance);
    }
}
Also used : EndpointInfo(org.platformlayer.core.model.EndpointInfo) OpsProvider(org.platformlayer.ops.OpsProvider) OpenstackPublicEndpoint(org.platformlayer.service.cloud.openstack.model.OpenstackPublicEndpoint) Tagger(org.platformlayer.ops.tagger.Tagger) OpenstackInstance(org.platformlayer.service.cloud.openstack.model.OpenstackInstance) TagChanges(org.platformlayer.core.model.TagChanges)

Example 2 with Tagger

use of org.platformlayer.ops.tagger.Tagger in project platformlayer by platformlayer.

the class PersistentInstanceMapper method addChildren.

@Override
protected void addChildren() throws OpsException {
    final PersistentInstance model = OpsContext.get().getInstance(PersistentInstance.class);
    {
        // Add tag with instance id to persistent instance (very helpful for DNS service!)
        Tagger tagger = injected(Tagger.class);
        tagger.platformLayerKey = model.getKey();
        tagger.tagChangesProvider = new OpsProvider<TagChanges>() {

            @Override
            public TagChanges get() throws OpsException {
                Machine machine = OpsContext.get().getInstance(Machine.class);
                if (machine == null) {
                    if (OpsContext.isDelete()) {
                        return null;
                    }
                    throw new OpsException("No machine in scope");
                }
                TagChanges changeTags = new TagChanges();
                changeTags.addTags.add(Tag.INSTANCE_KEY.build(machine.getKey()));
                platformLayer.changeTags(model.getKey(), changeTags, null);
                return changeTags;
            }
        };
        addChild(tagger);
    }
}
Also used : OpsException(org.platformlayer.ops.OpsException) PersistentInstance(org.platformlayer.instances.model.PersistentInstance) OpsProvider(org.platformlayer.ops.OpsProvider) Tagger(org.platformlayer.ops.tagger.Tagger) TagChanges(org.platformlayer.core.model.TagChanges) Machine(org.platformlayer.ops.Machine)

Example 3 with Tagger

use of org.platformlayer.ops.tagger.Tagger in project platformlayer by platformlayer.

the class GoogleCloudPublicEndpointController method addChildren.

@Override
protected void addChildren() throws OpsException {
    final GoogleCloudPublicEndpoint model = OpsContext.get().getInstance(GoogleCloudPublicEndpoint.class);
    GoogleCloudInstance instance = client.getItem(model.instance, GoogleCloudInstance.class);
    CloudInstanceMapper instanceMapper;
    {
        instanceMapper = injected(CloudInstanceMapper.class);
        instanceMapper.instance = instance;
        addChild(instanceMapper);
    }
    final EnsureFirewallIngress ingress;
    {
        ingress = injected(EnsureFirewallIngress.class);
        ingress.model = model;
        instanceMapper.addChild(ingress);
    }
    {
        OpsProvider<TagChanges> tagChanges = new OpsProvider<TagChanges>() {

            @Override
            public TagChanges get() {
                TagChanges tagChanges = new TagChanges();
                String address = ingress.getPublicAddress();
                if (Strings.isNullOrEmpty(address)) {
                    throw new IllegalStateException();
                }
                EndpointInfo endpoint = new EndpointInfo(address, model.publicPort);
                tagChanges.addTags.add(endpoint.toTag());
                return tagChanges;
            }
        };
        Tagger tagger = injected(Tagger.class);
        tagger.platformLayerKey = model.getKey();
        tagger.tagChangesProvider = tagChanges;
        instanceMapper.addChild(tagger);
        Tagger tagInstance = injected(Tagger.class);
        tagInstance.platformLayerKey = null;
        tagInstance.platformLayerKey = model.instance;
        tagInstance.tagChangesProvider = tagChanges;
        instanceMapper.addChild(tagInstance);
    }
}
Also used : EndpointInfo(org.platformlayer.core.model.EndpointInfo) OpsProvider(org.platformlayer.ops.OpsProvider) Tagger(org.platformlayer.ops.tagger.Tagger) GoogleCloudPublicEndpoint(org.platformlayer.service.cloud.google.model.GoogleCloudPublicEndpoint) TagChanges(org.platformlayer.core.model.TagChanges) GoogleCloudInstance(org.platformlayer.service.cloud.google.model.GoogleCloudInstance)

Example 4 with Tagger

use of org.platformlayer.ops.tagger.Tagger 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)

Example 5 with Tagger

use of org.platformlayer.ops.tagger.Tagger 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;
        }
    }
}
Also used : OpsException(org.platformlayer.ops.OpsException) Tagger(org.platformlayer.ops.tagger.Tagger) SocketAddressPoolAssignment(org.platformlayer.ops.pool.SocketAddressPoolAssignment) ItemBase(org.platformlayer.core.model.ItemBase) InetSocketAddress(java.net.InetSocketAddress) PlatformLayerCloudMachine(org.platformlayer.ops.machines.PlatformLayerCloudMachine) DirectInstance(org.platformlayer.service.cloud.direct.model.DirectInstance) TagChanges(org.platformlayer.core.model.TagChanges) EndpointInfo(org.platformlayer.core.model.EndpointInfo) OpsProvider(org.platformlayer.ops.OpsProvider) AddressModel(org.platformlayer.core.model.AddressModel) List(java.util.List) Protocol(org.platformlayer.ops.firewall.Protocol) IptablesForwardPort(org.platformlayer.ops.firewall.scripts.IptablesForwardPort) InetAddress(java.net.InetAddress)

Aggregations

TagChanges (org.platformlayer.core.model.TagChanges)5 OpsProvider (org.platformlayer.ops.OpsProvider)5 Tagger (org.platformlayer.ops.tagger.Tagger)5 EndpointInfo (org.platformlayer.core.model.EndpointInfo)4 OpsException (org.platformlayer.ops.OpsException)3 InetAddress (java.net.InetAddress)1 InetSocketAddress (java.net.InetSocketAddress)1 List (java.util.List)1 AddressModel (org.platformlayer.core.model.AddressModel)1 ItemBase (org.platformlayer.core.model.ItemBase)1 PublicEndpointBase (org.platformlayer.core.model.PublicEndpointBase)1 PersistentInstance (org.platformlayer.instances.model.PersistentInstance)1 Machine (org.platformlayer.ops.Machine)1 EndpointDnsRecord (org.platformlayer.ops.dns.EndpointDnsRecord)1 Protocol (org.platformlayer.ops.firewall.Protocol)1 IptablesForwardPort (org.platformlayer.ops.firewall.scripts.IptablesForwardPort)1 PlatformLayerCloudMachine (org.platformlayer.ops.machines.PlatformLayerCloudMachine)1 SocketAddressPoolAssignment (org.platformlayer.ops.pool.SocketAddressPoolAssignment)1 DirectInstance (org.platformlayer.service.cloud.direct.model.DirectInstance)1 GoogleCloudInstance (org.platformlayer.service.cloud.google.model.GoogleCloudInstance)1