@@ -37,6 +37,8 @@ public static void main(String[] args) throws IOException {
3737 }
3838 UUID projectId = UUID .fromString (projectIdString );
3939
40+ String region = "eu01" ;
41+
4042 try {
4143 /*
4244 * ///////////////////////////////////////////////////////
@@ -49,38 +51,40 @@ public static void main(String[] args) throws IOException {
4951 Network newNetwork =
5052 iaasApi .createNetwork (
5153 projectId ,
54+ region ,
5255 new CreateNetworkPayload ()
5356 .name ("java-sdk-example-network-01" )
5457 .dhcp (true )
5558 .routed (false )
5659 .labels (Collections .singletonMap ("some-network-label" , "bar" ))
57- .addressFamily (
58- new CreateNetworkAddressFamily ()
59- .ipv4 (
60- new CreateNetworkIPv4Body ()
61- .addNameserversItem (
62- "8.8.8.8" ))));
63-
60+ .ipv4 (
61+ new CreateNetworkIPv4 (
62+ new CreateNetworkIPv4WithPrefixLength ()
63+ .addNameserversItem ("8.8.8.8" )
64+ .prefixLength (24L ))));
6465 /* update the network we just created */
6566 iaasApi .partialUpdateNetwork (
6667 projectId ,
67- newNetwork .getNetworkId (),
68+ region ,
69+ newNetwork .getId (),
6870 new PartialUpdateNetworkPayload ()
6971 .dhcp (false )
7072 .labels (Collections .singletonMap ("some-network-label" , "bar-updated" )));
7173
7274 /* fetch the network we just created */
73- Network fetchedNetwork = iaasApi .getNetwork (projectId , newNetwork .getNetworkId ());
75+ Network fetchedNetwork = iaasApi .getNetwork (projectId , region , newNetwork .getId ());
7476 System .out .println ("\n Fetched network: " );
7577 System .out .println ("* Network name: " + fetchedNetwork .getName ());
76- System .out .println ("* Id: " + fetchedNetwork .getNetworkId ());
78+ System .out .println ("* Id: " + fetchedNetwork .getId ());
7779 System .out .println (
7880 "* DHCP: " + (Boolean .TRUE .equals (fetchedNetwork .getDhcp ()) ? "YES" : "NO" ));
79- System .out .println ("* Gateway: " + fetchedNetwork .getGateway ());
80- System .out .println ("* Public IP: " + fetchedNetwork .getPublicIp ());
81+ if (fetchedNetwork .getIpv4 () != null ) {
82+ System .out .println ("* Gateway: " + fetchedNetwork .getIpv4 ().getGateway ());
83+ System .out .println ("* Public IP: " + fetchedNetwork .getIpv4 ().getPublicIp ());
84+ }
8185
8286 /* list all available networks in the project */
83- NetworkListResponse networks = iaasApi .listNetworks (projectId , null );
87+ NetworkListResponse networks = iaasApi .listNetworks (projectId , region , null );
8488 System .out .println ("\n Available networks: " );
8589 for (Network network : networks .getItems ()) {
8690 System .out .println ("* " + network .getName ());
@@ -93,7 +97,7 @@ public static void main(String[] args) throws IOException {
9397 * */
9498
9599 /* list all available images */
96- ImageListResponse images = iaasApi .listImages (projectId , false , null );
100+ ImageListResponse images = iaasApi .listImages (projectId , region , false , null );
97101 System .out .println ("\n Available images: " );
98102 for (Image image : images .getItems ()) {
99103 System .out .println (image .getId () + " | " + image .getName ());
@@ -104,8 +108,8 @@ public static void main(String[] args) throws IOException {
104108 images .getItems ()
105109 .get (0 )
106110 .getId (); // we just use a random image id in our example
107- assert imageId != null ;
108- Image fetchedImage = iaasApi .getImage (projectId , imageId );
111+ Objects . requireNonNull ( imageId != null ) ;
112+ Image fetchedImage = iaasApi .getImage (projectId , region , imageId );
109113 System .out .println ("\n Fetched image:" );
110114 System .out .println ("* Image name: " + fetchedImage .getName ());
111115 System .out .println ("* Image id: " + fetchedImage .getId ());
@@ -137,7 +141,7 @@ public static void main(String[] args) throws IOException {
137141 System .out .println ("\n Keypair created: " + newKeypair .getName ());
138142
139143 /* update the keypair */
140- assert newKeypair .getName () != null ;
144+ Objects . requireNonNull ( newKeypair .getName ()) ;
141145 iaasApi .updateKeyPair (
142146 newKeypair .getName (),
143147 new UpdateKeyPairPayload ()
@@ -160,15 +164,17 @@ public static void main(String[] args) throws IOException {
160164 * */
161165
162166 /* list all available machine types */
163- MachineTypeListResponse machineTypes = iaasApi .listMachineTypes (projectId , null );
167+ MachineTypeListResponse machineTypes =
168+ iaasApi .listMachineTypes (projectId , region , null );
164169 System .out .println ("\n Available machine types: " );
165170 for (MachineType machineType : machineTypes .getItems ()) {
166171 System .out .println ("* " + machineType .getName ());
167172 }
168173
169174 /* fetch details about a machine type */
170175 MachineType fetchedMachineType =
171- iaasApi .getMachineType (projectId , machineTypes .getItems ().get (0 ).getName ());
176+ iaasApi .getMachineType (
177+ projectId , region , machineTypes .getItems ().get (0 ).getName ());
172178 System .out .println ("\n Fetched machine type: " );
173179 System .out .println ("* Machine type name: " + fetchedMachineType .getName ());
174180 System .out .println ("* Description: " + fetchedMachineType .getDescription ());
@@ -177,102 +183,121 @@ public static void main(String[] args) throws IOException {
177183 System .out .println ("* vCPUs: " + fetchedMachineType .getVcpus ());
178184 System .out .println ("* Extra specs: " + fetchedMachineType .getExtraSpecs ());
179185
180- /*
181- * create a server
182- *
183- * NOTE: see the following link for available machine types
184- * https://docs.stackit.cloud/products/compute-engine/server/basics/machine-types/
185- *
186- * */
187- Server newServer =
188- iaasApi .createServer (
189- projectId ,
190- new CreateServerPayload ()
191- .name ("java-sdk-example-server-01" )
192- .machineType ("t2i.1" )
193- .imageId (imageId )
194- .labels (Collections .singletonMap ("foo" , "bar" ))
195- // add the keypair we created above
196- .keypairName (newKeypair .getName ())
197- // add the server to the network we created above
198- .networking (
199- new CreateServerPayloadNetworking (
200- new CreateServerNetworking ()
201- .networkId (
202- newNetwork .getNetworkId ()))));
203- assert newServer .getId () != null ;
204-
205- /* wait for the server creation to complete */
206- UUID serverId = newServer .getId ();
207- assert serverId != null ;
208- while (Objects .equals (
209- iaasApi .getServer (projectId , serverId , false ).getStatus (), "CREATING" )) {
210- System .out .println ("Waiting for server creation to complete ..." );
211- TimeUnit .SECONDS .sleep (5 );
212- }
186+ UUID serverId = null ;
187+ try {
188+
189+ /*
190+ * create a server
191+ *
192+ * NOTE: see the following link for available machine types
193+ * https://docs.stackit.cloud/products/compute-engine/server/basics/machine-types/
194+ *
195+ * */
196+ Server newServer =
197+ iaasApi .createServer (
198+ projectId ,
199+ region ,
200+ new CreateServerPayload ()
201+ .name ("java-sdk-example-server-01" )
202+ .machineType ("t2i.1" )
203+ .bootVolume (
204+ new BootVolume ()
205+ .deleteOnTermination (true )
206+ .size (32L )
207+ .source (
208+ new BootVolumeSource ()
209+ .id (imageId )
210+ .type ("image" )))
211+ .labels (Collections .singletonMap ("foo" , "bar" ))
212+ // add the keypair we created above
213+ .keypairName (newKeypair .getName ())
214+ // add the server to the network we created above
215+ .networking (
216+ new CreateServerPayloadAllOfNetworking (
217+ new CreateServerNetworking ()
218+ .networkId (newNetwork .getId ()))));
219+ Objects .requireNonNull (newServer .getId ());
220+
221+ /* wait for the server creation to complete */
222+ serverId = newServer .getId ();
223+ Objects .requireNonNull (serverId );
224+ while (Objects .equals (
225+ iaasApi .getServer (projectId , region , serverId , false ).getStatus (),
226+ "CREATING" )) {
227+ System .out .println ("Waiting for server creation to complete ..." );
228+ TimeUnit .SECONDS .sleep (5 );
229+ }
213230
214- /* update the server we just created */
215- iaasApi .updateServer (
216- projectId ,
217- newServer .getId (),
218- new UpdateServerPayload ()
219- .labels (Collections .singletonMap ("foo" , "bar-updated" )));
220-
221- /* list all servers */
222- ServerListResponse servers = iaasApi .listServers (projectId , false , null );
223- System .out .println ("\n Available servers: " );
224- for (Server server : servers .getItems ()) {
225- System .out .println ("* " + server .getId () + " | " + server .getName ());
226- }
231+ /* update the server we just created */
232+ iaasApi .updateServer (
233+ projectId ,
234+ region ,
235+ newServer .getId (),
236+ new UpdateServerPayload ()
237+ .labels (Collections .singletonMap ("foo" , "bar-updated" )));
238+
239+ /* list all servers */
240+ ServerListResponse servers = iaasApi .listServers (projectId , region , false , null );
241+ System .out .println ("\n Available servers: " );
242+ for (Server server : servers .getItems ()) {
243+ System .out .println ("* " + server .getId () + " | " + server .getName ());
244+ }
227245
228- /* fetch the server we just created */
229- Server fetchedServer = iaasApi .getServer (projectId , serverId , false );
230- System .out .println ("\n Fetched server:" );
231- System .out .println ("* Name: " + fetchedServer .getName ());
232- System .out .println ("* Id: " + fetchedServer .getId ());
233- if (fetchedServer .getLabels () != null ) {
234- System .out .println ("* Labels: " + fetchedServer .getLabels ().toString ());
235- }
236- System .out .println ("* Machine type: " + fetchedServer .getMachineType ());
237- System .out .println ("* Created at: " + fetchedServer .getCreatedAt ());
238- System .out .println ("* Updated at: " + fetchedServer .getUpdatedAt ());
239- System .out .println ("* Launched at: " + fetchedServer .getLaunchedAt ());
240-
241- /* stop the server we just created */
242- iaasApi .stopServer (projectId , serverId );
243- /* wait for the server to stop */
244- while (!Objects .equals (
245- iaasApi .getServer (projectId , serverId , false ).getPowerStatus (), "STOPPED" )) {
246- System .out .println ("Waiting for server " + serverId + " to stop..." );
247- TimeUnit .SECONDS .sleep (5 );
248- }
246+ /* fetch the server we just created */
247+ Server fetchedServer = iaasApi .getServer (projectId , region , serverId , false );
248+ System .out .println ("\n Fetched server:" );
249+ System .out .println ("* Name: " + fetchedServer .getName ());
250+ System .out .println ("* Id: " + fetchedServer .getId ());
251+ if (fetchedServer .getLabels () != null ) {
252+ System .out .println ("* Labels: " + fetchedServer .getLabels ().toString ());
253+ }
254+ System .out .println ("* Machine type: " + fetchedServer .getMachineType ());
255+ System .out .println ("* Created at: " + fetchedServer .getCreatedAt ());
256+ System .out .println ("* Updated at: " + fetchedServer .getUpdatedAt ());
257+ System .out .println ("* Launched at: " + fetchedServer .getLaunchedAt ());
258+
259+ /* stop the server we just created */
260+ iaasApi .stopServer (projectId , region , serverId );
261+ /* wait for the server to stop */
262+ while (!Objects .equals (
263+ iaasApi .getServer (projectId , region , serverId , false ).getPowerStatus (),
264+ "STOPPED" )) {
265+ System .out .println ("Waiting for server " + serverId + " to stop..." );
266+ TimeUnit .SECONDS .sleep (5 );
267+ }
249268
250- /* boot the server we just created */
251- iaasApi .startServer (projectId , serverId );
252- /* wait for the server to boot */
253- while (!Objects .equals (
254- iaasApi .getServer (projectId , serverId , false ).getPowerStatus (), "RUNNING" )) {
255- System .out .println ("Waiting for server " + serverId + " to boot..." );
256- TimeUnit .SECONDS .sleep (5 );
257- }
269+ /* boot the server we just created */
270+ iaasApi .startServer (projectId , region , serverId );
271+ /* wait for the server to boot */
272+ while (!Objects .equals (
273+ iaasApi .getServer (projectId , region , serverId , false ).getPowerStatus (),
274+ "RUNNING" )) {
275+ System .out .println ("Waiting for server " + serverId + " to boot..." );
276+ TimeUnit .SECONDS .sleep (5 );
277+ }
258278
259- /* reboot the server we just created */
260- iaasApi .rebootServer (projectId , serverId , null );
279+ /* reboot the server we just created */
280+ iaasApi .rebootServer (projectId , region , serverId , null );
261281
282+ } catch (ApiException e ) {
283+ System .out .println ("server creation failed" + e );
284+ }
262285 /*
263286 * ///////////////////////////////////////////////////////
264287 * // D E L E T I O N //
265288 * ///////////////////////////////////////////////////////
266289 * */
267290
268291 /* delete the server we just created */
269- iaasApi .deleteServer (projectId , serverId );
270- System .out .println ("Deleted server: " + serverId );
292+ if (serverId != null ) {
293+ iaasApi .deleteServer (projectId , region , serverId );
294+ System .out .println ("Deleted server: " + serverId );
295+ }
271296
272297 /* wait for server deletion to complete */
273298 while (true ) {
274299 try {
275- iaasApi .getServer (projectId , serverId , false );
300+ iaasApi .getServer (projectId , region , serverId , false );
276301 System .out .println ("Waiting for server deletion to complete..." );
277302 TimeUnit .SECONDS .sleep (5 );
278303 } catch (ApiException e ) {
@@ -287,8 +312,8 @@ public static void main(String[] args) throws IOException {
287312 System .out .println ("Deleted key pair: " + newKeypair .getName ());
288313
289314 /* delete the network we just created */
290- iaasApi .deleteNetwork (projectId , newNetwork .getNetworkId ());
291- System .out .println ("Deleted network: " + newNetwork .getNetworkId ());
315+ iaasApi .deleteNetwork (projectId , region , newNetwork .getId ());
316+ System .out .println ("Deleted network: " + newNetwork .getId ());
292317
293318 } catch (ApiException | InterruptedException e ) {
294319 throw new RuntimeException (e );
0 commit comments