33
44package com .azure .resourcemanager .compute ;
55
6+ import com .azure .core .http .HttpMethod ;
67import com .azure .core .http .HttpPipeline ;
8+ import com .azure .core .http .HttpPipelineBuilder ;
9+ import com .azure .core .http .policy .HttpPipelinePolicy ;
710import com .azure .core .http .rest .PagedIterable ;
811import com .azure .core .http .rest .PagedResponse ;
912import com .azure .core .http .rest .Response ;
4043import com .azure .resourcemanager .compute .models .RunCommandInputParameter ;
4144import com .azure .resourcemanager .compute .models .RunCommandResult ;
4245import com .azure .resourcemanager .compute .models .SecurityTypes ;
43- import com .azure .resourcemanager .compute .models .StorageAccountTypes ;
4446import com .azure .resourcemanager .compute .models .Sku ;
47+ import com .azure .resourcemanager .compute .models .StorageAccountTypes ;
4548import com .azure .resourcemanager .compute .models .UpgradeMode ;
4649import com .azure .resourcemanager .compute .models .VirtualMachine ;
4750import com .azure .resourcemanager .compute .models .VirtualMachineDiskOptions ;
7477import com .azure .resourcemanager .resources .models .ResourceGroup ;
7578import com .azure .resourcemanager .storage .models .StorageAccount ;
7679import com .azure .resourcemanager .storage .models .StorageAccountSkuType ;
80+ import com .azure .resourcemanager .test .utils .TestIdentifierProvider ;
7781import com .azure .security .keyvault .keys .models .KeyType ;
7882import org .junit .jupiter .api .Assertions ;
7983import org .junit .jupiter .api .Disabled ;
8993import java .util .Locale ;
9094import java .util .Map ;
9195import java .util .Set ;
96+ import java .util .UUID ;
9297import java .util .concurrent .atomic .AtomicInteger ;
9398
9499public class VirtualMachineOperationsTests extends ComputeManagementTest {
@@ -105,11 +110,13 @@ public class VirtualMachineOperationsTests extends ComputeManagementTest {
105110 private final String availabilitySetName = "availset1" ;
106111 private final String availabilitySetName2 = "availset2" ;
107112 private final ProximityPlacementGroupType proxGroupType = ProximityPlacementGroupType .STANDARD ;
113+ private AzureProfile profile ;
108114
109115 @ Override
110116 protected void initializeClients (HttpPipeline httpPipeline , AzureProfile profile ) {
111117 rgName = generateRandomResourceName ("javacsmrg" , 15 );
112118 rgName2 = generateRandomResourceName ("javacsmrg2" , 15 );
119+ this .profile = profile ;
113120 super .initializeClients (httpPipeline , profile );
114121 }
115122
@@ -2303,7 +2310,61 @@ public void canCreateDiskWithShares() {
23032310 Assertions .assertEquals (2 , disk .virtualMachineIds ().size ());
23042311 }
23052312
2313+ @ Test
2314+ public void canBeginCreateWithContext () {
2315+ final String vmName = generateRandomResourceName ("jvm" , 15 );
2316+ final String correlationId = UUID .randomUUID ().toString ();
2317+ final String correlationKey = "x-ms-correlation-id" ;
2318+ final String publicIpDnsLabel = generateRandomResourceName ("pip" , 20 );
2319+ final AtomicInteger createCounter = new AtomicInteger (0 );
2320+ HttpPipelinePolicy verificationPolicy = (context , next ) -> {
2321+ if (context .getHttpRequest ().getHttpMethod () == HttpMethod .PUT ) {
2322+ // verify that all co-related resource creation requests will have the Context information
2323+ Object correlationData = context .getContext ().getData (correlationKey ).get ();
2324+ Assertions .assertEquals (correlationId , correlationData );
2325+ createCounter .incrementAndGet ();
2326+ }
2327+ return next .process ();
2328+ };
2329+ ComputeManager localComputeManager = buildComputeManager (computeManager .httpPipeline (), verificationPolicy );
2330+
2331+ Accepted <VirtualMachine > accepted = localComputeManager .virtualMachines ()
2332+ .define (vmName )
2333+ .withRegion (region )
2334+ .withNewResourceGroup (rgName )
2335+ .withNewPrimaryNetwork ("10.0.0.0/28" )
2336+ .withPrimaryPrivateIPAddressDynamic ()
2337+ .withNewPrimaryPublicIPAddress (publicIpDnsLabel )
2338+ .withPopularLinuxImage (KnownLinuxVirtualMachineImage .UBUNTU_SERVER_20_04_LTS )
2339+ .withRootUsername ("Foo12" )
2340+ .withSsh (sshPublicKey ())
2341+ .withSize (VirtualMachineSizeTypes .STANDARD_B1S )
2342+ .beginCreate (new Context (correlationKey , correlationId ));
2343+ accepted .getFinalResult ();
2344+
2345+ // resourceGroup + network + neworkInterface + publicIp + VM = 5
2346+ Assertions .assertEquals (5 , createCounter .get ());
2347+ }
2348+
23062349 // *********************************** helper methods ***********************************
2350+ private ComputeManager buildComputeManager (HttpPipeline httpPipeline , HttpPipelinePolicy policy ) {
2351+ List <HttpPipelinePolicy > pipelinePolicies = new ArrayList <>();
2352+ for (int i = 0 ; i < httpPipeline .getPolicyCount (); i ++) {
2353+ pipelinePolicies .add (httpPipeline .getPolicy (i ));
2354+ }
2355+
2356+ pipelinePolicies .add (policy );
2357+
2358+ HttpPipeline newPipeline = new HttpPipelineBuilder ().httpClient (httpPipeline .getHttpClient ())
2359+ .policies (pipelinePolicies .toArray (new HttpPipelinePolicy [0 ]))
2360+ .tracer (httpPipeline .getTracer ())
2361+ .build ();
2362+ ComputeManager manager = ComputeManager .authenticate (newPipeline , profile );
2363+ ResourceManagerUtils .InternalRuntimeContext internalContext = new ResourceManagerUtils .InternalRuntimeContext ();
2364+ internalContext .setIdentifierFunction (name -> new TestIdentifierProvider (testResourceNamer ));
2365+ setInternalContext (internalContext , manager );
2366+ return manager ;
2367+ }
23072368
23082369 private CreatablesInfo prepareCreatableVirtualMachines (Region region , String vmNamePrefix , String networkNamePrefix ,
23092370 String publicIpNamePrefix , int vmCount ) {
0 commit comments