99import java .util .Objects ;
1010import java .util .UUID ;
1111import java .util .concurrent .TimeUnit ;
12+ import java .util .logging .Level ;
1213import java .util .logging .Logger ;
1314
1415final class IaaSExample {
@@ -69,19 +70,22 @@ public static void main(String[] args) throws IOException {
6970
7071 /* fetch the network we just created */
7172 Network fetchedNetwork = iaasApi .getNetwork (projectId , newNetwork .getNetworkId ());
72- LOGGER .info ("\n Fetched network: " );
73- LOGGER .info ("* Network name: " + fetchedNetwork .getName ());
74- LOGGER .info ("* Id: " + fetchedNetwork .getNetworkId ());
75- LOGGER .info (
76- "* DHCP: " + (Boolean .TRUE .equals (fetchedNetwork .getDhcp ()) ? "YES" : "NO" ));
77- LOGGER .info ("* Gateway: " + fetchedNetwork .getGateway ());
78- LOGGER .info ("* Public IP: " + fetchedNetwork .getPublicIp ());
73+ if (LOGGER .isLoggable (Level .INFO )) {
74+ LOGGER .info ("\n Fetched network: " );
75+ LOGGER .info ("* Network name: " + fetchedNetwork .getName ());
76+ LOGGER .info ("* Id: " + fetchedNetwork .getNetworkId ());
77+ LOGGER .info ("* DHCP: " + (Boolean .TRUE .equals (fetchedNetwork .getDhcp ()) ? "YES" : "NO" ));
78+ LOGGER .info ("* Gateway: " + fetchedNetwork .getGateway ());
79+ LOGGER .info ("* Public IP: " + fetchedNetwork .getPublicIp ());
80+ }
7981
8082 /* list all available networks in the project */
8183 NetworkListResponse networks = iaasApi .listNetworks (projectId , null );
82- LOGGER .info ("\n Available networks: " );
83- for (Network network : networks .getItems ()) {
84- LOGGER .info ("* " + network .getName ());
84+ if (LOGGER .isLoggable (Level .INFO )) {
85+ LOGGER .info ("\n Available networks: " );
86+ for (Network network : networks .getItems ()) {
87+ LOGGER .info ("* " + network .getName ());
88+ }
8589 }
8690
8791 /*
@@ -92,9 +96,11 @@ public static void main(String[] args) throws IOException {
9296
9397 /* list all available images */
9498 ImageListResponse images = iaasApi .listImages (projectId , false , null );
95- LOGGER .info ("\n Available images: " );
96- for (Image image : images .getItems ()) {
97- LOGGER .info (image .getId () + " | " + image .getName ());
99+ if (LOGGER .isLoggable (Level .INFO )) {
100+ LOGGER .info ("\n Available images: " );
101+ for (Image image : images .getItems ()) {
102+ LOGGER .info (image .getId () + " | " + image .getName ());
103+ }
98104 }
99105
100106 /* get an image */
@@ -104,12 +110,14 @@ public static void main(String[] args) throws IOException {
104110 .getId (); // we just use a random image id in our example
105111 assert imageId != null ;
106112 Image fetchedImage = iaasApi .getImage (projectId , imageId );
107- LOGGER .info ("\n Fetched image:" );
108- LOGGER .info ("* Image name: " + fetchedImage .getName ());
109- LOGGER .info ("* Image id: " + fetchedImage .getId ());
110- LOGGER .info ("* Checksum: " + fetchedImage .getChecksum ());
111- LOGGER .info ("* Created at: " + fetchedImage .getCreatedAt ());
112- LOGGER .info ("* Updated at: " + fetchedImage .getUpdatedAt ());
113+ if (LOGGER .isLoggable (Level .INFO )) {
114+ LOGGER .info ("\n Fetched image:" );
115+ LOGGER .info ("* Image name: " + fetchedImage .getName ());
116+ LOGGER .info ("* Image id: " + fetchedImage .getId ());
117+ LOGGER .info ("* Checksum: " + fetchedImage .getChecksum ());
118+ LOGGER .info ("* Created at: " + fetchedImage .getCreatedAt ());
119+ LOGGER .info ("* Updated at: " + fetchedImage .getUpdatedAt ());
120+ }
113121
114122 /*
115123 * ///////////////////////////////////////////////////////
@@ -119,9 +127,11 @@ public static void main(String[] args) throws IOException {
119127
120128 /* list all available keypairs */
121129 KeyPairListResponse keypairs = iaasApi .listKeyPairs (null );
122- LOGGER .info ("\n Available keypairs: " );
123- for (Keypair keypair : keypairs .getItems ()) {
124- LOGGER .info ("* " + keypair .getName ());
130+ if (LOGGER .isLoggable (Level .INFO )) {
131+ LOGGER .info ("\n Available keypairs: " );
132+ for (Keypair keypair : keypairs .getItems ()) {
133+ LOGGER .info ("* " + keypair .getName ());
134+ }
125135 }
126136
127137 /* create a keypair */
@@ -132,7 +142,9 @@ public static void main(String[] args) throws IOException {
132142 new CreateKeyPairPayload ()
133143 .name ("java-sdk-example-keypair-01" )
134144 .publicKey (publicKey ));
135- LOGGER .info ("\n Keypair created: " + newKeypair .getName ());
145+ if (LOGGER .isLoggable (Level .INFO )) {
146+ LOGGER .info ("\n Keypair created: " + newKeypair .getName ());
147+ }
136148
137149 /* update the keypair */
138150 assert newKeypair .getName () != null ;
@@ -143,13 +155,15 @@ public static void main(String[] args) throws IOException {
143155
144156 /* fetch the keypair we just created / updated */
145157 Keypair fetchedKeypair = iaasApi .getKeyPair (newKeypair .getName ());
146- LOGGER .info ("\n Fetched key pair: " );
147- LOGGER .info ("* Name: " + fetchedKeypair .getName ());
148- if (fetchedKeypair .getLabels () != null ) {
149- LOGGER .info ("* Labels: " + fetchedKeypair .getLabels ().toString ());
158+ if (LOGGER .isLoggable (Level .INFO )) {
159+ LOGGER .info ("\n Fetched key pair: " );
160+ LOGGER .info ("* Name: " + fetchedKeypair .getName ());
161+ if (fetchedKeypair .getLabels () != null ) {
162+ LOGGER .info ("* Labels: " + fetchedKeypair .getLabels ().toString ()); // NOPMD GuardLogStatement
163+ }
164+ LOGGER .info ("* Fingerprint: " + fetchedKeypair .getFingerprint ());
165+ LOGGER .info ("* Public key: " + fetchedKeypair .getPublicKey ());
150166 }
151- LOGGER .info ("* Fingerprint: " + fetchedKeypair .getFingerprint ());
152- LOGGER .info ("* Public key: " + fetchedKeypair .getPublicKey ());
153167
154168 /*
155169 * ///////////////////////////////////////////////////////
@@ -159,21 +173,25 @@ public static void main(String[] args) throws IOException {
159173
160174 /* list all available machine types */
161175 MachineTypeListResponse machineTypes = iaasApi .listMachineTypes (projectId , null );
162- LOGGER .info ("\n Available machine types: " );
163- for (MachineType machineType : machineTypes .getItems ()) {
164- LOGGER .info ("* " + machineType .getName ());
176+ if (LOGGER .isLoggable (Level .INFO )) {
177+ LOGGER .info ("\n Available machine types: " );
178+ for (MachineType machineType : machineTypes .getItems ()) {
179+ LOGGER .info ("* " + machineType .getName ());
180+ }
165181 }
166182
167183 /* fetch details about a machine type */
168184 MachineType fetchedMachineType =
169185 iaasApi .getMachineType (projectId , machineTypes .getItems ().get (0 ).getName ());
170- LOGGER .info ("\n Fetched machine type: " );
171- LOGGER .info ("* Machine type name: " + fetchedMachineType .getName ());
172- LOGGER .info ("* Description: " + fetchedMachineType .getDescription ());
173- LOGGER .info ("* Disk size: " + fetchedMachineType .getDisk ());
174- LOGGER .info ("* RAM: " + fetchedMachineType .getRam ());
175- LOGGER .info ("* vCPUs: " + fetchedMachineType .getVcpus ());
176- LOGGER .info ("* Extra specs: " + fetchedMachineType .getExtraSpecs ());
186+ if (LOGGER .isLoggable (Level .INFO )) {
187+ LOGGER .info ("\n Fetched machine type: " );
188+ LOGGER .info ("* Machine type name: " + fetchedMachineType .getName ());
189+ LOGGER .info ("* Description: " + fetchedMachineType .getDescription ());
190+ LOGGER .info ("* Disk size: " + fetchedMachineType .getDisk ());
191+ LOGGER .info ("* RAM: " + fetchedMachineType .getRam ());
192+ LOGGER .info ("* vCPUs: " + fetchedMachineType .getVcpus ());
193+ LOGGER .info ("* Extra specs: " + fetchedMachineType .getExtraSpecs ());
194+ }
177195
178196 /*
179197 * create a server
@@ -216,30 +234,36 @@ public static void main(String[] args) throws IOException {
216234
217235 /* list all servers */
218236 ServerListResponse servers = iaasApi .listServers (projectId , false , null );
219- LOGGER .info ("\n Available servers: " );
220- for (Server server : servers .getItems ()) {
221- LOGGER .info ("* " + server .getId () + " | " + server .getName ());
237+ if (LOGGER .isLoggable (Level .INFO )) {
238+ LOGGER .info ("\n Available servers: " );
239+ for (Server server : servers .getItems ()) {
240+ LOGGER .info ("* " + server .getId () + " | " + server .getName ());
241+ }
222242 }
223243
224244 /* fetch the server we just created */
225245 Server fetchedServer = iaasApi .getServer (projectId , serverId , false );
226- LOGGER .info ("\n Fetched server:" );
227- LOGGER .info ("* Name: " + fetchedServer .getName ());
228- LOGGER .info ("* Id: " + fetchedServer .getId ());
229- if (fetchedServer .getLabels () != null ) {
230- LOGGER .info ("* Labels: " + fetchedServer .getLabels ().toString ());
246+ if (LOGGER .isLoggable (Level .INFO )) {
247+ LOGGER .info ("\n Fetched server:" );
248+ LOGGER .info ("* Name: " + fetchedServer .getName ());
249+ LOGGER .info ("* Id: " + fetchedServer .getId ());
250+ if (fetchedServer .getLabels () != null ) {
251+ LOGGER .info ("* Labels: " + fetchedServer .getLabels ().toString ()); // NOPMD GuardLogStatement
252+ }
253+ LOGGER .info ("* Machine type: " + fetchedServer .getMachineType ());
254+ LOGGER .info ("* Created at: " + fetchedServer .getCreatedAt ());
255+ LOGGER .info ("* Updated at: " + fetchedServer .getUpdatedAt ());
256+ LOGGER .info ("* Launched at: " + fetchedServer .getLaunchedAt ());
231257 }
232- LOGGER .info ("* Machine type: " + fetchedServer .getMachineType ());
233- LOGGER .info ("* Created at: " + fetchedServer .getCreatedAt ());
234- LOGGER .info ("* Updated at: " + fetchedServer .getUpdatedAt ());
235- LOGGER .info ("* Launched at: " + fetchedServer .getLaunchedAt ());
236258
237259 /* stop the server we just created */
238260 iaasApi .stopServer (projectId , serverId );
239261 /* wait for the server to stop */
240262 while (!Objects .equals (
241263 iaasApi .getServer (projectId , serverId , false ).getPowerStatus (), "STOPPED" )) {
242- LOGGER .info ("Waiting for server " + serverId + " to stop..." );
264+ if (LOGGER .isLoggable (Level .INFO )) {
265+ LOGGER .info ("Waiting for server " + serverId + " to stop..." );
266+ }
243267 TimeUnit .SECONDS .sleep (5 );
244268 }
245269
@@ -248,7 +272,9 @@ public static void main(String[] args) throws IOException {
248272 /* wait for the server to boot */
249273 while (!Objects .equals (
250274 iaasApi .getServer (projectId , serverId , false ).getPowerStatus (), "RUNNING" )) {
251- LOGGER .info ("Waiting for server " + serverId + " to boot..." );
275+ if (LOGGER .isLoggable (Level .INFO )) {
276+ LOGGER .info ("Waiting for server " + serverId + " to boot..." );
277+ }
252278 TimeUnit .SECONDS .sleep (5 );
253279 }
254280
@@ -263,13 +289,17 @@ public static void main(String[] args) throws IOException {
263289
264290 /* delete the server we just created */
265291 iaasApi .deleteServer (projectId , serverId );
266- LOGGER .info ("Deleted server: " + serverId );
292+ if (LOGGER .isLoggable (Level .INFO )) {
293+ LOGGER .info ("Deleted server: " + serverId );
294+ }
267295
268296 /* wait for server deletion to complete */
269297 while (true ) {
270298 try {
271299 iaasApi .getServer (projectId , serverId , false );
272- LOGGER .info ("Waiting for server deletion to complete..." );
300+ if (LOGGER .isLoggable (Level .INFO )) {
301+ LOGGER .info ("Waiting for server deletion to complete..." );
302+ }
273303 TimeUnit .SECONDS .sleep (5 );
274304 } catch (ApiException e ) {
275305 if (e .getCode () == HttpURLConnection .HTTP_NOT_FOUND ) {
@@ -280,11 +310,15 @@ public static void main(String[] args) throws IOException {
280310
281311 /* delete the keypair we just created */
282312 iaasApi .deleteKeyPair (newKeypair .getName ());
283- LOGGER .info ("Deleted key pair: " + newKeypair .getName ());
313+ if (LOGGER .isLoggable (Level .INFO )) {
314+ LOGGER .info ("Deleted key pair: " + newKeypair .getName ());
315+ }
284316
285317 /* delete the network we just created */
286318 iaasApi .deleteNetwork (projectId , newNetwork .getNetworkId ());
287- LOGGER .info ("Deleted network: " + newNetwork .getNetworkId ());
319+ if (LOGGER .isLoggable (Level .INFO )) {
320+ LOGGER .info ("Deleted network: " + newNetwork .getNetworkId ());
321+ }
288322
289323 } catch (ApiException | InterruptedException e ) {
290324 throw new RuntimeException (e );
0 commit comments