@@ -283,6 +283,63 @@ public static String getUserData() {
283
283
return getData (EC2_USERDATA_ROOT );
284
284
}
285
285
286
+ /**
287
+ * Retrieve some of the data from http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-instance-metadata.html as a typed
288
+ * object. This entire class will be removed as part of https://github.com/aws/aws-sdk-java-v2/issues/61, so don't rely on
289
+ * this sticking around.
290
+ *
291
+ * This should not be removed until https://github.com/aws/aws-sdk-java-v2/issues/61 is implemented.
292
+ */
293
+ public static InstanceInfo getInstanceInfo () {
294
+ return doGetInstanceInfo (getData (EC2_DYNAMICDATA_ROOT + INSTANCE_IDENTITY_DOCUMENT ));
295
+ }
296
+
297
+ static InstanceInfo doGetInstanceInfo (String json ) {
298
+ if (json != null ) {
299
+ try {
300
+ Map <String , JsonNode > jsonNode = JSON_PARSER .parse (json ).asObject ();
301
+ return new InstanceInfo (stringValue (jsonNode .get ("pendingTime" )),
302
+ stringValue (jsonNode .get ("instanceType" )),
303
+ stringValue (jsonNode .get ("imageId" )),
304
+ stringValue (jsonNode .get ("instanceId" )),
305
+ stringArrayValue (jsonNode .get ("billingProducts" )),
306
+ stringValue (jsonNode .get ("architecture" )),
307
+ stringValue (jsonNode .get ("accountId" )),
308
+ stringValue (jsonNode .get ("kernelId" )),
309
+ stringValue (jsonNode .get ("ramdiskId" )),
310
+ stringValue (jsonNode .get ("region" )),
311
+ stringValue (jsonNode .get ("version" )),
312
+ stringValue (jsonNode .get ("availabilityZone" )),
313
+ stringValue (jsonNode .get ("privateIp" )),
314
+ stringArrayValue (jsonNode .get ("devpayProductCodes" )),
315
+ stringArrayValue (jsonNode .get ("marketplaceProductCodes" )));
316
+ } catch (Exception e ) {
317
+ log .warn ("Unable to parse dynamic EC2 instance info (" + json + ") : " + e .getMessage (), e );
318
+ }
319
+ }
320
+ return null ;
321
+ }
322
+
323
+ private static String stringValue (JsonNode jsonNode ) {
324
+ if (jsonNode == null || !jsonNode .isString ()) {
325
+ return null ;
326
+ }
327
+
328
+ return jsonNode .asString ();
329
+ }
330
+
331
+ private static String [] stringArrayValue (JsonNode jsonNode ) {
332
+ if (jsonNode == null || !jsonNode .isArray ()) {
333
+ return null ;
334
+ }
335
+
336
+ return jsonNode .asArray ()
337
+ .stream ()
338
+ .filter (JsonNode ::isString )
339
+ .map (JsonNode ::asString )
340
+ .toArray (String []::new );
341
+ }
342
+
286
343
public static String getData (String path ) {
287
344
return getData (path , DEFAULT_QUERY_RETRIES );
288
345
}
@@ -587,4 +644,120 @@ public Map<String, String> headers() {
587
644
return requestHeaders ;
588
645
}
589
646
}
647
+
648
+
649
+ public static class InstanceInfo {
650
+ private final String pendingTime ;
651
+ private final String instanceType ;
652
+ private final String imageId ;
653
+ private final String instanceId ;
654
+ private final String [] billingProducts ;
655
+ private final String architecture ;
656
+ private final String accountId ;
657
+ private final String kernelId ;
658
+ private final String ramdiskId ;
659
+ private final String region ;
660
+ private final String version ;
661
+ private final String availabilityZone ;
662
+ private final String privateIp ;
663
+ private final String [] devpayProductCodes ;
664
+ private final String [] marketplaceProductCodes ;
665
+
666
+ public InstanceInfo (
667
+ String pendingTime ,
668
+ String instanceType ,
669
+ String imageId ,
670
+ String instanceId ,
671
+ String [] billingProducts ,
672
+ String architecture ,
673
+ String accountId ,
674
+ String kernelId ,
675
+ String ramdiskId ,
676
+ String region ,
677
+ String version ,
678
+ String availabilityZone ,
679
+ String privateIp ,
680
+ String [] devpayProductCodes ,
681
+ String [] marketplaceProductCodes ) {
682
+
683
+ this .pendingTime = pendingTime ;
684
+ this .instanceType = instanceType ;
685
+ this .imageId = imageId ;
686
+ this .instanceId = instanceId ;
687
+ this .billingProducts = billingProducts == null
688
+ ? null : billingProducts .clone ();
689
+ this .architecture = architecture ;
690
+ this .accountId = accountId ;
691
+ this .kernelId = kernelId ;
692
+ this .ramdiskId = ramdiskId ;
693
+ this .region = region ;
694
+ this .version = version ;
695
+ this .availabilityZone = availabilityZone ;
696
+ this .privateIp = privateIp ;
697
+ this .devpayProductCodes = devpayProductCodes == null
698
+ ? null : devpayProductCodes .clone ();
699
+ this .marketplaceProductCodes = marketplaceProductCodes == null
700
+ ? null : marketplaceProductCodes .clone ();
701
+ }
702
+
703
+ public String getPendingTime () {
704
+ return pendingTime ;
705
+ }
706
+
707
+ public String getInstanceType () {
708
+ return instanceType ;
709
+ }
710
+
711
+ public String getImageId () {
712
+ return imageId ;
713
+ }
714
+
715
+ public String getInstanceId () {
716
+ return instanceId ;
717
+ }
718
+
719
+ public String [] getBillingProducts () {
720
+ return billingProducts == null ? null : billingProducts .clone ();
721
+ }
722
+
723
+ public String getArchitecture () {
724
+ return architecture ;
725
+ }
726
+
727
+ public String getAccountId () {
728
+ return accountId ;
729
+ }
730
+
731
+ public String getKernelId () {
732
+ return kernelId ;
733
+ }
734
+
735
+ public String getRamdiskId () {
736
+ return ramdiskId ;
737
+ }
738
+
739
+ public String getRegion () {
740
+ return region ;
741
+ }
742
+
743
+ public String getVersion () {
744
+ return version ;
745
+ }
746
+
747
+ public String getAvailabilityZone () {
748
+ return availabilityZone ;
749
+ }
750
+
751
+ public String getPrivateIp () {
752
+ return privateIp ;
753
+ }
754
+
755
+ public String [] getDevpayProductCodes () {
756
+ return devpayProductCodes == null ? null : devpayProductCodes .clone ();
757
+ }
758
+
759
+ public String [] getMarketplaceProductCodes () {
760
+ return marketplaceProductCodes == null ? null : marketplaceProductCodes .clone ();
761
+ }
762
+ }
590
763
}
0 commit comments