Skip to content

Commit 58dcd34

Browse files
authored
Use embedme for MIGRATION GUIDE (Azure#17851)
* add migration guide samples * npx embedme MIGRATION_GUIDE.md * add back revapt since it may be conflict for another PR
1 parent e91c827 commit 58dcd34

File tree

3 files changed

+198
-21
lines changed

3 files changed

+198
-21
lines changed

sdk/resourcemanager/azure-resourcemanager/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,12 @@
184184
<version>1.6.3</version> <!-- {x-version-update;com.azure:azure-core-http-netty;dependency} -->
185185
<scope>test</scope>
186186
</dependency>
187+
<dependency>
188+
<groupId>com.azure</groupId>
189+
<artifactId>azure-core-http-okhttp</artifactId>
190+
<version>1.3.3</version> <!-- {x-version-update;com.azure:azure-core-http-okhttp;dependency} -->
191+
<scope>test</scope>
192+
</dependency>
187193
<dependency>
188194
<groupId>com.jcraft</groupId>
189195
<artifactId>jsch</artifactId>
Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
package com.azure.resourcemanager;
5+
6+
import com.azure.core.credential.TokenCredential;
7+
import com.azure.core.http.HttpClient;
8+
import com.azure.core.http.HttpPipelineCallContext;
9+
import com.azure.core.http.HttpPipelineNextPolicy;
10+
import com.azure.core.http.HttpResponse;
11+
import com.azure.core.http.ProxyOptions;
12+
import com.azure.core.http.okhttp.OkHttpAsyncHttpClientBuilder;
13+
import com.azure.core.http.policy.HttpPipelinePolicy;
14+
import com.azure.core.management.AzureEnvironment;
15+
import com.azure.core.management.Region;
16+
import com.azure.core.management.exception.ManagementException;
17+
import com.azure.core.management.profile.AzureProfile;
18+
import com.azure.identity.ClientSecretCredentialBuilder;
19+
import com.azure.resourcemanager.network.models.TransportProtocol;
20+
import reactor.core.publisher.Flux;
21+
import reactor.core.publisher.Mono;
22+
23+
import java.net.InetSocketAddress;
24+
import java.util.ArrayList;
25+
import java.util.List;
26+
27+
/**
28+
* WARNING: MODIFYING THIS FILE WILL REQUIRE CORRESPONDING UPDATES TO README.md FILE. LINE NUMBERS
29+
* ARE USED TO EXTRACT APPROPRIATE CODE SEGMENTS FROM THIS FILE. ADD NEW CODE AT THE BOTTOM TO AVOID CHANGING
30+
* LINE NUMBERS OF EXISTING CODE SAMPLES.
31+
*
32+
* Code samples for the MIGRATION_GUIDE.md
33+
*/
34+
public class MigrationGuideSamples {
35+
// extra empty lines to compensate import lines
36+
37+
38+
39+
40+
41+
42+
43+
44+
45+
46+
47+
48+
49+
50+
51+
52+
53+
54+
55+
56+
57+
58+
59+
60+
61+
62+
63+
64+
65+
66+
67+
68+
69+
70+
// THIS LINE MUST BE AT LINE NO. 70
71+
public void authetication() {
72+
TokenCredential credential = new ClientSecretCredentialBuilder()
73+
.clientId("<ClientId>")
74+
.clientSecret("<ClientSecret>")
75+
.tenantId("<TenantId>")
76+
.build();
77+
AzureProfile profile = new AzureProfile("<TenantId>", "<SubscriptionId>", AzureEnvironment.AZURE);
78+
}
79+
80+
public void customizedPolicy(TokenCredential credential, AzureProfile profile) {
81+
AzureResourceManager azure = AzureResourceManager.configure()
82+
.withPolicy(new CustomizedPolicy())
83+
.authenticate(credential, profile)
84+
.withDefaultSubscription();
85+
}
86+
87+
public static class CustomizedPolicy implements HttpPipelinePolicy {
88+
@Override
89+
public Mono<HttpResponse> process(HttpPipelineCallContext context, HttpPipelineNextPolicy next) {
90+
return next.process();
91+
}
92+
}
93+
94+
public void customizedHttpClient(TokenCredential credential, AzureProfile profile) {
95+
HttpClient client = new OkHttpAsyncHttpClientBuilder()
96+
.proxy(new ProxyOptions(ProxyOptions.Type.HTTP, new InetSocketAddress("127.0.0.1", 8888)))
97+
.build();
98+
99+
AzureResourceManager azure = AzureResourceManager.configure()
100+
.withHttpClient(client)
101+
.authenticate(credential, profile)
102+
.withDefaultSubscription();
103+
}
104+
105+
public void errorHandling(AzureResourceManager azure) {
106+
final String resourceGroupName = "invalid resource group name";
107+
try {
108+
azure.resourceGroups().define(resourceGroupName)
109+
.withRegion(Region.US_WEST2)
110+
.create();
111+
} catch (ManagementException e) {
112+
System.err.printf("Response code: %s%n", e.getValue().getCode());
113+
System.err.printf("Response message: %s%n", e.getValue().getMessage());
114+
}
115+
}
116+
117+
public void asynchronizeCreation(AzureResourceManager azure) {
118+
String rgName = "";
119+
Region region = Region.US_EAST;
120+
String vnetName = "";
121+
String publicIpName = "";
122+
String loadBalancerName1 = "";
123+
String httpLoadBalancingRule = "";
124+
String frontendName = "";
125+
String backendPoolName1 = "";
126+
String httpProbe = "";
127+
String httpsLoadBalancingRule = "";
128+
String backendPoolName2 = "";
129+
String httpsProbe = "";
130+
String natPool50XXto22 = "";
131+
String natPool60XXto23 = "";
132+
133+
final List<Object> createdResources = new ArrayList<>();
134+
azure.resourceGroups().define(rgName).withRegion(region).create();
135+
Flux.merge(
136+
azure.networks().define(vnetName)
137+
.withRegion(region)
138+
.withExistingResourceGroup(rgName)
139+
.withAddressSpace("172.16.0.0/16")
140+
.defineSubnet("Front-end").withAddressPrefix("172.16.1.0/24").attach()
141+
.createAsync(),
142+
azure.publicIpAddresses().define(publicIpName)
143+
.withRegion(region)
144+
.withExistingResourceGroup(rgName)
145+
.withLeafDomainLabel(publicIpName)
146+
.createAsync()
147+
.flatMapMany(publicIp -> Flux.merge(
148+
Flux.just(publicIp),
149+
azure.loadBalancers().define(loadBalancerName1)
150+
.withRegion(region)
151+
.withExistingResourceGroup(rgName)
152+
// Add two rules that uses above backend and probe
153+
.defineLoadBalancingRule(httpLoadBalancingRule).withProtocol(TransportProtocol.TCP).fromFrontend(frontendName).fromFrontendPort(80).toBackend(backendPoolName1).withProbe(httpProbe).attach()
154+
.defineLoadBalancingRule(httpsLoadBalancingRule).withProtocol(TransportProtocol.TCP).fromFrontend(frontendName).fromFrontendPort(443).toBackend(backendPoolName2).withProbe(httpsProbe).attach()
155+
// Add nat pools to enable direct VM connectivity for SSH to port 22 and TELNET to port 23
156+
.defineInboundNatPool(natPool50XXto22).withProtocol(TransportProtocol.TCP).fromFrontend(frontendName).fromFrontendPortRange(5000, 5099).toBackendPort(22).attach()
157+
.defineInboundNatPool(natPool60XXto23).withProtocol(TransportProtocol.TCP).fromFrontend(frontendName).fromFrontendPortRange(6000, 6099).toBackendPort(23).attach()
158+
// Explicitly define the frontend
159+
.definePublicFrontend(frontendName).withExistingPublicIpAddress(publicIp).attach()
160+
// Add two probes one per rule
161+
.defineHttpProbe(httpProbe).withRequestPath("/").withPort(80).attach()
162+
.defineHttpProbe(httpsProbe).withRequestPath("/").withPort(443).attach()
163+
.createAsync()))
164+
)
165+
.doOnNext(createdResources::add)
166+
.blockLast();
167+
}
168+
}

sdk/resourcemanager/docs/MIGRATION_GUIDE.md

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ ApplicationTokenCredential = new ApplicationTokenCredentials("<ClientId>", "<Ten
5454

5555
**Equivalent in new version (`com.azure.resourcemanager.**`)**
5656

57+
<!-- embedme ../azure-resourcemanager/src/samples/java/com/azure/resourcemanager/MigrationGuideSamples.java#L72-L77 -->
5758
```java
5859
TokenCredential credential = new ClientSecretCredentialBuilder()
5960
.clientId("<ClientId>")
@@ -99,6 +100,7 @@ Azure azure = Azure.configure()
99100

100101
**Equivalent in new version (`com.azure.resourcemanager.**`)**
101102

103+
<!-- embedme ../azure-resourcemanager/src/samples/java/com/azure/resourcemanager/MigrationGuideSamples.java#L81-L84 -->
102104
```java
103105
AzureResourceManager azure = AzureResourceManager.configure()
104106
.withPolicy(new CustomizedPolicy())
@@ -124,6 +126,7 @@ Azure azure = Azure.authenticate(client, "<TenantId>")
124126

125127
**Equivalent in new version (`com.azure.resourcemanager.**`)**
126128

129+
<!-- embedme ../azure-resourcemanager/src/samples/java/com/azure/resourcemanager/MigrationGuideSamples.java#L95-L102 -->
127130
```java
128131
HttpClient client = new OkHttpAsyncHttpClientBuilder()
129132
.proxy(new ProxyOptions(ProxyOptions.Type.HTTP, new InetSocketAddress("127.0.0.1", 8888)))
@@ -155,6 +158,7 @@ try {
155158

156159
**Equivalent in new version (`com.azure.resourcemanager.**`)**
157160

161+
<!-- embedme ../azure-resourcemanager/src/samples/java/com/azure/resourcemanager/MigrationGuideSamples.java#L106-L114 -->
158162
```java
159163
final String resourceGroupName = "invalid resource group name";
160164
try {
@@ -238,6 +242,7 @@ Observable.merge(
238242

239243
**Equivalent in new version (`com.azure.resourcemanager.**`)**
240244

245+
<!-- embedme ../azure-resourcemanager/src/samples/java/com/azure/resourcemanager/MigrationGuideSamples.java#L133-L166 -->
241246
```java
242247
final List<Object> createdResources = new ArrayList<>();
243248
azure.resourceGroups().define(rgName).withRegion(region).create();
@@ -253,28 +258,26 @@ Flux.merge(
253258
.withExistingResourceGroup(rgName)
254259
.withLeafDomainLabel(publicIpName)
255260
.createAsync()
256-
.flatMapMany(publicIp -> {
257-
return Flux.merge(
258-
Flux.just(publicIp),
259-
azure.loadBalancers().define(loadBalancerName1)
260-
.withRegion(region)
261-
.withExistingResourceGroup(rgName)
262-
// Add two rules that uses above backend and probe
263-
.defineLoadBalancingRule(httpLoadBalancingRule).withProtocol(TransportProtocol.TCP).fromFrontend(frontendName).fromFrontendPort(80).toBackend(backendPoolName1).withProbe(httpProbe).attach()
264-
.defineLoadBalancingRule(httpsLoadBalancingRule).withProtocol(TransportProtocol.TCP).fromFrontend(frontendName).fromFrontendPort(443).toBackend(backendPoolName2).withProbe(httpsProbe).attach()
265-
// Add nat pools to enable direct VM connectivity for SSH to port 22 and TELNET to port 23
266-
.defineInboundNatPool(natPool50XXto22).withProtocol(TransportProtocol.TCP).fromFrontend(frontendName).fromFrontendPortRange(5000, 5099).toBackendPort(22).attach()
267-
.defineInboundNatPool(natPool60XXto23).withProtocol(TransportProtocol.TCP).fromFrontend(frontendName).fromFrontendPortRange(6000, 6099).toBackendPort(23).attach()
268-
// Explicitly define the frontend
269-
.definePublicFrontend(frontendName).withExistingPublicIpAddress(publicIp).attach()
270-
// Add two probes one per rule
271-
.defineHttpProbe(httpProbe).withRequestPath("/").withPort(80).attach()
272-
.defineHttpProbe(httpsProbe).withRequestPath("/").withPort(443).attach()
273-
.createAsync());
274-
})
261+
.flatMapMany(publicIp -> Flux.merge(
262+
Flux.just(publicIp),
263+
azure.loadBalancers().define(loadBalancerName1)
264+
.withRegion(region)
265+
.withExistingResourceGroup(rgName)
266+
// Add two rules that uses above backend and probe
267+
.defineLoadBalancingRule(httpLoadBalancingRule).withProtocol(TransportProtocol.TCP).fromFrontend(frontendName).fromFrontendPort(80).toBackend(backendPoolName1).withProbe(httpProbe).attach()
268+
.defineLoadBalancingRule(httpsLoadBalancingRule).withProtocol(TransportProtocol.TCP).fromFrontend(frontendName).fromFrontendPort(443).toBackend(backendPoolName2).withProbe(httpsProbe).attach()
269+
// Add nat pools to enable direct VM connectivity for SSH to port 22 and TELNET to port 23
270+
.defineInboundNatPool(natPool50XXto22).withProtocol(TransportProtocol.TCP).fromFrontend(frontendName).fromFrontendPortRange(5000, 5099).toBackendPort(22).attach()
271+
.defineInboundNatPool(natPool60XXto23).withProtocol(TransportProtocol.TCP).fromFrontend(frontendName).fromFrontendPortRange(6000, 6099).toBackendPort(23).attach()
272+
// Explicitly define the frontend
273+
.definePublicFrontend(frontendName).withExistingPublicIpAddress(publicIp).attach()
274+
// Add two probes one per rule
275+
.defineHttpProbe(httpProbe).withRequestPath("/").withPort(80).attach()
276+
.defineHttpProbe(httpsProbe).withRequestPath("/").withPort(443).attach()
277+
.createAsync()))
275278
)
276-
.doOnNext(createdResources::add)
277-
.blockLast();
279+
.doOnNext(createdResources::add)
280+
.blockLast();
278281
```
279282

280283
[**Link to full sample**](https://github.com/Azure/azure-sdk-for-java/blob/15b8e62/sdk/resourcemanager/azure-resourcemanager-samples/src/main/java/com/azure/resourcemanager/compute/samples/ManageVirtualMachineScaleSetAsync.java#L88)

0 commit comments

Comments
 (0)