Skip to content

Commit

Permalink
support configured http client inherited by various clients (#10565)
Browse files Browse the repository at this point in the history
* support configured http client inherited by various clients

* update TestBase

* update samples

* fix redirect issue from curl getString

* fix redirect issue from curl getString in app service tests

* clean code
  • Loading branch information
xseeseesee authored Apr 30, 2020
1 parent 4882f41 commit f4babca
Show file tree
Hide file tree
Showing 13 changed files with 36 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public void canDeployMultipleWars() throws Exception {
Assertions.assertNotNull(body);
Assertions.assertTrue(body.contains("Azure Samples Hello World"));

response = curl("http://" + webappName + "." + "azurewebsites.net/app2");
response = curl("http://" + webappName + "." + "azurewebsites.net/app2/");
Assertions.assertEquals(200, response.getStatusCode());
body = response.getValue();
Assertions.assertNotNull(body);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public void canDeployWarFile() throws Exception {
Assertions.assertNotNull(deployment);
if (!isPlaybackMode()) {
SdkContext.sleep(10000);
Response<String> response = curl("http://" + webApp1.defaultHostName() + "/helloworld");
Response<String> response = curl("http://" + webApp1.defaultHostName() + "/helloworld/");
Assertions.assertEquals(200, response.getStatusCode());
String body = response.getValue();
Assertions.assertNotNull(body);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,10 @@ public static boolean runSample(Azure azure) {

// warm up
System.out.println("Warming up " + app2Url + "/helloworld...");
Utils.curl("http://" + app2Url + "/helloworld");
Utils.curl("http://" + app2Url + "/helloworld/");
SdkContext.sleep(5000);
System.out.println("CURLing " + app2Url + "/helloworld...");
System.out.println(Utils.curl("http://" + app2Url + "/helloworld"));
System.out.println(Utils.curl("http://" + app2Url + "/helloworld/"));

//============================================================
// Create a 3rd web app with a public GitHub repo in Azure-Samples
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package com.azure.management.appservice.samples;

import com.azure.core.credential.TokenCredential;
import com.azure.core.http.HttpClient;
import com.azure.core.management.AzureEnvironment;
import com.azure.identity.DefaultAzureCredentialBuilder;
import com.azure.management.Azure;
Expand Down Expand Up @@ -79,7 +80,7 @@ public static boolean runSample(Azure azure) {

System.out.println("Uploading 2 blobs to container " + containerName + "...");

BlobContainerClient container = setUpStorageAccount(connectionString, containerName);
BlobContainerClient container = setUpStorageAccount(connectionString, containerName, storageAccount.manager().httpPipeline().getHttpClient());
uploadFileToContainer(container, "helloworld.war", ManageLinuxWebAppStorageAccountConnection.class.getResource("/helloworld.war").getPath());
uploadFileToContainer(container, "install_apache.sh", ManageLinuxWebAppStorageAccountConnection.class.getResource("/install_apache.sh").getPath());

Expand Down Expand Up @@ -170,10 +171,11 @@ public static void main(String[] args) {
}
}

private static BlobContainerClient setUpStorageAccount(String connectionString, String containerName) {
private static BlobContainerClient setUpStorageAccount(String connectionString, String containerName, HttpClient httpClient) {
BlobContainerClient blobContainerClient = new BlobContainerClientBuilder()
.connectionString(connectionString)
.containerName(containerName)
.httpClient(httpClient)
.buildClient();

blobContainerClient.create();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ public static boolean runSample(Azure azure, TokenCredential credential, String
SecretClient client = new SecretClientBuilder()
.vaultUrl(vault.vaultUri())
.credential(credential)
.httpClient(vault.manager().httpPipeline().getHttpClient())
.buildClient();

//============================================================
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package com.azure.management.appservice.samples;

import com.azure.core.credential.TokenCredential;
import com.azure.core.http.HttpClient;
import com.azure.core.management.AzureEnvironment;
import com.azure.identity.DefaultAzureCredentialBuilder;
import com.azure.management.Azure;
Expand Down Expand Up @@ -80,7 +81,7 @@ public static boolean runSample(Azure azure) {

System.out.println("Uploading 2 blobs to container " + containerName + "...");

BlobContainerClient container = setUpStorageAccount(connectionString, containerName);
BlobContainerClient container = setUpStorageAccount(connectionString, containerName, storageAccount.manager().httpPipeline().getHttpClient());
uploadFileToContainer(container, "helloworld.war", ManageWebAppStorageAccountConnection.class.getResource("/helloworld.war").getPath());
uploadFileToContainer(container, "install_apache.sh", ManageWebAppStorageAccountConnection.class.getResource("/install_apache.sh").getPath());

Expand Down Expand Up @@ -171,10 +172,11 @@ public static void main(String[] args) {
}
}

private static BlobContainerClient setUpStorageAccount(String connectionString, String containerName) {
private static BlobContainerClient setUpStorageAccount(String connectionString, String containerName, HttpClient httpClient) {
BlobContainerClient blobContainerClient = new BlobContainerClientBuilder()
.connectionString(connectionString)
.containerName(containerName)
.httpClient(httpClient)
.buildClient();

blobContainerClient.create();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package com.azure.management.monitor.samples;

import com.azure.core.credential.TokenCredential;
import com.azure.core.http.HttpClient;
import com.azure.core.http.policy.HttpLogDetailLevel;
import com.azure.core.http.rest.PagedIterable;
import com.azure.core.management.AzureEnvironment;
Expand Down Expand Up @@ -84,7 +85,7 @@ public static boolean runSample(Azure azure) {
storageAccountKeys.get(0).value());

// Add some blob transaction events
addBlobTransactions(storageConnectionString);
addBlobTransactions(storageConnectionString, storageAccount.manager().httpPipeline().getHttpClient());

OffsetDateTime recordDateTime = azure.sdkContext().dateTimeNow();
// get metric definitions for storage account.
Expand Down Expand Up @@ -197,7 +198,7 @@ public static void main(String[] args) {
}
}

private static void addBlobTransactions(String storageConnectionString) throws IOException {
private static void addBlobTransactions(String storageConnectionString, HttpClient httpClient) throws IOException {
// Get the script to upload
//
try (InputStream scriptFileAsStream = QueryMetricsAndActivityLogs.class.getResourceAsStream("/install_apache.sh")) {
Expand All @@ -213,13 +214,15 @@ private static void addBlobTransactions(String storageConnectionString) throws I
BlobContainerClient blobContainerClient = new BlobContainerClientBuilder()
.connectionString(storageConnectionString)
.containerName("scripts")
.httpClient(httpClient)
.buildClient();

blobContainerClient.create();

// Get the service properties.
BlobServiceClient blobServiceClient = new BlobServiceClientBuilder()
.connectionString(storageConnectionString)
.httpClient(httpClient)
.buildClient();
BlobServiceProperties serviceProps = blobServiceClient.getProperties();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,10 @@ public static boolean runSample(Azure azure) {
String connectionString = String.format("DefaultEndpointsProtocol=https;AccountName=%s;AccountKey=%s",
storageAccount.name(), accountKey);

BlobServiceClient blobServiceClient = new BlobServiceClientBuilder().connectionString(connectionString).buildClient();
BlobServiceClient blobServiceClient = new BlobServiceClientBuilder()
.connectionString(connectionString)
.httpClient(storageAccount.manager().httpPipeline().getHttpClient())
.buildClient();
BlobContainerClient blobContainerClient = blobServiceClient.getBlobContainerClient(packetCaptureStorageContainer);
// iterate over subfolders structure to get the file
BlobItem item = blobContainerClient.listBlobs().iterator().next();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,10 @@ public static boolean runSample(Azure azure) {
// Create storage container to store troubleshooting results
String accountKey = storageAccount.getKeys().get(0).value();
String connectionString = String.format("DefaultEndpointsProtocol=https;AccountName=%s;AccountKey=%s", storageAccount.name(), accountKey);
BlobServiceClient blobServiceClient = new BlobServiceClientBuilder().connectionString(connectionString).buildClient();
BlobServiceClient blobServiceClient = new BlobServiceClientBuilder()
.connectionString(connectionString)
.httpClient(storageAccount.manager().httpPipeline().getHttpClient())
.buildClient();
BlobContainerClient containerClient = blobServiceClient.getBlobContainerClient(storageContainerName);
containerClient.create();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,16 @@
import com.azure.management.monitor.samples.SecurityBreachOrRiskActivityLogAlerts;
import com.azure.management.monitor.samples.WebAppPerformanceMonitoringAlerts;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

public class MonitorTests extends SamplesTestBase {

@Test
@Disabled("Live only sample due to the need to call non-management endpoints.")
public void testQueryMetricsAndActivityLogs() {
Assertions.assertTrue(QueryMetricsAndActivityLogs.runSample(azure));
// Skip test in "playback" mode due to HTTP calls made outside of the management plane which can not be recorded at this time
if (!isPlaybackMode()) {
Assertions.assertTrue(QueryMetricsAndActivityLogs.runSample(azure));
}
}

@Test
Expand All @@ -31,8 +32,10 @@ public void testWebAppPerformanceMonitoringAlerts() {
}

@Test
@Disabled("Live only sample due to the need to call non-management endpoints.")
public void testAutoscaleSettingsBasedOnPerformanceOrSchedule() {
Assertions.assertTrue(AutoscaleSettingsBasedOnPerformanceOrSchedule.runSample(azure));
// Skip test in "playback" mode due to HTTP calls made outside of the management plane which can not be recorded at this time
if (!isPlaybackMode()) {
Assertions.assertTrue(AutoscaleSettingsBasedOnPerformanceOrSchedule.runSample(azure));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,6 @@
import java.nio.file.Paths;

public class SqlSampleTests extends SamplesTestBase {
// @Override
// protected RestClient buildRestClient(RestClient.Builder builder, boolean isMocked) {
// if (!isMocked) {
// return super.buildRestClient(builder, isMocked);
// }
// return super.buildRestClient(builder.withReadTimeout(200, TimeUnit.SECONDS), isMocked);
// }

@Test
public void testManageSqlDatabase() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ null, profile, null, new HttpLogOptions().setLogLevel(HttpLogDetailLevel.BODY_AN
}
httpPipeline = HttpPipelineProvider.buildHttpPipeline(
credential, profile, null, new HttpLogOptions().setLogLevel(HttpLogDetailLevel.BODY_AND_HEADERS),
null, new RetryPolicy(), policies, null);
null, new RetryPolicy(), policies, generateHttpClientWithProxy(null));

interceptorManager.addTextReplacementRule(profile.subscriptionId(), ZERO_SUBSCRIPTION);
interceptorManager.addTextReplacementRule(profile.tenantId(), ZERO_TENANT);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ private Mono<Indexable> getOrCreateStorageAccountContainer(
new BlobServiceClientBuilder()
.endpoint(storageAccount.endPoints().primary().blob())
.sasToken(storageAccountKey.value())
.httpClient(sqlServerManager.httpPipeline().getHttpClient())
.buildClient();
blobServiceClient.createBlobContainer(containerName);
} catch (IndexOutOfBoundsException indexOutOfBoundsException) {
Expand Down

0 comments on commit f4babca

Please sign in to comment.