Search in sources :

Example 1 with PerunRPC

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);
    }
}
Also used : Options(org.apache.commons.cli.Options) HttpClientErrorException(org.springframework.web.client.HttpClientErrorException) PerunException(cz.metacentrum.perun.openapi.PerunException) LoggerContext(ch.qos.logback.classic.LoggerContext) PerunRPC(cz.metacentrum.perun.openapi.PerunRPC) CommandLine(org.apache.commons.cli.CommandLine) KerberosRestTemplate(org.springframework.security.kerberos.client.KerberosRestTemplate) RestClientException(org.springframework.web.client.RestClientException) MissingOptionException(org.apache.commons.cli.MissingOptionException) DefaultParser(org.apache.commons.cli.DefaultParser)

Example 2 with PerunRPC

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);
    }
}
Also used : PerunRPC(cz.metacentrum.perun.openapi.PerunRPC) Attribute(cz.metacentrum.perun.openapi.model.Attribute) IOException(java.io.IOException) Member(cz.metacentrum.perun.openapi.model.Member)

Example 3 with PerunRPC

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");
}
Also used : PerunRPC(cz.metacentrum.perun.openapi.PerunRPC) AttributesManagerApi(cz.metacentrum.perun.openapi.AttributesManagerApi) Attribute(cz.metacentrum.perun.openapi.model.Attribute) InputSetHostAttribute(cz.metacentrum.perun.openapi.model.InputSetHostAttribute) Host(cz.metacentrum.perun.openapi.model.Host) InputSetHostAttribute(cz.metacentrum.perun.openapi.model.InputSetHostAttribute)

Aggregations

PerunRPC (cz.metacentrum.perun.openapi.PerunRPC)3 Attribute (cz.metacentrum.perun.openapi.model.Attribute)2 LoggerContext (ch.qos.logback.classic.LoggerContext)1 AttributesManagerApi (cz.metacentrum.perun.openapi.AttributesManagerApi)1 PerunException (cz.metacentrum.perun.openapi.PerunException)1 Host (cz.metacentrum.perun.openapi.model.Host)1 InputSetHostAttribute (cz.metacentrum.perun.openapi.model.InputSetHostAttribute)1 Member (cz.metacentrum.perun.openapi.model.Member)1 IOException (java.io.IOException)1 CommandLine (org.apache.commons.cli.CommandLine)1 DefaultParser (org.apache.commons.cli.DefaultParser)1 MissingOptionException (org.apache.commons.cli.MissingOptionException)1 Options (org.apache.commons.cli.Options)1 KerberosRestTemplate (org.springframework.security.kerberos.client.KerberosRestTemplate)1 HttpClientErrorException (org.springframework.web.client.HttpClientErrorException)1 RestClientException (org.springframework.web.client.RestClientException)1