Search in sources :

Example 6 with SubscriptionManager

use of com.microsoft.azuretools.authmanage.SubscriptionManager in project azure-tools-for-java by Microsoft.

the class SubscriptionsDialog method refreshSubscriptions.

private void refreshSubscriptions() {
    EventUtil.executeWithLog(ACCOUNT, GET_SUBSCRIPTIONS, (operation) -> {
        AzureManager manager = AuthMethodManager.getInstance().getAzureManager();
        if (manager == null) {
            return;
        }
        final SubscriptionManager subscriptionManager = manager.getSubscriptionManager();
        subscriptionManager.cleanSubscriptions();
        Azure.az(AzureAccount.class).account().reloadSubscriptions().block();
        SelectSubscriptionsAction.loadSubscriptions(subscriptionManager, project);
        // System.out.println("refreshSubscriptions: calling getSubscriptionDetails()");
        sdl = subscriptionManager.getSubscriptionDetails();
        setSubscriptions();
        // to notify subscribers
        subscriptionManager.setSubscriptionDetails(sdl);
    }, (ex) -> {
        ex.printStackTrace();
        ErrorWindow.show(project, ex.getMessage(), "Refresh Subscriptions Error");
    });
}
Also used : AzureManager(com.microsoft.azuretools.sdkmanage.AzureManager) IdentityAzureManager(com.microsoft.azuretools.sdkmanage.IdentityAzureManager) SubscriptionManager(com.microsoft.azuretools.authmanage.SubscriptionManager) AzureAccount(com.microsoft.azure.toolkit.lib.auth.AzureAccount)

Example 7 with SubscriptionManager

use of com.microsoft.azuretools.authmanage.SubscriptionManager in project azure-tools-for-java by Microsoft.

the class SelectSubsriptionsCommandHandler method onSelectSubscriptions.

public static void onSelectSubscriptions(Shell parentShell) {
    try {
        AzureManager manager = AuthMethodManager.getInstance().getAzureManager();
        if (manager == null) {
            return;
        }
        SubscriptionManager sm = manager.getSubscriptionManager();
        SubscriptionsDialog.go(parentShell, sm);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}
Also used : AzureManager(com.microsoft.azuretools.sdkmanage.AzureManager) SubscriptionManager(com.microsoft.azuretools.authmanage.SubscriptionManager) ExecutionException(org.eclipse.core.commands.ExecutionException)

Example 8 with SubscriptionManager

use of com.microsoft.azuretools.authmanage.SubscriptionManager in project azure-tools-for-java by Microsoft.

the class AzureModule method composeName.

private static String composeName() {
    try {
        AzureManager azureManager = AuthMethodManager.getInstance().getAzureManager();
        if (AuthMethodManager.getInstance().isRestoringSignIn()) {
            return BASE_MODULE_NAME + " (Signing In...)";
        }
        // not signed in
        if (azureManager == null) {
            return BASE_MODULE_NAME + " (Not Signed In)";
        }
        SubscriptionManager subscriptionManager = azureManager.getSubscriptionManager();
        List<SubscriptionDetail> subscriptionDetails = subscriptionManager.getSubscriptionDetails();
        List<SubscriptionDetail> selectedSubscriptions = subscriptionDetails.stream().filter(SubscriptionDetail::isSelected).collect(Collectors.toList());
        return String.format("%s (%s)", BASE_MODULE_NAME, getAccountDescription(selectedSubscriptions));
    } catch (AzureRuntimeException e) {
        DefaultLoader.getUIHelper().showInfoNotification(ERROR_GETTING_SUBSCRIPTIONS_TITLE, ErrorEnum.getDisplayMessageByCode(e.getCode()));
    } catch (Exception e) {
        final String msg = String.format(ERROR_GETTING_SUBSCRIPTIONS_MESSAGE, e.getMessage());
        DefaultLoader.getUIHelper().showException(msg, e, ERROR_GETTING_SUBSCRIPTIONS_TITLE, false, true);
    }
    return BASE_MODULE_NAME;
}
Also used : AzureRuntimeException(com.microsoft.azuretools.exception.AzureRuntimeException) AzureManager(com.microsoft.azuretools.sdkmanage.AzureManager) SubscriptionDetail(com.microsoft.azuretools.authmanage.models.SubscriptionDetail) SubscriptionManager(com.microsoft.azuretools.authmanage.SubscriptionManager) AzureCmdException(com.microsoft.azuretools.azurecommons.helpers.AzureCmdException) AzureRuntimeException(com.microsoft.azuretools.exception.AzureRuntimeException)

Example 9 with SubscriptionManager

use of com.microsoft.azuretools.authmanage.SubscriptionManager in project azure-tools-for-java by Microsoft.

the class SynapseCosmosSparkPool method getConfigurationInfo.

@Override
public void getConfigurationInfo() throws IOException {
    if (!isConfigInfoAvailable()) {
        // Extract Subscription ID from ADLA resource ID
        // Sample adlaResouceId: /subscriptions/a00b00a0-00a0-0000-b000-a000b0a00000/resourceGroups/testRG/providers/Microsoft.DataLakeAnalytics/accounts/testAccount
        Matcher matcher = ADLA_RESOURCE_ID_PATTERN.matcher(getAdlaResourceId());
        if (!matcher.matches()) {
            String errorMsg = String.format("ADLA resource ID doesn't match with pattern. AdlaResourceId: %s. Pattern: %s", getAdlaResourceId(), ADLA_RESOURCE_ID_PATTERN);
            throw new IOException(errorMsg);
        }
        String subscriptionId = matcher.group("sid");
        // Get SubscriptionDetail from subscription ID
        AzureManager azureManager = AuthMethodManager.getInstance().getAzureManager();
        SubscriptionManager subscriptionManager = azureManager.getSubscriptionManager();
        SubscriptionDetail subscription = subscriptionManager.getSubscriptionIdToSubscriptionDetailsMap().getOrDefault(subscriptionId, null);
        if (subscription == null) {
            throw new IOException("User has no permission to access subscription " + subscriptionId + ".");
        }
        // Get ADLA account details through Azure REST API
        AzureHttpObservable managementHttp = new AzureManagementHttpObservable(subscription, ApiVersion.VERSION);
        String resourceManagerEndpoint = CommonSettings.getAdEnvironment().resourceManagerEndpoint();
        URI accountDetailUri = URI.create(resourceManagerEndpoint).resolve(getAdlaResourceId());
        AzureSparkServerlessAccount azureSparkServerlessAccount = managementHttp.withUuidUserAgent().get(accountDetailUri.toString(), null, null, DataLakeAnalyticsAccount.class).doOnError(err -> log().warn("Error getting ADLA account details with error: " + err.getMessage())).map(dataLakeAnalyticsAccount -> new AzureSparkServerlessAccount(subscription, URI.create("https://" + dataLakeAnalyticsAccount.endpoint()), dataLakeAnalyticsAccount.name()).setDetailResponse(dataLakeAnalyticsAccount)).subscribeOn(Schedulers.io()).toBlocking().singleOrDefault(null);
        // Get default storage account info from ADLA account
        String storageRootPath = azureSparkServerlessAccount.getStorageRootPath();
        IHDIStorageAccount adlsGen1StorageAccount = storageRootPath == null ? null : new AzureSparkCosmosCluster.StorageAccount(azureSparkServerlessAccount.getDetailResponse().defaultDataLakeStoreAccount(), storageRootPath, azureSparkServerlessAccount.getSubscription().getSubscriptionId());
        synchronized (this) {
            if (!isConfigInfoAvailable()) {
                this.http = managementHttp;
                this.storageAccount = adlsGen1StorageAccount;
                isConfigInfoAvailable = true;
            }
        }
    }
}
Also used : AuthMethodManager(com.microsoft.azuretools.authmanage.AuthMethodManager) AzureSparkServerlessAccount(com.microsoft.azure.hdinsight.sdk.common.azure.serverless.AzureSparkServerlessAccount) SparkSubmitStorageTypeOptionsForCluster(com.microsoft.azure.hdinsight.spark.common.SparkSubmitStorageTypeOptionsForCluster) ArcadiaWorkSpace(com.microsoft.azure.projectarcadia.common.ArcadiaWorkSpace) SubscriptionManager(com.microsoft.azuretools.authmanage.SubscriptionManager) Nullable(com.microsoft.azuretools.azurecommons.helpers.Nullable) SparkSubmitStorageType(com.microsoft.azure.hdinsight.spark.common.SparkSubmitStorageType) AzureManager(com.microsoft.azuretools.sdkmanage.AzureManager) IOException(java.io.IOException) ArcadiaSparkCompute(com.microsoft.azure.projectarcadia.common.ArcadiaSparkCompute) DataLakeAnalyticsAccount(com.microsoft.azure.hdinsight.sdk.rest.azure.datalake.analytics.accounts.models.DataLakeAnalyticsAccount) BigDataPoolResourceInfo(com.microsoft.azure.hdinsight.sdk.rest.azure.synapse.models.BigDataPoolResourceInfo) Matcher(java.util.regex.Matcher) Schedulers(rx.schedulers.Schedulers) AzureSparkCosmosCluster(com.microsoft.azure.hdinsight.sdk.common.azure.serverless.AzureSparkCosmosCluster) SubscriptionDetail(com.microsoft.azuretools.authmanage.models.SubscriptionDetail) CommonSettings(com.microsoft.azuretools.authmanage.CommonSettings) URI(java.net.URI) Pattern(java.util.regex.Pattern) AzureManagementHttpObservable(com.microsoft.azure.hdinsight.sdk.common.AzureManagementHttpObservable) IHDIStorageAccount(com.microsoft.azure.hdinsight.sdk.storage.IHDIStorageAccount) AzureHttpObservable(com.microsoft.azure.hdinsight.sdk.common.AzureHttpObservable) ApiVersion(com.microsoft.azure.hdinsight.sdk.rest.azure.datalake.analytics.accounts.models.ApiVersion) ExceptionUtils(org.apache.commons.lang3.exception.ExceptionUtils) IHDIStorageAccount(com.microsoft.azure.hdinsight.sdk.storage.IHDIStorageAccount) AzureManager(com.microsoft.azuretools.sdkmanage.AzureManager) Matcher(java.util.regex.Matcher) IOException(java.io.IOException) SubscriptionManager(com.microsoft.azuretools.authmanage.SubscriptionManager) AzureSparkCosmosCluster(com.microsoft.azure.hdinsight.sdk.common.azure.serverless.AzureSparkCosmosCluster) URI(java.net.URI) AzureHttpObservable(com.microsoft.azure.hdinsight.sdk.common.AzureHttpObservable) AzureSparkServerlessAccount(com.microsoft.azure.hdinsight.sdk.common.azure.serverless.AzureSparkServerlessAccount) SubscriptionDetail(com.microsoft.azuretools.authmanage.models.SubscriptionDetail) DataLakeAnalyticsAccount(com.microsoft.azure.hdinsight.sdk.rest.azure.datalake.analytics.accounts.models.DataLakeAnalyticsAccount) AzureManagementHttpObservable(com.microsoft.azure.hdinsight.sdk.common.AzureManagementHttpObservable)

Example 10 with SubscriptionManager

use of com.microsoft.azuretools.authmanage.SubscriptionManager in project azure-tools-for-java by Microsoft.

the class SignInDialog method doCreateServicePrincipal.

private void doCreateServicePrincipal() {
    setErrorMessage(null);
    AdAuthManager adAuthManager = null;
    try {
        adAuthManager = AdAuthManager.getInstance();
        if (adAuthManager.isSignedIn()) {
            adAuthManager.signOut();
        }
        signInAsync();
        if (!adAuthManager.isSignedIn()) {
            // canceled by the user
            System.out.println(">> Canceled by the user");
            return;
        }
        AccessTokenAzureManager accessTokenAzureManager = new AccessTokenAzureManager();
        SubscriptionManager subscriptionManager = accessTokenAzureManager.getSubscriptionManager();
        IRunnableWithProgress op = new IRunnableWithProgress() {

            @Override
            public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
                monitor.beginTask("Getting Subscription List...", IProgressMonitor.UNKNOWN);
                try {
                    subscriptionManager.getSubscriptionDetails();
                } catch (Exception ex) {
                    System.out.println("run@ProgressDialog@doCreateServicePrincipal@SignInDialog: " + ex.getMessage());
                    LOG.log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, "run@ProgressDialog@doCreateServicePrincipal@SignInDialogg", ex));
                }
            }
        };
        new ProgressMonitorDialog(this.getShell()).run(true, false, op);
        SrvPriSettingsDialog d = SrvPriSettingsDialog.go(this.getShell(), subscriptionManager.getSubscriptionDetails());
        List<SubscriptionDetail> subscriptionDetailsUpdated;
        String destinationFolder;
        if (d != null) {
            subscriptionDetailsUpdated = d.getSubscriptionDetails();
            destinationFolder = d.getDestinationFolder();
        } else {
            System.out.println(">> Canceled by the user");
            return;
        }
        Map<String, List<String>> tidSidsMap = new HashMap<>();
        for (SubscriptionDetail sd : subscriptionDetailsUpdated) {
            if (sd.isSelected()) {
                System.out.format(">> %s\n", sd.getSubscriptionName());
                String tid = sd.getTenantId();
                List<String> sidList;
                if (!tidSidsMap.containsKey(tid)) {
                    sidList = new LinkedList<>();
                } else {
                    sidList = tidSidsMap.get(tid);
                }
                sidList.add(sd.getSubscriptionId());
                tidSidsMap.put(tid, sidList);
            }
        }
        SrvPriCreationStatusDialog d1 = SrvPriCreationStatusDialog.go(this.getShell(), tidSidsMap, destinationFolder);
        if (d1 == null) {
            System.out.println(">> Canceled by the user");
            return;
        }
        String path = d1.getSelectedAuthFilePath();
        if (path == null) {
            System.out.println(">> No file was created");
            return;
        }
        textAuthenticationFilePath.setText(path);
        fileDialog.setFilterPath(destinationFolder);
    } catch (Exception ex) {
        ex.printStackTrace();
        LOG.log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, "doCreateServicePrincipal@SignInDialog", ex));
    } finally {
        if (adAuthManager != null) {
            adAuthManager.signOut();
        }
    }
}
Also used : IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) HashMap(java.util.HashMap) ProgressMonitorDialog(org.eclipse.jface.dialogs.ProgressMonitorDialog) AccessTokenAzureManager(com.microsoft.azuretools.sdkmanage.AccessTokenAzureManager) AdAuthManager(com.microsoft.azuretools.authmanage.AdAuthManager) SubscriptionManager(com.microsoft.azuretools.authmanage.SubscriptionManager) PartInitException(org.eclipse.ui.PartInitException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) AuthCanceledException(com.microsoft.azuretools.adauth.AuthCanceledException) IRunnableWithProgress(org.eclipse.jface.operation.IRunnableWithProgress) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) SubscriptionDetail(com.microsoft.azuretools.authmanage.models.SubscriptionDetail) LinkedList(java.util.LinkedList) List(java.util.List)

Aggregations

SubscriptionManager (com.microsoft.azuretools.authmanage.SubscriptionManager)19 AzureManager (com.microsoft.azuretools.sdkmanage.AzureManager)16 SubscriptionDetail (com.microsoft.azuretools.authmanage.models.SubscriptionDetail)12 Azure (com.microsoft.azure.management.Azure)6 AuthMethodManager (com.microsoft.azuretools.authmanage.AuthMethodManager)4 AzureCmdException (com.microsoft.azuretools.azurecommons.helpers.AzureCmdException)4 Location (com.microsoft.azure.management.resources.Location)3 ResourceGroup (com.microsoft.azure.management.resources.ResourceGroup)3 Subscription (com.microsoft.azure.management.resources.Subscription)3 AccessTokenAzureManager (com.microsoft.azuretools.sdkmanage.AccessTokenAzureManager)3 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 ImmutablePair (org.apache.commons.lang3.tuple.ImmutablePair)3 Pair (org.apache.commons.lang3.tuple.Pair)3 StorageAccount (com.microsoft.azure.management.storage.StorageAccount)2 AuthCanceledException (com.microsoft.azuretools.adauth.AuthCanceledException)2 AdAuthManager (com.microsoft.azuretools.authmanage.AdAuthManager)2 Nullable (com.microsoft.azuretools.azurecommons.helpers.Nullable)2 File (java.io.File)2