Search in sources :

Example 16 with SubscriptionManager

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

the class SelectSubscriptionsAction method onShowSubscriptions.

public static void onShowSubscriptions(Project project) {
    JFrame frame = WindowManager.getInstance().getFrame(project);
    try {
        //Project project = ProjectManager.getInstance().getDefaultProject();();
        AzureManager manager = AuthMethodManager.getInstance().getAzureManager();
        if (manager == null) {
            return;
        }
        final SubscriptionManager subscriptionManager = manager.getSubscriptionManager();
        updateSubscriptionWithProgressDialog(subscriptionManager, project);
        List<SubscriptionDetail> sdl = subscriptionManager.getSubscriptionDetails();
        for (SubscriptionDetail sd : sdl) {
            System.out.println(sd.getSubscriptionName());
        }
        //System.out.println("onShowSubscriptions: calling getSubscriptionDetails()");
        SubscriptionsDialog d = SubscriptionsDialog.go(subscriptionManager.getSubscriptionDetails(), project);
        List<SubscriptionDetail> subscriptionDetailsUpdated;
        if (d != null) {
            subscriptionDetailsUpdated = d.getSubscriptionDetails();
            subscriptionManager.setSubscriptionDetails(subscriptionDetailsUpdated);
        }
    } catch (Exception ex) {
        ex.printStackTrace();
        //LOGGER.error("onShowSubscriptions", ex);
        ErrorWindow.show(project, ex.getMessage(), "Select Subscriptions Action Error");
    }
}
Also used : AzureManager(com.microsoft.azuretools.sdkmanage.AzureManager) SubscriptionDetail(com.microsoft.azuretools.authmanage.models.SubscriptionDetail) SubscriptionsDialog(com.microsoft.azuretools.ijidea.ui.SubscriptionsDialog) SubscriptionManager(com.microsoft.azuretools.authmanage.SubscriptionManager)

Example 17 with SubscriptionManager

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

the class Program method main.

@SuppressWarnings("unused")
public static void main(String[] args) {
    LogManager.getLogManager().reset();
    try {
        try {
            if (CommonSettings.getUiFactory() == null)
                CommonSettings.setUiFactory(new UIFactory());
            String wd = "AuthManageWorkingDir";
            Path dirPath = Paths.get(System.getProperty("user.home"), wd);
            if (!Files.exists(dirPath)) {
                Files.createDirectory(dirPath);
            }
            CommonSettings.settingsBaseDir = dirPath.toString();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("--- Service Principals section ---");
        // create with sp
        //final File credFile = new File(System.getProperty("user.home") + "/ServicePrincipals/srv-pri.azureauth-778");
        String path = "C:\\Users\\shch\\_ServicePrincipalsTest\\sp5\\sp-9657ab5d-4a4a-4fd2-ae7a-4cd9fbd030ef_20161202161437.azureauth";
        final File credFile = new File("C:\\Users\\shch\\_ServicePrincipalsTest\\sp5\\sp-9657ab5d-4a4a-4fd2-ae7a-4cd9fbd030ef_20161202161437.azureauth");
        AuthMethodDetails amd = new AuthMethodDetails();
        amd.setAuthMethod(AuthMethod.SP);
        amd.setCredFilePath(path);
        AuthMethodManager amm = AuthMethodManager.getInstance();
        amm.setAuthMethodDetails(amd);
        AzureManager am = amm.getAzureManager();
        SubscriptionManager subscriptionManager = am.getSubscriptionManager();
        List<SubscriptionDetail> sdl = subscriptionManager.getSubscriptionDetails();
        //AzureManager spManager = new ServicePrincipalAzureManager(credFile);
        //printResourceGroups(spManager);
        //printSubscriptions(spManager);
        // create with token
        //AzureManager atManager = new AccessTokenAzureManager(Constants.tenant);
        //AzureManager atManager = new AccessTokenAzureManager();
        //SubscriptionManager.getInstance().showSubscriptionDialog();
        //            AdAuthManager adAuthManager = AdAuthManager.getInstance();
        System.out.println("=== Access token section ---");
        //            AuthenticationResult res = AdAuthManager.getInstance().signIn();
        //            UserInfo ui = res.getUserInfo();
        // Setup working dir
        String wd = "AuthManageWorkingDir";
        Path dirPath = Paths.get(System.getProperty("user.home"), wd);
        if (!Files.exists(dirPath)) {
            Files.createDirectory(dirPath);
        }
        CommonSettings.settingsBaseDir = dirPath.toString();
        // Setup uiFactory
        if (CommonSettings.getUiFactory() == null)
            CommonSettings.setUiFactory(new UIFactory());
    //testReporter(dirPath.toString());
    //graphApiAction();
    //createSp();
    //            AzureManager am = AuthMethodManager.getInstance().getAzureManager(AuthMethod.AD);
    //            if (am == null) {
    //                return;
    //            }
    //
    //            System.out.println("-- Subscription list ---");
    //            printSubscriptions(am);
    //           sidList = AdAuthManager.getInstance().getAccountSidList();
    //            for (String sid : AdAuthManager.getInstance().getAccountSidList()) {
    //                System.out.println("  - " + sid);
    //            }
    //printResourceGroups(am);
    //printTenants(atManager);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        System.exit(1);
    }
}
Also used : Path(java.nio.file.Path) AuthMethodDetails(com.microsoft.azuretools.authmanage.models.AuthMethodDetails) IUIFactory(com.microsoft.azuretools.authmanage.interact.IUIFactory) AccessTokenAzureManager(com.microsoft.azuretools.sdkmanage.AccessTokenAzureManager) AzureManager(com.microsoft.azuretools.sdkmanage.AzureManager) SubscriptionDetail(com.microsoft.azuretools.authmanage.models.SubscriptionDetail) IOException(java.io.IOException) SubscriptionManager(com.microsoft.azuretools.authmanage.SubscriptionManager) File(java.io.File) IOException(java.io.IOException) AuthMethodManager(com.microsoft.azuretools.authmanage.AuthMethodManager)

Example 18 with SubscriptionManager

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

the class VMArmModule method refreshItems.

@Override
protected void refreshItems() throws AzureCmdException {
    List<Pair<String, String>> failedSubscriptions = new ArrayList<>();
    try {
        AzureManager azureManager = AuthMethodManager.getInstance().getAzureManager();
        // not signed in
        if (azureManager == null) {
            return;
        }
        SubscriptionManager subscriptionManager = azureManager.getSubscriptionManager();
        Set<String> sidList = subscriptionManager.getSelectedSubscriptionDetails().stream().filter(subscriptionDetail -> Objects.nonNull(subscriptionDetail) && subscriptionDetail.isSelected()).map(subscriptionDetail -> subscriptionDetail.getSubscriptionId()).collect(Collectors.toSet());
        for (String sid : sidList) {
            try {
                Azure azure = azureManager.getAzure(sid);
                List<VirtualMachine> virtualMachines = azure.virtualMachines().list();
                for (VirtualMachine vm : virtualMachines) {
                    addChildNode(new VMNode(this, sid, vm));
                }
            } catch (Exception ex) {
                failedSubscriptions.add(new ImmutablePair<>(sid, ex.getMessage()));
                continue;
            }
        }
    } catch (Exception ex) {
        DefaultLoader.getUIHelper().logError("An error occurred when trying to load Virtual Machines\n\n" + ex.getMessage(), ex);
    }
    if (!failedSubscriptions.isEmpty()) {
        StringBuilder errorMessage = new StringBuilder("An error occurred when trying to load Storage Accounts for the subscriptions:\n\n");
        for (Pair error : failedSubscriptions) {
            errorMessage.append(error.getKey()).append(": ").append(error.getValue()).append("\n");
        }
        DefaultLoader.getUIHelper().logError("An error occurred when trying to load Storage Accounts\n\n" + errorMessage.toString(), null);
    }
}
Also used : AuthMethodManager(com.microsoft.azuretools.authmanage.AuthMethodManager) VirtualMachine(com.microsoft.azure.management.compute.VirtualMachine) SubscriptionManager(com.microsoft.azuretools.authmanage.SubscriptionManager) Nullable(com.microsoft.azuretools.azurecommons.helpers.Nullable) Node(com.microsoft.tooling.msservices.serviceexplorer.Node) Set(java.util.Set) AzureManager(com.microsoft.azuretools.sdkmanage.AzureManager) Collectors(java.util.stream.Collectors) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) ArrayList(java.util.ArrayList) Objects(java.util.Objects) Azure(com.microsoft.azure.management.Azure) AzureIconSymbol(com.microsoft.tooling.msservices.serviceexplorer.AzureIconSymbol) List(java.util.List) Pair(org.apache.commons.lang3.tuple.Pair) AzureCmdException(com.microsoft.azuretools.azurecommons.helpers.AzureCmdException) DefaultLoader(com.microsoft.tooling.msservices.components.DefaultLoader) AzureRefreshableNode(com.microsoft.tooling.msservices.serviceexplorer.AzureRefreshableNode) Azure(com.microsoft.azure.management.Azure) AzureManager(com.microsoft.azuretools.sdkmanage.AzureManager) ArrayList(java.util.ArrayList) SubscriptionManager(com.microsoft.azuretools.authmanage.SubscriptionManager) AzureCmdException(com.microsoft.azuretools.azurecommons.helpers.AzureCmdException) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) Pair(org.apache.commons.lang3.tuple.Pair) VirtualMachine(com.microsoft.azure.management.compute.VirtualMachine)

Example 19 with SubscriptionManager

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

the class SelectSubscriptionsAction method selectSubscriptions.

public static Single<List<SubscriptionDetail>> selectSubscriptions(Project project) {
    final AuthMethodManager authMethodManager = AuthMethodManager.getInstance();
    final AzureManager manager = authMethodManager.getAzureManager();
    if (manager == null) {
        return Single.fromCallable(() -> null);
    }
    final SubscriptionManager subscriptionManager = manager.getSubscriptionManager();
    return loadSubscriptions(subscriptionManager, project).switchMap((subs) -> selectSubscriptions(project, subs)).toSingle().doOnSuccess((subs) -> Optional.ofNullable(subs).ifPresent(subscriptionManager::setSubscriptionDetails));
}
Also used : AzureManager(com.microsoft.azuretools.sdkmanage.AzureManager) SubscriptionManager(com.microsoft.azuretools.authmanage.SubscriptionManager) AuthMethodManager(com.microsoft.azuretools.authmanage.AuthMethodManager)

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