Search in sources :

Example 1 with ProviderRegistrationPolicy

use of com.azure.resourcemanager.resources.fluentcore.policy.ProviderRegistrationPolicy in project azure-maven-plugins by microsoft.

the class ApplicationInsights method getApplicationInsightsManager.

@Cacheable(cacheName = "applicationinsights/{}/manager", key = "$subscriptionId")
private ApplicationInsightsManager getApplicationInsightsManager(String subscriptionId) {
    final Account account = Azure.az(AzureAccount.class).account();
    final String tenantId = account.getSubscription(subscriptionId).getTenantId();
    final AzureConfiguration config = Azure.az().config();
    final String userAgent = config.getUserAgent();
    final HttpLogOptions logOptions = new HttpLogOptions();
    logOptions.setLogLevel(Optional.ofNullable(config.getLogLevel()).map(HttpLogDetailLevel::valueOf).orElse(HttpLogDetailLevel.NONE));
    final AzureProfile azureProfile = new AzureProfile(tenantId, subscriptionId, account.getEnvironment());
    // todo: migrate resource provider related codes to common library
    final Providers providers = ResourceManager.configure().withHttpClient(AzureService.getDefaultHttpClient()).withPolicy(getUserAgentPolicy(userAgent)).authenticate(account.getTokenCredential(subscriptionId), azureProfile).withSubscription(subscriptionId).providers();
    return ApplicationInsightsManager.configure().withHttpClient(AzureService.getDefaultHttpClient()).withLogOptions(logOptions).withPolicy(getUserAgentPolicy(userAgent)).withPolicy(// add policy to auto register resource providers
    new ProviderRegistrationPolicy(providers)).authenticate(account.getTokenCredential(subscriptionId), azureProfile);
}
Also used : Account(com.microsoft.azure.toolkit.lib.auth.Account) AzureAccount(com.microsoft.azure.toolkit.lib.auth.AzureAccount) AzureConfiguration(com.microsoft.azure.toolkit.lib.AzureConfiguration) HttpLogDetailLevel(com.azure.core.http.policy.HttpLogDetailLevel) ProviderRegistrationPolicy(com.azure.resourcemanager.resources.fluentcore.policy.ProviderRegistrationPolicy) AzureProfile(com.azure.core.management.profile.AzureProfile) AzureAccount(com.microsoft.azure.toolkit.lib.auth.AzureAccount) HttpLogOptions(com.azure.core.http.policy.HttpLogOptions) Providers(com.azure.resourcemanager.resources.models.Providers) Cacheable(com.microsoft.azure.toolkit.lib.common.cache.Cacheable)

Example 2 with ProviderRegistrationPolicy

use of com.azure.resourcemanager.resources.fluentcore.policy.ProviderRegistrationPolicy in project azure-maven-plugins by microsoft.

the class AzureService method getResourceManager.

@Cacheable(cacheName = "resource/{}/manager", key = "$subscriptionId")
default ResourceManager getResourceManager(String subscriptionId) {
    // make sure it is signed in.
    final IAccount account = az(IAzureAccount.class).account();
    final AzureConfiguration config = Azure.az().config();
    final String userAgent = config.getUserAgent();
    final HttpLogDetailLevel logDetailLevel = config.getLogLevel() == null ? HttpLogDetailLevel.NONE : HttpLogDetailLevel.valueOf(config.getLogLevel());
    final AzureProfile azureProfile = new AzureProfile(account.getEnvironment());
    final Providers providers = ResourceManager.configure().withHttpClient(getDefaultHttpClient()).withPolicy(getUserAgentPolicy(userAgent)).authenticate(account.getTokenCredential(subscriptionId), azureProfile).withSubscription(subscriptionId).providers();
    return ResourceManager.configure().withHttpClient(getDefaultHttpClient()).withLogLevel(logDetailLevel).withPolicy(// set user agent with policy
    getUserAgentPolicy(userAgent)).withPolicy(// add policy to auto register resource providers
    new ProviderRegistrationPolicy(providers)).authenticate(account.getTokenCredential(subscriptionId), azureProfile).withSubscription(subscriptionId);
}
Also used : IAccount(com.microsoft.azure.toolkit.lib.account.IAccount) IAzureAccount(com.microsoft.azure.toolkit.lib.account.IAzureAccount) HttpLogDetailLevel(com.azure.core.http.policy.HttpLogDetailLevel) ProviderRegistrationPolicy(com.azure.resourcemanager.resources.fluentcore.policy.ProviderRegistrationPolicy) AzureProfile(com.azure.core.management.profile.AzureProfile) Providers(com.azure.resourcemanager.resources.models.Providers) Cacheable(com.microsoft.azure.toolkit.lib.common.cache.Cacheable)

Example 3 with ProviderRegistrationPolicy

use of com.azure.resourcemanager.resources.fluentcore.policy.ProviderRegistrationPolicy in project azure-maven-plugins by microsoft.

the class AppServiceKuduManager method getClient.

public static AppServiceKuduManager getClient(@Nonnull WebAppBase webAppBase, @Nonnull IAppService appService) {
    // com/azure/resourcemanager/appservice/implementation/KuduClient.java
    if (webAppBase.defaultHostname() == null) {
        throw new AzureToolkitRuntimeException("Cannot initialize kudu client before web app is created");
    }
    String host = webAppBase.defaultHostname().toLowerCase(Locale.ROOT).replace("http://", "").replace("https://", "");
    String[] parts = host.split("\\.", 2);
    host = parts[0] + ".scm." + parts[1];
    host = "https://" + host;
    final List<HttpPipelinePolicy> policies = Utils.getPolicyFromPipeline(webAppBase.manager().httpPipeline(), policy -> !(policy instanceof AuthenticationPolicy || policy instanceof ProviderRegistrationPolicy || policy instanceof AuxiliaryAuthenticationPolicy));
    policies.add(new KuduAuthenticationPolicy(webAppBase));
    final HttpPipeline httpPipeline = new HttpPipelineBuilder().policies(policies.toArray(new HttpPipelinePolicy[0])).httpClient(webAppBase.manager().httpPipeline().getHttpClient()).build();
    final KuduService kuduService = RestProxy.create(KuduService.class, httpPipeline, SerializerFactory.createDefaultManagementSerializerAdapter());
    return new AppServiceKuduManager(host, kuduService, appService);
}
Also used : AuxiliaryAuthenticationPolicy(com.azure.resourcemanager.resources.fluentcore.policy.AuxiliaryAuthenticationPolicy) HttpPipelineBuilder(com.azure.core.http.HttpPipelineBuilder) ProviderRegistrationPolicy(com.azure.resourcemanager.resources.fluentcore.policy.ProviderRegistrationPolicy) AzureToolkitRuntimeException(com.microsoft.azure.toolkit.lib.common.exception.AzureToolkitRuntimeException) HttpPipelinePolicy(com.azure.core.http.policy.HttpPipelinePolicy) AuthenticationPolicy(com.azure.resourcemanager.resources.fluentcore.policy.AuthenticationPolicy) AuxiliaryAuthenticationPolicy(com.azure.resourcemanager.resources.fluentcore.policy.AuxiliaryAuthenticationPolicy) KuduAuthenticationPolicy(com.azure.resourcemanager.appservice.models.KuduAuthenticationPolicy) HttpPipeline(com.azure.core.http.HttpPipeline) KuduAuthenticationPolicy(com.azure.resourcemanager.appservice.models.KuduAuthenticationPolicy)

Example 4 with ProviderRegistrationPolicy

use of com.azure.resourcemanager.resources.fluentcore.policy.ProviderRegistrationPolicy in project azure-maven-plugins by microsoft.

the class AzureFunctionsResourceManager method getClient.

public static AzureFunctionsResourceManager getClient(@Nonnull WebAppBase functionApp, @Nonnull IFunctionAppBase appService) {
    // com/azure/resourcemanager/appservice/implementation/KuduClient.java
    if (!(functionApp instanceof FunctionApp || functionApp instanceof FunctionDeploymentSlot)) {
        throw new AzureToolkitRuntimeException("Functions resource manager only applies to Azure Functions");
    }
    final List<HttpPipelinePolicy> policies = Utils.getPolicyFromPipeline(functionApp.manager().httpPipeline(), policy -> !(policy instanceof AuthenticationPolicy || policy instanceof ProviderRegistrationPolicy || policy instanceof AuxiliaryAuthenticationPolicy));
    policies.add(new AddHeadersPolicy(new HttpHeaders(Collections.singletonMap("x-functions-key", appService.getMasterKey()))));
    final HttpPipeline httpPipeline = new HttpPipelineBuilder().policies(policies.toArray(new HttpPipelinePolicy[0])).httpClient(functionApp.manager().httpPipeline().getHttpClient()).build();
    final FunctionsService functionsService = RestProxy.create(FunctionsService.class, httpPipeline, SerializerFactory.createDefaultManagementSerializerAdapter());
    return new AzureFunctionsResourceManager(functionsService, appService);
}
Also used : HttpHeaders(com.azure.core.http.HttpHeaders) AuxiliaryAuthenticationPolicy(com.azure.resourcemanager.resources.fluentcore.policy.AuxiliaryAuthenticationPolicy) HttpPipelineBuilder(com.azure.core.http.HttpPipelineBuilder) ProviderRegistrationPolicy(com.azure.resourcemanager.resources.fluentcore.policy.ProviderRegistrationPolicy) AzureToolkitRuntimeException(com.microsoft.azure.toolkit.lib.common.exception.AzureToolkitRuntimeException) FunctionApp(com.azure.resourcemanager.appservice.models.FunctionApp) HttpPipelinePolicy(com.azure.core.http.policy.HttpPipelinePolicy) FunctionDeploymentSlot(com.azure.resourcemanager.appservice.models.FunctionDeploymentSlot) AuthenticationPolicy(com.azure.resourcemanager.resources.fluentcore.policy.AuthenticationPolicy) AuxiliaryAuthenticationPolicy(com.azure.resourcemanager.resources.fluentcore.policy.AuxiliaryAuthenticationPolicy) HttpPipeline(com.azure.core.http.HttpPipeline) AddHeadersPolicy(com.azure.core.http.policy.AddHeadersPolicy)

Aggregations

ProviderRegistrationPolicy (com.azure.resourcemanager.resources.fluentcore.policy.ProviderRegistrationPolicy)4 HttpPipeline (com.azure.core.http.HttpPipeline)2 HttpPipelineBuilder (com.azure.core.http.HttpPipelineBuilder)2 HttpLogDetailLevel (com.azure.core.http.policy.HttpLogDetailLevel)2 HttpPipelinePolicy (com.azure.core.http.policy.HttpPipelinePolicy)2 AzureProfile (com.azure.core.management.profile.AzureProfile)2 AuthenticationPolicy (com.azure.resourcemanager.resources.fluentcore.policy.AuthenticationPolicy)2 AuxiliaryAuthenticationPolicy (com.azure.resourcemanager.resources.fluentcore.policy.AuxiliaryAuthenticationPolicy)2 Providers (com.azure.resourcemanager.resources.models.Providers)2 Cacheable (com.microsoft.azure.toolkit.lib.common.cache.Cacheable)2 AzureToolkitRuntimeException (com.microsoft.azure.toolkit.lib.common.exception.AzureToolkitRuntimeException)2 HttpHeaders (com.azure.core.http.HttpHeaders)1 AddHeadersPolicy (com.azure.core.http.policy.AddHeadersPolicy)1 HttpLogOptions (com.azure.core.http.policy.HttpLogOptions)1 FunctionApp (com.azure.resourcemanager.appservice.models.FunctionApp)1 FunctionDeploymentSlot (com.azure.resourcemanager.appservice.models.FunctionDeploymentSlot)1 KuduAuthenticationPolicy (com.azure.resourcemanager.appservice.models.KuduAuthenticationPolicy)1 AzureConfiguration (com.microsoft.azure.toolkit.lib.AzureConfiguration)1 IAccount (com.microsoft.azure.toolkit.lib.account.IAccount)1 IAzureAccount (com.microsoft.azure.toolkit.lib.account.IAzureAccount)1