Skip to content

Commit

Permalink
Adopt to upcoming changes in Hetzner Cloud API
Browse files Browse the repository at this point in the history
Starting on 1 September, Hetzner Cloud API will start to use 52-bit IDs.
Client library already adopted to this change.
Update code so it uses long instead of integer where it deal with IDs.

See more info here https://docs.hetzner.cloud/changelog#2023-05-15-api-will-return-up-to-52-bit-ids
  • Loading branch information
rkosegi committed Jul 29, 2023
1 parent 7ef1524 commit 3442972
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 26 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@
<dependency>
<groupId>cloud.dnation.integration</groupId>
<artifactId>hetzner-cloud-client-java</artifactId>
<version>1.3.0</version>
<version>1.6.0</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ static ValidationResult verifyImage(String image, String credentialsId) {
"Expected exactly one result, got %s", result.getImages().size());
return new ValidationResult(true, "Found: " +
result.getImages().get(0).getDescription());
} else if (Helper.isPossiblyInteger(image)) {
final GetImageByIdResponse result = api.getImageById(Integer.parseInt(image)).execute().body();
} else if (Helper.isPossiblyLong(image)) {
final GetImageByIdResponse result = api.getImageById(Long.parseLong(image)).execute().body();
return new ValidationResult(true, "Found: " +
result.getImage().getDescription());
} else {
Expand All @@ -128,7 +128,7 @@ static ValidationResult verifyNetwork(String network, String credentialsId) {
return new ValidationResult(true, "Found: " +
result.getNetworks().get(0).getName() + " " +
result.getNetworks().get(0).getIpRange());
} else if (Helper.isPossiblyInteger(network)) {
} else if (Helper.isPossiblyLong(network)) {
final GetNetworkByIdResponse result = api.getNetworkById(Integer.parseInt(network)).execute().body();
return new ValidationResult(true, "Found: " +
result.getNetwork().getName() + " " + result.getNetwork().getIpRange());
Expand All @@ -150,7 +150,7 @@ static ValidationResult verifyPlacementGroup(String placementGroup, String crede
return new ValidationResult(true, "Found: " +
result.getPlacementGroups().get(0).getName() + " " +
result.getPlacementGroups().get(0).getId());
} else if (Helper.isPossiblyInteger(placementGroup)) {
} else if (Helper.isPossiblyLong(placementGroup)) {
final GetPlacementGroupByIdResponse result = api.getPlacementGroupById(Integer.parseInt(placementGroup)).execute().body();
return new ValidationResult(true, "Found: " +
result.getPlacementGroup().getName() + " " + result.getPlacementGroup().getId());
Expand Down
15 changes: 9 additions & 6 deletions src/main/java/cloud/dnation/jenkins/plugins/hetzner/Helper.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import com.cloudbees.plugins.credentials.CredentialsProvider;
import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import com.google.common.primitives.Ints;
import com.trilead.ssh2.crypto.PEMDecoder;
import hudson.security.ACL;
import jenkins.model.Jenkins;
Expand Down Expand Up @@ -92,14 +91,18 @@ public static boolean isLabelExpression(String expression) {
}

/**
* Check if given string could be parsed as positive integer.
* Check if given string could be parsed as positive long.
*
* @param str string to check
* @return <code>true</code> if given string could be parsed as positive integer, <code>false</code> otherwise
* @return <code>true</code> if given string could be parsed as positive long, <code>false</code> otherwise
*/
public static boolean isPossiblyInteger(String str) {
final Integer value = Ints.tryParse(str);
return value != null && value > 0;
public static boolean isPossiblyLong(String str) {
try {
final long value = Long.parseLong(str);
return value > 0;
} catch (NumberFormatException e) {
return false;
}
}

public static <T, E> List<E> getPayload(@Nonnull Response<T> response, @Nonnull Function<T, List<E>> mapper) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ private HetznerApi proxy() {
* @throws IllegalStateException if there was invalid response from API server
* @throws IllegalArgumentException if label expression didn't yield single image
*/
private int getImageIdForLabelExpression(String labelExpression) throws IOException {
private long getImageIdForLabelExpression(String labelExpression) throws IOException {
return searchResourceByLabelExpression(labelExpression, proxy()::getImagesBySelector,
GetImagesBySelectorResponse::getImages);
}
Expand All @@ -132,7 +132,7 @@ private int getImageIdForLabelExpression(String labelExpression) throws IOExcept
* @throws IllegalStateException if there was invalid response from API server
* @throws IllegalArgumentException if label expression didn't yield single network
*/
private int getNetworkIdForLabelExpression(String labelExpression) throws IOException {
private long getNetworkIdForLabelExpression(String labelExpression) throws IOException {
return searchResourceByLabelExpression(labelExpression, proxy()::getNetworkBySelector,
GetNetworksBySelectorResponse::getNetworks);
}
Expand All @@ -147,12 +147,12 @@ private int getNetworkIdForLabelExpression(String labelExpression) throws IOExce
* @throws IllegalStateException if there was invalid response from API server
* @throws IllegalArgumentException if label expression didn't yield single placement group
*/
private int getPlacementGroupForLabelExpression(String labelExpression) throws IOException {
private long getPlacementGroupForLabelExpression(String labelExpression) throws IOException {
return searchResourceByLabelExpression(labelExpression, proxy()::getPlacementGroups,
GetPlacementGroupsResponse::getPlacementGroups);
}

private <R extends AbstractSearchResponse, I extends IdentifiableResource> int searchResourceByLabelExpression(
private <R extends AbstractSearchResponse, I extends IdentifiableResource> long searchResourceByLabelExpression(
String labelExpression,
Function<String, Call<R>> searchFunction,
Function<R, List<I>> getItemsFunction) throws IOException {
Expand All @@ -172,7 +172,7 @@ private <R extends AbstractSearchResponse, I extends IdentifiableResource> int s
* @param server server instance to remove from cloud
*/
public void destroyServer(ServerDetail server) {
final int serverId = server.getId();
final long serverId = server.getId();
final HetznerApi client = proxy();
try {
assertValidResponse(client.powerOffServer(serverId).execute());
Expand Down Expand Up @@ -204,11 +204,10 @@ private SshKeyDetail getOrCreateSshKey(HetznerServerTemplate template) throws IO
}
final String publicKey = getSSHPublicKeyFromPrivate(privateKey.getPrivateKey(),
Secret.toString(privateKey.getPassphrase()));
final Response<CreateSshKeyResponse> createResponse = proxy().createSshKey(CreateSshKeyRequest.builder()
final Response<CreateSshKeyResponse> createResponse = proxy().createSshKey(new CreateSshKeyRequest()
.labels(createLabelsForSshKey(credentialsId))
.name(template.getConnector().getSshCredentialsId())
.publicKey(publicKey)
.build())
.publicKey(publicKey))
.execute();
return assertValidResponse(createResponse, CreateSshKeyResponse::getSshKey);
}
Expand Down Expand Up @@ -255,9 +254,9 @@ public HetznerServerInfo createServer(HetznerServerAgent agent) {
final ConnectivityType ct = agent.getTemplate().getConnectivity().getType();
if (ct == ConnectivityType.BOTH || ct == ConnectivityType.PRIVATE) {
if (!Strings.isNullOrEmpty(agent.getTemplate().getNetwork())) {
final int networkId;
if (Helper.isPossiblyInteger(agent.getTemplate().getNetwork())) {
networkId = Integer.parseInt(agent.getTemplate().getNetwork());
final long networkId;
if (Helper.isPossiblyLong(agent.getTemplate().getNetwork())) {
networkId = Long.parseLong(agent.getTemplate().getNetwork());
} else {
networkId = getNetworkIdForLabelExpression(agent.getTemplate().getNetwork());
}
Expand All @@ -272,8 +271,8 @@ public HetznerServerInfo createServer(HetznerServerAgent agent) {
}
final String placementGroup = agent.getTemplate().getPlacementGroup();
if (!Strings.isNullOrEmpty(placementGroup)) {
if(Helper.isPossiblyInteger(placementGroup)) {
createServerRequest.setPlacementGroup(Integer.parseInt(placementGroup));
if(Helper.isPossiblyLong(placementGroup)) {
createServerRequest.setPlacementGroup(Long.parseLong(placementGroup));
} else {
createServerRequest.setPlacementGroup(getPlacementGroupForLabelExpression(placementGroup));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public AbstractByLabelSelector(boolean failIfError, String selector) {

@Override
public void applyInternal(HetznerApi api, CreateServerRequest server) throws IOException {
final PrimaryIpDetail pip = api.getAllPrimaryIps(selector).execute().body().getIps().stream()
final PrimaryIpDetail pip = api.getAllPrimaryIps(selector).execute().body().getPrimaryIps().stream()
.filter(ip -> isIpUsable(ip, server)).findFirst().get();
final PublicNetRequest net = new PublicNetRequest();
net.setIpv4(pip.getId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,11 @@ public void testCanShutdownServer() {
assertFalse(Helper.canShutdownServer(str, time("2022-08-08T11:03:02")));
assertTrue(Helper.canShutdownServer(str, time("2022-08-08T11:59:02")));
}

@Test
public void testIsPossiblyLong() {
assertTrue(Helper.isPossiblyLong("1"));
assertFalse(Helper.isPossiblyLong("0"));
assertFalse(Helper.isPossiblyLong("not-a-number"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public void testIpIsUsable() {
assertFalse(isIpUsable(ip, server));

//Already allocated
ip.setAssigneeId(0);
ip.setAssigneeId(0L);
assertFalse(isIpUsable(ip, server));
}
}

0 comments on commit 3442972

Please sign in to comment.