Search in sources :

Example 11 with SubscriptionManager

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

the class SignInWindow method doCreateServicePrincipal.

private void doCreateServicePrincipal() {
    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();
        ProgressManager.getInstance().run(new Task.Modal(project, "Load Subscriptions Progress", true) {

            @Override
            public void run(ProgressIndicator progressIndicator) {
                progressIndicator.setText("Loading subscriptions...");
                try {
                    progressIndicator.setIndeterminate(true);
                    subscriptionManager.getSubscriptionDetails();
                } catch (Exception ex) {
                    ex.printStackTrace();
                    //LOGGER.error("doCreateServicePrincipal::Task.Modal", ex);
                    ApplicationManager.getApplication().invokeLater(new Runnable() {

                        @Override
                        public void run() {
                            ErrorWindow.show(project, ex.getMessage(), "Load Subscription Error");
                        }
                    });
                }
            }
        });
        SrvPriSettingsDialog d = SrvPriSettingsDialog.go(subscriptionManager.getSubscriptionDetails(), project);
        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(tidSidsMap, destinationFolder, project);
        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;
        }
        authFileTextField.setText(path);
        fileChooser.setCurrentDirectory(new File(destinationFolder));
    } catch (Exception ex) {
        ex.printStackTrace();
        //LOGGER.error("doCreateServicePrincipal", ex);
        ErrorWindow.show(project, ex.getMessage(), "Get Subscription Error");
    } finally {
        if (adAuthManager != null) {
            try {
                System.out.println(">> Signing out...");
                adAuthManager.signOut();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
Also used : Task(com.intellij.openapi.progress.Task) HashMap(java.util.HashMap) AccessTokenAzureManager(com.microsoft.azuretools.sdkmanage.AccessTokenAzureManager) AdAuthManager(com.microsoft.azuretools.authmanage.AdAuthManager) SubscriptionManager(com.microsoft.azuretools.authmanage.SubscriptionManager) IOException(java.io.IOException) AuthCanceledException(com.microsoft.azuretools.adauth.AuthCanceledException) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) SubscriptionDetail(com.microsoft.azuretools.authmanage.models.SubscriptionDetail) LinkedList(java.util.LinkedList) List(java.util.List) File(java.io.File)

Example 12 with SubscriptionManager

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

the class AzureModelController method updateSubscriptionMaps.

public static synchronized void updateSubscriptionMaps(IProgressIndicator progressIndicator) throws IOException, CanceledByUserException, AuthException {
    AzureManager azureManager = AuthMethodManager.getInstance().getAzureManager();
    // not signed in
    if (azureManager == null) {
        return;
    }
    if (progressIndicator != null && progressIndicator.isCanceled()) {
        clearAll();
        throw new CanceledByUserException();
    }
    AzureModel azureModel = AzureModel.getInstance();
    // to get regions we nees subscription objects
    if (progressIndicator != null)
        progressIndicator.setText("Reading subscription list...");
    List<Subscription> sl = azureManager.getSubscriptions();
    // convert to map to easier find by sid
    Map<String, Subscription> sidToSubscriptionMap = azureModel.createSidToSubscriptionMap();
    for (Subscription s : sl) {
        sidToSubscriptionMap.put(s.subscriptionId(), s);
    }
    azureModel.setSidToSubscriptionMap(sidToSubscriptionMap);
    Map<SubscriptionDetail, List<Location>> sdlocMap = azureModel.createSubscriptionToRegionMap();
    Map<SubscriptionDetail, List<ResourceGroup>> sdrgMap = azureModel.createSubscriptionToResourceGroupMap();
    SubscriptionManager subscriptionManager = azureManager.getSubscriptionManager();
    subscriptionManager.addListener(subscriptionSelectionListener);
    List<SubscriptionDetail> sdl = subscriptionManager.getSubscriptionDetails();
    for (SubscriptionDetail sd : sdl) {
        if (!sd.isSelected())
            continue;
        if (progressIndicator != null && progressIndicator.isCanceled()) {
            clearAll();
            throw new CanceledByUserException();
        }
        System.out.println("sn : " + sd.getSubscriptionName());
        if (progressIndicator != null)
            progressIndicator.setText("Reading subscription '" + sd.getSubscriptionName() + "'");
        Azure azure = azureManager.getAzure(sd.getSubscriptionId());
        List<ResourceGroup> rgList = azure.resourceGroups().list();
        sdrgMap.put(sd, rgList);
        List<Location> locl = sidToSubscriptionMap.get(sd.getSubscriptionId()).listLocations();
        Collections.sort(locl, new Comparator<Location>() {

            @Override
            public int compare(Location lhs, Location rhs) {
                return lhs.displayName().compareTo(rhs.displayName());
            }
        });
        sdlocMap.put(sd, locl);
    }
    azureModel.setSubscriptionToResourceGroupMap(sdrgMap);
    azureModel.setSubscriptionToLocationMap(sdlocMap);
}
Also used : Azure(com.microsoft.azure.management.Azure) AzureManager(com.microsoft.azuretools.sdkmanage.AzureManager) SubscriptionManager(com.microsoft.azuretools.authmanage.SubscriptionManager) SubscriptionDetail(com.microsoft.azuretools.authmanage.models.SubscriptionDetail) Subscription(com.microsoft.azure.management.resources.Subscription) ResourceGroup(com.microsoft.azure.management.resources.ResourceGroup) Location(com.microsoft.azure.management.resources.Location)

Example 13 with SubscriptionManager

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

the class AzureModelController method subscriptionSelectionChanged.

private static synchronized void subscriptionSelectionChanged(IProgressIndicator progressIndicator) throws IOException, AuthException {
    System.out.println("AzureModelController.subscriptionSelectionChanged: starting");
    AzureManager azureManager = AuthMethodManager.getInstance().getAzureManager();
    // not signed in
    if (azureManager == null) {
        System.out.println("AzureModelController.subscriptionSelectionChanged: azureManager == null -> return");
        return;
    }
    SubscriptionManager subscriptionManager = azureManager.getSubscriptionManager();
    AzureModel azureModel = AzureModel.getInstance();
    Map<SubscriptionDetail, List<ResourceGroup>> srgMap = azureModel.getSubscriptionToResourceGroupMap();
    if (srgMap == null) {
        System.out.println("AzureModelController.subscriptionSelectionChanged: srgMap == null -> return");
        return;
    }
    Map<String, Subscription> sidToSubscriptionMap = azureModel.getSidToSubscriptionMap();
    if (sidToSubscriptionMap == null) {
        System.out.println("AzureModelController.subscriptionSelectionChanged: sidToSubscriptionMap == null -> return");
        return;
    }
    Map<ResourceGroup, List<WebApp>> rgwaMap = azureModel.getResourceGroupToWebAppMap();
    Map<ResourceGroup, List<AppServicePlan>> rgspMap = azureModel.getResourceGroupToAppServicePlanMap();
    System.out.println("AzureModelController.subscriptionSelectionChanged: getting subscription details...");
    List<SubscriptionDetail> sdl = subscriptionManager.getSubscriptionDetails();
    if (sdl == null) {
        System.out.println("AzureModelController.subscriptionSelectionChanged: sdl == null -> return");
        return;
    }
    for (SubscriptionDetail sd : sdl) {
        if (!srgMap.containsKey(sd)) {
            if (!sd.isSelected())
                continue;
            if (progressIndicator != null && progressIndicator.isCanceled()) {
                progressIndicator.setText("Cancelling...");
                clearAll();
                return;
            // FIXME: throw exception?
            }
            Azure azure = azureManager.getAzure(sd.getSubscriptionId());
            // subscription locations
            List<Subscription> sl = azureManager.getSubscriptions();
            System.out.println("Updating subscription locations");
            Subscription subscription = sidToSubscriptionMap.get(sd.getSubscriptionId());
            if (progressIndicator != null)
                progressIndicator.setText(String.format("Updating subscription '%s' locations...", subscription.displayName()));
            List<Location> locl = subscription.listLocations();
            Collections.sort(locl, new Comparator<Location>() {

                @Override
                public int compare(Location lhs, Location rhs) {
                    return lhs.displayName().compareTo(rhs.displayName());
                }
            });
            Map<SubscriptionDetail, List<Location>> sdlocMap = azureModel.getSubscriptionToLocationMap();
            sdlocMap.put(sd, locl);
            // resource group maps
            List<ResourceGroup> rgList = azure.resourceGroups().list();
            srgMap.put(sd, rgList);
            updateResGrDependency(azure, rgList, progressIndicator, rgwaMap, rgspMap);
        } else {
            // find and modify the key
            for (SubscriptionDetail sdk : srgMap.keySet()) {
                if (sdk.equals(sd)) {
                    sdk.setSelected(sd.isSelected());
                }
            }
        }
    }
}
Also used : Azure(com.microsoft.azure.management.Azure) AzureManager(com.microsoft.azuretools.sdkmanage.AzureManager) SubscriptionManager(com.microsoft.azuretools.authmanage.SubscriptionManager) SubscriptionDetail(com.microsoft.azuretools.authmanage.models.SubscriptionDetail) Subscription(com.microsoft.azure.management.resources.Subscription) ResourceGroup(com.microsoft.azure.management.resources.ResourceGroup) Location(com.microsoft.azure.management.resources.Location)

Example 14 with SubscriptionManager

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

the class SubscriptionStep method loadSubscriptions.

private void loadSubscriptions() {
    try {
        AzureManager azureManager = AuthMethodManager.getInstance().getAzureManager();
        // not signed in
        if (azureManager == null) {
            model.getCurrentNavigationState().NEXT.setEnabled(false);
            return;
        }
        SubscriptionManager subscriptionManager = azureManager.getSubscriptionManager();
        List<SubscriptionDetail> subscriptionDetails = subscriptionManager.getSubscriptionDetails();
        List<SubscriptionDetail> selectedSubscriptions = subscriptionDetails.stream().filter(SubscriptionDetail::isSelected).collect(Collectors.toList());
        subscriptionComboBox.setModel(new DefaultComboBoxModel<>(selectedSubscriptions.toArray(new SubscriptionDetail[selectedSubscriptions.size()])));
        if (selectedSubscriptions.size() > 0) {
            model.setSubscription(selectedSubscriptions.get(0));
        }
    } catch (Exception ex) {
        DefaultLoader.getUIHelper().logError("An error occurred when trying to load Subscriptions\n\n" + ex.getMessage(), ex);
    }
//            if (manager.authenticated()) {
//                String upn = manager.getUserInfo().getUniqueName();
//                userInfoLabel.setText("Signed in as: " + (upn.contains("#") ? upn.split("#")[1] : upn));
//            } else {
//                userInfoLabel.setText("");
//            }
}
Also used : AzureManager(com.microsoft.azuretools.sdkmanage.AzureManager) SubscriptionDetail(com.microsoft.azuretools.authmanage.models.SubscriptionDetail) SubscriptionManager(com.microsoft.azuretools.authmanage.SubscriptionManager)

Example 15 with SubscriptionManager

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

the class AzureDockerHostsManager method subscriptionsChanged.

private static boolean subscriptionsChanged(AzureManager azureAuthManager) {
    try {
        SubscriptionManager subscriptionManager = azureAuthManager.getSubscriptionManager();
        List<SubscriptionDetail> subscriptions = subscriptionManager.getSubscriptionDetails();
        if (instance.subscriptionsMap == null || instance.subscriptionsMap.isEmpty()) {
            return true;
        }
        for (SubscriptionDetail subscriptionDetail : subscriptions) {
            if (subscriptionDetail.isSelected() != instance.subscriptionsMap.get(subscriptionDetail.getSubscriptionId()).isSelected)
                return true;
        }
        return false;
    } catch (Exception ignored) {
        return true;
    }
}
Also used : SubscriptionDetail(com.microsoft.azuretools.authmanage.models.SubscriptionDetail) SubscriptionManager(com.microsoft.azuretools.authmanage.SubscriptionManager)

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