Skip to content

Commit ca98c88

Browse files
committed
feat(iaas): initial onboarding
relates to STACKITSDK-209
1 parent 8f19d90 commit ca98c88

File tree

148 files changed

+66903
-9
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

148 files changed

+66903
-9
lines changed

build.gradle

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@ allprojects {
2121
]
2222
}
2323

24-
sourceCompatibility = JavaVersion.VERSION_1_8
25-
targetCompatibility = JavaVersion.VERSION_1_8
24+
java {
25+
sourceCompatibility = JavaVersion.VERSION_1_8
26+
targetCompatibility = JavaVersion.VERSION_1_8
27+
}
2628

2729
tasks.withType(JavaCompile).configureEach {
2830
options.encoding = 'UTF-8'
@@ -63,7 +65,6 @@ subprojects {
6365
apply plugin: 'eclipse'
6466

6567
group = 'cloud.stackit'
66-
version = 'SNAPSHOT'
6768

6869
afterEvaluate { project ->
6970
// only apply to service sub-projects and core
@@ -75,10 +76,12 @@ subprojects {
7576
try {
7677
version = versionFile.text.trim()
7778
} catch (IOException e) {
78-
logger.error("Could not read VERSION file for project '${project.name}': ${e.message}")
79+
version = 'SNAPSHOT'
80+
logger.error("Could not read VERSION file for project '${project.path}': ${e.message}")
7981
}
8082
} else {
81-
logger.warn("VERSION file not found in project '${project.name}'. Skipping version setting.")
83+
version = 'SNAPSHOT'
84+
logger.warn("VERSION file not found in project '${project.path}'. Skipping version setting.")
8285
}
8386

8487

@@ -121,8 +124,19 @@ subprojects {
121124

122125
// only apply to example sub-projects
123126
if (project.path.startsWith(':examples:')) {
124-
task execute(type:JavaExec) {
125-
main = System.getProperty('mainClass')
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
126140
classpath = sourceSets.main.runtimeClasspath
127141
}
128142
}

examples/iaas/build.gradle

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
dependencies {
2+
implementation project (':services:iaas')
3+
}
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: 267 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,267 @@
1+
package cloud.stackit.sdk.iaas.examples;
2+
3+
import cloud.stackit.sdk.core.exception.ApiException;
4+
import cloud.stackit.sdk.iaas.api.IaasApi;
5+
import cloud.stackit.sdk.iaas.model.*;
6+
import java.io.IOException;
7+
import java.util.Map;
8+
import java.util.Objects;
9+
import java.util.UUID;
10+
import java.util.concurrent.TimeUnit;
11+
12+
public class IaaSExample {
13+
public static void main(String[] args) throws IOException {
14+
// Credentials are read from the credentialsFile in `~/.stackit/credentials.json` or the env
15+
// STACKIT_SERVICE_ACCOUNT_KEY_PATH / STACKIT_SERVICE_ACCOUNT_KEY
16+
IaasApi iaasApi = new IaasApi();
17+
18+
// the id of your STACKIT project, read from env var for this example
19+
String projectIdString = System.getenv("STACKIT_PROJECT_ID");
20+
if (projectIdString == null || projectIdString.isEmpty()) {
21+
System.err.println("Environment variable 'STACKIT_PROJECT_ID' not found.");
22+
return;
23+
}
24+
UUID projectId = UUID.fromString(projectIdString);
25+
26+
try {
27+
///////////////////////////////////////////////////////
28+
// N E T W O R K S //
29+
///////////////////////////////////////////////////////
30+
31+
/* create a network in the project */
32+
Network newNetwork =
33+
iaasApi.createNetwork(
34+
projectId,
35+
new CreateNetworkPayload()
36+
.name("java-sdk-example-network-01")
37+
.dhcp(true)
38+
.routed(false)
39+
.labels(Map.ofEntries(Map.entry("foo", "bar")))
40+
.addressFamily(
41+
new CreateNetworkAddressFamily()
42+
.ipv4(
43+
new CreateNetworkIPv4Body()
44+
.addNameserversItem(
45+
"8.8.8.8"))));
46+
47+
/* update the network we just created */
48+
iaasApi.partialUpdateNetwork(
49+
projectId,
50+
newNetwork.getNetworkId(),
51+
new PartialUpdateNetworkPayload()
52+
.dhcp(false)
53+
.labels(Map.ofEntries(Map.entry("foo", "bar-updated"))));
54+
55+
/* fetch the network we just created */
56+
Network fetchedNetwork = iaasApi.getNetwork(projectId, newNetwork.getNetworkId());
57+
System.out.println("\nFetched network: ");
58+
System.out.println("* Name: " + fetchedNetwork.getName());
59+
System.out.println("* Id: " + fetchedNetwork.getNetworkId());
60+
System.out.println(
61+
"* DHCP: " + (Boolean.TRUE.equals(fetchedNetwork.getDhcp()) ? "YES" : "NO"));
62+
System.out.println("* Gateway: " + fetchedNetwork.getGateway());
63+
System.out.println("* Public IP: " + fetchedNetwork.getPublicIp());
64+
65+
/* list all available networks in the project */
66+
NetworkListResponse networks = iaasApi.listNetworks(projectId, null);
67+
System.out.println("\nAvailable networks: ");
68+
for (Network network : networks.getItems()) {
69+
System.out.println("* " + network.getName());
70+
}
71+
72+
///////////////////////////////////////////////////////
73+
// I M A G E S //
74+
///////////////////////////////////////////////////////
75+
76+
/* list all available images */
77+
ImageListResponse images = iaasApi.listImages(projectId, false, null);
78+
System.out.println("\nAvailable images: ");
79+
for (Image image : images.getItems()) {
80+
System.out.println(image.getId() + " | " + image.getName());
81+
}
82+
83+
/* get an image */
84+
UUID imageId =
85+
images.getItems()
86+
.getFirst()
87+
.getId(); // we just use a random image id in our example
88+
assert imageId != null;
89+
Image fetchedImage = iaasApi.getImage(projectId, imageId);
90+
System.out.println("\nFetched image:");
91+
System.out.println("* Name: " + fetchedImage.getName());
92+
System.out.println("* Id: " + fetchedImage.getId());
93+
System.out.println("* Checksum: " + fetchedImage.getChecksum());
94+
System.out.println("* Created at: " + fetchedImage.getCreatedAt());
95+
System.out.println("* Updated at: " + fetchedImage.getUpdatedAt());
96+
97+
///////////////////////////////////////////////////////
98+
// K E Y P A I R S //
99+
///////////////////////////////////////////////////////
100+
101+
/* list all available keypairs */
102+
KeyPairListResponse keypairs = iaasApi.listKeyPairs(null);
103+
System.out.println("\nAvailable keypairs: ");
104+
for (Keypair keypair : keypairs.getItems()) {
105+
System.out.println("* " + keypair.getName());
106+
}
107+
108+
/* create a keypair */
109+
String publicKey =
110+
"ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIAcLPdv9r0P+PJWX7C2tdV/7vr8k+fbPcTkC6Z6yjclx";
111+
Keypair newKeypair =
112+
iaasApi.createKeyPair(
113+
new CreateKeyPairPayload()
114+
.name("java-sdk-example-keypair-01")
115+
.publicKey(publicKey));
116+
System.out.println("\nKeypair created: " + newKeypair.getName());
117+
118+
/* update the keypair */
119+
assert newKeypair.getName() != null;
120+
iaasApi.updateKeyPair(
121+
newKeypair.getName(),
122+
new UpdateKeyPairPayload().labels(Map.ofEntries(Map.entry("foo", "bar"))));
123+
124+
/* fetch the keypair we just created / updated */
125+
Keypair fetchedKeypair = iaasApi.getKeyPair(newKeypair.getName());
126+
System.out.println("\nFetched key pair: ");
127+
System.out.println("* Name: " + fetchedKeypair.getName());
128+
if (fetchedKeypair.getLabels() != null) {
129+
System.out.println("* Labels: " + fetchedKeypair.getLabels().toString());
130+
}
131+
System.out.println("* Fingerprint: " + fetchedKeypair.getFingerprint());
132+
System.out.println("* Public key: " + fetchedKeypair.getPublicKey());
133+
134+
///////////////////////////////////////////////////////
135+
// S E R V E R S //
136+
///////////////////////////////////////////////////////
137+
138+
/* list all available machine types */
139+
MachineTypeListResponse machineTypes = iaasApi.listMachineTypes(projectId, null);
140+
System.out.println("\nAvailable machine types: ");
141+
for (MachineType machineType : machineTypes.getItems()) {
142+
System.out.println("* " + machineType.getName());
143+
}
144+
145+
/* fetch details about a machine type */
146+
MachineType fetchedMachineType =
147+
iaasApi.getMachineType(projectId, machineTypes.getItems().getFirst().getName());
148+
System.out.println("\nFetched machine type: ");
149+
System.out.println("* Name: " + fetchedMachineType.getName());
150+
System.out.println("* Description: " + fetchedMachineType.getDescription());
151+
System.out.println("* Disk size: " + fetchedMachineType.getDisk());
152+
System.out.println("* RAM: " + fetchedMachineType.getRam());
153+
System.out.println("* vCPUs: " + fetchedMachineType.getVcpus());
154+
System.out.println("* Extra specs: " + fetchedMachineType.getExtraSpecs());
155+
156+
/* create a server */
157+
// NOTE: see https://docs.stackit.cloud/stackit/en/virtual-machine-flavors-75137231.html
158+
// for available machine types
159+
Server newServer =
160+
iaasApi.createServer(
161+
projectId,
162+
new CreateServerPayload()
163+
.name("java-sdk-example-server-01")
164+
.machineType("t2i.1")
165+
.imageId(imageId)
166+
.labels(Map.ofEntries(Map.entry("foo", "bar")))
167+
// add the keypair we created above
168+
.keypairName(newKeypair.getName())
169+
// add the server to the network we created above
170+
.networking(
171+
new CreateServerPayloadNetworking(
172+
new CreateServerNetworking()
173+
.networkId(
174+
newNetwork.getNetworkId()))));
175+
assert newServer.getId() != null;
176+
177+
/* wait for the server creation to complete */
178+
UUID serverId = newServer.getId();
179+
assert serverId != null;
180+
while (Objects.equals(
181+
iaasApi.getServer(projectId, serverId, false).getStatus(), "CREATING")) {
182+
System.out.println("Waiting for server creation to complete ...");
183+
TimeUnit.SECONDS.sleep(5);
184+
}
185+
186+
/* update the server we just created */
187+
iaasApi.updateServer(
188+
projectId,
189+
newServer.getId(),
190+
new UpdateServerPayload()
191+
.labels(Map.ofEntries(Map.entry("foo", "bar-updated"))));
192+
193+
/* list all servers */
194+
ServerListResponse servers = iaasApi.listServers(projectId, false, null);
195+
System.out.println("\nAvailable servers: ");
196+
for (Server server : servers.getItems()) {
197+
System.out.println("* " + server.getId() + " | " + server.getName());
198+
}
199+
200+
/* fetch the server we just created */
201+
Server fetchedServer = iaasApi.getServer(projectId, serverId, false);
202+
System.out.println("\nFetched server:");
203+
System.out.println("* Name: " + fetchedServer.getName());
204+
System.out.println("* Id: " + fetchedServer.getId());
205+
if (fetchedServer.getLabels() != null) {
206+
System.out.println("* Labels: " + fetchedServer.getLabels().toString());
207+
}
208+
System.out.println("* Machine type: " + fetchedServer.getMachineType());
209+
System.out.println("* Created at: " + fetchedServer.getCreatedAt());
210+
System.out.println("* Updated at: " + fetchedServer.getUpdatedAt());
211+
System.out.println("* Launched at: " + fetchedServer.getLaunchedAt());
212+
213+
/* stop the server we just created */
214+
iaasApi.stopServer(projectId, serverId);
215+
/* wait for the server to stop */
216+
while (!Objects.equals(
217+
iaasApi.getServer(projectId, serverId, false).getPowerStatus(), "STOPPED")) {
218+
System.out.println("Waiting for server " + serverId + " to stop...");
219+
TimeUnit.SECONDS.sleep(5);
220+
}
221+
222+
/* boot the server we just created */
223+
iaasApi.startServer(projectId, serverId);
224+
/* wait for the server to boot */
225+
while (!Objects.equals(
226+
iaasApi.getServer(projectId, serverId, false).getPowerStatus(), "RUNNING")) {
227+
System.out.println("Waiting for server " + serverId + " to boot...");
228+
TimeUnit.SECONDS.sleep(5);
229+
}
230+
231+
/* reboot the server we just created */
232+
iaasApi.rebootServer(projectId, serverId, null);
233+
234+
///////////////////////////////////////////////////////
235+
// D E L E T I O N //
236+
///////////////////////////////////////////////////////
237+
238+
/* delete the server we just created */
239+
iaasApi.deleteServer(projectId, serverId);
240+
System.out.println("Deleted server: " + serverId);
241+
242+
/* wait for server deletion to complete */
243+
while (true) {
244+
try {
245+
iaasApi.getServer(projectId, serverId, false);
246+
System.out.println("Waiting for server deletion to complete...");
247+
TimeUnit.SECONDS.sleep(5);
248+
} catch (ApiException e) {
249+
if (e.getCode() == 404) {
250+
break;
251+
}
252+
}
253+
}
254+
255+
/* delete the keypair we just created */
256+
iaasApi.deleteKeyPair(newKeypair.getName());
257+
System.out.println("Deleted key pair: " + newKeypair.getName());
258+
259+
/* delete the network we just created */
260+
iaasApi.deleteNetwork(projectId, newNetwork.getNetworkId());
261+
System.out.println("Deleted network: " + newNetwork.getNetworkId());
262+
263+
} catch (ApiException | InterruptedException e) {
264+
throw new RuntimeException(e);
265+
}
266+
}
267+
}

0 commit comments

Comments
 (0)