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

test #311

Closed
wants to merge 13 commits into from
Closed

test #311

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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

# salt-netapi-client

Java bindings for the [Salt API](http://docs.saltstack.com/en/latest/ref/netapi/all/salt.netapi.rest_cherrypy.html#module-salt.netapi.rest_cherrypy.app), please have a look at the Javadoc for [v0.19.0](http://suse.github.io/salt-netapi-client/docs/v0.19.0) or [master](http://suse.github.io/salt-netapi-client/docs/master).
Java bindings for the [Salt API](http://docs.saltstack.com/en/latest/ref/netapi/all/salt.netapi.rest_cherrypy.html#module-salt.netapi.rest_cherrypy.app), please have a look at the Javadoc for [v0.21.0](http://suse.github.io/salt-netapi-client/docs/v0.21.0) or [master](http://suse.github.io/salt-netapi-client/docs/master).

## How to use it

Expand All @@ -13,7 +13,7 @@ Add the following dependency to the `pom.xml` file of your project:
<dependency>
<groupId>com.suse.salt</groupId>
<artifactId>salt-netapi-client</artifactId>
<version>0.19.0</version>
<version>0.21.0</version>
</dependency>
```

Expand Down
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<groupId>com.suse.salt</groupId>
<artifactId>salt-netapi-client</artifactId>
<packaging>jar</packaging>
<version>0.20.0-SNAPSHOT</version>
<version>0.22.0-SNAPSHOT</version>
<name>salt-netapi-client</name>
<url>https://github.com/SUSE/salt-netapi-client</url>
<description>Java bindings for the Salt API</description>
Expand Down Expand Up @@ -131,7 +131,7 @@
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.2</version>
<version>2.8.9</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.suse.salt.netapi.calls.modules;

import com.suse.salt.netapi.calls.LocalCall;

import com.google.gson.reflect.TypeToken;

import java.util.Optional;

/**
* salt.modules.transactional_update
*
* https://docs.saltproject.io/en/latest/ref/modules/all/salt.modules.transactional_update.html
*/
public class TransactionalUpdate {
private TransactionalUpdate() { }

public static LocalCall<String> reboot() {
return new LocalCall<>("transactional_update.reboot", Optional.empty(), Optional.empty(),
new TypeToken<String>() {
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,14 @@
import org.apache.http.entity.StringEntity;
import org.apache.http.nio.client.HttpAsyncClient;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URI;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import java.util.stream.Collectors;

/**
* AsyncHttpClient implemented with Apache's HttpAsyncClient.
Expand Down Expand Up @@ -118,7 +122,7 @@ public void completed(HttpResponse response) {
future.completeExceptionally(e);
}
} else {
future.completeExceptionally(createSaltException(statusCode));
future.completeExceptionally(createSaltException(response));
}
}

Expand All @@ -132,16 +136,27 @@ public void cancelled() {
}

/**
* Create the appropriate exception for the given HTTP status code.
* Create the appropriate exception for the given HTTP response.
*
* @param statusCode HTTP status code
* @param response HTTP response
* @return {@link SaltException} instance
*/
private SaltException createSaltException(int statusCode) {
private SaltException createSaltException(HttpResponse response) {
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode == HttpStatus.SC_UNAUTHORIZED) {
return new SaltUserUnauthorizedException(
"Salt user does not have sufficient permissions");
}
return new SaltException("Response code: " + statusCode);
else {
String content = "";
try {
content = new BufferedReader(new InputStreamReader(response.getEntity().getContent()))
.lines().parallel().collect(Collectors.joining("\n"));
}
catch (IOException e) {
// error trying to get the response body, nothing to do...
}
return new SaltException("Response code: " + statusCode + ". Response body:\n" + content);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ public Result<SSHResult<R>> read(JsonReader in) throws IOException {
.flatMap(SaltErrorUtils::deriveError)
.orElse(
new SaltSSHError(result.getRetcode(),
result.getStderr().orElse(""))
"stderr: \"" + result.getStderr().orElse("") +
"\", stdout: \"" + result.getStdout().orElse("") + "\"")
)
);
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.suse.salt.netapi.results;

import com.google.gson.annotations.SerializedName;
import com.suse.salt.netapi.utils.Xor;

import java.util.Optional;

/**
* Result structure as returned by state.apply to be parsed from event data.
Expand All @@ -10,7 +13,7 @@
public class StateApplyResult<R> {

private String comment;
private String name;
private Optional<Xor<String[], String>> name = Optional.empty();
@SerializedName("start_time")
private String startTime;
private boolean result;
Expand All @@ -23,7 +26,7 @@ public String getComment() {
return comment;
}

public String getName() {
public Optional<Xor<String[], String>> getName() {
return name;
}

Expand Down
Loading