Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 26 additions & 8 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,18 @@ allprojects {
]
}

sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
java {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8

toolchain {
languageVersion = JavaLanguageVersion.of(8)
}
}

tasks.withType(JavaCompile).configureEach {
options.encoding = 'UTF-8'
options.release = 8 // enforce java 8 compatibility
}

spotless {
Expand Down Expand Up @@ -63,22 +70,22 @@ subprojects {
apply plugin: 'eclipse'

group = 'cloud.stackit'
version = 'SNAPSHOT'

afterEvaluate { project ->
// only apply to service sub-projects and core
if (project.path.startsWith(':services:') || project.name == "core" ) {

// override the version of each service with the ones obtained from the VERSION files
def versionFile = project.file("VERSION")
if (versionFile.exists()) {
try {
version = versionFile.text.trim()
} catch (IOException e) {
logger.error("Could not read VERSION file for project '${project.name}': ${e.message}")
version = 'SNAPSHOT'
logger.error("Could not read VERSION file for project '${project.path}': ${e.message}")
}
} else {
logger.warn("VERSION file not found in project '${project.name}'. Skipping version setting.")
version = 'SNAPSHOT'
logger.warn("VERSION file not found in project '${project.path}'. Skipping version setting.")
}


Expand Down Expand Up @@ -121,8 +128,19 @@ subprojects {

// only apply to example sub-projects
if (project.path.startsWith(':examples:')) {
task execute(type:JavaExec) {
main = System.getProperty('mainClass')
if (!project.hasProperty('mainClassName')) {
logger.warn("'mainClassName' property not defined for subproject '${project.path}'. Skipping execution of this task.")
}

tasks.register('execute', JavaExec) {
if (!project.hasProperty('mainClassName')) {
doLast {
logger.warn("'mainClassName' property not defined for subproject '${project.path}'. Skipping execution of this task.")
}
enabled = false // Disable the task if no main class is specified
return
}
mainClass = project.mainClassName
classpath = sourceSets.main.runtimeClasspath
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package cloud.stackit.sdk.authentication.examples;

import cloud.stackit.sdk.core.config.CoreConfiguration;
import cloud.stackit.sdk.resourcemanager.api.DefaultApi;
import cloud.stackit.sdk.resourcemanager.api.ResourceManagerApi;
import cloud.stackit.sdk.resourcemanager.model.ListOrganizationsResponse;

class AuthenticationExample {
Expand All @@ -13,7 +13,7 @@ public static void main(String[] args) {
new CoreConfiguration().serviceAccountKeyPath(SERVICE_ACCOUNT_KEY_PATH);

try {
DefaultApi api = new DefaultApi(config);
ResourceManagerApi api = new ResourceManagerApi(config);

/* list all organizations */
ListOrganizationsResponse response =
Expand Down
5 changes: 5 additions & 0 deletions examples/iaas/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
dependencies {
implementation project (':services:iaas')
}

ext.mainClassName = 'cloud.stackit.sdk.iaas.examples.IaaSExample'
1 change: 1 addition & 0 deletions examples/iaas/settings.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Original file line number Diff line number Diff line change
@@ -0,0 +1,267 @@
package cloud.stackit.sdk.iaas.examples;

import cloud.stackit.sdk.core.exception.ApiException;
import cloud.stackit.sdk.iaas.api.IaasApi;
import cloud.stackit.sdk.iaas.model.*;
import java.io.IOException;
import java.util.Collections;
import java.util.Objects;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

public class IaaSExample {
public static void main(String[] args) throws IOException {
// Credentials are read from the credentialsFile in `~/.stackit/credentials.json` or the env
// STACKIT_SERVICE_ACCOUNT_KEY_PATH / STACKIT_SERVICE_ACCOUNT_KEY
IaasApi iaasApi = new IaasApi();

// the id of your STACKIT project, read from env var for this example
String projectIdString = System.getenv("STACKIT_PROJECT_ID");
if (projectIdString == null || projectIdString.isEmpty()) {
System.err.println("Environment variable 'STACKIT_PROJECT_ID' not found.");
return;
}
UUID projectId = UUID.fromString(projectIdString);

try {
///////////////////////////////////////////////////////
// N E T W O R K S //
///////////////////////////////////////////////////////

/* create a network in the project */
Network newNetwork =
iaasApi.createNetwork(
projectId,
new CreateNetworkPayload()
.name("java-sdk-example-network-01")
.dhcp(true)
.routed(false)
.labels(Collections.singletonMap("foo", "bar"))
.addressFamily(
new CreateNetworkAddressFamily()
.ipv4(
new CreateNetworkIPv4Body()
.addNameserversItem(
"8.8.8.8"))));

/* update the network we just created */
iaasApi.partialUpdateNetwork(
projectId,
newNetwork.getNetworkId(),
new PartialUpdateNetworkPayload()
.dhcp(false)
.labels(Collections.singletonMap("foo", "bar-updated")));

/* fetch the network we just created */
Network fetchedNetwork = iaasApi.getNetwork(projectId, newNetwork.getNetworkId());
System.out.println("\nFetched network: ");
System.out.println("* Name: " + fetchedNetwork.getName());
System.out.println("* Id: " + fetchedNetwork.getNetworkId());
System.out.println(
"* DHCP: " + (Boolean.TRUE.equals(fetchedNetwork.getDhcp()) ? "YES" : "NO"));
System.out.println("* Gateway: " + fetchedNetwork.getGateway());
System.out.println("* Public IP: " + fetchedNetwork.getPublicIp());

/* list all available networks in the project */
NetworkListResponse networks = iaasApi.listNetworks(projectId, null);
System.out.println("\nAvailable networks: ");
for (Network network : networks.getItems()) {
System.out.println("* " + network.getName());
}

///////////////////////////////////////////////////////
// I M A G E S //
///////////////////////////////////////////////////////

/* list all available images */
ImageListResponse images = iaasApi.listImages(projectId, false, null);
System.out.println("\nAvailable images: ");
for (Image image : images.getItems()) {
System.out.println(image.getId() + " | " + image.getName());
}

/* get an image */
UUID imageId =
images.getItems()
.get(0)
.getId(); // we just use a random image id in our example
assert imageId != null;
Image fetchedImage = iaasApi.getImage(projectId, imageId);
System.out.println("\nFetched image:");
System.out.println("* Name: " + fetchedImage.getName());
System.out.println("* Id: " + fetchedImage.getId());
System.out.println("* Checksum: " + fetchedImage.getChecksum());
System.out.println("* Created at: " + fetchedImage.getCreatedAt());
System.out.println("* Updated at: " + fetchedImage.getUpdatedAt());

///////////////////////////////////////////////////////
// K E Y P A I R S //
///////////////////////////////////////////////////////

/* list all available keypairs */
KeyPairListResponse keypairs = iaasApi.listKeyPairs(null);
System.out.println("\nAvailable keypairs: ");
for (Keypair keypair : keypairs.getItems()) {
System.out.println("* " + keypair.getName());
}

/* create a keypair */
String publicKey =
"ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIAcLPdv9r0P+PJWX7C2tdV/7vr8k+fbPcTkC6Z6yjclx";
Keypair newKeypair =
iaasApi.createKeyPair(
new CreateKeyPairPayload()
.name("java-sdk-example-keypair-01")
.publicKey(publicKey));
System.out.println("\nKeypair created: " + newKeypair.getName());

/* update the keypair */
assert newKeypair.getName() != null;
iaasApi.updateKeyPair(
newKeypair.getName(),
new UpdateKeyPairPayload().labels(Collections.singletonMap("foo", "bar")));

/* fetch the keypair we just created / updated */
Keypair fetchedKeypair = iaasApi.getKeyPair(newKeypair.getName());
System.out.println("\nFetched key pair: ");
System.out.println("* Name: " + fetchedKeypair.getName());
if (fetchedKeypair.getLabels() != null) {
System.out.println("* Labels: " + fetchedKeypair.getLabels().toString());
}
System.out.println("* Fingerprint: " + fetchedKeypair.getFingerprint());
System.out.println("* Public key: " + fetchedKeypair.getPublicKey());

///////////////////////////////////////////////////////
// S E R V E R S //
///////////////////////////////////////////////////////

/* list all available machine types */
MachineTypeListResponse machineTypes = iaasApi.listMachineTypes(projectId, null);
System.out.println("\nAvailable machine types: ");
for (MachineType machineType : machineTypes.getItems()) {
System.out.println("* " + machineType.getName());
}

/* fetch details about a machine type */
MachineType fetchedMachineType =
iaasApi.getMachineType(projectId, machineTypes.getItems().get(0).getName());
System.out.println("\nFetched machine type: ");
System.out.println("* Name: " + fetchedMachineType.getName());
System.out.println("* Description: " + fetchedMachineType.getDescription());
System.out.println("* Disk size: " + fetchedMachineType.getDisk());
System.out.println("* RAM: " + fetchedMachineType.getRam());
System.out.println("* vCPUs: " + fetchedMachineType.getVcpus());
System.out.println("* Extra specs: " + fetchedMachineType.getExtraSpecs());

/* create a server */
// NOTE: see https://docs.stackit.cloud/stackit/en/virtual-machine-flavors-75137231.html
// for available machine types
Server newServer =
iaasApi.createServer(
projectId,
new CreateServerPayload()
.name("java-sdk-example-server-01")
.machineType("t2i.1")
.imageId(imageId)
.labels(Collections.singletonMap("foo", "bar"))
// add the keypair we created above
.keypairName(newKeypair.getName())
// add the server to the network we created above
.networking(
new CreateServerPayloadNetworking(
new CreateServerNetworking()
.networkId(
newNetwork.getNetworkId()))));
assert newServer.getId() != null;

/* wait for the server creation to complete */
UUID serverId = newServer.getId();
assert serverId != null;
while (Objects.equals(
iaasApi.getServer(projectId, serverId, false).getStatus(), "CREATING")) {
System.out.println("Waiting for server creation to complete ...");
TimeUnit.SECONDS.sleep(5);
}

/* update the server we just created */
iaasApi.updateServer(
projectId,
newServer.getId(),
new UpdateServerPayload()
.labels(Collections.singletonMap("foo", "bar-updated")));

/* list all servers */
ServerListResponse servers = iaasApi.listServers(projectId, false, null);
System.out.println("\nAvailable servers: ");
for (Server server : servers.getItems()) {
System.out.println("* " + server.getId() + " | " + server.getName());
}

/* fetch the server we just created */
Server fetchedServer = iaasApi.getServer(projectId, serverId, false);
System.out.println("\nFetched server:");
System.out.println("* Name: " + fetchedServer.getName());
System.out.println("* Id: " + fetchedServer.getId());
if (fetchedServer.getLabels() != null) {
System.out.println("* Labels: " + fetchedServer.getLabels().toString());
}
System.out.println("* Machine type: " + fetchedServer.getMachineType());
System.out.println("* Created at: " + fetchedServer.getCreatedAt());
System.out.println("* Updated at: " + fetchedServer.getUpdatedAt());
System.out.println("* Launched at: " + fetchedServer.getLaunchedAt());

/* stop the server we just created */
iaasApi.stopServer(projectId, serverId);
/* wait for the server to stop */
while (!Objects.equals(
iaasApi.getServer(projectId, serverId, false).getPowerStatus(), "STOPPED")) {
System.out.println("Waiting for server " + serverId + " to stop...");
TimeUnit.SECONDS.sleep(5);
}

/* boot the server we just created */
iaasApi.startServer(projectId, serverId);
/* wait for the server to boot */
while (!Objects.equals(
iaasApi.getServer(projectId, serverId, false).getPowerStatus(), "RUNNING")) {
System.out.println("Waiting for server " + serverId + " to boot...");
TimeUnit.SECONDS.sleep(5);
}

/* reboot the server we just created */
iaasApi.rebootServer(projectId, serverId, null);

///////////////////////////////////////////////////////
// D E L E T I O N //
///////////////////////////////////////////////////////

/* delete the server we just created */
iaasApi.deleteServer(projectId, serverId);
System.out.println("Deleted server: " + serverId);

/* wait for server deletion to complete */
while (true) {
try {
iaasApi.getServer(projectId, serverId, false);
System.out.println("Waiting for server deletion to complete...");
TimeUnit.SECONDS.sleep(5);
} catch (ApiException e) {
if (e.getCode() == 404) {
break;
}
}
}

/* delete the keypair we just created */
iaasApi.deleteKeyPair(newKeypair.getName());
System.out.println("Deleted key pair: " + newKeypair.getName());

/* delete the network we just created */
iaasApi.deleteNetwork(projectId, newNetwork.getNetworkId());
System.out.println("Deleted network: " + newNetwork.getNetworkId());

} catch (ApiException | InterruptedException e) {
throw new RuntimeException(e);
}
}
}
Loading