|
| 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.database.postgre.connection; |
| 7 | + |
| 8 | +import com.intellij.openapi.application.PreloadingActivity; |
| 9 | +import com.intellij.openapi.progress.ProgressIndicator; |
| 10 | +import com.intellij.openapi.project.Project; |
| 11 | +import com.microsoft.azure.arm.resources.ResourceId; |
| 12 | +import com.microsoft.azure.toolkit.intellij.common.AzureFormJPanel; |
| 13 | +import com.microsoft.azure.toolkit.intellij.connector.*; |
| 14 | +import com.microsoft.azure.toolkit.intellij.connector.spring.SpringSupported; |
| 15 | +import com.microsoft.azure.toolkit.lib.Azure; |
| 16 | +import com.microsoft.azure.toolkit.lib.common.exception.AzureToolkitRuntimeException; |
| 17 | +import com.microsoft.azure.toolkit.lib.database.JdbcUrl; |
| 18 | +import com.microsoft.azure.toolkit.lib.postgre.AzurePostgreSql; |
| 19 | +import com.microsoft.azure.toolkit.lib.postgre.PostgreSqlDatabase; |
| 20 | +import com.microsoft.azure.toolkit.lib.postgre.PostgreSqlServer; |
| 21 | +import org.apache.commons.lang3.ArrayUtils; |
| 22 | +import org.apache.commons.lang3.StringUtils; |
| 23 | +import org.apache.commons.lang3.tuple.Pair; |
| 24 | +import org.jdom.Attribute; |
| 25 | +import org.jdom.Element; |
| 26 | +import org.jetbrains.annotations.NotNull; |
| 27 | + |
| 28 | +import javax.annotation.Nonnull; |
| 29 | +import java.util.*; |
| 30 | + |
| 31 | + |
| 32 | +public class PostgreSqlResourceDefinition extends AzureServiceResource.Definition<PostgreSqlDatabase> implements SpringSupported<PostgreSqlDatabase> { |
| 33 | + public static final PostgreSqlResourceDefinition INSTANCE = new PostgreSqlResourceDefinition(); |
| 34 | + |
| 35 | + |
| 36 | + private PostgreSqlResourceDefinition() { |
| 37 | + super("Azure.PostgreSQL", "Azure Database for PostgreSQL", "/icons/postgre.svg"); |
| 38 | + } |
| 39 | + |
| 40 | + @Override |
| 41 | + public Map<String, String> initEnv(AzureServiceResource<PostgreSqlDatabase> data, Project project) { |
| 42 | + PostgreSqlDatabaseResource resource = (PostgreSqlDatabaseResource) data; |
| 43 | + final HashMap<String, String> env = new HashMap<>(); |
| 44 | + env.put(String.format("%s_URL", Connection.ENV_PREFIX), resource.getJdbcUrl().toString()); |
| 45 | + env.put(String.format("%s_USERNAME", Connection.ENV_PREFIX), resource.getUsername()); |
| 46 | + env.put(String.format("%s_PASSWORD", Connection.ENV_PREFIX), Optional.ofNullable(resource.loadPassword()).or(() -> Optional.ofNullable(resource.inputPassword(project))).orElse("")); |
| 47 | + return env; |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public List<Pair<String, String>> getSpringProperties() { |
| 52 | + final List<Pair<String, String>> properties = new ArrayList<>(); |
| 53 | + properties.add(Pair.of("spring.datasource.url", String.format("${%s_URL}", Connection.ENV_PREFIX))); |
| 54 | + properties.add(Pair.of("spring.datasource.username", String.format("${%s_USERNAME}", Connection.ENV_PREFIX))); |
| 55 | + properties.add(Pair.of("spring.datasource.password", String.format("${%s_PASSWORD}", Connection.ENV_PREFIX))); |
| 56 | + return properties; |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + public boolean write(@Nonnull Element resourceEle, @Nonnull Resource<PostgreSqlDatabase> paramResource) { |
| 61 | + PostgreSqlDatabaseResource resource = (PostgreSqlDatabaseResource) paramResource; |
| 62 | + final String defName = resource.getDefinition().getName(); |
| 63 | + |
| 64 | + final Password.SaveType saveType = resource.getPassword().saveType(); |
| 65 | + resourceEle.addContent(new Element("url").setText(resource.getJdbcUrl().toString())); |
| 66 | + resourceEle.addContent(new Element("username").setText(resource.getUsername())); |
| 67 | + resourceEle.addContent(new Element("passwordSave").setText(saveType.name())); |
| 68 | + final char[] password = resource.getPassword().password(); |
| 69 | + final String storedPassword = PasswordStore.loadPassword(defName, resource.getDataId(), resource.getUsername(), saveType); |
| 70 | + if (ArrayUtils.isNotEmpty(password) && !StringUtils.equals(String.valueOf(password), storedPassword)) { |
| 71 | + PasswordStore.savePassword(defName, resource.getDataId(), resource.getUsername(), resource.getPassword().password(), saveType); |
| 72 | + } |
| 73 | + |
| 74 | + resourceEle.setAttribute(new Attribute("id", resource.getId())); |
| 75 | + resourceEle.addContent(new Element("dataId").addContent(resource.getDataId())); |
| 76 | + |
| 77 | + return true; |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + public Resource<PostgreSqlDatabase> read(@Nonnull Element resourceEle) { |
| 82 | + final String dataId = resourceEle.getChildTextTrim("dataId"); |
| 83 | + |
| 84 | + if (StringUtils.isBlank(dataId)) { |
| 85 | + throw new AzureToolkitRuntimeException("Missing required dataId for postgre SQL database in service link."); |
| 86 | + } |
| 87 | + PostgreSqlDatabaseResource resource = new PostgreSqlDatabaseResource(getResource(dataId), resourceEle.getChildTextTrim("username"), this); |
| 88 | + |
| 89 | + final String defName = this.getName(); |
| 90 | + resource.setJdbcUrl(JdbcUrl.from(resourceEle.getChildTextTrim("url"))); |
| 91 | + |
| 92 | + resource.setPassword(new Password().saveType(Password.SaveType.valueOf(resourceEle.getChildTextTrim("passwordSave")))); |
| 93 | + if (resource.getPassword().saveType() == Password.SaveType.FOREVER) { |
| 94 | + PasswordStore.migratePassword(resource.getId(), resource.getUsername(), defName, resource.getId(), resource.getUsername()); |
| 95 | + } |
| 96 | + final String savedPassword = PasswordStore.loadPassword(defName, resource.getId(), resource.getUsername(), resource.getPassword().saveType()); |
| 97 | + if (StringUtils.isNotBlank(savedPassword)) { |
| 98 | + resource.getPassword().password(savedPassword.toCharArray()); |
| 99 | + } |
| 100 | + return resource; |
| 101 | + } |
| 102 | + |
| 103 | + @Override |
| 104 | + public Resource<PostgreSqlDatabase> define(PostgreSqlDatabase resource) { |
| 105 | + return new PostgreSqlDatabaseResource(resource, null, this); |
| 106 | + } |
| 107 | + |
| 108 | + @Override |
| 109 | + public Resource<PostgreSqlDatabase> define(String dataId) { |
| 110 | + throw new UnsupportedOperationException("xx"); |
| 111 | + } |
| 112 | + |
| 113 | + @Override |
| 114 | + public PostgreSqlDatabase getResource(String dataId) { |
| 115 | + final ResourceId resourceId = ResourceId.fromString(dataId); |
| 116 | + final ResourceId serverId = resourceId.parent(); |
| 117 | + final String databaseName = resourceId.name(); |
| 118 | + final PostgreSqlServer postgreSqlServer = Azure.az(AzurePostgreSql.class).get(serverId.id()); |
| 119 | + return postgreSqlServer.database(databaseName); |
| 120 | + } |
| 121 | + |
| 122 | + @Override |
| 123 | + public AzureFormJPanel<Resource<PostgreSqlDatabase>> getResourcePanel(Project project) { |
| 124 | + return new PostgreSqlResourcePanel(); |
| 125 | + } |
| 126 | + |
| 127 | + public static class RegisterActivity extends PreloadingActivity { |
| 128 | + @Override |
| 129 | + public void preload(@NotNull ProgressIndicator progressIndicator) { |
| 130 | + ResourceManager.registerDefinition(INSTANCE); |
| 131 | + } |
| 132 | + } |
| 133 | +} |
0 commit comments