Skip to content

Commit

Permalink
Server now creatable with SSH-Key name or ID, SSH-Key can now be foun…
Browse files Browse the repository at this point in the history
…d by fingerprint, Added InvalidParametersException
  • Loading branch information
Tom Siewert committed Mar 29, 2018
1 parent e5bf60e commit 05f217f
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 6 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Hetzner Cloud API for Java

Simple Java client for the Hetzner Cloud API.

The current version is **2.0.0**.
The current version is **2.1.0**.

It would be nice, if you submit pull requests.

Expand Down Expand Up @@ -37,7 +37,7 @@ Dependency:
<dependency>
<groupId>me.tomsdevsn</groupId>
<artifactId>hetznercloud-api</artifactId>
<version>2.0.0</version>
<version>2.1.0</version>
<scope>compile</scope>
</dependency>
</dependencies>
Expand All @@ -55,7 +55,7 @@ repositories({
})
dependencies({
compile "me.tomsdevsn:hetznercloud-api:2.0.0"
compile "me.tomsdevsn:hetznercloud-api:2.1.0"
})
```

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>me.tomsdevsn</groupId>
<artifactId>hetznercloud-api</artifactId>
<version>2.0.0</version>
<version>2.1.0</version>

<name>HetznerCloud-API</name>
<description>Java-client for the Hetzner Cloud</description>
Expand Down
16 changes: 15 additions & 1 deletion src/main/java/me/tomsdevsn/hetznercloud/HetznerCloudAPI.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package me.tomsdevsn.hetznercloud;

import me.tomsdevsn.hetznercloud.exception.InvalidParametersException;
import me.tomsdevsn.hetznercloud.objects.request.*;
import me.tomsdevsn.hetznercloud.objects.response.*;
import org.springframework.http.HttpEntity;
Expand Down Expand Up @@ -87,6 +88,9 @@ public GetServerResponse getServerById(long id) {
* @return response of the API
*/
public ServerResponse createServer(ServerRequest serverRequest) {
serverRequest.getSshKeys().forEach(object -> {
if (!(object instanceof Long || object instanceof String)) throw new InvalidParametersException("Object not Long or String");
});
return restTemplate.postForEntity(API_URL + "/servers", new HttpEntity<>(serverRequest, httpHeaders), ServerResponse.class).getBody();
}

Expand All @@ -112,7 +116,7 @@ public ServernameChangeResponse changeServerName(long id, ServernameChangeReques
}

/**
* Request a VNC over websocket Console
* Request a VNC over Websocket-console
*
* @param id ID of the server
* @return ConsoleResponse object
Expand Down Expand Up @@ -571,6 +575,16 @@ public SSHKeysResponse getSSHKeyByName(String name) {
return restTemplate.exchange(API_URL + "/ssh_keys?" + name, HttpMethod.GET, httpEntity, SSHKeysResponse.class).getBody();
}

/**
* Get a SSH key by the fingerprint.
*
* @param fingerprint Fingerprint of the SSH key
* @return SSHKeysResponse object
*/
public SSHKeysResponse getSSHKeyByFingerprint(String fingerprint) {
return restTemplate.exchange(API_URL + "/ssh_keys?" + fingerprint, HttpMethod.GET, httpEntity, SSHKeysResponse.class).getBody();
}

/**
* Create a SSH key.
* <p>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package me.tomsdevsn.hetznercloud.exception;

/**
* The Exception will be called, if you use invalid parameters.
*/
public class InvalidParametersException extends RuntimeException {

private static final long serialVersionUID = 7465859521263546503L;

public InvalidParametersException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,11 @@ public class ServerRequest {
@JsonProperty("start_after_create")
private boolean startAfterCreate;

/**
* The objects in the list have to be a Long or a String, or it will throw an Exception {@link me.tomsdevsn.hetznercloud.exception.InvalidParametersException}
*/
@JsonProperty("ssh_keys")
private List<Long> sshKeys;
private List<Object> sshKeys;

@JsonProperty("user_data")
private String userData;
Expand Down

0 comments on commit 05f217f

Please sign in to comment.