55import cloud .stackit .sdk .iaas .model .*;
66import java .io .IOException ;
77import java .util .Map ;
8+ import java .util .Objects ;
89import java .util .UUID ;
10+ import java .util .concurrent .TimeUnit ;
911
1012public class IaaSExample {
1113 public static void main (String [] args ) throws IOException {
@@ -27,13 +29,28 @@ public static void main(String[] args) throws IOException {
2729 ///////////////////////////////////////////////////////
2830
2931 /* create a network in the project */
30- // TODO
3132 Network newNetwork =
3233 iaasApi .createNetwork (
3334 projectId ,
34- new CreateNetworkPayload ().name ("java-sdk-example-network-01" ));
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" ))));
3546
36- /* update the network we just created*/
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" ))));
3754
3855 /* fetch the network we just created */
3956 Network fetchedNetwork = iaasApi .getNetwork (projectId , newNetwork .getNetworkId ());
@@ -118,21 +135,54 @@ public static void main(String[] args) throws IOException {
118135 // S E R V E R S //
119136 ///////////////////////////////////////////////////////
120137
138+ /* list all available machine types */
139+ MachineTypeListResponse machineTypes = iaasApi .listMachineTypes (projectId , null );
140+ System .out .println ("\n Available 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 ("\n Fetched 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+
121156 /* create a server */
122157 // NOTE: see https://docs.stackit.cloud/stackit/en/virtual-machine-flavors-75137231.html
123158 // for available machine types
124- String machineType = "t2i.1" ;
125159 Server newServer =
126160 iaasApi .createServer (
127161 projectId ,
128162 new CreateServerPayload ()
129163 .name ("java-sdk-example-server-01" )
130- .machineType (machineType )
164+ .machineType ("t2i.1" )
131165 .imageId (imageId )
132166 .labels (Map .ofEntries (Map .entry ("foo" , "bar" )))
133- .keypairName (newKeypair .getName ()));
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 ()))));
134175 assert newServer .getId () != null ;
135176
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+
136186 /* update the server we just created */
137187 iaasApi .updateServer (
138188 projectId ,
@@ -148,8 +198,6 @@ public static void main(String[] args) throws IOException {
148198 }
149199
150200 /* fetch the server we just created */
151- UUID serverId = newServer .getId ();
152- assert serverId != null ;
153201 Server fetchedServer = iaasApi .getServer (projectId , serverId , false );
154202 System .out .println ("\n Fetched server:" );
155203 System .out .println ("* Name: " + fetchedServer .getName ());
@@ -164,9 +212,21 @@ public static void main(String[] args) throws IOException {
164212
165213 /* stop the server we just created */
166214 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+ }
167221
168222 /* boot the server we just created */
169223 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+ }
170230
171231 /* reboot the server we just created */
172232 iaasApi .rebootServer (projectId , serverId , null );
@@ -179,6 +239,19 @@ public static void main(String[] args) throws IOException {
179239 iaasApi .deleteServer (projectId , serverId );
180240 System .out .println ("Deleted server: " + serverId );
181241
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+
182255 /* delete the keypair we just created */
183256 iaasApi .deleteKeyPair (newKeypair .getName ());
184257 System .out .println ("Deleted key pair: " + newKeypair .getName ());
@@ -187,7 +260,7 @@ public static void main(String[] args) throws IOException {
187260 iaasApi .deleteNetwork (projectId , newNetwork .getNetworkId ());
188261 System .out .println ("Deleted network: " + newNetwork .getNetworkId ());
189262
190- } catch (ApiException e ) {
263+ } catch (ApiException | InterruptedException e ) {
191264 throw new RuntimeException (e );
192265 }
193266 }
0 commit comments