Search in sources :

Example 1 with CloudError

use of com.microsoft.azure.CloudError in project azure-sdk-for-java by Azure.

the class ProviderRegistrationInterceptor method intercept.

@Override
public Response intercept(Chain chain) throws IOException {
    Response response = chain.proceed(chain.request());
    if (!response.isSuccessful()) {
        String content = errorBody(response.body());
        AzureJacksonAdapter jacksonAdapter = new AzureJacksonAdapter();
        CloudError cloudError = jacksonAdapter.deserialize(content, CloudError.class);
        if (cloudError != null && "MissingSubscriptionRegistration".equals(cloudError.code())) {
            Pattern pattern = Pattern.compile("/subscriptions/([\\w-]+)/", Pattern.CASE_INSENSITIVE);
            Matcher matcher = pattern.matcher(chain.request().url().toString());
            matcher.find();
            RestClient restClient = new RestClient.Builder().withBaseUrl("https://" + chain.request().url().host()).withCredentials(credentials).withSerializerAdapter(jacksonAdapter).withResponseBuilderFactory(new AzureResponseBuilder.Factory()).build();
            ResourceManager resourceManager = ResourceManager.authenticate(restClient).withSubscription(matcher.group(1));
            pattern = Pattern.compile(".*'(.*)'");
            matcher = pattern.matcher(cloudError.message());
            matcher.find();
            Provider provider = registerProvider(matcher.group(1), resourceManager);
            while (provider.registrationState().equalsIgnoreCase("Unregistered") || provider.registrationState().equalsIgnoreCase("Registering")) {
                SdkContext.sleep(5 * 1000);
                provider = resourceManager.providers().getByName(provider.namespace());
            }
            // Retry
            response = chain.proceed(chain.request());
        }
    }
    return response;
}
Also used : Response(okhttp3.Response) Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) AzureJacksonAdapter(com.microsoft.azure.serializer.AzureJacksonAdapter) RestClient(com.microsoft.rest.RestClient) CloudError(com.microsoft.azure.CloudError) ResourceManager(com.microsoft.azure.management.resources.implementation.ResourceManager) Provider(com.microsoft.azure.management.resources.Provider)

Aggregations

CloudError (com.microsoft.azure.CloudError)1 Provider (com.microsoft.azure.management.resources.Provider)1 ResourceManager (com.microsoft.azure.management.resources.implementation.ResourceManager)1 AzureJacksonAdapter (com.microsoft.azure.serializer.AzureJacksonAdapter)1 RestClient (com.microsoft.rest.RestClient)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 Response (okhttp3.Response)1