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;
}
}
}
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;
}
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));
}
}
}
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();
}
}
});
}
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);
}
Aggregations