Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mgmt resources fix TODOs #12814

Merged
merged 3 commits into from
Jul 7, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import com.azure.core.http.rest.Response;
import com.azure.core.http.rest.RestProxy;
import com.azure.core.http.rest.SimpleResponse;
import com.azure.core.util.FluxUtil;
import com.azure.resourcemanager.appservice.models.AppServiceCertificateOrder;
import com.azure.resourcemanager.appservice.models.AppServiceDomain;
import com.azure.resourcemanager.appservice.models.PublishingProfile;
Expand All @@ -34,11 +33,10 @@
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.time.temporal.ChronoUnit;

import com.azure.resourcemanager.resources.fluentcore.utils.Utils;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.junit.jupiter.api.Assertions;
Expand Down Expand Up @@ -171,7 +169,7 @@ public static void uploadFileToWebApp(PublishingProfile profile, String fileName

protected Response<String> curl(String urlString) throws IOException {
try {
return stringResponse(httpClient.getString(getHost(urlString), getPathAndQuery(urlString))).block();
return Utils.stringResponse(httpClient.getString(Utils.getHost(urlString), Utils.getPathAndQuery(urlString))).block();
} catch (MalformedURLException e) {
Assertions.fail();
return null;
Expand All @@ -180,44 +178,14 @@ protected Response<String> curl(String urlString) throws IOException {

protected String post(String urlString, String body) {
try {
return stringResponse(httpClient.postString(getHost(urlString), getPathAndQuery(urlString), body))
return Utils.stringResponse(httpClient.postString(Utils.getHost(urlString), Utils.getPathAndQuery(urlString), body))
.block()
.getValue();
} catch (Exception e) {
return null;
}
}

private static Mono<SimpleResponse<String>> stringResponse(Mono<SimpleResponse<Flux<ByteBuffer>>> responseMono) {
return responseMono
.flatMap(
response ->
FluxUtil
.collectBytesInByteBufferStream(response.getValue())
.map(bytes -> new String(bytes, StandardCharsets.UTF_8))
.map(
str ->
new SimpleResponse<>(
response.getRequest(), response.getStatusCode(), response.getHeaders(), str)));
}

private static String getHost(String urlString) throws MalformedURLException {
URL url = new URL(urlString);
String protocol = url.getProtocol();
String host = url.getAuthority();
return protocol + "://" + host;
}

private static String getPathAndQuery(String urlString) throws MalformedURLException {
URL url = new URL(urlString);
String path = url.getPath();
String query = url.getQuery();
if (query != null && !query.isEmpty()) {
path = path + "?" + query;
}
return path;
}

protected WebAppTestClient httpClient =
RestProxy
.create(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -413,17 +413,6 @@ public Mono<Indexable> invokeTaskAsync(TaskGroup.InvocationContext context) {
.doOnNext(createdExternalChild -> externalChild.setPendingOperation(PendingOperation.None))
.map(updatedExternalChild -> updatedExternalChild);
case ToBeRemoved:
// With 2.0 runtime, deleteResourceAsync() will be
// returning 'Completable' then use below code instead
//
// return this.externalChild.deleteResourceAsync().doOnCompleted(new Action0() {
// @Override
// public void call() {
// externalChild.setPendingOperation(PendingOperation.None);
// }
// }).andThen(voidObservable());
//
// TODO: Fix void mono result.
return this.externalChild.deleteResourceAsync()
.doOnSuccess(aVoid -> externalChild.setPendingOperation(PendingOperation.None))
.map(aVoid -> voidIndexable());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ public Mono<HttpResponse> process(HttpPipelineCallContext context, HttpPipelineN
}

if (cloudError != null && MISSING_SUBSCRIPTION_REGISTRATION.equals(cloudError.getCode())) {
// TODO: add proxy in rest client
ResourceManager resourceManager = ResourceManager.authenticate(credential, profile)
.withDefaultSubscription();
Pattern providerPattern = Pattern.compile(".*'(.*)'");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,29 @@
package com.azure.resourcemanager.resources.fluentcore.utils;

import com.azure.core.annotation.Get;
import com.azure.core.annotation.Host;
import com.azure.core.annotation.HostParam;
import com.azure.core.annotation.PathParam;
import com.azure.core.annotation.ServiceInterface;
import com.azure.core.http.HttpPipeline;
import com.azure.core.http.HttpRequest;
import com.azure.core.http.HttpResponse;
import com.azure.core.http.rest.PagedIterable;
import com.azure.core.http.rest.RestProxy;
import com.azure.core.http.rest.SimpleResponse;
import com.azure.core.management.AzureEnvironment;
import com.azure.core.util.FluxUtil;
import com.azure.core.util.logging.ClientLogger;
import com.azure.resourcemanager.resources.fluentcore.arm.ResourceId;
import com.azure.resourcemanager.resources.fluentcore.model.Indexable;
import com.azure.resourcemanager.resources.models.Subscription;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.net.MalformedURLException;
import java.net.URL;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
Expand Down Expand Up @@ -115,20 +125,67 @@ public static <U extends Indexable> Mono<U> rootResource(Mono<Indexable> stream)
* Download a file asynchronously.
*
* @param url the URL pointing to the file
* @param retrofit the retrofit client
* @param httpPipeline the http pipeline
* @return an Observable pointing to the content of the file
*/
public static Mono<byte[]> downloadFileAsync(String url, HttpPipeline httpPipeline) {
FileService service = RestProxy.create(FileService.class, httpPipeline);
try {
return stringResponse(service.download(getHost(url), getPathAndQuery(url)))
.map(simpleResponse -> simpleResponse.getValue().getBytes(Charset.defaultCharset()));
} catch (MalformedURLException ex) {
return Mono.empty();
}
}

/**
* Download a file asynchronously.
* Convert the simple response.
*
* @param url the URL pointing to the file
* @param retrofit the retrofit client
* @return an Observable pointing to the content of the file
* @param responseMono the response with flux of byte buffer
* @return a response with string
*/
public static Mono<byte[]> downloadFileAsync(String url, HttpPipeline retrofit) {
FileService service = RestProxy.create(FileService.class, retrofit);
Mono<HttpResponse> response = service.download(url);
return response.flatMap(httpResponse -> httpResponse.getBodyAsByteArray());
public static Mono<SimpleResponse<String>> stringResponse(Mono<SimpleResponse<Flux<ByteBuffer>>> responseMono) {
return responseMono
.flatMap(
response ->
FluxUtil
.collectBytesInByteBufferStream(response.getValue())
.map(bytes -> new String(bytes, StandardCharsets.UTF_8))
.map(
str ->
new SimpleResponse<>(
response.getRequest(), response.getStatusCode(), response.getHeaders(), str)));
}

/**
* Get host from url.
*
* @param urlString the url string
* @return the host
* @throws MalformedURLException when url is invalid format
*/
public static String getHost(String urlString) throws MalformedURLException {
URL url = new URL(urlString);
String protocol = url.getProtocol();
String host = url.getAuthority();
return protocol + "://" + host;
}

/**
* Get path from url.
*
* @param urlString the url string
* @return the path
* @throws MalformedURLException when the url is invalid format
*/
public static String getPathAndQuery(String urlString) throws MalformedURLException {
URL url = new URL(urlString);
String path = url.getPath();
String query = url.getQuery();
if (query != null && !query.isEmpty()) {
path = path + "?" + query;
}
return path;
}

/**
Expand Down Expand Up @@ -185,9 +242,12 @@ public static String resourceGroupId(String id) {
/**
* A Retrofit service used to download a file.
*/
@Host("{$host}")
@ServiceInterface(name = "FileService")
private interface FileService {
@Get("{url}")
Mono<HttpResponse> download(@PathParam("url") String url);
@Get("{path}")
Mono<SimpleResponse<Flux<ByteBuffer>>> download(
@HostParam("$host") String host, @PathParam(value = "path", encoded = true) String path);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,20 @@

package com.azure.resourcemanager.resources;

import com.azure.core.http.HttpPipeline;
import com.azure.core.http.HttpPipelineBuilder;
import com.azure.core.http.policy.HttpLogDetailLevel;
import com.azure.core.http.policy.HttpLogOptions;
import com.azure.core.http.policy.HttpLoggingPolicy;
import com.azure.core.http.policy.RetryPolicy;
import com.azure.core.management.AzureEnvironment;
import com.azure.resourcemanager.resources.fluentcore.arm.ResourceUtils;
import com.azure.resourcemanager.resources.fluentcore.utils.Utils;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.time.temporal.ChronoUnit;

public class ResourceUtilsTests {
@Test
public void canExtractGroupFromId() throws Exception {
Expand Down Expand Up @@ -39,11 +47,16 @@ public void canExtractRelativePathFromId() throws Exception {

@Test
public void canDownloadFile() throws Exception {
// TODO(not known): Fix this
// RestProxy retrofit = new RestProxyBuilder().baseUrl("http://microsoft.com").addCallAdapterFactory(RxJavaCallAdapterFactory.create()).build();
// byte[] content = Utils.downloadFileAsync("http://google.com/humans.txt", retrofit).toBlocking().single();
// String contentString = new String(content);
// Assertions.assertNotNull(contentString);
HttpPipeline httpPipeline = new HttpPipelineBuilder()
.policies(
new HttpLoggingPolicy(new HttpLogOptions().setLogLevel(HttpLogDetailLevel.BODY_AND_HEADERS)),
new RetryPolicy("Retry-After", ChronoUnit.SECONDS)
)
.build();
byte[] content = Utils.downloadFileAsync("https://www.google.com/humans.txt", httpPipeline).block();
String contentString = new String(content);
Assertions.assertNotNull(contentString);
Assertions.assertTrue(contentString.startsWith("Google is built by a large team of engineers,"));
}

@Test
Expand Down