use of org.platformlayer.service.cloud.direct.model.DirectHost in project platformlayer by platformlayer.
the class CloudInstanceMapper method doOperation.
@Handler
public void doOperation() throws OpsException, IOException {
Tag tag = Tag.build(Tag.ASSIGNED, instance.getKey().getUrl());
List<DirectHost> hosts = Lists.newArrayList(platformLayer.listItems(DirectHost.class, TagFilter.byTag(tag)));
if (hosts.size() > 1) {
// Huh?
throw new OpsException("Multiple hosts already assigned");
}
DirectHost host;
if (hosts.isEmpty()) {
if (OpsContext.isDelete()) {
host = null;
} else {
if (createInstance) {
DirectCloudHost cloudHost = cloudMap.pickHost(instance);
host = cloudHost.getModel();
platformLayer.addTag(host.getKey(), tag);
} else {
throw new OpsException("Instance not yet assigned");
}
}
} else {
host = hosts.get(0);
}
RecursionState recursion = getRecursionState();
if (host != null) {
this.cloud = platformLayer.getItem(host.cloud, DirectCloud.class);
this.hostTarget = directHelpers.toTarget(host);
recursion.pushChildScope(cloud);
recursion.pushChildScope(host);
recursion.pushChildScope(hostTarget);
} else {
if (!OpsContext.isDelete()) {
throw new IllegalStateException();
}
log.info("No host set; won't recurse in");
recursion.setPreventRecursion(true);
}
}
use of org.platformlayer.service.cloud.direct.model.DirectHost in project platformlayer by platformlayer.
the class DirectCloudUtils method getAddressPool6.
public OpsProvider<ResourcePool<AddressModel>> getAddressPool6() {
OpsProvider<PoolBuilder<AddressModel>> poolBuilder = new OpsProvider<PoolBuilder<AddressModel>>() {
@Override
public PoolBuilder<AddressModel> get() throws OpsException {
DirectHost host = OpsContext.get().getInstance(DirectHost.class);
PlatformLayerKey sharedNetworkKey = getSharedNetworkKey();
// Skip the first entries in the CIDR as it's probably not valid
// 0: Network identifier
// 1: Gateway
// 2: Host
int skipCount = 3;
if (sharedNetworkKey != null) {
DirectNetwork network = platformLayer.getItem(sharedNetworkKey, DirectNetwork.class);
for (AddressModel net : network.getNetworks()) {
if (Strings.isNullOrEmpty(net.cidr)) {
continue;
}
IpRange cidr = IpRange.parse(net.cidr);
if (!cidr.isIpv6()) {
continue;
}
return new NetworkPoolBuilder(net.cidr, skipCount, net);
}
log.warn("Unable to find an IPV6 network configured on " + sharedNetworkKey);
return null;
} else {
String privateCidr = host.ipv6;
if (privateCidr != null) {
return new NetworkPoolBuilder(privateCidr, skipCount, null);
}
return null;
}
}
};
return getNetworkPoolProvider(AddressModel.class, "addresses-ipv6", poolBuilder);
}
use of org.platformlayer.service.cloud.direct.model.DirectHost in project platformlayer by platformlayer.
the class DirectCloudUtils method getPrivateAddressPool4.
public static OpsProvider<ResourcePool<AddressModel>> getPrivateAddressPool4() {
OpsProvider<PoolBuilder<AddressModel>> poolBuilder = new OpsProvider<PoolBuilder<AddressModel>>() {
@Override
public PoolBuilder<AddressModel> get() {
DirectHost host = OpsContext.get().getInstance(DirectHost.class);
String privateCidr = host.ipv4Private;
if (privateCidr != null) {
// Skip the first entries in the CIDR as it's probably not valid
// 0: Network identifier
// 1: Gateway (?)
// 2: Host (?)
int skipCount = 3;
return new NetworkPoolBuilder(privateCidr, skipCount);
}
return null;
}
};
return getUnsharedPrivatePoolProvider(AddressModel.class, "addresses-ipv4-private", poolBuilder);
}
use of org.platformlayer.service.cloud.direct.model.DirectHost in project platformlayer by platformlayer.
the class DirectCloudUtils method getSharedNetworkKey.
private static PlatformLayerKey getSharedNetworkKey() {
DirectCloud cloud = OpsContext.get().getInstance(DirectCloud.class);
DirectHost host = OpsContext.get().getInstance(DirectHost.class);
PlatformLayerKey sharedNetwork = host.network;
if (sharedNetwork == null) {
sharedNetwork = cloud.network;
}
return sharedNetwork;
}
use of org.platformlayer.service.cloud.direct.model.DirectHost in project platformlayer by platformlayer.
the class DirectCloudUtils method getPublicAddressPool4.
public OpsProvider<ResourcePool<InetSocketAddress>> getPublicAddressPool4(final int publicPort, final List<Integer> publicPortGroup) {
OpsProvider<ResourcePool<InetSocketAddress>> pool = new OpsProvider<ResourcePool<InetSocketAddress>>() {
@Override
public ResourcePool<InetSocketAddress> get() throws OpsException {
DirectHost host = OpsContext.get().getInstance(DirectHost.class);
OpsTarget target = OpsContext.get().getInstance(OpsTarget.class);
PlatformLayerKey sharedNetworkKey = getSharedNetworkKey();
// We don't skip here, at the moment.
// We may just need a comma separated list in future...
int skipCount = 0;
if (sharedNetworkKey == null) {
File poolPath = getPoolPath("sockets-ipv4-public");
File resourceDir = new File(poolPath, "all");
File assignedBase = new File(poolPath, "assigned");
File perPortDir = new File(assignedBase, "port" + publicPort);
PoolBuilder<AddressModel> poolBuilder = null;
String ipv4Public = host.ipv4Public;
if (ipv4Public != null) {
poolBuilder = new NetworkPoolBuilder(ipv4Public, skipCount);
}
StaticFilesystemBackedPool<AddressModel> addressPool = new StaticFilesystemBackedPool<AddressModel>(AddressModel.class, poolBuilder, target, resourceDir, perPortDir);
return new AssignPortToAddressPool(addressPool, publicPort);
} else {
throw new UnsupportedOperationException();
// DirectNetwork network = platformLayer.getItem(sharedNetworkKey, DirectNetwork.class);
//
// for (AddressModel net : network.getNetworks()) {
// if (Strings.isNullOrEmpty(net.cidr)) {
// continue;
// }
//
// IpRange cidr = IpRange.parse(net.cidr);
// if (!cidr.isIpv4()) {
// continue;
// }
//
// NetworkPoolBuilder poolBuilder = new NetworkPoolBuilder(net.cidr, skipCount, net);
// PlatformlayerBackedPool<AddressModel> addressPool = new PlatformlayerBackedPool<AddressModel>(
// platformLayer, sharedNetworkKey, AddressModel.class, poolBuilder);
// return new AssignPortToAddressPool(addressPool, publicPort);
// }
//
// log.warn("Unable to find an IPV4 network configured on " + sharedNetworkKey);
// return null;
}
}
};
return pool;
}
Aggregations