3
3
4
4
package software.aws.toolkits.jetbrains.core.credentials
5
5
6
+ import com.intellij.icons.AllIcons
6
7
import com.intellij.openapi.actionSystem.ActionManager
7
8
import com.intellij.openapi.actionSystem.AnAction
8
9
import com.intellij.openapi.actionSystem.AnActionEvent
9
10
import com.intellij.openapi.actionSystem.DefaultActionGroup
10
11
import com.intellij.openapi.actionSystem.Separator
11
12
import com.intellij.openapi.actionSystem.ToggleAction
12
13
import com.intellij.openapi.project.DumbAware
14
+ import com.intellij.openapi.project.DumbAwareAction
13
15
import com.intellij.openapi.project.Project
14
16
import software.aws.toolkits.core.credentials.CredentialIdentifier
15
17
import software.aws.toolkits.core.region.AwsRegion
@@ -19,32 +21,99 @@ import software.aws.toolkits.resources.message
19
21
20
22
class ConnectionSettingsMenuBuilder private constructor() {
21
23
private data class RegionSelectionSettings (val currentSelection : AwsRegion ? , val onChange : (AwsRegion ) -> Unit )
22
- private data class CredentialsSelectionSettings (val currentSelection : CredentialIdentifier ? , val onChange : (CredentialIdentifier ) -> Unit )
24
+ private data class ProfileSelectionSettings (val currentSelection : CredentialIdentifier ? , val onChange : (CredentialIdentifier ) -> Unit )
25
+
26
+ private sealed interface IdentitySelectionSettings
27
+ private data class SelectableIdentitySelectionSettings (
28
+ val currentSelection : AwsBearerTokenConnection ? ,
29
+ val onChange : (AwsBearerTokenConnection ) -> Unit
30
+ ) : IdentitySelectionSettings
31
+ private data class ActionsIdentitySelectionSettings (val project : Project ? ) : IdentitySelectionSettings
23
32
24
33
private var regionSelectionSettings: RegionSelectionSettings ? = null
25
- private var credentialsSelectionSettings: CredentialsSelectionSettings ? = null
34
+ private var profileSelectionSettings: ProfileSelectionSettings ? = null
35
+ private var identitySelectionSettings: IdentitySelectionSettings ? = null
26
36
private var accountSettingsManager: AwsConnectionManager ? = null
27
37
28
38
fun withRegions (currentSelection : AwsRegion ? , onChange : (AwsRegion ) -> Unit ): ConnectionSettingsMenuBuilder = apply {
29
39
regionSelectionSettings = RegionSelectionSettings (currentSelection, onChange)
30
40
}
31
41
32
42
fun withCredentials (currentSelection : CredentialIdentifier ? , onChange : (CredentialIdentifier ) -> Unit ): ConnectionSettingsMenuBuilder = apply {
33
- credentialsSelectionSettings = CredentialsSelectionSettings (currentSelection, onChange)
43
+ profileSelectionSettings = ProfileSelectionSettings (currentSelection, onChange)
34
44
}
35
45
36
46
fun withRecentChoices (project : Project ): ConnectionSettingsMenuBuilder = apply {
37
47
accountSettingsManager = AwsConnectionManager .getInstance(project)
38
48
}
39
49
50
+ fun withIndividualIdentitySettings (project : Project ) {
51
+ identitySelectionSettings = SelectableIdentitySelectionSettings (
52
+ currentSelection = ToolkitConnectionManager .getInstance(project).activeConnection() as ? AwsBearerTokenConnection ,
53
+ onChange = ToolkitConnectionManager .getInstance(project)::switchConnection
54
+ )
55
+ }
56
+
57
+ fun withIndividualIdentityActions (project : Project ? ) {
58
+ identitySelectionSettings = ActionsIdentitySelectionSettings (project)
59
+ }
60
+
40
61
fun build (): DefaultActionGroup {
41
62
val topLevelGroup = DefaultActionGroup ()
42
63
64
+ identitySelectionSettings?.let { settings ->
65
+ val connections = ToolkitAuthManager .getInstance().listConnections().filterIsInstance<AwsBearerTokenConnection >()
66
+ if (connections.isEmpty()) {
67
+ return @let
68
+ }
69
+
70
+ topLevelGroup.add(Separator .create(message(" settings.credentials.individual_identity_sub_menu" )))
71
+ val actions = when (settings) {
72
+ is SelectableIdentitySelectionSettings -> {
73
+ connections.map {
74
+ object : DumbAwareToggleAction <AwsBearerTokenConnection >(
75
+ title = it.label,
76
+ value = it,
77
+ selected = it == settings.currentSelection,
78
+ onSelect = settings.onChange
79
+ ) {
80
+ override fun update (e : AnActionEvent ) {
81
+ super .update(e)
82
+ if (value.lazyIsUnauthedBearerConnection()) {
83
+ e.presentation.icon = AllIcons .General .Warning
84
+ }
85
+ }
86
+ }
87
+ }
88
+ }
89
+
90
+ is ActionsIdentitySelectionSettings -> {
91
+ connections.map {
92
+ IndividualIdentityActionGroup (it)
93
+ }
94
+ }
95
+ }
96
+
97
+ topLevelGroup.addAll(actions)
98
+
99
+ topLevelGroup.add(Separator .create())
100
+ }
101
+
102
+ val profileActions = createProfileActions()
43
103
val regionActions = createRegionActions()
104
+
105
+ // no header if only regions
106
+ if (profileActions.isNotEmpty() && regionActions.isNotEmpty()) {
107
+ // both profiles & regions
108
+ topLevelGroup.add(Separator .create(message(" settings.credentials.iam_and_regions" )))
109
+ } else if (profileActions.isNotEmpty() && regionActions.isEmpty()) {
110
+ // only profiles
111
+ topLevelGroup.add(Separator .create(message(" settings.credentials.iam" )))
112
+ }
113
+
44
114
val regionSettings = regionSelectionSettings
45
115
val recentRegions = accountSettingsManager?.recentlyUsedRegions()
46
116
if (recentRegions?.isNotEmpty() == true && regionSettings != null ) {
47
- topLevelGroup.add(Separator .create(message(" settings.regions.recent" )))
48
117
recentRegions.forEach {
49
118
topLevelGroup.add(SwitchRegionAction (it, it == regionSettings.currentSelection, regionSettings.onChange))
50
119
}
@@ -56,11 +125,11 @@ class ConnectionSettingsMenuBuilder private constructor() {
56
125
topLevelGroup.addAll(regionActions)
57
126
}
58
127
59
- val profileActions = createProfileActions()
60
- val credentialsSettings = credentialsSelectionSettings
128
+ topLevelGroup.add(Separator .create())
129
+
130
+ val credentialsSettings = profileSelectionSettings
61
131
val recentCredentials = accountSettingsManager?.recentlyUsedCredentials()
62
132
if (recentCredentials?.isNotEmpty() == true && credentialsSettings != null ) {
63
- topLevelGroup.add(Separator .create(message(" settings.credentials.recent" )))
64
133
recentCredentials.forEach {
65
134
topLevelGroup.add(SwitchCredentialsAction (it, it == credentialsSettings.currentSelection, credentialsSettings.onChange))
66
135
}
@@ -111,7 +180,7 @@ class ConnectionSettingsMenuBuilder private constructor() {
111
180
}
112
181
113
182
private fun createProfileActions (): List <AnAction > = buildList {
114
- val (currentSelection, onChange) = credentialsSelectionSettings ? : return @buildList
183
+ val (currentSelection, onChange) = profileSelectionSettings ? : return @buildList
115
184
116
185
add(Separator .create(message(" settings.credentials" )))
117
186
@@ -153,6 +222,39 @@ class ConnectionSettingsMenuBuilder private constructor() {
153
222
onSelect : (CredentialIdentifier ) -> Unit
154
223
) : DumbAwareToggleAction<CredentialIdentifier>(value.displayName, value, selected, onSelect)
155
224
225
+ inner class IndividualIdentityActionGroup (private val value : AwsBearerTokenConnection ) :
226
+ DefaultActionGroup (
227
+ {
228
+ val suffix = if (value.lazyIsUnauthedBearerConnection()) {
229
+ message(" credentials.individual_identity.expired" )
230
+ } else {
231
+ message(" credentials.individual_identity.connected" )
232
+ }
233
+
234
+ " ${value.label} $suffix "
235
+ },
236
+ true
237
+ ) {
238
+ init {
239
+ templatePresentation.icon = if (value.lazyIsUnauthedBearerConnection()) AllIcons .General .Warning else null
240
+
241
+ addAll(
242
+ object : DumbAwareAction (message(" credentials.individual_identity.reconnect" )) {
243
+ override fun actionPerformed (e : AnActionEvent ) {
244
+ reauthProviderIfNeeded(value)
245
+ }
246
+ },
247
+
248
+ object : DumbAwareAction (message(" credentials.individual_identity.signout" )) {
249
+ override fun actionPerformed (e : AnActionEvent ) {
250
+ val settings = identitySelectionSettings as ? ActionsIdentitySelectionSettings
251
+ logoutFromSsoConnection(settings?.project, value)
252
+ }
253
+ }
254
+ )
255
+ }
256
+ }
257
+
156
258
companion object {
157
259
fun connectionSettingsMenuBuilder (): ConnectionSettingsMenuBuilder = ConnectionSettingsMenuBuilder ()
158
260
}
0 commit comments