Skip to content

Commit

Permalink
Add support for Primary IPs in client API
Browse files Browse the repository at this point in the history
Required by #39
  • Loading branch information
rkosegi committed Jul 15, 2022
1 parent b2f31f5 commit 5d43c33
Show file tree
Hide file tree
Showing 5 changed files with 251 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright 2022 https://dnation.cloud
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cloud.dnation.jenkins.plugins.hetzner.client;


import com.google.gson.annotations.SerializedName;
import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.util.List;

@EqualsAndHashCode(callSuper = true)
@Data
@SuppressFBWarnings("RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE")
public class GetAllPrimaryIpsResponse extends AbstractSearchResponse {
@SerializedName("primary_ips")
@NonNull
private List<PrimaryIpDetail> ips;
}
Original file line number Diff line number Diff line change
Expand Up @@ -174,4 +174,15 @@ Call<GetServersBySelectorResponse> getServersBySelector(@Query("label_selector")
*/
@GET("/v1/networks/{id}")
Call<GetNetworkByIdResponse> getNetworkById(@Path("id") int id);

/**
* Get all Primary IP objects.
*
* @param selector Can be used to filter resources by labels.
* The response will only contain resources matching the label selector.
* @return returns all Primary IP objects.
* see <a href="https://docs.hetzner.cloud/#primary-ips-get-all-primary-ips">API reference</a>
*/
@GET("/v1/primary_ips")
Call<GetAllPrimaryIpsResponse> getAllPrimaryIps(@Query("label_selector") String selector);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package cloud.dnation.jenkins.plugins.hetzner.client;


import com.google.gson.annotations.SerializedName;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.util.List;
import java.util.Map;

@Data
@EqualsAndHashCode(callSuper = true)
public class PrimaryIpDetail extends IdentifiableResource{
/**
* ID of the resource the Primary IP is assigned to, null if it is not assigned at all.
*/
@SerializedName("assignee_id")
private Integer assigneeId;

/**
* Resource type the Primary IP can be assigned to.
*/
@SerializedName("assignee_type")
private String assigneeType;

/**
* Delete this Primary IP when the resource it is assigned to is deleted.
*/
@SerializedName("auto_delete")
private boolean autoDelete;

/**
* Whether the IP is blocked.
*/
private boolean blocked;

/**
* Point in time when the Resource was created (in ISO-8601 format).
*/
private String created;

/**
* Datacenter this Primary IP is located at.
*/
private DatacenterDetail datacenter;

/**
* Array of reverse DNS entries.
*/
@SerializedName("dns_ptr")
private List<Ipv4Detail> dnsPtr;

/**
* IP address.
*/
private String ip;

/**
* User-defined labels (key-value pairs)
*/
private Map<String, String> labels;

/**
* Name of the Resource. Must be unique per Project.
*/
private String name;

/**
* Type of the Primary IP.
*/
private String type;
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

import static cloud.dnation.jenkins.plugins.hetzner.TestHelper.resourceAsString;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;

public class HetznerApiTest {
private static MockWebServer ws;
Expand Down Expand Up @@ -91,4 +92,14 @@ public void testGetNetworksBySelectorEmpty() throws IOException {
assertEquals(0, result.getNetworks().size());
assertEquals("0", result.getMeta().getPagination().getTotalEntries());
}

@Test
public void testGetPrimaryIpsBySelector() throws IOException {
ws.enqueue(new MockResponse().setBody(resourceAsString("get-primary-ips-by-selector.json")));
Call<GetAllPrimaryIpsResponse> call = api.getAllPrimaryIps("jenkins");
GetAllPrimaryIpsResponse result = call.execute().body();
assertEquals(1, result.getIps().size());
assertEquals("1.2.3.4", result.getIps().get(0).getIp());
assertNull(result.getIps().get(0).getAssigneeId());
}
}
123 changes: 123 additions & 0 deletions src/test/resources/get-primary-ips-by-selector.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
{
"primary_ips": [
{
"id": 7890123,
"name": "primary_ip-123456",
"labels": {
"jenkins": "1"
},
"created": "2022-07-15T05:21:04.780842+00:00",
"blocked": false,
"datacenter": {
"id": 4,
"name": "fsn1-dc14",
"description": "Falkenstein 1 DC14",
"location": {
"id": 1,
"name": "fsn1",
"description": "Falkenstein DC Park 1",
"country": "DE",
"city": "Falkenstein",
"latitude": 50.47612,
"longitude": 12.370071,
"network_zone": "eu-central"
},
"server_types": {
"supported": [
9,
7,
5,
3,
1,
11,
12,
13,
14,
15,
22,
23,
24,
25,
26,
33,
34,
35,
36,
37,
38
],
"available": [
1,
3,
5,
7,
9,
11,
12,
13,
14,
15,
22,
23,
24,
25,
26,
33,
34,
35,
36,
37,
38
],
"available_for_migration": [
1,
3,
5,
7,
9,
11,
12,
13,
14,
15,
22,
23,
24,
25,
26,
33,
34,
35,
36,
37,
38
]
}
},
"ip": "1.2.3.4",
"dns_ptr": [
{
"ip": "1.2.3.4",
"dns_ptr": "static.14.3.2.1.clients.your-server.de"
}
],
"protection": {
"delete": false
},
"type": "ipv4",
"auto_delete": false,
"assignee_type": "server",
"assignee_id": null
}
],
"meta": {
"pagination": {
"page": 1,
"per_page": 25,
"previous_page": null,
"next_page": null,
"last_page": 1,
"total_entries": 1
}
}
}

0 comments on commit 5d43c33

Please sign in to comment.