Skip to content

cowwoc/digitalocean

Repository files navigation

Maven Central build-status

logo DigitalOcean Java Client

API Changelog

A Java client for the DigitalOcean cloud platform.

To get started, add this Maven dependency:

<dependency>
  <groupId>io.github.cowwoc.digitalocean</groupId>
  <artifactId>digitalocean</artifactId>
  <version>0.13</version>
</dependency>

Example

import io.github.cowwoc.digitalocean.compute.client.ComputeClient;
import io.github.cowwoc.digitalocean.compute.resource.Droplet;
import io.github.cowwoc.digitalocean.compute.resource.DropletImage;
import io.github.cowwoc.digitalocean.compute.resource.DropletType;
import io.github.cowwoc.digitalocean.core.util.Configuration;
import io.github.cowwoc.digitalocean.network.resource.Region;

import java.io.IOException;
import java.nio.file.Path;
import java.util.Comparator;

class Example
{
  public static void main(String[] args) throws IOException, InterruptedException
  {
	  try (ComputeClient client = ComputeClient.build())
	  {
		  Configuration configuration = Configuration.fromPath(Path.of("example.properties"));
		  String accessToken = configuration.getString("ACCESS_TOKEN");
		  client.login(accessToken);

		  DropletImage image = client.getDropletImage("debian-12-x64");
		  Region region = client.getRegions(true).getFirst();

		  // Get the least expensive droplet type with at least 2 GiB of memory
		  ComputeDropletType dropletType = client.getDropletTypes().stream().filter(type ->
				  type.getRegionIds().contains(region.getId()) && type.getRamInMiB() >= 2 * 1024).
			  min(Comparator.comparing(ComputeDropletType::getCostPerHour)).orElseThrow();

		  Droplet droplet = client.createDroplet("Node123", dropletType.getId(), image.getId()).apply();
		  while (droplet.getAddresses().isEmpty())
		  {
			  Thread.sleep(1000);
			  droplet = droplet.reload();
		  }
		  System.out.println("The droplet's address is: " + droplet.getAddresses().iterator().next());
	  }
  }
}

Getting Started

See the API documentation for more details.

Licenses

About

A Java client for the DigitalOcean cloud platform

Topics

Resources

License

Stars

Watchers

Forks

Sponsor this project

 

Packages

No packages published