Search in sources :

Example 26 with Operation

use of com.microsoft.azuretools.telemetrywrapper.Operation in project azure-tools-for-java by Microsoft.

the class AzureSignInAction method call.

private static <T> T call(Callable<T> loginCallable, String authMethod) {
    final Operation operation = TelemetryManager.createOperation(ACCOUNT, SIGNIN);
    final Map<String, String> properties = new HashMap<>();
    properties.put(SIGNIN_METHOD, authMethod);
    Optional.ofNullable(ProgressManager.getInstance().getProgressIndicator()).ifPresent(indicator -> indicator.setText2("Signing in..."));
    try {
        operation.start();
        operation.trackProperties(properties);
        operation.trackProperty(AZURE_ENVIRONMENT, Azure.az(AzureCloud.class).getName());
        return loginCallable.call();
    } catch (Exception e) {
        if (shouldNoticeErrorToUser(e)) {
            EventUtil.logError(operation, ErrorType.userError, e, properties, null);
        }
        throw new AzureToolkitRuntimeException(e.getMessage(), e);
    } finally {
        operation.complete();
    }
}
Also used : HashMap(java.util.HashMap) AzureToolkitRuntimeException(com.microsoft.azure.toolkit.lib.common.exception.AzureToolkitRuntimeException) Operation(com.microsoft.azuretools.telemetrywrapper.Operation) AzureOperation(com.microsoft.azure.toolkit.lib.common.operation.AzureOperation) AzureString(com.microsoft.azure.toolkit.lib.common.bundle.AzureString) MsalClientException(com.microsoft.aad.msal4j.MsalClientException) CancellationException(java.util.concurrent.CancellationException) AzureToolkitRuntimeException(com.microsoft.azure.toolkit.lib.common.exception.AzureToolkitRuntimeException)

Example 27 with Operation

use of com.microsoft.azuretools.telemetrywrapper.Operation in project azure-tools-for-java by Microsoft.

the class WebAppService method createWebApp.

@AzureOperation(name = "webapp.create_detail", params = { "config.getName()" }, type = AzureOperation.Type.SERVICE)
public IWebApp createWebApp(final WebAppConfig config) {
    final WebAppSettingModel settings = convertConfig2Settings(config);
    settings.setCreatingNew(true);
    final Map<String, String> properties = settings.getTelemetryProperties(null);
    final Operation operation = TelemetryManager.createOperation(WEBAPP, CREATE_WEBAPP);
    try {
        operation.start();
        operation.trackProperties(properties);
        return AzureWebAppMvpModel.getInstance().createWebAppFromSettingModel(settings);
    } catch (final RuntimeException e) {
        EventUtil.logError(operation, ErrorType.userError, e, properties, null);
        throw e;
    } finally {
        operation.complete();
    }
}
Also used : AzureOperation(com.microsoft.azure.toolkit.lib.common.operation.AzureOperation) Operation(com.microsoft.azuretools.telemetrywrapper.Operation) WebAppSettingModel(com.microsoft.azuretools.core.mvp.model.webapp.WebAppSettingModel) AzureOperation(com.microsoft.azure.toolkit.lib.common.operation.AzureOperation)

Example 28 with Operation

use of com.microsoft.azuretools.telemetrywrapper.Operation in project azure-tools-for-java by Microsoft.

the class CreateVMWizard method performFinish.

@Override
public boolean performFinish() {
    Operation operation = TelemetryManager.createOperation(VM, CREATE_VM);
    AzureTaskManager.getInstance().runInBackground("Creating virtual machine " + name + "...", new Runnable() {

        @Override
        public void run() {
            try {
                operation.start();
                byte[] certData = new byte[0];
                if (!certificate.isEmpty()) {
                    File certFile = new File(certificate);
                    if (certFile.exists()) {
                        try (FileInputStream certStream = new FileInputStream(certFile)) {
                            certData = new byte[(int) certFile.length()];
                            if (certStream.read(certData) != certData.length) {
                                throw new Exception("Unable to process certificate: " + "stream longer than informed size.");
                            }
                        } finally {
                        }
                    }
                }
                // create storage account when use choose to create new one
                if (Objects.nonNull(newStorageAccount)) {
                    storageAccount = new CreateStorageAccountTask(newStorageAccount).execute();
                }
                VirtualMachine vm = AzureSDKManager.createVirtualMachine(subscription.getId(), name, resourceGroupName, isNewResourceGroup, size.name(), region.getName(), virtualMachineImage, knownMachineImage, isKnownMachineImage, storageAccount, virtualNetwork, newNetwork, isNewNetwork, subnet, publicIpAddress, withNewPip, availabilitySet, withNewAvailabilitySet, userName, password, certData.length > 0 ? new String(certData) : null);
                // update resource groups cache if new resource group was created when creating storage account
                DefaultLoader.getIdeHelper().invokeLater(new Runnable() {

                    @Override
                    public void run() {
                        try {
                            node.addChildNode(new VMNode(node, subscription.getId(), vm));
                        } catch (AzureCmdException e) {
                            PluginUtil.displayErrorDialogWithAzureMsg(PluginUtil.getParentShell(), Messages.err, "An error occurred while refreshing the list of virtual machines.", e);
                        }
                    }
                });
            } catch (Exception e) {
                EventUtil.logError(operation, ErrorType.userError, e, null, null);
                DefaultLoader.getIdeHelper().invokeLater(new Runnable() {

                    public void run() {
                        PluginUtil.displayErrorDialogWithAzureMsg(PluginUtil.getParentShell(), "Error Creating Virtual Machine", "An error occurred while trying to create the specified virtual machine", e);
                    }
                });
            } finally {
                operation.complete();
            }
        }
    });
    return true;
}
Also used : AzureCmdException(com.microsoft.azuretools.azurecommons.helpers.AzureCmdException) Operation(com.microsoft.azuretools.telemetrywrapper.Operation) VMNode(com.microsoft.tooling.msservices.serviceexplorer.azure.vmarm.VMNode) File(java.io.File) CreateStorageAccountTask(com.microsoft.azuretools.azureexplorer.helpers.CreateStorageAccountTask) FileInputStream(java.io.FileInputStream) AzureCmdException(com.microsoft.azuretools.azurecommons.helpers.AzureCmdException) VirtualMachine(com.microsoft.azure.management.compute.VirtualMachine)

Example 29 with Operation

use of com.microsoft.azuretools.telemetrywrapper.Operation in project azure-tools-for-java by Microsoft.

the class AddDockerSupportAction method onActionPerformed.

@Override
@AzureOperation(name = "docker.add_docker_support.configuration", type = AzureOperation.Type.ACTION)
public boolean onActionPerformed(@NotNull AnActionEvent anActionEvent, @Nullable Operation operation) {
    module = DataKeys.MODULE.getData(anActionEvent.getDataContext());
    if (module == null) {
        notifyError(Constant.ERROR_NO_SELECTED_PROJECT);
        return true;
    }
    pomXmlBasePath = Paths.get(module.getModuleFilePath()).getParent().toString();
    String artifactRelativePath = Constant.DOCKERFILE_ARTIFACT_PLACEHOLDER;
    String dockerFileContent = Constant.DOCKERFILE_CONTENT_TOMCAT;
    List<MavenProject> mavenProjects = MavenProjectsManager.getInstance(module.getProject()).getProjects();
    Optional<MavenProject> res = mavenProjects.stream().filter(mvnprj -> Comparing.equal(Paths.get(mvnprj.getDirectory()).normalize(), Paths.get(pomXmlBasePath).normalize())).findFirst();
    if (res.isPresent()) {
        MavenProject mvnPrj = res.get();
        String artifactName = mvnPrj.getFinalName() + "." + mvnPrj.getPackaging();
        artifactRelativePath = Paths.get(pomXmlBasePath).toUri().relativize(Paths.get(mvnPrj.getBuildDirectory(), artifactName).toUri()).getPath();
        // pre-define dockerfile content according to artifact type
        if (MavenConstants.TYPE_WAR.equals(mvnPrj.getPackaging())) {
            // maven war: tomcat
            dockerFileContent = Constant.DOCKERFILE_CONTENT_TOMCAT;
        } else if (MavenConstants.TYPE_JAR.equals(mvnPrj.getPackaging())) {
            // maven jar: spring boot
            dockerFileContent = Constant.DOCKERFILE_CONTENT_SPRING;
        }
    }
    final Path path = Paths.get(pomXmlBasePath, Constant.DOCKERFILE_FOLDER, Constant.DOCKERFILE_NAME);
    try {
        // create docker file
        DockerUtil.createDockerFile(pomXmlBasePath, Constant.DOCKERFILE_FOLDER, Constant.DOCKERFILE_NAME, String.format(dockerFileContent, artifactRelativePath));
        VirtualFileManager.getInstance().asyncRefresh(() -> {
            VirtualFile virtualDockerFile = LocalFileSystem.getInstance().findFileByPath(path.toString());
            if (virtualDockerFile != null) {
                new OpenFileDescriptor(module.getProject(), virtualDockerFile).navigate(true);
            }
        });
    } catch (IOException e) {
        EventUtil.logError(operation, ErrorType.userError, e, null, null);
        e.printStackTrace();
        notifyError(e.getMessage());
        return true;
    }
    // detect docker daemon
    String defaultDockerHost = null;
    try {
        defaultDockerHost = DefaultDockerClient.fromEnv().uri().toString();
    } catch (DockerCertificateException e) {
        EventUtil.logError(operation, ErrorType.userError, e, null, null);
        e.printStackTrace();
    // leave defaultDockerHost null
    }
    // print instructions
    String notificationContent = "";
    notificationContent += String.format(Constant.MESSAGE_DOCKERFILE_CREATED, path.normalize()) + "\n";
    notificationContent += String.format(Constant.MESSAGE_DOCKER_HOST_INFO, defaultDockerHost) + "\n";
    notificationContent += Constant.MESSAGE_ADD_DOCKER_SUPPORT_OK + "\n";
    notificationContent += Constant.MESSAGE_INSTRUCTION + "\n";
    notifyInfo(notificationContent);
    return true;
}
Also used : AzureOperation(com.microsoft.azure.toolkit.lib.common.operation.AzureOperation) DockerCertificateException(com.spotify.docker.client.exceptions.DockerCertificateException) DataKeys(com.intellij.openapi.actionSystem.DataKeys) VirtualFile(com.intellij.openapi.vfs.VirtualFile) Constant(com.microsoft.azure.toolkit.intellij.webapp.docker.utils.Constant) VirtualFileManager(com.intellij.openapi.vfs.VirtualFileManager) Comparing(com.intellij.openapi.util.Comparing) DefaultDockerClient(com.spotify.docker.client.DefaultDockerClient) AzureAnAction(com.microsoft.intellij.AzureAnAction) MavenConstants(org.jetbrains.idea.maven.model.MavenConstants) Module(com.intellij.openapi.module.Module) Path(java.nio.file.Path) Notifications(com.intellij.notification.Notifications) OpenFileDescriptor(com.intellij.openapi.fileEditor.OpenFileDescriptor) MavenProject(org.jetbrains.idea.maven.project.MavenProject) ErrorType(com.microsoft.azuretools.telemetrywrapper.ErrorType) Operation(com.microsoft.azuretools.telemetrywrapper.Operation) IOException(java.io.IOException) LocalFileSystem(com.intellij.openapi.vfs.LocalFileSystem) NotificationType(com.intellij.notification.NotificationType) MavenProjectsManager(org.jetbrains.idea.maven.project.MavenProjectsManager) Notification(com.intellij.notification.Notification) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) TelemetryConstants(com.microsoft.azuretools.telemetry.TelemetryConstants) Paths(java.nio.file.Paths) AnActionEvent(com.intellij.openapi.actionSystem.AnActionEvent) Optional(java.util.Optional) EventUtil(com.microsoft.azuretools.telemetrywrapper.EventUtil) NotNull(org.jetbrains.annotations.NotNull) DockerUtil(com.microsoft.azure.toolkit.intellij.webapp.docker.utils.DockerUtil) Path(java.nio.file.Path) VirtualFile(com.intellij.openapi.vfs.VirtualFile) MavenProject(org.jetbrains.idea.maven.project.MavenProject) OpenFileDescriptor(com.intellij.openapi.fileEditor.OpenFileDescriptor) IOException(java.io.IOException) DockerCertificateException(com.spotify.docker.client.exceptions.DockerCertificateException) AzureOperation(com.microsoft.azure.toolkit.lib.common.operation.AzureOperation)

Aggregations

Operation (com.microsoft.azuretools.telemetrywrapper.Operation)29 AzureOperation (com.microsoft.azure.toolkit.lib.common.operation.AzureOperation)9 IOException (java.io.IOException)8 File (java.io.File)7 HashMap (java.util.HashMap)7 AzureString (com.microsoft.azure.toolkit.lib.common.bundle.AzureString)6 EventUtil (com.microsoft.azuretools.telemetrywrapper.EventUtil)6 AzureToolkitRuntimeException (com.microsoft.azure.toolkit.lib.common.exception.AzureToolkitRuntimeException)5 TelemetryConstants (com.microsoft.azuretools.telemetry.TelemetryConstants)5 ErrorType (com.microsoft.azuretools.telemetrywrapper.ErrorType)5 Map (java.util.Map)5 Project (com.intellij.openapi.project.Project)4 MsalClientException (com.microsoft.aad.msal4j.MsalClientException)4 TelemetryManager (com.microsoft.azuretools.telemetrywrapper.TelemetryManager)4 DefaultDockerClient (com.spotify.docker.client.DefaultDockerClient)4 Path (java.nio.file.Path)4 CancellationException (java.util.concurrent.CancellationException)4 DefaultExecutionResult (com.intellij.execution.DefaultExecutionResult)3 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)3 AzureTask (com.microsoft.azure.toolkit.lib.common.task.AzureTask)3