Search in sources :

Example 31 with AzureManager

use of com.microsoft.azuretools.sdkmanage.AzureManager in project azure-tools-for-java by Microsoft.

the class ClusterOperationImpl method requestWithToken.

@NotNull
public <T> T requestWithToken(@NotNull String tenantId, @NotNull final RequestCallback<T> requestCallback) throws Throwable {
    AzureManager azureManager = AuthMethodManager.getInstance().getAzureManager();
    // not signed in
    if (azureManager == null) {
        return null;
    }
    String accessToken = azureManager.getAccessToken(tenantId);
    return requestCallback.execute(accessToken);
}
Also used : AzureManager(com.microsoft.azuretools.sdkmanage.AzureManager) NotNull(com.microsoft.azuretools.azurecommons.helpers.NotNull)

Example 32 with AzureManager

use of com.microsoft.azuretools.sdkmanage.AzureManager in project azure-tools-for-java by Microsoft.

the class AppInsightsMngmtPanel method loadInfoFirstTime.

private void loadInfoFirstTime() {
    try {
        if (AuthMethodManager.getInstance().isSignedIn()) {
            AzureManager azureManager = AuthMethodManager.getInstance().getAzureManager();
            List<SubscriptionDetail> subList = azureManager.getSubscriptionManager().getSubscriptionDetails();
            if (subList.size() > 0) {
                //                if (!AzureSettings.getSafeInstance(myProject).isAppInsightsLoaded()) {
                updateApplicationInsightsResourceRegistry(subList, myProject);
            } else {
                // just show manually added list from preferences
                // Neither clear subscription list nor show sign in dialog as user may just want to add key manually.
                keeepManuallyAddedList(myProject);
            }
        //                } else {
        // show list from preferences - getTableContent() does it. So nothing to handle here
        //                }
        } else {
            // just show manually added list from preferences
            keeepManuallyAddedList(myProject);
        }
    } catch (Exception ex) {
        AzurePlugin.log(ex.getMessage(), ex);
    }
}
Also used : AzureManager(com.microsoft.azuretools.sdkmanage.AzureManager) SubscriptionDetail(com.microsoft.azuretools.authmanage.models.SubscriptionDetail)

Example 33 with AzureManager

use of com.microsoft.azuretools.sdkmanage.AzureManager in project azure-tools-for-java by Microsoft.

the class AzureSelectDockerWizard method createDockerContainerDeployTask.

public void createDockerContainerDeployTask(IProject project, AzureDockerImageInstance dockerImageInstance, AzureDockerHostsManager dockerManager) {
    String url = AzureDockerUtils.getUrl(dockerImageInstance);
    String deploymentName = url;
    String jobDescription = String.format("Publishing %s as Docker Container", new File(dockerImageInstance.artifactPath).getName());
    AzureDeploymentProgressNotification.createAzureDeploymentProgressNotification(deploymentName, jobDescription);
    Map<String, String> postEventProperties = new HashMap<String, String>();
    postEventProperties.put("DockerFileOption", dockerImageInstance.predefinedDockerfile);
    Job createDockerHostJob = new Job(jobDescription) {

        @Override
        protected IStatus run(IProgressMonitor progressMonitor) {
            try {
                // Setup Azure Console and Azure Activity Log Window notifications
                MessageConsole console = com.microsoft.azuretools.core.Activator.findConsole(com.microsoft.azuretools.core.Activator.CONSOLE_NAME);
                console.activate();
                final MessageConsoleStream azureConsoleOut = console.newMessageStream();
                progressMonitor.beginTask("start task", 100);
                //		            com.microsoft.azuretools.core.Activator.removeUnNecessaryListener();
                DeploymentEventListener undeployListnr = new DeploymentEventListener() {

                    @Override
                    public void onDeploymentStep(DeploymentEventArgs args) {
                        progressMonitor.subTask(args.getDeployMessage());
                        progressMonitor.worked(args.getDeployCompleteness());
                        azureConsoleOut.println(String.format("%s: %s", deploymentName, args.getDeployMessage()));
                    }
                };
                com.microsoft.azuretools.core.Activator.getDefault().addDeploymentEventListener(undeployListnr);
                com.microsoft.azuretools.core.Activator.depEveList.add(undeployListnr);
                // Start the real job here
                String msg = String.format("Publishing %s to Docker host %s ...", new File(dockerImageInstance.artifactPath).getName(), dockerImageInstance.host.name);
                AzureDeploymentProgressNotification.notifyProgress(this, deploymentName, url, 0, msg);
                msg = "Connecting to Azure...";
                AzureDeploymentProgressNotification.notifyProgress(this, deploymentName, url, 5, msg);
                AzureManager azureAuthManager = AuthMethodManager.getInstance().getAzureManager();
                // not signed in
                if (azureAuthManager == null) {
                    throw new RuntimeException("User not signed in");
                }
                AzureDockerHostsManager dockerManager = AzureDockerHostsManager.getAzureDockerHostsManagerEmpty(azureAuthManager);
                Azure azureClient = dockerManager.getSubscriptionsMap().get(dockerImageInstance.host.sid).azureClient;
                if (progressMonitor.isCanceled()) {
                    displayWarningOnCreateDockerContainerDeployTask(this, progressMonitor, deploymentName);
                    return Status.CANCEL_STATUS;
                }
                if (dockerImageInstance.hasNewDockerHost) {
                    msg = String.format("Creating new virtual machine %s ...", dockerImageInstance.host.name);
                    AzureDeploymentProgressNotification.notifyProgress(this, deploymentName, url, 20, msg);
                    AzureDockerUIResources.printDebugMessage(this, "Creating new virtual machine: " + new Date().toString());
                    AzureDockerVMOps.createDockerHostVM(azureClient, dockerImageInstance.host);
                    AzureDockerUIResources.printDebugMessage(this, "Done creating new virtual machine: " + new Date().toString());
                    if (progressMonitor.isCanceled()) {
                        displayWarningOnCreateDockerContainerDeployTask(this, progressMonitor, deploymentName);
                        return Status.CANCEL_STATUS;
                    }
                    msg = String.format("Updating Docker hosts ...");
                    AzureDeploymentProgressNotification.notifyProgress(this, deploymentName, url, 5, msg);
                    AzureDockerUIResources.printDebugMessage(this, "Getting the new docker host details: " + new Date().toString());
                    //			            dockerManager.refreshDockerHostDetails();
                    VirtualMachine vm = azureClient.virtualMachines().getByResourceGroup(dockerImageInstance.host.hostVM.resourceGroupName, dockerImageInstance.host.hostVM.name);
                    if (vm != null) {
                        DockerHost updatedHost = AzureDockerVMOps.getDockerHost(vm, dockerManager.getDockerVaultsMap());
                        if (updatedHost != null) {
                            dockerImageInstance.host.hostVM = updatedHost.hostVM;
                            dockerImageInstance.host.apiUrl = updatedHost.apiUrl;
                        }
                    }
                    AzureDockerUIResources.printDebugMessage(this, "Done getting new Docker host details: " + new Date().toString());
                    msg = String.format("Waiting for virtual machine to be up %s ...", dockerImageInstance.host.name);
                    AzureDeploymentProgressNotification.notifyProgress(this, deploymentName, url, 10, msg);
                    AzureDockerUIResources.printDebugMessage(this, "Waiting for virtual machine to be up: " + new Date().toString());
                    AzureDockerVMOps.waitForVirtualMachineStartup(azureClient, dockerImageInstance.host);
                    AzureDockerUIResources.printDebugMessage(this, "Done Waiting for virtual machine to be up: " + new Date().toString());
                    if (progressMonitor.isCanceled()) {
                        displayWarningOnCreateDockerContainerDeployTask(this, progressMonitor, deploymentName);
                        return Status.CANCEL_STATUS;
                    }
                    msg = String.format("Configuring Docker service for %s ...", dockerImageInstance.host.name);
                    AzureDeploymentProgressNotification.notifyProgress(this, deploymentName, url, 10, msg);
                    AzureDockerUIResources.printDebugMessage(this, "Configuring Docker host: " + new Date().toString());
                    AzureDockerVMOps.installDocker(dockerImageInstance.host);
                    AzureDockerUIResources.printDebugMessage(this, "Done configuring Docker host: " + new Date().toString());
                    msg = String.format("Updating Docker hosts ...");
                    AzureDeploymentProgressNotification.notifyProgress(this, deploymentName, url, 5, msg);
                    AzureDockerUIResources.printDebugMessage(this, "Refreshing docker hosts: " + new Date().toString());
                    //			            dockerManager.refreshDockerHostDetails();
                    vm = azureClient.virtualMachines().getByResourceGroup(dockerImageInstance.host.hostVM.resourceGroupName, dockerImageInstance.host.hostVM.name);
                    if (vm != null) {
                        DockerHost updatedHost = AzureDockerVMOps.getDockerHost(vm, dockerManager.getDockerVaultsMap());
                        if (updatedHost != null) {
                            updatedHost.sid = dockerImageInstance.host.sid;
                            updatedHost.hostVM.sid = dockerImageInstance.host.hostVM.sid;
                            if (updatedHost.certVault == null) {
                                updatedHost.certVault = dockerImageInstance.host.certVault;
                                updatedHost.hasPwdLogIn = dockerImageInstance.host.hasPwdLogIn;
                                updatedHost.hasSSHLogIn = dockerImageInstance.host.hasSSHLogIn;
                                updatedHost.isTLSSecured = dockerImageInstance.host.isTLSSecured;
                            }
                            dockerManager.addDockerHostDetails(updatedHost);
                            if (AzureUIRefreshCore.listeners != null) {
                                AzureUIRefreshCore.execute(new AzureUIRefreshEvent(AzureUIRefreshEvent.EventType.ADD, updatedHost));
                            }
                        }
                    }
                    AzureDockerUIResources.printDebugMessage(this, "Done refreshing Docker hosts: " + new Date().toString());
                    AzureDockerUIResources.printDebugMessage(this, "Finished setting up Docker host");
                } else {
                    msg = String.format("Using virtual machine %s ...", dockerImageInstance.host.name);
                    AzureDeploymentProgressNotification.notifyProgress(this, deploymentName, url, 55, msg);
                }
                if (progressMonitor.isCanceled()) {
                    displayWarningOnCreateDockerContainerDeployTask(this, progressMonitor, deploymentName);
                    return Status.CANCEL_STATUS;
                }
                if (dockerImageInstance.host.session == null) {
                    AzureDockerUIResources.printDebugMessage(this, "Opening a remote connection to the Docker host: " + new Date().toString());
                    dockerImageInstance.host.session = AzureDockerSSHOps.createLoginInstance(dockerImageInstance.host);
                    AzureDockerUIResources.printDebugMessage(this, "Done opening a remote connection to the Docker host: " + new Date().toString());
                }
                if (dockerImageInstance.hasNewDockerHost) {
                    if (dockerImageInstance.host.certVault != null && dockerImageInstance.host.certVault.hostName != null) {
                        AzureDockerUIResources.createDockerKeyVault(dockerImageInstance.host, dockerManager);
                    }
                }
                msg = String.format("Uploading Dockerfile and artifact %s on %s ...", dockerImageInstance.artifactName, dockerImageInstance.host.name);
                AzureDeploymentProgressNotification.notifyProgress(this, deploymentName, url, 15, msg);
                AzureDockerUIResources.printDebugMessage(this, "Uploading Dockerfile and artifact: " + new Date().toString());
                AzureDockerVMOps.uploadDockerfileAndArtifact(dockerImageInstance, dockerImageInstance.host.session);
                AzureDockerUIResources.printDebugMessage(this, "Uploading Dockerfile and artifact: " + new Date().toString());
                if (progressMonitor.isCanceled()) {
                    displayWarningOnCreateDockerContainerDeployTask(this, progressMonitor, deploymentName);
                    return Status.CANCEL_STATUS;
                }
                msg = String.format("Creating Docker image %s on %s ...", dockerImageInstance.dockerImageName, dockerImageInstance.host.name);
                AzureDeploymentProgressNotification.notifyProgress(this, deploymentName, url, 15, msg);
                AzureDockerUIResources.printDebugMessage(this, "Creating a Docker image to the Docker host: " + new Date().toString());
                AzureDockerImageOps.create(dockerImageInstance, dockerImageInstance.host.session);
                AzureDockerUIResources.printDebugMessage(this, "Done creating a Docker image to the Docker host: " + new Date().toString());
                if (progressMonitor.isCanceled()) {
                    displayWarningOnCreateDockerContainerDeployTask(this, progressMonitor, deploymentName);
                    return Status.CANCEL_STATUS;
                }
                msg = String.format("Creating Docker container %s for image %s on %s ...", dockerImageInstance.dockerContainerName, dockerImageInstance.dockerImageName, dockerImageInstance.host.name);
                AzureDeploymentProgressNotification.notifyProgress(this, deploymentName, url, 5, msg);
                AzureDockerUIResources.printDebugMessage(this, "Creating a Docker container to the Docker host: " + new Date().toString());
                AzureDockerContainerOps.create(dockerImageInstance, dockerImageInstance.host.session);
                AzureDockerUIResources.printDebugMessage(this, "Done creating a Docker container to the Docker host: " + new Date().toString());
                if (progressMonitor.isCanceled()) {
                    displayWarningOnCreateDockerContainerDeployTask(this, progressMonitor, deploymentName);
                    return Status.CANCEL_STATUS;
                }
                msg = String.format("Starting Docker container %s for image %s on %s ...", dockerImageInstance.dockerContainerName, dockerImageInstance.dockerImageName, dockerImageInstance.host.name);
                AzureDeploymentProgressNotification.notifyProgress(this, deploymentName, url, 5, msg);
                AzureDockerUIResources.printDebugMessage(this, "Starting a Docker container to the Docker host: " + new Date().toString());
                AzureDockerContainerOps.start(dockerImageInstance, dockerImageInstance.host.session);
                AzureDockerUIResources.printDebugMessage(this, "Done starting a Docker container to the Docker host: " + new Date().toString());
                if (progressMonitor.isCanceled()) {
                    displayWarningOnCreateDockerContainerDeployTask(this, progressMonitor, deploymentName);
                    return Status.CANCEL_STATUS;
                }
                AzureDeploymentProgressNotification.notifyProgress(this, deploymentName, url, 100, "");
                try {
                    com.microsoft.azuretools.core.Activator.depEveList.remove(undeployListnr);
                    com.microsoft.azuretools.core.Activator.removeDeploymentEventListener(undeployListnr);
                } catch (Exception ignored) {
                }
                progressMonitor.done();
                AppInsightsClient.create("Deploy as DockerContainer", "", postEventProperties);
                return Status.OK_STATUS;
            } catch (Exception e) {
                String msg = "An error occurred while attempting to publish a Docker container!" + "\n" + e.getMessage();
                log.log(Level.SEVERE, "createDockerContainerDeployTask: " + msg, e);
                e.printStackTrace();
                AzureDeploymentProgressNotification.notifyProgress(this, deploymentName, null, -1, "Error: " + e.getMessage());
                return Status.CANCEL_STATUS;
            }
        }
    };
    createDockerHostJob.schedule();
}
Also used : Azure(com.microsoft.azure.management.Azure) MessageConsole(org.eclipse.ui.console.MessageConsole) AzureManager(com.microsoft.azuretools.sdkmanage.AzureManager) HashMap(java.util.HashMap) DeploymentEventArgs(com.microsoft.azuretools.azurecommons.deploy.DeploymentEventArgs) MessageConsoleStream(org.eclipse.ui.console.MessageConsoleStream) AzureUIRefreshEvent(com.microsoft.azuretools.utils.AzureUIRefreshEvent) Date(java.util.Date) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) EditableDockerHost(com.microsoft.azure.docker.model.EditableDockerHost) DockerHost(com.microsoft.azure.docker.model.DockerHost) DeploymentEventListener(com.microsoft.azuretools.azurecommons.deploy.DeploymentEventListener) Job(org.eclipse.core.runtime.jobs.Job) AzureDockerHostsManager(com.microsoft.azure.docker.AzureDockerHostsManager) File(java.io.File) VirtualMachine(com.microsoft.azure.management.compute.VirtualMachine)

Example 34 with AzureManager

use of com.microsoft.azuretools.sdkmanage.AzureManager in project azure-tools-for-java by Microsoft.

the class AzureDockerUIResources method updateAzureResourcesWithProgressDialog.

public static void updateAzureResourcesWithProgressDialog(Shell shell, IProject project) {
    ProgressMonitorDialog dialog = new ProgressMonitorDialog(shell);
    try {
        dialog.run(true, true, new IRunnableWithProgress() {

            public void run(IProgressMonitor monitor) {
                monitor.beginTask("Loading Azure Resources", 100);
                try {
                    monitor.subTask("Creating an Azure instance...");
                    AzureManager azureAuthManager = AuthMethodManager.getInstance().getAzureManager();
                    if (monitor.isCanceled()) {
                        monitor.done();
                        return;
                    }
                    // not signed in
                    if (azureAuthManager == null) {
                        monitor.done();
                        return;
                    }
                    AzureDockerHostsManager dockerManager = AzureDockerHostsManager.getAzureDockerHostsManagerEmpty(azureAuthManager);
                    monitor.worked(10);
                    monitor.subTask("Retrieving the subscription details...");
                    dockerManager.refreshDockerSubscriptions();
                    if (monitor.isCanceled()) {
                        monitor.done();
                        return;
                    }
                    monitor.worked(10);
                    monitor.subTask("Retrieving the key vault...");
                    dockerManager.refreshDockerVaults();
                    monitor.worked(10);
                    if (monitor.isCanceled()) {
                        monitor.done();
                        return;
                    }
                    monitor.worked(10);
                    monitor.subTask("Retrieving the key vault details...");
                    dockerManager.refreshDockerVaultDetails();
                    if (monitor.isCanceled()) {
                        CANCELED = true;
                        monitor.done();
                        return;
                    }
                    monitor.worked(10);
                    monitor.subTask("Retrieving the network details...");
                    dockerManager.refreshDockerVnetDetails();
                    if (monitor.isCanceled()) {
                        monitor.done();
                        return;
                    }
                    monitor.worked(10);
                    monitor.subTask("Retrieving the storage account details...");
                    dockerManager.refreshDockerStorageAccountDetails();
                    if (monitor.isCanceled()) {
                        monitor.done();
                        return;
                    }
                    monitor.subTask("Retrieving the Docker virtual machines details...");
                    dockerManager.refreshDockerHostDetails();
                    CANCELED = false;
                } catch (Exception e) {
                    CANCELED = true;
                    log.log(Level.SEVERE, "updateAzureResourcesWithProgressDialog: " + e.getMessage(), e);
                    e.printStackTrace();
                }
                monitor.done();
            }
        });
    } catch (Exception e) {
        CANCELED = true;
        log.log(Level.SEVERE, "updateAzureResourcesWithProgressDialog: " + e.getMessage(), e);
        e.printStackTrace();
    }
}
Also used : IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) AzureManager(com.microsoft.azuretools.sdkmanage.AzureManager) ProgressMonitorDialog(org.eclipse.jface.dialogs.ProgressMonitorDialog) AzureDockerHostsManager(com.microsoft.azure.docker.AzureDockerHostsManager) IRunnableWithProgress(org.eclipse.jface.operation.IRunnableWithProgress)

Example 35 with AzureManager

use of com.microsoft.azuretools.sdkmanage.AzureManager 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)

Aggregations

AzureManager (com.microsoft.azuretools.sdkmanage.AzureManager)48 SubscriptionDetail (com.microsoft.azuretools.authmanage.models.SubscriptionDetail)17 Azure (com.microsoft.azure.management.Azure)15 SubscriptionManager (com.microsoft.azuretools.authmanage.SubscriptionManager)15 AzureCmdException (com.microsoft.azuretools.azurecommons.helpers.AzureCmdException)12 AzureDockerHostsManager (com.microsoft.azure.docker.AzureDockerHostsManager)8 ResourceGroup (com.microsoft.azure.management.resources.ResourceGroup)8 DockerHost (com.microsoft.azure.docker.model.DockerHost)6 Location (com.microsoft.azure.management.resources.Location)4 AzureDockerImageInstance (com.microsoft.azure.docker.model.AzureDockerImageInstance)3 EditableDockerHost (com.microsoft.azure.docker.model.EditableDockerHost)3 VirtualMachine (com.microsoft.azure.management.compute.VirtualMachine)3 Subscription (com.microsoft.azure.management.resources.Subscription)3 File (java.io.File)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 ImmutablePair (org.apache.commons.lang3.tuple.ImmutablePair)3 KeyVaultClient (com.microsoft.azure.keyvault.KeyVaultClient)2 Network (com.microsoft.azure.management.network.Network)2 Kind (com.microsoft.azure.management.storage.Kind)2