use of com.bluenimble.platform.api.DescribeOption in project serverless by bluenimble.
the class ApiImpl method describe.
@Override
public JsonObject describe(final DescribeOption... options) {
if (options == null || options.length == 0) {
return JsonObject.Blank;
}
final Map<DescribeOption.Option, DescribeOption> opts = DescribeUtils.toMap(options);
JsonObject describe = new JsonObject();
if (opts.containsKey(DescribeOption.Option.info)) {
describe.set(Api.Spec.Namespace, getNamespace());
describe.set(Api.Spec.Name, getName());
describe.set(Api.Spec.Description, getDescription());
describe.set(Api.Spec.Status, status().name());
if (failure != null) {
describe.set(Describe.Failure, failure);
}
describe.set(Api.Spec.Release, getRelease());
}
if (getSecurity() != null && opts.containsKey(DescribeOption.Option.security)) {
describe.set(Api.Spec.Security.class.getSimpleName().toLowerCase(), getSecurity().duplicate());
}
if (getTracking() != null && opts.containsKey(DescribeOption.Option.tracking)) {
describe.set(Api.Spec.Tracking.class.getSimpleName().toLowerCase(), getTracking().duplicate());
}
if (getFeatures() != null && opts.containsKey(DescribeOption.Option.features)) {
describe.set(Api.Spec.Features, getFeatures().duplicate());
}
if (getCustom() != null && opts.containsKey(DescribeOption.Option.custom)) {
describe.set(Api.Spec.Custom, getCustom().duplicate());
}
if (servicesManager.isEmpty(null)) {
return describe;
}
final JsonObject failedServices = new JsonObject();
describe.set(Describe.FailedServices, failedServices);
JsonArray aServices = new JsonArray();
describe.set(Describe.Services, aServices);
final JsonArray fServices = aServices;
final ValueHolder<Integer> apiMarkers = new ValueHolder<Integer>(0);
servicesManager.list(new Selector() {
@Override
public boolean select(ApiService service) {
if (opts.containsKey(DescribeOption.Option.services)) {
DescribeOption opt = opts.get(DescribeOption.Option.services);
JsonObject jService = service.toJson();
JsonObject sDesc = jService;
if (!opt.isVerbose()) {
sDesc = new JsonObject();
sDesc.set(ApiService.Spec.Name, jService.get(ApiService.Spec.Name));
sDesc.set(ApiService.Spec.Endpoint, jService.get(ApiService.Spec.Endpoint));
sDesc.set(ApiService.Spec.Verb, jService.get(ApiService.Spec.Verb));
sDesc.set(ApiService.Spec.Status, jService.get(ApiService.Spec.Status));
sDesc.set(ApiService.Spec.Security.class.getSimpleName().toLowerCase(), service.getSecurity());
JsonArray aMarkers = Json.getArray(jService, Api.Spec.Markers);
int markers = aMarkers != null ? aMarkers.size() : 0;
apiMarkers.set(apiMarkers.get() + markers);
if (markers > 0) {
sDesc.set(Api.Spec.Markers, markers);
}
}
fServices.add(sDesc);
}
if (ApiStatus.Failed.equals(service.status())) {
failedServices.put(service.getVerb().name() + Lang.SPACE + Json.getString(service.toJson(), ApiService.Spec.Endpoint), service.getFailure());
}
return false;
}
});
describe.set(Api.Spec.Markers, apiMarkers.get());
if (fServices.isEmpty()) {
describe.remove(Describe.Services);
}
if (failedServices.isEmpty()) {
describe.remove(Describe.FailedServices);
}
return describe;
}
use of com.bluenimble.platform.api.DescribeOption in project serverless by bluenimble.
the class ApiSpaceImpl method describe.
@Override
public JsonObject describe(DescribeOption... options) {
if (options == null || options.length == 0) {
return JsonObject.Blank;
}
Map<DescribeOption.Option, DescribeOption> opts = DescribeUtils.toMap(options);
JsonObject describe = new JsonObject();
if (opts.containsKey(DescribeOption.Option.info)) {
describe.set(ApiSpace.Spec.Namespace, getNamespace());
describe.set(ApiSpace.Spec.Name, getName());
describe.set(ApiSpace.Spec.Description, getDescription());
describe.set(Describe.Status, isStarted() ? ApiStatus.Running.name() : ApiStatus.Stopped.name());
describe.set(ApiSpace.Spec.Blocked, isBlocked());
if (opts.size() == 1) {
return describe;
}
}
descriptor = descriptor.duplicate();
if (opts.containsKey(DescribeOption.Option.keys) && keystore != null) {
List<KeyPair> keys = null;
try {
keys = keystore.list(0, 100);
} catch (SpaceKeyStoreException e) {
tracer.log(Tracer.Level.Error, Lang.BLANK, e);
}
JsonArray aKeys = new JsonArray();
if (keys != null) {
for (KeyPair kp : keys) {
JsonObject okp = kp.toJson().duplicate();
okp.remove(KeyPair.Fields.SecretKey);
aKeys.add(okp);
}
}
describe.set(DescribeOption.Option.keys.name(), aKeys);
}
if (opts.containsKey(DescribeOption.Option.secrets)) {
describe.set(DescribeOption.Option.secrets.name(), descriptor.get(Spec.secrets.class.getSimpleName()));
}
if (opts.containsKey(DescribeOption.Option.features)) {
describe.set(DescribeOption.Option.features.name(), descriptor.get(Spec.Features));
}
if (opts.containsKey(DescribeOption.Option.runtime)) {
describe.set(DescribeOption.Option.runtime.name(), descriptor.get(RuntimeKey));
}
if (opts.containsKey(DescribeOption.Option.apis)) {
final JsonArray aApis = new JsonArray();
describe.set(DescribeOption.Option.apis.name(), aApis);
list(new Selector() {
@Override
public boolean select(Api api) {
aApis.add(api.describe(options));
return false;
}
});
}
if (opts.containsKey(DescribeOption.Option.workers) && executor != null) {
describe.set(DescribeOption.Option.workers.name(), executor.describe());
}
return describe;
}
use of com.bluenimble.platform.api.DescribeOption in project serverless by bluenimble.
the class AbstractApiServer method describe.
@Override
public JsonObject describe(DescribeOption... options) {
if (options == null || options.length == 0) {
return JsonObject.Blank;
}
Map<DescribeOption.Option, DescribeOption> opts = DescribeUtils.toMap(options);
JsonObject describe = new JsonObject();
if (opts.containsKey(DescribeOption.Option.info)) {
describe.set(ConfigKeys.Name, Json.getString(descriptor, ConfigKeys.Name));
describe.set(ConfigKeys.Description, Json.getString(descriptor, ConfigKeys.Description));
describe.set(ConfigKeys.Version, Json.getString(descriptor, ConfigKeys.Version));
}
if (opts.containsKey(DescribeOption.Option.keys)) {
JsonObject okeys = keys.toJson().duplicate();
okeys.remove(KeyPair.Fields.SecretKey);
describe.set(DescribeOption.Option.keys.name(), okeys);
}
if (instanceDescriber != null && opts.containsKey(DescribeOption.Option.hardware)) {
describe.set(DescribeOption.Option.hardware.name(), instanceDescriber.describe());
}
// plugins
if (opts.containsKey(DescribeOption.Option.plugins)) {
JsonArray aPlugins = new JsonArray();
describe.set(DescribeOption.Option.plugins.name(), aPlugins);
Iterator<String> pNames = pluginsRegistry.getNames();
while (pNames.hasNext()) {
String pName = pNames.next();
Plugin plugin = pluginsRegistry.lockup(pName);
JsonObject oPlugin = new JsonObject();
oPlugin.set(ConfigKeys.Namespace, plugin.getName());
oPlugin.set(ConfigKeys.Name, plugin.getTitle());
oPlugin.set(ConfigKeys.Description, plugin.getDescription());
oPlugin.set(ConfigKeys.Version, plugin.getVersion());
oPlugin.set(ConfigKeys.Vendor, plugin.getVendor());
aPlugins.add(oPlugin);
}
}
// features
if (opts.containsKey(DescribeOption.Option.features)) {
JsonObject oFeatures = new JsonObject();
describe.set(DescribeOption.Option.features.name(), oFeatures);
for (ServerFeature feature : features.values()) {
String name = feature.type().getAnnotation(Feature.class).name();
JsonArray aVendors = Json.getArray(oFeatures, name);
if (aVendors == null) {
aVendors = new JsonArray();
oFeatures.set(name, aVendors);
}
JsonObject oVendor = new JsonObject();
oVendor.set(Describe.Vendor, feature.implementor().getVendor());
aVendors.add(oVendor);
}
}
// spaces
if (opts.containsKey(DescribeOption.Option.spaces)) {
Collection<ApiSpace> spaces = spaces();
if (spaces != null && !spaces.isEmpty()) {
JsonArray aSpaces = new JsonArray();
describe.set(DescribeOption.Option.spaces.name(), aSpaces);
for (ApiSpace space : spaces) {
aSpaces.add(space.describe(options));
}
}
}
return describe;
}
use of com.bluenimble.platform.api.DescribeOption in project serverless by bluenimble.
the class MgmUtils method options.
public static DescribeOption[] options(String spec, DescribeOption _default) {
if (Lang.isNullOrEmpty(spec)) {
if (_default == null) {
return null;
}
return new DescribeOption[] { _default };
}
Set<DescribeOption> options = new HashSet<DescribeOption>();
String[] aOptions = Lang.split(spec, Lang.COMMA, true);
for (String o : aOptions) {
boolean verbose = false;
o = o.toLowerCase();
if (o.endsWith(VerbosePostfix)) {
o = o.substring(0, o.length() - VerbosePostfix.length());
verbose = true;
}
String optName = o;
String offsetAndLength = null;
int indexOfColon = o.indexOf(Lang.COLON);
if (indexOfColon > 0) {
optName = o.substring(0, indexOfColon).trim();
offsetAndLength = o.substring(indexOfColon + 1).trim();
}
DescribeOption.Option option = DescribeOptions.get(optName);
if (option == null) {
continue;
}
int offset = 0;
int length = -1;
if (!Lang.isNullOrEmpty(offsetAndLength)) {
int indexOfDash = o.indexOf(Lang.DASH);
if (indexOfDash > 0) {
try {
offset = Integer.valueOf(offsetAndLength.substring(0, indexOfDash).trim());
} catch (NumberFormatException nfex) {
// IGNORE, default to 0
}
try {
length = Integer.valueOf(offsetAndLength.substring(indexOfDash + 1).trim());
} catch (NumberFormatException nfex) {
// IGNORE, default to -1
}
} else {
try {
length = Integer.valueOf(offsetAndLength);
} catch (NumberFormatException nfex) {
// IGNORE, default to -1
}
}
}
options.add(new DescribeOption(option, verbose, offset, length));
}
DescribeOption[] array = new DescribeOption[options.size()];
options.toArray(array);
return array;
}
Aggregations