|
| 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 | +} |
0 commit comments