|
| 1 | +/* |
| 2 | + * Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | + * Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 | + */ |
| 5 | + |
| 6 | +package com.microsoft.azure.toolkit.intellij.mysql.action; |
| 7 | + |
| 8 | +import com.intellij.openapi.project.Project; |
| 9 | +import com.microsoft.azure.toolkit.intellij.common.IntellijDatasourceService; |
| 10 | +import com.microsoft.azure.toolkit.lib.common.operation.AzureOperation; |
| 11 | +import com.microsoft.azure.toolkit.lib.common.utils.JdbcUrl; |
| 12 | +import com.microsoft.azuretools.ActionConstants; |
| 13 | +import com.microsoft.azuretools.authmanage.AuthMethodManager; |
| 14 | +import com.microsoft.intellij.AzurePlugin; |
| 15 | +import com.microsoft.intellij.actions.AzureSignInAction; |
| 16 | +import com.microsoft.intellij.util.AzureLoginHelper; |
| 17 | +import com.microsoft.tooling.msservices.components.DefaultLoader; |
| 18 | +import com.microsoft.tooling.msservices.helpers.Name; |
| 19 | +import com.microsoft.tooling.msservices.serviceexplorer.AzureIconSymbol; |
| 20 | +import com.microsoft.tooling.msservices.serviceexplorer.NodeActionEvent; |
| 21 | +import com.microsoft.tooling.msservices.serviceexplorer.NodeActionListener; |
| 22 | +import com.microsoft.tooling.msservices.serviceexplorer.azure.mysql.MySQLNode; |
| 23 | + |
| 24 | +import static com.microsoft.intellij.ui.messages.AzureBundle.message; |
| 25 | + |
| 26 | +@Name(MySQLConnectToServerAction.ACTION_NAME) |
| 27 | +public class MySQLConnectToServerAction extends NodeActionListener { |
| 28 | + |
| 29 | + public static final String ACTION_NAME = "Open by Database Tools"; |
| 30 | + private static final String MYSQL_PATTERN_NAME = "Azure Database for MySQL - %s"; |
| 31 | + private static final String MYSQL_DEFAULT_DRIVER = "com.mysql.cj.jdbc.Driver"; |
| 32 | + |
| 33 | + private final MySQLNode node; |
| 34 | + private final Project project; |
| 35 | + |
| 36 | + public MySQLConnectToServerAction(MySQLNode node) { |
| 37 | + super(); |
| 38 | + this.node = node; |
| 39 | + this.project = (Project) node.getProject(); |
| 40 | + } |
| 41 | + |
| 42 | + @Override |
| 43 | + public AzureIconSymbol getIconSymbol() { |
| 44 | + return AzureIconSymbol.MySQL.CONNECT_TO_SERVER; |
| 45 | + } |
| 46 | + |
| 47 | + @Override |
| 48 | + public void actionPerformed(NodeActionEvent e) { |
| 49 | + AzureSignInAction.doSignIn(AuthMethodManager.getInstance(), project).subscribe((isSuccess) -> { |
| 50 | + this.doActionPerformed(e, isSuccess, project); |
| 51 | + }); |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + protected String getServiceName(NodeActionEvent event) { |
| 56 | + return ActionConstants.parse(ActionConstants.MySQL.CONNECT_TO_SERVER).getServiceName(); |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + protected String getOperationName(NodeActionEvent event) { |
| 61 | + return ActionConstants.parse(ActionConstants.MySQL.CONNECT_TO_SERVER).getOperationName(); |
| 62 | + } |
| 63 | + |
| 64 | + @AzureOperation(name = ActionConstants.MySQL.CONNECT_TO_SERVER, type = AzureOperation.Type.ACTION) |
| 65 | + private void doActionPerformed(NodeActionEvent e, boolean isLoggedIn, Project project) { |
| 66 | + try { |
| 67 | + if (!isLoggedIn || |
| 68 | + !AzureLoginHelper.isAzureSubsAvailableOrReportError(message("common.error.signIn"))) { |
| 69 | + return; |
| 70 | + } |
| 71 | + } catch (final Exception ex) { |
| 72 | + AzurePlugin.log(message("common.error.signIn"), ex); |
| 73 | + DefaultLoader.getUIHelper().showException(message("common.error.signIn"), ex, message("common.error.signIn"), false, true); |
| 74 | + } |
| 75 | + IntellijDatasourceService.DatasourceProperties properties = IntellijDatasourceService.DatasourceProperties.builder() |
| 76 | + .name(String.format(MYSQL_PATTERN_NAME, node.getServer().name())) |
| 77 | + .driverClassName(MYSQL_DEFAULT_DRIVER) |
| 78 | + .url(JdbcUrl.sqlserver(node.getServer().fullyQualifiedDomainName()).toString()) |
| 79 | + .username(node.getServer().administratorLogin() + "@" + node.getServer().name()) |
| 80 | + .build(); |
| 81 | + IntellijDatasourceService.getInstance().openDataSourceManagerDialog(project, properties); |
| 82 | + } |
| 83 | + |
| 84 | +} |
0 commit comments