Skip to content

Commit 2489bb1

Browse files
committed
wip: examples
1 parent c7cb831 commit 2489bb1

File tree

4 files changed

+216
-8
lines changed

4 files changed

+216
-8
lines changed

build.gradle

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,11 @@ subprojects {
7777
version = versionFile.text.trim()
7878
} catch (IOException e) {
7979
version = 'SNAPSHOT'
80-
logger.error("Could not read VERSION file for project '${project.name}': ${e.message}")
80+
logger.error("Could not read VERSION file for project '${project.path}': ${e.message}")
8181
}
8282
} else {
8383
version = 'SNAPSHOT'
84-
logger.warn("VERSION file not found in project '${project.name}'. Skipping version setting.")
84+
logger.warn("VERSION file not found in project '${project.path}'. Skipping version setting.")
8585
}
8686

8787

@@ -121,13 +121,24 @@ subprojects {
121121
}
122122
}
123123
}
124-
}
125124

126-
// only apply to example sub-projects
127-
if (project.path.startsWith(':examples:')) {
128-
tasks.register('execute', JavaExec) {
129-
mainClass = System.getProperty('mainClass')
130-
classpath = sourceSets.main.runtimeClasspath
125+
// only apply to example sub-projects
126+
if (project.path.startsWith(':examples:')) {
127+
if (!project.hasProperty('mainClassName')) {
128+
logger.warn("'mainClassName' property not defined for subproject '${project.path}'. Skipping execution of this task.")
129+
}
130+
131+
tasks.register('execute', JavaExec) {
132+
if (!project.hasProperty('mainClassName')) {
133+
doLast {
134+
logger.warn("'mainClassName' property not defined for subproject '${project.path}'. Skipping execution of this task.")
135+
}
136+
enabled = false // Disable the task if no main class is specified
137+
return
138+
}
139+
mainClass = project.mainClassName
140+
classpath = sourceSets.main.runtimeClasspath
141+
}
131142
}
132143
}
133144

examples/iaas/build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
dependencies {
22
implementation project (':services:iaas')
33
}
4+
5+
ext.mainClassName = 'cloud.stackit.sdk.iaas.examples.IaaSExample'

examples/iaas/settings.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
package cloud.stackit.sdk.iaas.examples;
2+
3+
import cloud.stackit.sdk.core.exception.ApiException;
4+
import cloud.stackit.sdk.iaas.api.DefaultApi;
5+
import cloud.stackit.sdk.iaas.model.*;
6+
import java.io.IOException;
7+
import java.util.Map;
8+
import java.util.UUID;
9+
10+
public class IaaSExample {
11+
public static void main(String[] args) throws IOException {
12+
// Credentials are read from the credentialsFile in `~/.stackit/credentials.json` or the env
13+
// STACKIT_SERVICE_ACCOUNT_KEY_PATH / STACKIT_SERVICE_ACCOUNT_KEY
14+
DefaultApi iaasApi = new DefaultApi();
15+
16+
// the id of your STACKIT project, read from env var for this example
17+
String projectIdString = System.getenv("STACKIT_PROJECT_ID");
18+
if (projectIdString == null || projectIdString.isEmpty()) {
19+
System.err.println("Environment variable 'STACKIT_PROJECT_ID' not found.");
20+
return;
21+
}
22+
UUID projectId = UUID.fromString(projectIdString);
23+
24+
try {
25+
///////////////////////////////////////////////////////
26+
// N E T W O R K S //
27+
///////////////////////////////////////////////////////
28+
29+
/* create a network in the project */
30+
// TODO
31+
Network newNetwork =
32+
iaasApi.createNetwork(
33+
projectId,
34+
new CreateNetworkPayload().name("java-sdk-example-network-01"));
35+
36+
/* update the network we just created*/
37+
38+
/* fetch the network we just created */
39+
Network fetchedNetwork = iaasApi.getNetwork(projectId, newNetwork.getNetworkId());
40+
System.out.println("\nFetched network: ");
41+
System.out.println("* Name: " + fetchedNetwork.getName());
42+
System.out.println("* Id: " + fetchedNetwork.getNetworkId());
43+
System.out.println(
44+
"* DHCP: " + (Boolean.TRUE.equals(fetchedNetwork.getDhcp()) ? "YES" : "NO"));
45+
System.out.println("* Gateway: " + fetchedNetwork.getGateway());
46+
System.out.println("* Public IP: " + fetchedNetwork.getPublicIp());
47+
48+
/* list all available networks in the project */
49+
NetworkListResponse networks = iaasApi.listNetworks(projectId, null);
50+
System.out.println("\nAvailable networks: ");
51+
for (Network network : networks.getItems()) {
52+
System.out.println("* " + network.getName());
53+
}
54+
55+
///////////////////////////////////////////////////////
56+
// I M A G E S //
57+
///////////////////////////////////////////////////////
58+
59+
/* list all available images */
60+
ImageListResponse images = iaasApi.listImages(projectId, false, null);
61+
System.out.println("\nAvailable images: ");
62+
for (Image image : images.getItems()) {
63+
System.out.println(image.getId() + " | " + image.getName());
64+
}
65+
66+
/* get an image */
67+
UUID imageId =
68+
images.getItems()
69+
.getFirst()
70+
.getId(); // we just use a random image id in our example
71+
assert imageId != null;
72+
Image fetchedImage = iaasApi.getImage(projectId, imageId);
73+
System.out.println("\nFetched image:");
74+
System.out.println("* Name: " + fetchedImage.getName());
75+
System.out.println("* Id: " + fetchedImage.getId());
76+
System.out.println("* Checksum: " + fetchedImage.getChecksum());
77+
System.out.println("* Created at: " + fetchedImage.getCreatedAt());
78+
System.out.println("* Updated at: " + fetchedImage.getUpdatedAt());
79+
80+
///////////////////////////////////////////////////////
81+
// K E Y P A I R S //
82+
///////////////////////////////////////////////////////
83+
84+
/* list all available keypairs */
85+
KeyPairListResponse keypairs = iaasApi.listKeyPairs(null);
86+
System.out.println("\nAvailable keypairs: ");
87+
for (Keypair keypair : keypairs.getItems()) {
88+
System.out.println("* " + keypair.getName());
89+
}
90+
91+
/* create a keypair */
92+
String publicKey =
93+
"ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIAcLPdv9r0P+PJWX7C2tdV/7vr8k+fbPcTkC6Z6yjclx";
94+
Keypair newKeypair =
95+
iaasApi.createKeyPair(
96+
new CreateKeyPairPayload()
97+
.name("java-sdk-example-keypair-01")
98+
.publicKey(publicKey));
99+
System.out.println("\nKeypair created: " + newKeypair.getName());
100+
101+
/* update the keypair */
102+
assert newKeypair.getName() != null;
103+
iaasApi.updateKeyPair(
104+
newKeypair.getName(),
105+
new UpdateKeyPairPayload().labels(Map.ofEntries(Map.entry("foo", "bar"))));
106+
107+
/* fetch the keypair we just created / updated */
108+
Keypair fetchedKeypair = iaasApi.getKeyPair(newKeypair.getName());
109+
System.out.println("\nFetched key pair: ");
110+
System.out.println("* Name: " + fetchedKeypair.getName());
111+
if (fetchedKeypair.getLabels() != null) {
112+
System.out.println("* Labels: " + fetchedKeypair.getLabels().toString());
113+
}
114+
System.out.println("* Fingerprint: " + fetchedKeypair.getFingerprint());
115+
System.out.println("* Public key: " + fetchedKeypair.getPublicKey());
116+
117+
///////////////////////////////////////////////////////
118+
// S E R V E R S //
119+
///////////////////////////////////////////////////////
120+
121+
/* create a server */
122+
// NOTE: see https://docs.stackit.cloud/stackit/en/virtual-machine-flavors-75137231.html
123+
// for available machine types
124+
String machineType = "t2i.1";
125+
Server newServer =
126+
iaasApi.createServer(
127+
projectId,
128+
new CreateServerPayload()
129+
.name("java-sdk-example-server-01")
130+
.machineType(machineType)
131+
.imageId(imageId)
132+
.labels(Map.ofEntries(Map.entry("foo", "bar")))
133+
.keypairName(newKeypair.getName()));
134+
assert newServer.getId() != null;
135+
136+
/* update the server we just created */
137+
iaasApi.updateServer(
138+
projectId,
139+
newServer.getId(),
140+
new UpdateServerPayload()
141+
.labels(Map.ofEntries(Map.entry("foo", "bar-updated"))));
142+
143+
/* list all servers */
144+
ServerListResponse servers = iaasApi.listServers(projectId, false, null);
145+
System.out.println("\nAvailable servers: ");
146+
for (Server server : servers.getItems()) {
147+
System.out.println("* " + server.getId() + " | " + server.getName());
148+
}
149+
150+
/* fetch the server we just created */
151+
UUID serverId = newServer.getId();
152+
assert serverId != null;
153+
Server fetchedServer = iaasApi.getServer(projectId, serverId, false);
154+
System.out.println("\nFetched server:");
155+
System.out.println("* Name: " + fetchedServer.getName());
156+
System.out.println("* Id: " + fetchedServer.getId());
157+
if (fetchedServer.getLabels() != null) {
158+
System.out.println("* Labels: " + fetchedServer.getLabels().toString());
159+
}
160+
System.out.println("* Machine type: " + fetchedServer.getMachineType());
161+
System.out.println("* Created at: " + fetchedServer.getCreatedAt());
162+
System.out.println("* Updated at: " + fetchedServer.getUpdatedAt());
163+
System.out.println("* Launched at: " + fetchedServer.getLaunchedAt());
164+
165+
/* stop the server we just created */
166+
iaasApi.stopServer(projectId, serverId);
167+
168+
/* boot the server we just created */
169+
iaasApi.startServer(projectId, serverId);
170+
171+
/* reboot the server we just created */
172+
iaasApi.rebootServer(projectId, serverId, null);
173+
174+
///////////////////////////////////////////////////////
175+
// D E L E T I O N //
176+
///////////////////////////////////////////////////////
177+
178+
/* delete the server we just created */
179+
iaasApi.deleteServer(projectId, serverId);
180+
System.out.println("Deleted server: " + serverId);
181+
182+
/* delete the keypair we just created */
183+
iaasApi.deleteKeyPair(newKeypair.getName());
184+
System.out.println("Deleted key pair: " + newKeypair.getName());
185+
186+
/* delete the network we just created */
187+
iaasApi.deleteNetwork(projectId, newNetwork.getNetworkId());
188+
System.out.println("Deleted network: " + newNetwork.getNetworkId());
189+
190+
} catch (ApiException e) {
191+
throw new RuntimeException(e);
192+
}
193+
}
194+
}

0 commit comments

Comments
 (0)