Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Major API cleanup #170

Merged
merged 5 commits into from
Jun 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
235 changes: 0 additions & 235 deletions src/main/java/com/suse/salt/netapi/client/SaltClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,9 @@
import com.suse.salt.netapi.calls.Client;
import com.suse.salt.netapi.calls.SaltSSHConfig;
import com.suse.salt.netapi.calls.SaltSSHUtils;
import com.suse.salt.netapi.calls.wheel.Key;
import com.suse.salt.netapi.client.impl.HttpClientConnectionFactory;
import com.suse.salt.netapi.config.ClientConfig;
import com.suse.salt.netapi.config.ProxySettings;
import com.suse.salt.netapi.datatypes.Job;
import com.suse.salt.netapi.datatypes.ScheduledJob;
import com.suse.salt.netapi.datatypes.Token;
import com.suse.salt.netapi.datatypes.cherrypy.Stats;
import com.suse.salt.netapi.datatypes.target.Target;
Expand All @@ -21,7 +18,6 @@
import com.suse.salt.netapi.results.Return;
import com.suse.salt.netapi.results.SSHRawResult;
import com.suse.salt.netapi.results.Result;
import com.suse.salt.netapi.results.ResultInfoSet;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
Expand Down Expand Up @@ -200,174 +196,6 @@ public Future<Boolean> logoutAsync() {
return executor.submit(() -> (Boolean) this.logout());
}

/**
* Query for all minions and immediately return a map of minions keyed by minion id.
* <p>
* {@code GET /minions}
*
* @return map containing maps representing minions, keyed by minion id
* @throws SaltException if anything goes wrong
* @see <a href="http://docs.saltstack.com/en/latest/topics/targeting/grains.html">
* Grains</a>
*/
public Map<String, Map<String, Object>> getMinions() throws SaltException {
return connectionFactory.create("/minions", JsonParser.RETMAPS, config)
.getResult().getResult().get(0);
}

/**
* Asynchronously query for all minions and return a map of minions keyed by minion id.
* <p>
* {@code GET /minions}
*
* @return Future with a map containing maps representing minions, keyed by minion id
* @throws SaltException if anything goes wrong
* @see <a href="http://docs.saltstack.com/en/latest/topics/targeting/grains.html">
* Grains</a>
*/
public Future<Map<String, Map<String, Object>>> getMinionsAsync()
throws SaltException {
return executor.submit(this::getMinions);
}

/**
* Query for details (grains) of the specified minion.
* <p>
* {@code GET /minions/<minion-id>}
*
* @param minionId the minion ID
* @return Map key: grain name, value: grain value
* @throws SaltException if anything goes wrong
* @see <a href="http://docs.saltstack.com/en/latest/topics/targeting/grains.html">
* Grains</a>
*/
public Map<String, Object> getMinionDetails(String minionId) throws SaltException {
return connectionFactory.create("/minions/" + minionId, JsonParser.RETMAPS, config)
.getResult().getResult().get(0).get(minionId);
}

/**
* Query for details (grains) of the specified minion asynchronously.
* <p>
* {@code GET /minions/<minion-id>}
*
* @param minionId the minion ID
* @return Future with a map containing details of the minion
* @throws SaltException if anything goes wrong
* @see <a href="http://docs.saltstack.com/en/latest/topics/targeting/grains.html">
* Grains</a>
*/
public Future<Map<String, Object>> getMinionDetailsAsync(final String minionId)
throws SaltException {
return executor.submit(() -> getMinionDetails(minionId));
}

/**
* Generic interface to start any execution command and immediately return an object
* representing the scheduled job.
* <p>
* {@code POST /minions}
*
* @param <T> type of the tgt property for this command
* @param target the target
* @param function the function to execute
* @param args list of non-keyword arguments
* @param kwargs map containing keyword arguments
* @return object representing the scheduled job
* @throws SaltException if anything goes wrong
*/
public <T> ScheduledJob startCommand(final Target<T> target, final String function,
List<Object> args, Map<String, Object> kwargs) throws SaltException {
Map<String, Object> props = new LinkedHashMap<>();
props.putAll(target.getProps());
props.put("fun", function);
props.put("arg", args);
props.put("kwarg", kwargs);

String payload = gson.toJson(Collections.singleton(props));

// Connect to the minions endpoint and send the above lowstate data
Return<List<ScheduledJob>> result = connectionFactory
.create("/minions", JsonParser.SCHEDULED_JOB, config)
.getResult(payload);

// They return a list of tokens here, we take the first
return result.getResult().get(0);
}

/**
* Asynchronously start any execution command and immediately return an object
* representing the scheduled job.
* <p>
* {@code POST /minions}
*
* @param <T> type of the tgt property for this command
* @param target the target
* @param function the function to execute
* @param args list of non-keyword arguments
* @param kwargs map containing keyword arguments
* @return Future containing the scheduled job
*/
public <T> Future<ScheduledJob> startCommandAsync(final Target<T> target,
final String function, final List<Object> args,
final Map<String, Object> kwargs) {
return executor.submit(() -> startCommand(target, function, args, kwargs));
}

/**
* Query for the result of a supplied job.
* <p>
* {@code GET /job/<job-id>}
*
* @param job {@link ScheduledJob} object representing scheduled job
* @return {@link ResultInfoSet} representing result set from minions
* @throws SaltException if anything goes wrong
*/
public ResultInfoSet getJobResult(final ScheduledJob job) throws SaltException {
return getJobResult(job.getJid());
}

/**
* Query for the result of a supplied job.
* <p>
* {@code GET /job/<job-id>}
*
* @param job String representing scheduled job
* @return {@link ResultInfoSet} representing result set from minions
* @throws SaltException if anything goes wrong
*/
public ResultInfoSet getJobResult(final String job) throws SaltException {
return connectionFactory
.create("/jobs/" + job, JsonParser.JOB_RESULTS, config)
.getResult();
}

/**
* Get previously run jobs.
* <p>
* {@code GET /jobs}
*
* @return map containing run jobs keyed by job id
* @throws SaltException if anything goes wrong
*/
public Map<String, Job> getJobs() throws SaltException {
Return<List<Map<String, Job>>> result = connectionFactory
.create("/jobs", JsonParser.JOBS, config)
.getResult();
return result.getResult().get(0);
}

/**
* Asynchronously get previously run jobs.
* <p>
* {@code GET /jobs}
*
* @return Future with a map containing run jobs keyed by job id
*/
public Future<Map<String, Job>> getJobsAsync() {
return executor.submit(this::getJobs);
}

/**
* Generic interface to start any execution command bypassing normal session handling.
* <p>
Expand Down Expand Up @@ -491,34 +319,6 @@ public Future<Stats> statsAsync() {
return executor.submit(this::stats);
}

/**
* Query general key information.
* <p>
* Required permissions: {@code @wheel}
* <p>
* {@code GET /keys}
*
* @return the keys
* @throws SaltException if anything goes wrong
*/
public Key.Names keys() throws SaltException {
return connectionFactory.create("/keys", JsonParser.KEYS, config).getResult()
.getResult();
}

/**
* Asynchronously query general key information.
* <p>
* Required permissions: {@code @wheel}
* <p>
* {@code GET /keys}
*
* @return Future containing the keys
*/
public Future<Key.Names> keysAsync() {
return executor.submit(this::keys);
}

/**
* Returns a WebSocket @ClientEndpoint annotated object connected
* to the /ws ServerEndpoint.
Expand All @@ -541,41 +341,6 @@ public EventStream events(EventListener... listeners) throws SaltException {
return new EventStream(config, listeners);
}

/**
* Trigger an event in Salt with the specified tag and data.
* <p>
* {@code POST /hook}
*
* @param eventTag the event tag
* @param eventData the event data. Must be valid JSON.
* @return the boolean value returned by Salt. If true the event was triggered
* successfully.
* A value of false is returned only if Salt itself returns false; it does not mean a
* communication failure.
* @throws SaltException if anything goes wrong
*/
public boolean sendEvent(String eventTag, String eventData) throws SaltException {
String tag = eventTag != null ? eventTag : "";
Map<String, Object> result = connectionFactory
.create("/hook/" + tag, JsonParser.MAP, config)
.getResult(eventData);
return Boolean.TRUE.equals(result.get("success"));
}

/**
* Asynchronously trigger an event in Salt with the specified tag and data.
* <p>
* {@code POST /hook}
*
* @param eventTag the event tag
* @param eventData the event data. Must be valid JSON.
* @return Future containing a boolean value indicating the success or failure of
* triggering the event.
*/
public Future<Boolean> sendEventAsync(final String eventTag, final String eventData) {
return executor.submit(() -> { return sendEvent(eventTag, eventData); });
}

/**
* Generic interface to make a {@link Call} to an endpoint using a given {@link Client}.
*
Expand Down
69 changes: 0 additions & 69 deletions src/main/java/com/suse/salt/netapi/datatypes/Job.java

This file was deleted.

20 changes: 0 additions & 20 deletions src/main/java/com/suse/salt/netapi/datatypes/ScheduledJob.java

This file was deleted.

Loading