1+ package com .pulumi .example .provider ;
2+
3+ import com .pulumi .core .annotations .Import ;
4+ import com .pulumi .core .annotations .Export ;
5+ import com .pulumi .core .Output ;
6+ import com .pulumi .random .RandomString ;
7+ import com .pulumi .random .RandomStringArgs ;
8+ import com .pulumi .resources .ComponentResource ;
9+ import com .pulumi .resources .ComponentResourceOptions ;
10+ import com .pulumi .resources .CustomResourceOptions ;
11+ import com .pulumi .resources .ResourceArgs ;
12+ import com .pulumi .std .inputs .AbsArgs ;
13+ import com .pulumi .std .StdFunctions ;
14+ import com .pulumi .std .inputs .AbsPlainArgs ;
15+ import java .util .concurrent .CompletableFuture ;
16+ import java .net .http .HttpClient ;
17+ import java .net .http .HttpRequest ;
18+ import java .net .http .HttpResponse ;
19+ import java .net .URI ;
20+
21+ class SampleAsyncArgs extends ResourceArgs {
22+ @ Import (name ="length" , required =true )
23+ private Output <Integer > length ;
24+
25+ public Output <Integer > length () {
26+ return this .length ;
27+ }
28+
29+ private SampleAsyncArgs () {}
30+
31+ public SampleAsyncArgs (Output <Integer > length ) {
32+ this .length = length ;
33+ }
34+ }
35+
36+ // This component is a test case for a number of async operations to make sure they work
37+ // correctly for our context-aware completable future implementation.
38+ class SampleAsync extends ComponentResource {
39+ @ Export (name ="value" , refs ={String .class }, tree ="[0]" )
40+ public final Output <String > value ;
41+
42+ public SampleAsync (String name , SampleAsyncArgs args , ComponentResourceOptions opts ) {
43+ super ("javap:index:SampleAsync" , name , null , opts );
44+
45+ var resOpts = CustomResourceOptions .builder ()
46+ .parent (this )
47+ .build ();
48+
49+ // First async operation for length
50+ var asyncLength = Output .of (CompletableFuture .supplyAsync (() -> {
51+ try {
52+ Thread .sleep (2000 );
53+ return -5.0 ;
54+ } catch (InterruptedException e ) {
55+ Thread .currentThread ().interrupt ();
56+ throw new RuntimeException ("Async operation interrupted" , e );
57+ }
58+ }));
59+
60+ // Invoke a remote function.
61+ var absLength = StdFunctions .abs (AbsArgs .builder ().input (asyncLength ).build ());
62+ var absLengthInt = absLength .applyValue (d -> d .result ().intValue ());
63+
64+ // Invoke a remote function with a plain value.
65+ var absPlainLength = StdFunctions .absPlain (AbsPlainArgs .builder ().input (-1.0 ).build ());
66+ var absPlainLengthInt = Output .of (absPlainLength ).applyValue (d -> d .result ().intValue ());
67+
68+ var totalLength = Output .tuple (args .length (), absLengthInt , absPlainLengthInt ).applyValue (values -> values .t1 + values .t2 + values .t3 );
69+
70+ // Create the random string with modified length.
71+ var randomString = new RandomString (name + "1" ,
72+ RandomStringArgs .builder ()
73+ .length (totalLength )
74+ .special (false )
75+ .build (), resOpts );
76+
77+
78+ // Make HTTP request based on the input length
79+ this .value = Output .tuple (randomString .result (), totalLength )
80+ .applyValue (values -> {
81+ String randomStr = values .t1 ;
82+ Integer todoId = (values .t2 % 10 ) + 1 ;
83+
84+ var randomString2 = new RandomString (name + "2" ,
85+ RandomStringArgs .builder ()
86+ .length (totalLength )
87+ .special (false )
88+ .build (), resOpts );
89+
90+ try {
91+ HttpClient client = HttpClient .newHttpClient ();
92+ HttpRequest request = HttpRequest .newBuilder ()
93+ .uri (URI .create ("https://jsonplaceholder.typicode.com/todos/" + todoId ))
94+ .build ();
95+
96+ HttpResponse <String > response = client .send (request ,
97+ HttpResponse .BodyHandlers .ofString ());
98+ return randomStr + " - Todo #" + todoId + ": " + response .body ();
99+ } catch (Exception e ) {
100+ throw new RuntimeException ("HTTP request failed" , e );
101+ }
102+ });
103+ }
104+ }
0 commit comments