|
| 1 | +# Tailscale Java Wrapper |
| 2 | + |
| 3 | +A wrapper for the Tailscale API. For more information, visit the official [Tailscale API documentation](https://tailscale.com/api). |
| 4 | + |
| 5 | +This example demonstrates how to use the `TailscaleClient` in Java to interact with the Tailscale API. It covers retrieving device details, updating device attributes, managing routes, and more. |
| 6 | + |
| 7 | +## Prerequisites |
| 8 | + |
| 9 | +- A Tailscale account. |
| 10 | +- A valid Tailscale [API key](https://login.tailscale.com/admin/settings/keys). |
| 11 | + |
| 12 | +## Example Usage |
| 13 | + |
| 14 | +```java |
| 15 | +final TailscaleClient client = new TailscaleClient("Organization Name", "API Token"); |
| 16 | + |
| 17 | +// You can also set a custom OkHttp client, Gson client, and a custom BaseUrl |
| 18 | +// TailscaleClient#setHttpClient |
| 19 | +// TailscaleClient#setGson |
| 20 | +// TailscaleClient#setBaseUrl |
| 21 | + |
| 22 | +// Getting all network devices |
| 23 | +final List<Device> devices = client.getDevices(); |
| 24 | + |
| 25 | +final String deviceId = devices.get(0).id(); |
| 26 | +final Device device = client.getDevice(deviceId); |
| 27 | + |
| 28 | +System.out.println(); |
| 29 | +System.out.println(); |
| 30 | + |
| 31 | +// Example of expiring a device |
| 32 | +System.out.println("Device expired: " + client.setExpire(deviceId)); |
| 33 | + |
| 34 | +// Example of changing the authorization status |
| 35 | +System.out.println("Authorization changed: " + client.setAuthorized(deviceId, true)); |
| 36 | + |
| 37 | +// Example of changing the device name |
| 38 | +System.out.println("Device name changed: " + client.setName(deviceId, "Device " + MessageUtil.generateCode(10))); |
| 39 | + |
| 40 | +// Example of setting tags |
| 41 | +System.out.println("Tags set: " + client.setTags(deviceId, Map.of("tagKey", "tagValue"))); |
| 42 | + |
| 43 | +// Example of disabling key expiry |
| 44 | +System.out.println("Key expiry disabled: " + client.setKeyExpiryDisabled(deviceId, true)); |
| 45 | + |
| 46 | +// Example of setting an IP address |
| 47 | +System.out.println("IPv4 set: " + client.setIpV4(deviceId, "100.80.0.1")); |
| 48 | + |
| 49 | +// Example of fetching device attributes |
| 50 | +final AttributesResponse attributesResponse = client.getAttributes(deviceId); |
| 51 | +System.out.println("Attributes: " + attributesResponse); |
| 52 | + |
| 53 | +// Example of setting an attribute on the device |
| 54 | +final AttributesRequest attributesRequest = new AttributesRequest("value", "my_value"); |
| 55 | +final boolean isSet = client.setAttributes(deviceId, "custom:Koo", attributesRequest); |
| 56 | +System.out.println("Attribute set: " + isSet); |
| 57 | + |
| 58 | +// Example of deleting an attribute from the device |
| 59 | +final boolean isDeleted = client.deleteAttributes(deviceId, "custom:Koo"); |
| 60 | +System.out.println("Attribute deleted: " + isDeleted); |
| 61 | + |
| 62 | +// Example of working with routes |
| 63 | +System.out.println(client.getRoutes(deviceId)); |
| 64 | +System.out.println(client.setRoutes(deviceId, new RoutesRequest(List.of("10.0.0.0/16", "192.168.1.0/24")))); |
| 65 | +System.out.println(client.getRoutes(deviceId)); |
| 66 | + |
| 67 | +// Example of deleting a device |
| 68 | +System.out.println("Device deleted: " + client.deleteDevice(deviceId)); |
0 commit comments