Search in sources :

Example 1 with InputError

use of org.rundeck.client.tool.InputError in project rundeck-cli by rundeck.

the class ACLs method performACLModify.

/**
 * Upload a file to create/modify an ACLPolicy
 *
 * @param options file options
 * @param func    create the request
 * @param rdTool  rdTool
 * @param output  output
 *
 * @return result policy
 */
public static ACLPolicy performACLModify(final ACLFileOptions options, BiFunction<RequestBody, RundeckApi, Call<ACLPolicy>> func, final RdTool rdTool, final CommandOutput output) throws IOException, InputError {
    File input = options.getFile();
    if (!input.canRead() || !input.isFile()) {
        throw new InputError(String.format("File is not readable or does not exist: %s", input));
    }
    RequestBody requestBody = RequestBody.create(Client.MEDIA_TYPE_YAML, input);
    ServiceClient.WithErrorResponse<ACLPolicy> execute = rdTool.apiWithErrorResponseDowngradable(api -> func.apply(requestBody, api));
    checkValidationError(output, rdTool.getClient(), execute, input.getAbsolutePath(), rdTool.getAppConfig().isAnsiEnabled());
    return rdTool.getClient().checkError(execute);
}
Also used : ServiceClient(org.rundeck.client.util.ServiceClient) InputError(org.rundeck.client.tool.InputError) ACLPolicy(org.rundeck.client.api.model.ACLPolicy) File(java.io.File) RequestBody(okhttp3.RequestBody)

Example 2 with InputError

use of org.rundeck.client.tool.InputError in project rundeck-cli by rundeck.

the class Archives method export.

@Command(description = "Export a project archive")
public boolean export(ArchiveExportOpts opts, CommandOutput output) throws IOException, InputError {
    if (opts.isIncludeFlags() && opts.isExecutionIds()) {
        throw new InputError("Cannot use --execids/-e with --include/-i");
    }
    boolean apiv19 = getClient().getApiVersion() >= 19;
    Set<Flags> includeFlags = opts.isIncludeFlags() ? opts.getIncludeFlags() : new HashSet<>();
    if (!opts.isIncludeFlags()) {
        includeFlags.add(Flags.all);
    }
    String project = projectOrEnv(opts);
    if (!apiv19) {
        if (opts.isIncludeFlags() && includeFlags.size() > 1) {
            throw new InputError("Cannot use --include: " + includeFlags + " with API < 19");
        }
        if (opts.isIncludeFlags() && !includeFlags.contains(Flags.all)) {
            throw new InputError("Cannot use --include: " + includeFlags + " with API < 19");
        }
        output.info(String.format("Export Archive for project: %s", project));
        if (opts.isExecutionIds()) {
            output.info(String.format("Contents: only execution IDs: %s", opts.getExecutionIds()));
        } else {
            output.info("Contents: all");
        }
        output.info("Begin synchronous request...");
        // sync
        receiveArchiveFile(output, apiCall(api -> api.exportProject(project, opts.getExecutionIds())), opts.getFile());
        return true;
    }
    output.info(String.format("Export Archive for project: %s", project));
    if (opts.isExecutionIds()) {
        output.info(String.format("Contents: only execution IDs: %s", opts.getExecutionIds()));
    } else {
        output.info(String.format("Contents: %s", opts.getIncludeFlags()));
    }
    output.info("Begin asynchronous request...");
    ProjectExportStatus status;
    if (opts.isExecutionIds()) {
        status = apiCall(api -> api.exportProjectAsync(project, opts.getExecutionIds()));
    } else {
        status = apiCall(api -> api.exportProjectAsync(project, includeFlags.contains(Flags.all), includeFlags.contains(Flags.jobs), includeFlags.contains(Flags.executions), includeFlags.contains(Flags.configs), includeFlags.contains(Flags.readmes), includeFlags.contains(Flags.acls), includeFlags.contains(Flags.scm)));
    }
    return loopStatus(getClient(), status, project, opts.getFile(), output, () -> {
        try {
            Thread.sleep(2000);
            return true;
        } catch (InterruptedException e) {
            return false;
        }
    });
}
Also used : Command(org.rundeck.toolbelt.Command) CommandOutput(org.rundeck.toolbelt.CommandOutput) CommandLineInterface(com.lexicalscope.jewel.cli.CommandLineInterface) ProjectExportStatus(org.rundeck.client.api.model.ProjectExportStatus) ProjectImportStatus(org.rundeck.client.api.model.ProjectImportStatus) Util(org.rundeck.client.util.Util) Option(com.lexicalscope.jewel.cli.Option) AppCommand(org.rundeck.client.tool.commands.AppCommand) ProjectNameOptions(org.rundeck.client.tool.options.ProjectNameOptions) FileOutputStream(java.io.FileOutputStream) Set(java.util.Set) Client(org.rundeck.client.util.Client) IOException(java.io.IOException) File(java.io.File) RequestBody(okhttp3.RequestBody) BooleanSupplier(java.util.function.BooleanSupplier) HashSet(java.util.HashSet) InputError(org.rundeck.client.tool.InputError) RdApp(org.rundeck.client.tool.RdApp) List(java.util.List) ServiceClient(org.rundeck.client.util.ServiceClient) RundeckApi(org.rundeck.client.api.RundeckApi) ResponseBody(okhttp3.ResponseBody) InputStream(java.io.InputStream) InputError(org.rundeck.client.tool.InputError) ProjectExportStatus(org.rundeck.client.api.model.ProjectExportStatus) Command(org.rundeck.toolbelt.Command) AppCommand(org.rundeck.client.tool.commands.AppCommand)

Example 3 with InputError

use of org.rundeck.client.tool.InputError in project rundeck-cli by rundeck.

the class Readme method put.

@Command(description = "set project readme/motd file")
public void put(SetOptions options, CommandOutput output) throws IOException, InputError {
    if (!options.isText() && !options.isFile()) {
        throw new InputError("-f/--file or -t/--text is required");
    }
    RequestBody requestBody;
    if (options.isFile()) {
        requestBody = RequestBody.create(MediaType.parse("text/plain"), options.getFile());
    } else {
        requestBody = RequestBody.create(MediaType.parse("text/plain"), options.getText());
    }
    String project = projectOrEnv(options);
    ProjectReadme readme = apiCall(api -> api.putReadme(project, getReadmeFile(options), requestBody));
    output.output(readme.getContents());
}
Also used : ProjectReadme(org.rundeck.client.api.model.ProjectReadme) InputError(org.rundeck.client.tool.InputError) RequestBody(okhttp3.RequestBody) Command(org.rundeck.toolbelt.Command) AppCommand(org.rundeck.client.tool.commands.AppCommand)

Example 4 with InputError

use of org.rundeck.client.tool.InputError in project rundeck-cli by rundeck.

the class AppCommand method projectOrEnv.

/**
 * @param options project options
 * @return project name from options or ENV
 * @throws InputError if project is not set via options or ENV
 */
public String projectOrEnv(final ProjectInput options) throws InputError {
    if (null != options.getProject()) {
        return options.getProject();
    }
    try {
        String rd_project = getAppConfig().require("RD_PROJECT", "or specify as `-p/--project value` : Project name.");
        Pattern pat = Pattern.compile(PROJECT_NAME_PATTERN);
        if (!pat.matcher(rd_project).matches()) {
            throw new InputError(String.format("Cannot match (%s) to pattern: /%s/ : RD_PROJECT", rd_project, PROJECT_NAME_PATTERN));
        }
        return rd_project;
    } catch (ConfigSource.ConfigSourceError configSourceError) {
        throw new InputError(configSourceError.getMessage());
    }
}
Also used : ConfigSource(org.rundeck.client.util.ConfigSource) Pattern(java.util.regex.Pattern) InputError(org.rundeck.client.tool.InputError)

Example 5 with InputError

use of org.rundeck.client.tool.InputError in project rundeck-cli by rundeck.

the class Jobs method getJobList.

/* Bulk toggle execution */
private List<String> getJobList(BulkJobActionOptions options) throws InputError, IOException {
    // if id,idlist specified, use directly
    // otherwise query for the list and assemble the ids
    List<String> ids = new ArrayList<>();
    if (options.isIdlist()) {
        ids = Arrays.asList(options.getIdlist().split("\\s*,\\s*"));
    } else {
        if (!options.isJob() && !options.isGroup() && !options.isGroupExact() && !options.isJobExact()) {
            throw new InputError("must specify -i, or -j/-g/-J/-G to specify jobs to enable.");
        }
        String project = projectOrEnv(options);
        List<JobItem> body = apiCall(api -> api.listJobs(project, options.getJob(), options.getGroup(), options.getJobExact(), options.getGroupExact()));
        for (JobItem jobItem : body) {
            ids.add(jobItem.getId());
        }
    }
    return ids;
}
Also used : InputError(org.rundeck.client.tool.InputError) ArrayList(java.util.ArrayList) ScheduledJobItem(org.rundeck.client.api.model.scheduler.ScheduledJobItem) ForecastJobItem(org.rundeck.client.api.model.scheduler.ForecastJobItem)

Aggregations

InputError (org.rundeck.client.tool.InputError)25 Command (org.rundeck.toolbelt.Command)19 File (java.io.File)12 RequestBody (okhttp3.RequestBody)12 IOException (java.io.IOException)9 CommandOutput (org.rundeck.toolbelt.CommandOutput)9 RdApp (org.rundeck.client.tool.RdApp)8 CommandLineInterface (com.lexicalscope.jewel.cli.CommandLineInterface)6 Option (com.lexicalscope.jewel.cli.Option)6 AppCommand (org.rundeck.client.tool.commands.AppCommand)6 List (java.util.List)4 ResponseBody (okhttp3.ResponseBody)4 RundeckApi (org.rundeck.client.api.RundeckApi)4 Format (org.rundeck.client.util.Format)4 Util (org.rundeck.client.util.Util)4 ArrayList (java.util.ArrayList)3 Function (java.util.function.Function)3 Collectors (java.util.stream.Collectors)3 JobFileUploadResult (org.rundeck.client.api.model.JobFileUploadResult)3 RdTool (org.rundeck.client.tool.extension.RdTool)3