|
1 |
| -// Copyright 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 1 | +// Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
2 | 2 | // SPDX-License-Identifier: Apache-2.0
|
3 | 3 |
|
4 | 4 | package software.aws.toolkits.jetbrains.ui
|
5 | 5 |
|
6 |
| -abstract class AwsAuthWidget(userFieldEnabled: Boolean = true) : @Suppress("DEPRECATION") AwsAuthWidgetBase(userFieldEnabled) |
| 6 | +import com.intellij.database.dataSource.DataSourceUiUtil |
| 7 | +import com.intellij.database.dataSource.DatabaseConnectionConfig |
| 8 | +import com.intellij.database.dataSource.DatabaseConnectionPoint |
| 9 | +import com.intellij.database.dataSource.DatabaseCredentialsAuthProviderUi |
| 10 | +import com.intellij.database.dataSource.url.template.ParametersHolder |
| 11 | +import com.intellij.database.dataSource.url.template.UrlEditorModel |
| 12 | +import com.intellij.ui.components.JBLabel |
| 13 | +import com.intellij.uiDesigner.core.GridLayoutManager |
| 14 | +import com.intellij.util.text.nullize |
| 15 | +import org.jetbrains.annotations.TestOnly |
| 16 | +import software.aws.toolkits.jetbrains.UiConstraints |
| 17 | +import software.aws.toolkits.jetbrains.core.credentials.CredentialManager |
| 18 | +import software.aws.toolkits.jetbrains.core.region.AwsRegionProvider |
| 19 | +import software.aws.toolkits.jetbrains.datagrip.CREDENTIAL_ID_PROPERTY |
| 20 | +import software.aws.toolkits.jetbrains.datagrip.REGION_ID_PROPERTY |
| 21 | +import software.aws.toolkits.jetbrains.utils.ui.selected |
| 22 | +import software.aws.toolkits.resources.message |
| 23 | +import javax.swing.JPanel |
| 24 | + |
| 25 | +abstract class AwsAuthWidget(private val userFieldEnabled: Boolean = true) : DatabaseCredentialsAuthProviderUi.UserWidget() { |
| 26 | + private val credentialSelector = CredentialProviderSelector() |
| 27 | + private val regionSelector = RegionSelector() |
| 28 | + |
| 29 | + // These are defined in DataGrip and used in the url holder which is called in `updateFromUrl` |
| 30 | + protected val hostParameter = "host" |
| 31 | + protected val portParameter = "port" |
| 32 | + |
| 33 | + abstract fun getRegionFromUrl(url: String?): String? |
| 34 | + abstract val serviceId: String |
| 35 | + |
| 36 | + open val rowCount: Int = 3 |
| 37 | + open val columnCount: Int = 6 |
| 38 | + |
| 39 | + override fun createPanel(): JPanel { |
| 40 | + val panel = JPanel(GridLayoutManager(rowCount, columnCount)) |
| 41 | + addUserField(panel, 0) |
| 42 | + |
| 43 | + // Disable the user field if we treat it as immutable |
| 44 | + myUserField.isEnabled = userFieldEnabled |
| 45 | + |
| 46 | + val credsLabel = JBLabel(message("aws_connection.credentials.label")) |
| 47 | + val regionLabel = JBLabel(message("aws_connection.region.label")) |
| 48 | + panel.add(credsLabel, UiConstraints.createLabelConstraints(1, 0, credsLabel.preferredSize.getWidth())) |
| 49 | + panel.add(credentialSelector, UiConstraints.createSimpleConstraints(1, 1, 3)) |
| 50 | + panel.add(regionLabel, UiConstraints.createLabelConstraints(2, 0, regionLabel.preferredSize.getWidth())) |
| 51 | + panel.add(regionSelector, UiConstraints.createSimpleConstraints(2, 1, 3)) |
| 52 | + |
| 53 | + return panel |
| 54 | + } |
| 55 | + |
| 56 | + override fun save(config: DatabaseConnectionConfig, copyCredentials: Boolean) { |
| 57 | + super.save(config, copyCredentials) |
| 58 | + |
| 59 | + DataSourceUiUtil.putOrRemove( |
| 60 | + config.additionalProperties, |
| 61 | + CREDENTIAL_ID_PROPERTY, |
| 62 | + credentialSelector.getSelectedCredentialsProvider() |
| 63 | + ) |
| 64 | + DataSourceUiUtil.putOrRemove( |
| 65 | + config.additionalProperties, |
| 66 | + REGION_ID_PROPERTY, |
| 67 | + regionSelector.selectedRegion?.id |
| 68 | + ) |
| 69 | + } |
| 70 | + |
| 71 | + override fun reset(config: DatabaseConnectionPoint, resetCredentials: Boolean) { |
| 72 | + super.reset(config, resetCredentials) |
| 73 | + |
| 74 | + val regionProvider = AwsRegionProvider.getInstance() |
| 75 | + val allRegions = regionProvider.allRegionsForService(serviceId) |
| 76 | + regionSelector.setRegions(allRegions.values.toMutableList()) |
| 77 | + val regionId = config.additionalProperties[REGION_ID_PROPERTY]?.nullize() |
| 78 | + regionId?.let { |
| 79 | + allRegions[regionId]?.let { region -> |
| 80 | + regionSelector.selectedRegion = region |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + val credentialManager = CredentialManager.getInstance() |
| 85 | + credentialSelector.setCredentialsProviders(credentialManager.getCredentialIdentifiers()) |
| 86 | + val credentialId = config.additionalProperties[CREDENTIAL_ID_PROPERTY]?.nullize() |
| 87 | + if (credentialId != null) { |
| 88 | + val credentialIdentifierById = credentialManager.getCredentialIdentifierById(credentialId) |
| 89 | + if (credentialIdentifierById != null) { |
| 90 | + credentialSelector.setSelectedCredentialsProvider(credentialIdentifierById) |
| 91 | + } else { |
| 92 | + credentialSelector.setSelectedInvalidCredentialsProvider(credentialId) |
| 93 | + } |
| 94 | + } else { |
| 95 | + credentialSelector.model.selectedItem = null |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + override fun isPasswordChanged(): Boolean = false |
| 100 | + |
| 101 | + override fun onChanged(r: Runnable) { |
| 102 | + // Tries to set username so if we don't have one, don't set |
| 103 | + if (userFieldEnabled) { |
| 104 | + super.onChanged(r) |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + override fun updateFromUrl(holder: ParametersHolder) { |
| 109 | + // Try to get region from url and set the region box on a best effort basis |
| 110 | + val url = (holder as? UrlEditorModel)?.url |
| 111 | + val regionId = getRegionFromUrl(url) |
| 112 | + val region = AwsRegionProvider.getInstance().allRegions()[regionId] |
| 113 | + region?.let { |
| 114 | + regionSelector.selectedRegion = it |
| 115 | + } |
| 116 | + super.updateFromUrl(holder) |
| 117 | + } |
| 118 | + |
| 119 | + @TestOnly |
| 120 | + internal fun getSelectedCredential() = credentialSelector.getSelectedCredentialsProvider() |
| 121 | + |
| 122 | + @TestOnly |
| 123 | + internal fun getSelectedRegion() = regionSelector.selected() |
| 124 | +} |
0 commit comments