Search in sources :

Example 46 with ItemBase

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

Example 47 with ItemBase

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

the class ProviderHelper method listItemsProviding.

public <T> List<ProviderOf<T>> listItemsProviding(Class<T> serviceClass) throws OpsException {
    List<ProviderOf<T>> providers = Lists.newArrayList();
    List<ModelClass<?>> models = findModelsProviding(serviceClass);
    for (ModelClass<? extends ItemBase> model : models) {
        for (ItemBase item : platformLayer.listItems(model.getJavaClass())) {
            providers.add(new ProviderOf<T>(model, item));
        }
    }
    return providers;
}
Also used : ModelClass(org.platformlayer.xaas.services.ModelClass) ItemBase(org.platformlayer.core.model.ItemBase)

Example 48 with ItemBase

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

the class CreationValidator method validateCreateItem.

public void validateCreateItem(ItemBase item) throws OpsException {
    // Object model;
    // try {
    // model = managed.getModel(); // Throws if not valid XML
    // } catch (Exception e) {
    // throw new OpsException("Invalid model", e);
    // }
    Class<? extends ItemBase> modelClass = item.getClass();
    SingletonService singletonServiceAnnotation = modelClass.getAnnotation(SingletonService.class);
    if (singletonServiceAnnotation != null) {
        // Only one can be created per scope
        Iterable<? extends ItemBase> items = platformLayer.listItems(modelClass);
        List<PlatformLayerKey> matches = Lists.newArrayList();
        for (ItemBase peer : items) {
            switch(peer.getState()) {
                case ACTIVE:
                case CREATION_REQUESTED:
                case BUILD:
                case BUILD_ERROR:
                    matches.add(peer.getKey());
                    break;
                case DELETE_REQUESTED:
                case DELETED:
                    break;
                default:
                    throw new IllegalStateException();
            }
        }
        if (!matches.isEmpty()) {
            throw new OpsException("Cannot create multiple instances of: " + modelClass.getName() + ".  Others=" + Joiner.on(",").join(matches));
        }
    }
}
Also used : SingletonService(org.platformlayer.xaas.SingletonService) ItemBase(org.platformlayer.core.model.ItemBase) PlatformLayerKey(org.platformlayer.core.model.PlatformLayerKey)

Example 49 with ItemBase

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

the class MetricsResource method getMetrics.

@POST
@Produces({ XML, JSON })
public MetricDataSource getMetrics(final MetricQuery query) throws RepositoryException, OpsException {
    final ItemBase managedItem = getManagedItem();
    final ServiceProvider serviceProvider = getServiceProvider();
    OpsContextBuilder opsContextBuilder = objectInjector.getInstance(OpsContextBuilder.class);
    final OpsContext opsContext = opsContextBuilder.buildTemporaryOpsContext(getServiceType(), getProjectAuthorization());
    return OpsContext.runInContext(opsContext, new CheckedCallable<MetricDataSource, Exception>() {

        @Override
        public MetricDataSource call() throws Exception {
            BindingScope bindingScope = BindingScope.push(managedItem, managedItem);
            try {
                MetricDataSource metrics = serviceProvider.getMetricValues(managedItem, query);
                return metrics;
            } finally {
                bindingScope.pop();
            }
        }
    });
}
Also used : MetricDataSource(org.platformlayer.metrics.model.MetricDataSource) ItemBase(org.platformlayer.core.model.ItemBase) ServiceProvider(org.platformlayer.xaas.services.ServiceProvider) OpsContext(org.platformlayer.ops.OpsContext) RepositoryException(org.platformlayer.RepositoryException) OpsException(org.platformlayer.ops.OpsException) OpsContextBuilder(org.platformlayer.ops.tasks.OpsContextBuilder) BindingScope(org.platformlayer.ops.BindingScope) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces)

Example 50 with ItemBase

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

the class PlatformlayerBackedPool method findAssigned.

@Override
public T findAssigned(PlatformLayerKey holder) throws OpsException {
    // Tag assignedTag = Tag.ASSIGNED_TO.build(holder);
    //
    // Filter filter = Filter.and(TagFilter.byParent(resourceKey), TagFilter.byTag(assignedTag));
    //
    // List<T> assigned = platformLayer.listItems(childType, filter);
    // if (assigned == null || assigned.isEmpty()) {
    // return null;
    // }
    ItemBase item = platformLayer.getItem(resourceKey);
    List<Assignment> assignments = findAssignments(item);
    String findOwner = holder.getUrl();
    List<String> keys = Lists.newArrayList();
    for (Assignment assignment : assignments) {
        if (findOwner.equals(assignment.owner)) {
            keys.add(assignment.item);
        }
    }
    if (keys.size() == 0) {
        return null;
    }
    if (keys.size() == 1) {
        return adapter.toItem(keys.get(0));
    }
    throw new OpsException("Found multiple assignments to: " + holder);
}
Also used : OpsException(org.platformlayer.ops.OpsException) ItemBase(org.platformlayer.core.model.ItemBase)

Aggregations

ItemBase (org.platformlayer.core.model.ItemBase)56 OpsException (org.platformlayer.ops.OpsException)20 PlatformLayerKey (org.platformlayer.core.model.PlatformLayerKey)14 RepositoryException (org.platformlayer.RepositoryException)11 OpsContext (org.platformlayer.ops.OpsContext)11 Tag (org.platformlayer.core.model.Tag)8 BindingScope (org.platformlayer.ops.BindingScope)7 Machine (org.platformlayer.ops.Machine)7 DatabaseServer (org.platformlayer.ops.databases.DatabaseServer)6 ServiceProvider (org.platformlayer.xaas.services.ServiceProvider)6 Produces (javax.ws.rs.Produces)5 List (java.util.List)4 GET (javax.ws.rs.GET)4 TagChanges (org.platformlayer.core.model.TagChanges)4 OpsTarget (org.platformlayer.ops.OpsTarget)4 NetworkPoint (org.platformlayer.ops.networks.NetworkPoint)4 JAXBException (javax.xml.bind.JAXBException)3 Filter (org.platformlayer.Filter)3 InetAddressChooser (org.platformlayer.InetAddressChooser)3 StateFilter (org.platformlayer.StateFilter)3