Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import software.amazon.awssdk.services.codewhispererruntime.CodeWhispererRuntime
import software.amazon.awssdk.services.codewhispererstreaming.CodeWhispererStreamingAsyncClientBuilder
import software.aws.toolkits.core.ToolkitClientCustomizer
import software.aws.toolkits.core.utils.tryOrNull
import software.aws.toolkits.jetbrains.services.amazonq.profile.QDefaultServiceConfig
import software.aws.toolkits.jetbrains.services.amazonq.profile.QEndpoints
import software.aws.toolkits.jetbrains.settings.CodeWhispererSettings
import java.net.Proxy
Expand All @@ -42,7 +43,7 @@ class CodeWhispererEndpointCustomizer : ToolkitClientCustomizer {
if (builder is CodeWhispererRuntimeClientBuilder || builder is CodeWhispererStreamingAsyncClientBuilder) {
val endpoint = tryOrNull { QEndpoints.getQEndpointWithRegion(regionId) }
?.let { URI.create(it) }
?: URI.create(QEndpoints.Q_DEFAULT_SERVICE_CONFIG.ENDPOINT)
?: URI.create(QDefaultServiceConfig.ENDPOINT)
builder
.endpointOverride(endpoint)
.region(Region.of(regionId))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@
package software.aws.toolkits.jetbrains.services.codewhisperer

import com.intellij.testFramework.ApplicationExtension
import com.intellij.testFramework.fixtures.BasePlatformTestCase
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith
import org.junit.jupiter.api.extension.RegisterExtension
import software.aws.toolkits.jetbrains.services.amazonq.profile.QDefaultServiceConfig
import software.aws.toolkits.jetbrains.services.amazonq.profile.QEndpoints
import software.aws.toolkits.jetbrains.services.amazonq.profile.QRegionEndpoint
import software.aws.toolkits.jetbrains.utils.rules.RegistryExtension
import software.aws.toolkits.jetbrains.utils.satisfiesKt

@ExtendWith(ApplicationExtension::class)
class QEndpointsTest : BasePlatformTestCase() {
class QEndpointsTest {

@JvmField
@RegisterExtension
Expand All @@ -30,12 +33,29 @@ class QEndpointsTest : BasePlatformTestCase() {
registryExtension.setValue("amazon.q.endpoints.json", testJson)

val parsed = QEndpoints.listRegionEndpoints()
assertEquals(2, parsed.size)
assertThat(parsed).hasSize(2)
.satisfiesKt { endpoints ->
assertThat(endpoints).satisfiesExactlyInAnyOrder(
{ assertThat(it).isEqualTo(QRegionEndpoint("us-east-1", "https://codewhisperer.us-east-1.amazonaws.com/")) },
{ assertThat(it).isEqualTo(QRegionEndpoint("eu-central-1", "https://rts.prod-eu-central-1.codewhisperer.ai.aws.dev/")) },
)
}
}

@Test
fun `uses default entries if blank`() {
registryExtension.setValue("amazon.q.endpoints.json", "")

assertThat(QEndpoints.listRegionEndpoints()).isEqualTo(QDefaultServiceConfig.ENDPOINT_MAP.toEndpointList())
}


val iad = parsed.first { it.region == "us-east-1" }
assertEquals("https://codewhisperer.us-east-1.amazonaws.com/", iad.endpoint)
@Test
fun `uses default entries if invalid`() {
registryExtension.setValue("amazon.q.endpoints.json", "asdfadfkajdklf32.4;'2l4;234l23.424';1l1!!@#!")

val fra = parsed.first { it.region == "eu-central-1" }
assertEquals("https://rts.prod-eu-central-1.codewhisperer.ai.aws.dev/", fra.endpoint)
assertThat(QEndpoints.listRegionEndpoints()).isEqualTo(QDefaultServiceConfig.ENDPOINT_MAP.toEndpointList())
}

private fun Map<String, String>.toEndpointList() = map { (region, endpoint) -> QRegionEndpoint(region, endpoint) }
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,29 @@ import com.intellij.openapi.util.registry.Registry
import software.aws.toolkits.core.utils.getLogger
import software.aws.toolkits.core.utils.warn

object QEndpoints {
private val LOG = getLogger<QRegionEndpoint>()
data class QRegionEndpoint(val region: String, val endpoint: String)
data class QRegionEndpoint(val region: String, val endpoint: String)

object Q_DEFAULT_SERVICE_CONFIG {
const val REGION = "us-east-1"
const val ENDPOINT = "https://codewhisperer.us-east-1.amazonaws.com/"
}
object QDefaultServiceConfig {
const val REGION = "us-east-1"
const val ENDPOINT = "https://codewhisperer.us-east-1.amazonaws.com/"

val ENDPOINT_MAP = mapOf(
"us-east-1" to "https://q.us-east-1.amazonaws.com/",
"eu-central-1" to "https://q.eu-central-1.amazonaws.com/"
)
}

object QEndpoints {
private val LOG = getLogger<QEndpoints>()

private fun parseEndpoints(): Map<String, String> {
val rawJson = Registry.get("amazon.q.endpoints.json").asString().takeIf { it.isNotBlank() } ?: return emptyMap()
val rawJson = Registry.get("amazon.q.endpoints.json").asString().takeIf { it.isNotBlank() } ?: return QDefaultServiceConfig.ENDPOINT_MAP
return try {
val regionList: List<QRegionEndpoint> = jacksonObjectMapper().readValue(rawJson)
regionList.associate { it.region to it.endpoint }
} catch (e: Exception) {
LOG.warn(e) { "Failed to parse amazon.q.endpoints.json: $rawJson" }
emptyMap()
QDefaultServiceConfig.ENDPOINT_MAP
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ class QRegionProfileManager : PersistentStateComponent<QProfileState>, Disposabl
}

val settings = conn.getConnectionSettings()
val awsRegion = AwsRegionProvider.getInstance()[QEndpoints.Q_DEFAULT_SERVICE_CONFIG.REGION] ?: error("unknown region from Q default service config")
val awsRegion = AwsRegionProvider.getInstance()[QDefaultServiceConfig.REGION] ?: error("unknown region from Q default service config")

// TODO: different window should be able to select different profile
return activeProfile(project)?.let { profile ->
Expand Down
3 changes: 1 addition & 2 deletions plugins/amazonq/src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,7 @@
<registryKey key="amazon.q.endpoint" description="Endpoint to use for Amazon Q"
defaultValue="" restartRequired="true"/>
<registryKey key="amazon.q.endpoints.json" description="List of region-endpoint pairs in JSON array form"
defaultValue="[{&quot;region&quot;:&quot;us-east-1&quot;,&quot;endpoint&quot;:&quot;https://q.us-east-1.amazonaws.com/&quot;},
{&quot;region&quot;:&quot;eu-central-1&quot;,&quot;endpoint&quot;:&quot;https://q.eu-central-1.amazonaws.com/&quot;}]"
defaultValue=""
restartRequired="true"/>
<registryKey key="inline.completion.rem.dev.use.rhizome" description="Defined by IntelliJ. Used for Amazon Q to display suggestions on remote."
defaultValue="false" restartRequired="true"/>
Expand Down
Loading