44package software.aws.toolkits.jetbrains.services.amazonq.profile
55
66import com.intellij.openapi.Disposable
7+ import com.intellij.openapi.application.ApplicationManager
78import com.intellij.openapi.components.BaseState
89import com.intellij.openapi.components.PersistentStateComponent
910import com.intellij.openapi.components.Service
@@ -26,6 +27,7 @@ import software.aws.toolkits.jetbrains.core.credentials.AwsBearerTokenConnection
2627import software.aws.toolkits.jetbrains.core.credentials.ToolkitConnectionManager
2728import software.aws.toolkits.jetbrains.core.credentials.pinning.QConnection
2829import software.aws.toolkits.jetbrains.core.credentials.sono.isSono
30+ import software.aws.toolkits.jetbrains.core.credentials.sso.bearer.BearerTokenProviderListener
2931import software.aws.toolkits.jetbrains.core.region.AwsRegionProvider
3032import software.aws.toolkits.jetbrains.utils.notifyInfo
3133import software.aws.toolkits.resources.AmazonQBundle.message
@@ -41,7 +43,20 @@ class QRegionProfileManager : PersistentStateComponent<QProfileState>, Disposabl
4143
4244 // Map to store connectionId to its active profile
4345 private val connectionIdToActiveProfile = Collections .synchronizedMap<String , QRegionProfile >(mutableMapOf ())
44- private val connectionIdToProfileList = mutableMapOf<String , Int >()
46+ private val connectionIdToProfileCount = mutableMapOf<String , Int >()
47+
48+ init {
49+ ApplicationManager .getApplication().messageBus.connect(this )
50+ .subscribe(
51+ BearerTokenProviderListener .TOPIC ,
52+ object : BearerTokenProviderListener {
53+ override fun invalidate (providerId : String ) {
54+ connectionIdToActiveProfile.remove(providerId)
55+ connectionIdToProfileCount.remove(providerId)
56+ }
57+ }
58+ )
59+ }
4560
4661 // should be call on project startup to validate if profile is still active
4762 @RequiresBackgroundThread
@@ -80,7 +95,7 @@ class QRegionProfileManager : PersistentStateComponent<QProfileState>, Disposabl
8095 switchProfile(project, mappedProfiles.first(), intent = QProfileSwitchIntent .Update )
8196 }
8297 mappedProfiles.takeIf { it.isNotEmpty() }?.also {
83- connectionIdToProfileList [connection.id] = it.size
98+ connectionIdToProfileCount [connection.id] = it.size
8499 } ? : error(" You don't have access to the resource" )
85100 } catch (e: Exception ) {
86101 LOG .warn(e) { " Failed to list region profiles: ${e.message} " }
@@ -112,7 +127,7 @@ class QRegionProfileManager : PersistentStateComponent<QProfileState>, Disposabl
112127 Telemetry .amazonq.didSelectProfile.use { span ->
113128 span.source(intent.value)
114129 .amazonQProfileRegion(newProfile.region)
115- .profileCount(connectionIdToProfileList [conn.id])
130+ .profileCount(connectionIdToProfileCount [conn.id])
116131 .ssoRegion(conn.region)
117132 .credentialStartUrl(conn.startUrl)
118133 .result(MetricResult .Succeeded )
@@ -141,13 +156,13 @@ class QRegionProfileManager : PersistentStateComponent<QProfileState>, Disposabl
141156
142157 // for each idc connection, user should have a profile, otherwise should show the profile selection error page
143158 fun isPendingProfileSelection (project : Project ): Boolean = getIdcConnectionOrNull(project)?.let { conn ->
144- val profileCounts = connectionIdToProfileList [conn.id] ? : 0
159+ val profileCounts = connectionIdToProfileCount [conn.id] ? : 0
145160 val activeProfile = connectionIdToActiveProfile[conn.id]
146161 profileCounts == 0 || (profileCounts > 1 && activeProfile?.arn.isNullOrEmpty())
147162 } ? : false
148163
149164 fun shouldDisplayProfileInfo (project : Project ): Boolean = getIdcConnectionOrNull(project)?.let { conn ->
150- (connectionIdToProfileList [conn.id] ? : 0 ) > 1
165+ (connectionIdToProfileCount [conn.id] ? : 0 ) > 1
151166 } ? : false
152167
153168 fun getQClientSettings (project : Project ): TokenConnectionSettings {
@@ -193,16 +208,16 @@ class QRegionProfileManager : PersistentStateComponent<QProfileState>, Disposabl
193208 override fun getState (): QProfileState {
194209 val state = QProfileState ()
195210 state.connectionIdToActiveProfile.putAll(this .connectionIdToActiveProfile)
196- state.connectionIdToProfileList.putAll(this .connectionIdToProfileList )
211+ state.connectionIdToProfileList.putAll(this .connectionIdToProfileCount )
197212 return state
198213 }
199214
200215 override fun loadState (state : QProfileState ) {
201216 connectionIdToActiveProfile.clear()
202217 connectionIdToActiveProfile.putAll(state.connectionIdToActiveProfile)
203218
204- connectionIdToProfileList .clear()
205- connectionIdToProfileList .putAll(state.connectionIdToProfileList)
219+ connectionIdToProfileCount .clear()
220+ connectionIdToProfileCount .putAll(state.connectionIdToProfileList)
206221 }
207222}
208223
0 commit comments