use of cz.metacentrum.perun.openapi.PerunRPC in project perun by CESNET.
the class PerunCLI method call.
private static void call(PerunCommand command, String[] cliArgs) throws ParseException {
// prepare CLI options
// first options common to all commands
Options options = new Options();
options.addOption(Option.builder(PERUN_URL_OPTION).required(false).hasArg().longOpt(PERUN_URL_VARIABLE).desc("Perun base URL").build());
options.addOption(Option.builder(PERUN_USER_OPTION).required(false).hasArg().longOpt(PERUN_USER_VARIABLE).desc("HTTP Basic Auth user/password").build());
options.addOption(Option.builder(DEBUG_OPTION).required(false).hasArg(false).longOpt("debug").desc("debugging output").build());
options.addOption(Option.builder(HELP_OPTION).required(false).hasArg(false).longOpt("help").desc("print options").build());
// then options specific to the command
command.addOptions(options);
// parse options
CommandLine commandLine;
try {
commandLine = new DefaultParser().parse(options, cliArgs);
} catch (MissingOptionException ex) {
printHelp(command, options);
return;
}
if (commandLine.hasOption(HELP_OPTION)) {
printHelp(command, options);
return;
}
if (commandLine.hasOption(DEBUG_OPTION)) {
LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
ch.qos.logback.classic.Logger rootLogger = loggerContext.getLogger(Logger.ROOT_LOGGER_NAME);
rootLogger.setLevel(Level.DEBUG);
}
// find URL
String perunUrl = System.getenv(PERUN_URL_VARIABLE);
if (commandLine.hasOption(PERUN_URL_OPTION)) {
perunUrl = commandLine.getOptionValue(PERUN_URL_OPTION);
}
if (perunUrl == null)
perunUrl = "https://perun.cesnet.cz/krb/rpc";
// find user and password
String user = System.getenv(PERUN_USER_VARIABLE);
if (commandLine.hasOption(PERUN_USER_OPTION)) {
user = commandLine.getOptionValue(PERUN_USER_OPTION);
}
PerunRPC perunRPC;
if (user == null) {
perunRPC = new PerunRPC(perunUrl, null, null, new KerberosRestTemplate(null, "-"));
} else {
int slash = user.indexOf('/');
if (slash == -1) {
System.err.println("the username and password must be separated by the '/' character");
System.exit(1);
}
String username = user.substring(0, slash);
String password = user.substring(slash + 1);
perunRPC = new PerunRPC(perunUrl, username, password);
}
// execute the command
HttpClientErrorException hce = null;
try {
command.executeCommand(new CommandContext(perunRPC, commandLine));
} catch (HttpClientErrorException e1) {
// normal RestTemplate throws this exception on status 400
hce = e1;
} catch (RestClientException e2) {
if (e2.getCause() instanceof HttpClientErrorException) {
// KerberosRestTemplate throws the exception wrapped
hce = (HttpClientErrorException) e2.getCause();
} else {
// something other went wrong
throw e2;
}
}
if (hce != null) {
PerunException pe = PerunException.to(hce);
System.err.println(pe.getMessage());
System.exit(1);
}
}
use of cz.metacentrum.perun.openapi.PerunRPC in project perun by CESNET.
the class GetExpirationByExtLogin method executeCommand.
@Override
public void executeCommand(PerunCLI.CommandContext ctx) {
String extSourceName = ctx.getCommandLine().getOptionValue("E");
Integer voId = getVoId(ctx, true);
String filename = ctx.getCommandLine().getOptionValue("f");
try {
PerunRPC perunRPC = ctx.getPerunRPC();
for (String login : Files.readAllLines(Paths.get(filename))) {
Member member = perunRPC.getMembersManager().getMemberByExtSourceNameAndExtLogin(voId, login, extSourceName);
Attribute attr = perunRPC.getAttributesManager().getMemberAttributeByName(member.getId(), "urn:perun:member:attribute-def:def:membershipExpiration");
String expiration = (String) attr.getValue();
System.out.println(login + ";" + expiration);
}
} catch (IOException e) {
System.err.println("file " + filename + " cannot be read");
System.exit(1);
}
}
use of cz.metacentrum.perun.openapi.PerunRPC in project perun by CESNET.
the class SetFacilityHostsAttributeValue method executeCommand.
@Override
public void executeCommand(PerunCLI.CommandContext ctx) {
int facilityId = this.getFacilityId(ctx, true);
int attributeDefinitionId = this.getAttributeDefinitionId(ctx, true);
String value = ctx.getCommandLine().getOptionValue("w");
Object attributeValue;
if (value.matches("\\d+")) {
attributeValue = Integer.valueOf(value);
} else {
attributeValue = value;
}
PerunRPC perunRPC = ctx.getPerunRPC();
AttributesManagerApi attributesManager = perunRPC.getAttributesManager();
List<Host> hosts = perunRPC.getFacilitiesManager().getHosts(facilityId);
for (Host host : hosts) {
Attribute attribute = attributesManager.getHostAttributeById(host.getId(), attributeDefinitionId);
attribute.setValue(attributeValue);
System.out.println("setting attribute on host " + host.getHostname() + " to " + attributeValue + " ...");
attributesManager.setHostAttribute(new InputSetHostAttribute().host(host.getId()).attribute(attribute));
}
System.out.println("OK");
}
Aggregations