1
+ /**
2
+ * Copyright (c) 2016-2017 Oracle and/or its affiliates. All rights reserved.
3
+ */
4
+ import java .util .List ;
5
+
6
+ import com .oracle .bmc .Region ;
7
+ import com .oracle .bmc .auth .AuthenticationDetailsProvider ;
8
+ import com .oracle .bmc .auth .ConfigFileAuthenticationDetailsProvider ;
9
+ import com .oracle .bmc .core .ComputeClient ;
10
+ import com .oracle .bmc .core .model .Image ;
11
+ import com .oracle .bmc .core .requests .ListImagesRequest ;
12
+ import com .oracle .bmc .core .responses .ListImagesResponse ;
13
+
14
+ public class GetImageLatestVersionExample {
15
+
16
+ public static void main (String [] args ) throws Exception {
17
+
18
+ // TODO: Fill in these values
19
+ Region region = Region .US_PHOENIX_1 ;
20
+ String operatingSystem = "Oracle Linux" ;
21
+ String operatingSystemVersion = "6.7" ;
22
+
23
+ String configurationFilePath = "~/.oraclebmc/config" ;
24
+ String profile = "DEFAULT" ;
25
+
26
+ AuthenticationDetailsProvider provider =
27
+ new ConfigFileAuthenticationDetailsProvider (configurationFilePath , profile );
28
+
29
+ // Find matching images
30
+ ComputeClient computeClient = new ComputeClient (provider );
31
+ computeClient .setRegion (region );
32
+
33
+ ListImagesResponse listImagesResponse = computeClient .listImages (
34
+ ListImagesRequest .builder ()
35
+ .compartmentId (provider .getTenantId ())
36
+ .operatingSystem (operatingSystem )
37
+ .operatingSystemVersion (operatingSystemVersion )
38
+ .build ());
39
+
40
+ computeClient .close ();
41
+
42
+ List <Image > matchingImages = listImagesResponse .getItems ();
43
+
44
+ if (matchingImages .size () == 0 ) {
45
+ System .out .println ("No matching images found." );
46
+ }
47
+ else {
48
+ /*
49
+ * Images are sorted by system images first, then custom images, secondarily sorted by image display name.
50
+ * Since image display names take the format of "<OS>-<OSVersion>-<Year>.<Month>.<Day>-<Iteration>",
51
+ * the first image returned for a list operation sorted by OS and OS version will be the the latest system
52
+ * image for that OS / OS version.
53
+ */
54
+ Image latestImageVersion = matchingImages .get (0 );
55
+
56
+ System .out .println (latestImageVersion );
57
+ }
58
+ }
59
+ }
0 commit comments