创建 Azure 网络安全组 - 改造错误

Create Azure Network Security Group - Retrofit Error

我使用的是 Azure Java SDK 版本 1.15.1。由于我需要使用非默认 Java TrustStore 连接到 Azure,因此我创建了自己的自定义信任存储文件,我正在将其加载到 TrustManager 中,然后创建一个 OkHttpClient.Builder,如下所示:

OkHttpClient.Builder httpClientBuilder = new OkHttpClient.Builder().sslSocketFactory(sslSocketFactory, trustManager);

然后我正在创建一个 Retrofit Builder 并使用所有这些来创建我的 Azure 连接对象:

retrofit2.Retrofit.Builder retrofitBuilder = new retrofit2.Retrofit.Builder(); 
retrofitBuilder.baseUrl("https://management.azure.com"); 

com.microsoft.rest.RestClient restClient = new RestClient.Builder(httpClientBuilder, retrofitBuilder) 
    .withCredentials(credentials) 
    .withBaseUrl("https://management.azure.com") 
    .withResponseBuilderFactory(new ServiceResponseBuilder.Factory()) 
    .withSerializerAdapter(new JacksonAdapter())
    .build(); 

Azure azureConnection = Azure.authenticate(restClient, tenantId).withSubscription(subscription);

这适用于从 Azure(网络、安全组等)检索各种数据。

但是如果我想使用这个连接来创建一个新的安全组:

newSecGroup = azureConnection.getAzureConnection().networkSecurityGroups() 
    .define(groupName) 
    .withRegion(regionName) 
    .withExistingResourceGroup(resourceGroupName) 
    .withTag("MyCustomTag", groupName) 
    .create();

它抛出以下异常,这并不能真正帮助我找到根本原因 - 也许有人可以指出正确的方向? 谢谢 库尔特

Error when trying to create a new Security Group within resource groupksembatest, region eastus with name Cloud__Web Server for policy Web Server: com.microsoft.azure.CloudException
at com.microsoft.azure.AzureClient.createExceptionFromResponse(AzureClient.java:740)
at com.microsoft.azure.AzureClient.access0(AzureClient.java:33)
at com.microsoft.azure.AzureClient.call(AzureClient.java:160)
at com.microsoft.azure.AzureClient.call(AzureClient.java:157)
at rx.internal.operators.OnSubscribeMap$MapSubscriber.onNext(OnSubscribeMap.java:69)
at retrofit2.adapter.rxjava.CallArbiter.deliverResponse(CallArbiter.java:120)
at retrofit2.adapter.rxjava.CallArbiter.emitResponse(CallArbiter.java:102)
at retrofit2.adapter.rxjava.CallExecuteOnSubscribe.call(CallExecuteOnSubscribe.java:46)
at retrofit2.adapter.rxjava.CallExecuteOnSubscribe.call(CallExecuteOnSubscribe.java:24)
at rx.Observable.unsafeSubscribe(Observable.java:10327)
at rx.internal.operators.OnSubscribeMap.call(OnSubscribeMap.java:48)
at rx.internal.operators.OnSubscribeMap.call(OnSubscribeMap.java:33)
at rx.Observable.unsafeSubscribe(Observable.java:10327)
at rx.internal.operators.OnSubscribeSingle.call(OnSubscribeSingle.java:81)
at rx.internal.operators.OnSubscribeSingle.call(OnSubscribeSingle.java:27)
at rx.internal.operators.SingleToObservable.call(SingleToObservable.java:39)
at rx.internal.operators.SingleToObservable.call(SingleToObservable.java:27)
at rx.Observable.unsafeSubscribe(Observable.java:10327)
at rx.internal.operators.OnSubscribeMap.call(OnSubscribeMap.java:48)
at rx.internal.operators.OnSubscribeMap.call(OnSubscribeMap.java:33)
at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:48)
at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:30)
at rx.Observable.unsafeSubscribe(Observable.java:10327)
at rx.internal.operators.DeferredScalarSubscriber.subscribeTo(DeferredScalarSubscriber.java:153)
at rx.internal.operators.OnSubscribeTakeLastOne.call(OnSubscribeTakeLastOne.java:32)
at rx.internal.operators.OnSubscribeTakeLastOne.call(OnSubscribeTakeLastOne.java:22)
at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:48)
at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:30)
at rx.Observable.unsafeSubscribe(Observable.java:10327)
at rx.internal.operators.OnSubscribeMap.call(OnSubscribeMap.java:48)
at rx.internal.operators.OnSubscribeMap.call(OnSubscribeMap.java:33)
at rx.Observable.unsafeSubscribe(Observable.java:10327)
at rx.internal.operators.OnSubscribeMap.call(OnSubscribeMap.java:48)
at rx.internal.operators.OnSubscribeMap.call(OnSubscribeMap.java:33)
at rx.Observable.unsafeSubscribe(Observable.java:10327)
at rx.internal.operators.OnSubscribeMap.call(OnSubscribeMap.java:48)
at rx.internal.operators.OnSubscribeMap.call(OnSubscribeMap.java:33)
at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:48)
at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:30)
at rx.Observable.unsafeSubscribe(Observable.java:10327)
at rx.internal.operators.OperatorSubscribeOn$SubscribeOnSubscriber.call(OperatorSubscribeOn.java:100)
at rx.internal.schedulers.CachedThreadScheduler$EventLoopWorker.call(CachedThreadScheduler.java:230)
at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:55)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access1(ScheduledThreadPoolExecutor.java:180)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
Caused by: rx.exceptions.OnErrorThrowable$OnNextValue: OnError while emitting onNext value: retrofit2.Response.class
at rx.exceptions.OnErrorThrowable.addValueAsLastCause(OnErrorThrowable.java:118)
at rx.internal.operators.OnSubscribeMap$MapSubscriber.onNext(OnSubscribeMap.java:73)
... 45 more

发现可以在 RestClient 上启用调试日志记录后

com.microsoft.rest.RestClient restClient = new RestClient.Builder(httpClientBuilder, retrofitBuilder)
            .withCredentials(credentials)
            .withBaseUrl("https://management.azure.com")
            .withResponseBuilderFactory(new ServiceResponseBuilder.Factory())
            .withSerializerAdapter(new JacksonAdapter())
            .withLogLevel(com.microsoft.rest.LogLevel.BODY_AND_HEADERS)
            .build();

很容易找到并解决我的问题。原因很简单,我想创建的网络安全组名称中有一个空的 space,Azure 不允许这样做。 HTTP 响应比我从 java API 错误跟踪中得到的 "CloudException" 清晰得多。