Skip to content

Commit c7ec48f

Browse files
authored
Merge pull request #5240 from microsoft/qianjin-sqlserver-04-showproperties-4-actions
[SQL Server] (No.9) add sqlserver show-property action codes.
2 parents 9cad4b1 + 884627c commit c7ec48f

File tree

3 files changed

+164
-2
lines changed

3 files changed

+164
-2
lines changed

PluginsAndFeatures/azure-toolkit-for-intellij/src/com/microsoft/azure/toolkit/intellij/sqlserver/CreateSqlServerAction.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
import com.microsoft.azure.toolkit.lib.common.operation.AzureOperation;
1313
import com.microsoft.azure.toolkit.lib.common.task.AzureTask;
1414
import com.microsoft.azure.toolkit.lib.common.task.AzureTaskManager;
15+
import com.microsoft.azure.toolkit.intellij.sqlserver.task.CreateSqlServerTask;
1516
import com.microsoft.azure.toolkit.lib.sqlserver.SqlServerConfig;
16-
import com.microsoft.azure.toolkit.lib.sqlserver.SqlServerService;
1717
import com.microsoft.azure.toolkit.lib.sqlserver.service.ISqlServer;
1818
import com.microsoft.azuretools.authmanage.AuthMethodManager;
1919
import com.microsoft.azuretools.utils.AzureUIRefreshCore;
@@ -74,7 +74,7 @@ private void createSqlServer(final SqlServerConfig config, SqlServerCreationDial
7474
final ProgressIndicator indicator = ProgressManager.getInstance().getProgressIndicator();
7575
indicator.setIndeterminate(true);
7676
DefaultLoader.getIdeHelper().invokeLater(dialog::close);
77-
ISqlServer server = SqlServerService.getInstance().create(config);
77+
ISqlServer server = new CreateSqlServerTask(config).execute();
7878
refreshAzureExplorer(server);
7979
};
8080
String progressMessage = Node.getProgressMessage(AzureActionEnum.CREATE.getDoingName(), SqlServerModule.MODULE_NAME, config.getServerName());
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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.sqlserver;
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.azure.toolkit.lib.sqlserver.model.SqlServerEntity;
13+
import com.microsoft.azuretools.ActionConstants;
14+
import com.microsoft.azuretools.authmanage.AuthMethodManager;
15+
import com.microsoft.intellij.AzurePlugin;
16+
import com.microsoft.intellij.actions.AzureSignInAction;
17+
import com.microsoft.intellij.util.AzureLoginHelper;
18+
import com.microsoft.tooling.msservices.components.DefaultLoader;
19+
import com.microsoft.tooling.msservices.helpers.Name;
20+
import com.microsoft.tooling.msservices.serviceexplorer.AzureIconSymbol;
21+
import com.microsoft.tooling.msservices.serviceexplorer.NodeActionEvent;
22+
import com.microsoft.tooling.msservices.serviceexplorer.NodeActionListener;
23+
import com.microsoft.tooling.msservices.serviceexplorer.azure.sqlserver.SqlServerNode;
24+
25+
import static com.microsoft.intellij.ui.messages.AzureBundle.message;
26+
27+
@Name(OpenSqlServerByToolsAction.ACTION_NAME)
28+
public class OpenSqlServerByToolsAction extends NodeActionListener {
29+
30+
public static final String ACTION_NAME = "Open by Database Tools";
31+
private static final String NAME_PREFIX = "SQL Server - %s";
32+
private static final String DEFAULT_DRIVER_CLASS_NAME = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
33+
34+
private final SqlServerNode node;
35+
private final Project project;
36+
37+
public OpenSqlServerByToolsAction(SqlServerNode node) {
38+
super();
39+
this.node = node;
40+
this.project = (Project) node.getProject();
41+
}
42+
43+
@Override
44+
public AzureIconSymbol getIconSymbol() {
45+
return AzureIconSymbol.SqlServer.CONNECT_TO_SERVER;
46+
}
47+
48+
@Override
49+
public void actionPerformed(NodeActionEvent e) {
50+
AzureSignInAction.doSignIn(AuthMethodManager.getInstance(), project).subscribe((isSuccess) -> {
51+
this.doActionPerformed(e, isSuccess, project);
52+
});
53+
}
54+
55+
@Override
56+
protected String getServiceName(NodeActionEvent event) {
57+
return ActionConstants.parse(ActionConstants.SqlServer.CONNECT_TO_SERVER).getServiceName();
58+
}
59+
60+
@Override
61+
protected String getOperationName(NodeActionEvent event) {
62+
return ActionConstants.parse(ActionConstants.SqlServer.CONNECT_TO_SERVER).getOperationName();
63+
}
64+
65+
@AzureOperation(name = ActionConstants.SqlServer.CONNECT_TO_SERVER, type = AzureOperation.Type.ACTION)
66+
private void doActionPerformed(NodeActionEvent e, boolean isLoggedIn, Project project) {
67+
try {
68+
if (!isLoggedIn ||
69+
!AzureLoginHelper.isAzureSubsAvailableOrReportError(message("common.error.signIn"))) {
70+
return;
71+
}
72+
} catch (final Exception ex) {
73+
AzurePlugin.log(message("common.error.signIn"), ex);
74+
DefaultLoader.getUIHelper().showException(message("common.error.signIn"), ex, message("common.error.signIn"), false, true);
75+
}
76+
SqlServerEntity entity = node.getServer().entity();
77+
IntellijDatasourceService.DatasourceProperties properties = IntellijDatasourceService.DatasourceProperties.builder()
78+
.name(String.format(NAME_PREFIX, entity.getName()))
79+
.driverClassName(DEFAULT_DRIVER_CLASS_NAME)
80+
.url(JdbcUrl.sqlserver(entity.getFullyQualifiedDomainName()).toString())
81+
.username(entity.getAdministratorLoginName() + "@" + entity.getName())
82+
.build();
83+
IntellijDatasourceService.getInstance().openDataSourceManagerDialog(project, properties);
84+
}
85+
86+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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.sqlserver.task;
7+
8+
import com.microsoft.azure.toolkit.intellij.common.Draft;
9+
import com.microsoft.azure.toolkit.lib.Azure;
10+
import com.microsoft.azure.toolkit.lib.common.model.Region;
11+
import com.microsoft.azure.toolkit.lib.common.operation.AzureOperation;
12+
import com.microsoft.azure.toolkit.lib.resource.AzureGroup;
13+
import com.microsoft.azure.toolkit.lib.resource.ResourceGroupEntity;
14+
import com.microsoft.azure.toolkit.lib.sqlserver.SqlServerConfig;
15+
import com.microsoft.azure.toolkit.lib.sqlserver.model.SqlServerEntity;
16+
import com.microsoft.azure.toolkit.lib.sqlserver.service.AzureSqlServer;
17+
import com.microsoft.azure.toolkit.lib.sqlserver.service.ISqlServer;
18+
import com.microsoft.azuretools.ActionConstants;
19+
import com.microsoft.azuretools.telemetry.TelemetryConstants;
20+
import com.microsoft.azuretools.telemetrywrapper.ErrorType;
21+
import com.microsoft.azuretools.telemetrywrapper.EventType;
22+
import com.microsoft.azuretools.telemetrywrapper.EventUtil;
23+
import com.microsoft.azuretools.telemetrywrapper.Operation;
24+
import com.microsoft.azuretools.telemetrywrapper.TelemetryManager;
25+
26+
import java.util.Collections;
27+
28+
public class CreateSqlServerTask {
29+
30+
private final SqlServerConfig config;
31+
32+
public CreateSqlServerTask(SqlServerConfig config) {
33+
this.config = config;
34+
}
35+
36+
@AzureOperation(
37+
name = "sqlserver.create",
38+
params = {
39+
"config.getServerName()",
40+
"config.getSubscription().displayName()"
41+
},
42+
type = AzureOperation.Type.SERVICE
43+
)
44+
public ISqlServer execute() {
45+
final Operation operation = TelemetryManager.createOperation(ActionConstants.MySQL.CREATE);
46+
try {
47+
operation.start();
48+
final String subscriptionId = config.getSubscription().subscriptionId();
49+
EventUtil.logEvent(EventType.info, operation, Collections.singletonMap(TelemetryConstants.SUBSCRIPTIONID, subscriptionId));
50+
// create resource group if necessary.
51+
if (config.getResourceGroup() instanceof Draft) {
52+
ResourceGroupEntity newResourceGroup = Azure.az(AzureGroup.class)
53+
.subscription(subscriptionId).create(config.getResourceGroup().name(), config.getRegion().getName());
54+
config.setResourceGroup(newResourceGroup);
55+
}
56+
// create sql server
57+
SqlServerEntity entity = this.fromConfig(this.config);
58+
return Azure.az(AzureSqlServer.class).sqlServer(entity).create()
59+
.withAdministratorLoginPassword(String.valueOf(config.getPassword()))
60+
.commit();
61+
} catch (final RuntimeException e) {
62+
EventUtil.logError(operation, ErrorType.systemError, e, null, null);
63+
throw e;
64+
} finally {
65+
operation.complete();
66+
}
67+
}
68+
69+
private SqlServerEntity fromConfig(SqlServerConfig config) {
70+
return SqlServerEntity.builder().name(config.getServerName()).subscriptionId(config.getSubscription().subscriptionId())
71+
.resourceGroup(config.getResourceGroup().name()).region(Region.fromName(config.getRegion().getName())).administratorLoginName(config.getAdminUsername())
72+
.enableAccessFromAzureServices(config.isAllowAccessFromAzureServices()).enableAccessFromLocalMachine(config.isAllowAccessFromLocalMachine())
73+
.build();
74+
}
75+
76+
}

0 commit comments

Comments
 (0)