use of org.platformlayer.ops.Handler in project platformlayer by platformlayer.
the class OwnedItem method handler.
@Handler
public void handler() throws OpsException {
T itemTemplate = buildItemTemplate();
Tag uniqueTag = getUniqueTag(itemTemplate);
if (OpsContext.isConfigure()) {
try {
item = platformLayer.putItemByTag(itemTemplate, uniqueTag);
} catch (PlatformLayerClientException e) {
throw new OpsException("Error creating owned item", e);
}
}
if (OpsContext.isDelete()) {
List<? extends ItemBase> items = platformLayer.listItems(itemTemplate.getClass(), TagFilter.byTag(uniqueTag));
if (items.size() != 0) {
if (items.size() != 1) {
throw new OpsException("Found multiple items with unique tag: " + uniqueTag);
}
item = (T) items.get(0);
platformLayer.ensureDeleted(item);
}
}
}
use of org.platformlayer.ops.Handler in project platformlayer by platformlayer.
the class PosixUser method doOperation.
@Handler
public void doOperation() throws OpsException {
OpsTarget target = OpsContext.get().getInstance(OpsTarget.class);
// TODO: Only if user not found
{
Command command = Command.build("adduser");
command.addLiteral("--system");
command.addLiteral("--no-create-home");
if (shell != null) {
command.addLiteral("--shell").addFile(shell);
}
if (!Strings.isNullOrEmpty(primaryGroup)) {
command.addLiteral("--ingroup");
command.addQuoted(primaryGroup);
}
command.addQuoted(userName);
target.executeCommand(command);
}
for (String secondaryGroup : secondaryGroups) {
Command command = Command.build("adduser");
command.addQuoted(userName);
command.addQuoted(secondaryGroup);
target.executeCommand(command);
}
}
use of org.platformlayer.ops.Handler in project platformlayer by platformlayer.
the class TagFromChildren method handler.
@Handler
public void handler() throws OpsException {
if (OpsContext.isConfigure()) {
for (OwnedItem<?> childServer : parentController.getChildren(OwnedItem.class)) {
ItemBase server = childServer.getItem();
if (server == null) {
// Right now, we have to go through a retry cycle
throw new OpsException("Child server not ready");
}
List<EndpointInfo> endpoints = EndpointInfo.findEndpoints(server.getTags(), port);
if (endpoints.isEmpty()) {
// TODO: Cope in future e.g. if we only need one of two in a cluster
throw new OpsException("Child server not ready");
}
for (EndpointInfo endpoint : endpoints) {
platformLayer.addTag(parentItem.getKey(), endpoint.toTag());
}
}
}
}
use of org.platformlayer.ops.Handler in project platformlayer by platformlayer.
the class OperationInvoker method findBetter.
private Method findBetter(Method l, Method r, BindingScope scope) {
Handler lHandler = l.getAnnotation(Handler.class);
Handler rHandler = r.getAnnotation(Handler.class);
if (lHandler != null && rHandler == null) {
return l;
}
if (rHandler != null && lHandler == null) {
return r;
}
if (rHandler != null && lHandler != null) {
Action action = scope.getInstance(Action.class);
if (action != null) {
boolean lExplicit = canHandleAction(lHandler, action, false);
boolean rExplicit = canHandleAction(rHandler, action, false);
if (lExplicit && !rExplicit) {
return l;
}
if (rExplicit && !lExplicit) {
return r;
}
// TODO: How do we get here??
boolean lWildcard = canHandleAction(lHandler, action, true);
boolean rWildcard = canHandleAction(lHandler, action, true);
if (lWildcard && !rWildcard) {
return l;
}
if (rWildcard && !lWildcard) {
return r;
}
} else {
log.warn("No OperationType in scope");
}
}
// Favor the more derived class
Class<?> declaringClassL = l.getDeclaringClass();
Class<?> declaringClassR = r.getDeclaringClass();
if (!declaringClassL.equals(declaringClassR)) {
if (declaringClassL.isAssignableFrom(declaringClassR)) {
// R is derived from L
return r;
}
if (declaringClassR.isAssignableFrom(declaringClassL)) {
// L is derived from R
return l;
}
}
throw new IllegalArgumentException("Cannot compare " + l + " with " + r);
}
use of org.platformlayer.ops.Handler 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);
}
}
}
Aggregations