Skip to content

Commit 5ea9709

Browse files
committed
fix: get agents and knowledgeBases correctly
1 parent 5a4db41 commit 5ea9709

File tree

5 files changed

+31
-22
lines changed

5 files changed

+31
-22
lines changed

src/ai/k8s.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ const debug = Debug('otomi:ai:k8s')
1414
let appsApiClient: AppsV1Api | undefined
1515
let customObjectsApiClient: CustomObjectsApi | undefined
1616

17+
export interface KubernetesObjectWithStatus extends KubernetesObject {
18+
status: {
19+
[key: string]: any
20+
}
21+
}
22+
1723
// Export function to reset api clients for testing
1824
export function resetApiClients(): void {
1925
appsApiClient = undefined
@@ -141,7 +147,7 @@ export async function getAkamaiKnowledgeBaseCR(namespace: string, name: string):
141147
}
142148
}
143149

144-
export async function listAkamaiAgentCRs(namespace: string): Promise<KubernetesObject[]> {
150+
export async function listAkamaiAgentCRs(namespace: string): Promise<KubernetesObjectWithStatus[]> {
145151
const customObjectsApi = getCustomObjectsApiClient()
146152

147153
try {
@@ -150,7 +156,7 @@ export async function listAkamaiAgentCRs(namespace: string): Promise<KubernetesO
150156
version: 'v1alpha1',
151157
namespace,
152158
plural: 'akamaiagents',
153-
})) as KubernetesListObject<KubernetesObject>
159+
})) as KubernetesListObject<KubernetesObjectWithStatus>
154160

155161
debug(`Found ${result.items.length} AkamaiAgent CRs in namespace ${namespace}`)
156162
return result.items
@@ -160,7 +166,7 @@ export async function listAkamaiAgentCRs(namespace: string): Promise<KubernetesO
160166
}
161167
}
162168

163-
export async function listAkamaiKnowledgeBaseCRs(namespace: string): Promise<KubernetesObject[]> {
169+
export async function listAkamaiKnowledgeBaseCRs(namespace: string): Promise<KubernetesObjectWithStatus[]> {
164170
const customObjectsApi = getCustomObjectsApiClient()
165171

166172
try {
@@ -169,7 +175,7 @@ export async function listAkamaiKnowledgeBaseCRs(namespace: string): Promise<Kub
169175
version: 'v1alpha1',
170176
namespace,
171177
plural: 'akamaiknowledgebases',
172-
})) as KubernetesListObject<KubernetesObject>
178+
})) as KubernetesListObject<KubernetesObjectWithStatus>
173179

174180
debug(`Found ${result.items.length} AkamaiKnowledgeBase CRs in namespace ${namespace}`)
175181
return result.items

src/api.authz.test.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -856,7 +856,7 @@ describe('API authz tests', () => {
856856
})
857857

858858
test('platform admin can get knowledge bases', async () => {
859-
jest.spyOn(otomiStack, 'getAplKnowledgeBases').mockReturnValue([])
859+
jest.spyOn(otomiStack, 'getAplKnowledgeBases').mockResolvedValue([])
860860
await agent
861861
.get('/alpha/teams/team1/kb')
862862
.set('Authorization', `Bearer ${platformAdminToken}`)
@@ -865,7 +865,7 @@ describe('API authz tests', () => {
865865
})
866866

867867
test('team admin can get knowledge bases', async () => {
868-
jest.spyOn(otomiStack, 'getAplKnowledgeBases').mockReturnValue([])
868+
jest.spyOn(otomiStack, 'getAplKnowledgeBases').mockResolvedValue([])
869869
await agent
870870
.get('/alpha/teams/team1/kb')
871871
.set('Authorization', `Bearer ${teamAdminToken}`)
@@ -874,7 +874,7 @@ describe('API authz tests', () => {
874874
})
875875

876876
test('team member can get knowledge bases', async () => {
877-
jest.spyOn(otomiStack, 'getAplKnowledgeBases').mockReturnValue([])
877+
jest.spyOn(otomiStack, 'getAplKnowledgeBases').mockResolvedValue([])
878878
await agent
879879
.get('/alpha/teams/team1/kb')
880880
.set('Authorization', `Bearer ${teamMemberToken}`)
@@ -1017,7 +1017,7 @@ describe('API authz tests', () => {
10171017
})
10181018

10191019
test('platform admin can get agents', async () => {
1020-
jest.spyOn(otomiStack, 'getAplAgents').mockReturnValue([])
1020+
jest.spyOn(otomiStack, 'getAplAgents').mockResolvedValue([])
10211021
await agent
10221022
.get('/alpha/teams/team1/agents')
10231023
.set('Authorization', `Bearer ${platformAdminToken}`)
@@ -1026,7 +1026,7 @@ describe('API authz tests', () => {
10261026
})
10271027

10281028
test('team admin can get agents', async () => {
1029-
jest.spyOn(otomiStack, 'getAplAgents').mockReturnValue([])
1029+
jest.spyOn(otomiStack, 'getAplAgents').mockResolvedValue([])
10301030
await agent
10311031
.get('/alpha/teams/team1/agents')
10321032
.set('Authorization', `Bearer ${teamAdminToken}`)
@@ -1035,7 +1035,7 @@ describe('API authz tests', () => {
10351035
})
10361036

10371037
test('team member can get agents', async () => {
1038-
jest.spyOn(otomiStack, 'getAplAgents').mockReturnValue([])
1038+
jest.spyOn(otomiStack, 'getAplAgents').mockResolvedValue([])
10391039
await agent
10401040
.get('/alpha/teams/team1/agents')
10411041
.set('Authorization', `Bearer ${teamMemberToken}`)

src/api/alpha/teams/{teamId}/agents.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ const debug = Debug('otomi:api:alpha:teams:agents')
88
* GET /alpha/teams/{teamId}/agents
99
* Get all agents for a team
1010
*/
11-
export const getAplAgents = (req: OpenApiRequestExt, res: Response): void => {
11+
export const getAplAgents = async (req: OpenApiRequestExt, res: Response): Promise<void> => {
1212
const { teamId } = req.params
1313
debug(`getAplAgents(${teamId})`)
14-
const v = req.otomi.getAplAgents(decodeURIComponent(teamId))
14+
const v = await req.otomi.getAplAgents(decodeURIComponent(teamId))
1515
res.json(v)
1616
}
1717

src/api/alpha/teams/{teamId}/kb.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ const debug = Debug('otomi:api:alpha:teams:kb')
88
* GET /alpha/teams/{teamId}/kb
99
* Get all knowledge bases for a team
1010
*/
11-
export const getAplKnowledgeBases = (req: OpenApiRequestExt, res: Response): void => {
11+
export const getAplKnowledgeBases = async (req: OpenApiRequestExt, res: Response): Promise<void> => {
1212
const { teamId } = req.params
1313
debug(`getAplKnowledgeBases(${teamId})`)
14-
const v = req.otomi.getAplKnowledgeBases(decodeURIComponent(teamId))
14+
const v = await req.otomi.getAplKnowledgeBases(decodeURIComponent(teamId))
1515
res.json(v)
1616
}
1717

src/otomi-stack.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,9 @@ import { getSealedSecretsPEM, sealedSecretManifest } from './utils/sealedSecretU
133133
import { getKeycloakUsers, isValidUsername } from './utils/userUtils'
134134
import { defineClusterId, ObjectStorageClient } from './utils/wizardUtils'
135135
import { fetchChartYaml, fetchWorkloadCatalog, NewHelmChartValues, sparseCloneChart } from './utils/workloadUtils'
136+
import { listAkamaiAgentCRs, listAkamaiKnowledgeBaseCRs } from './ai/k8s'
137+
import { AkamaiAgentCR } from './ai/AkamaiAgentCR'
138+
import { AkamaiKnowledgeBaseCR } from './ai/AkamaiKnowledgeBaseCR'
136139

137140
interface ExcludedApp extends App {
138141
managed: boolean
@@ -2230,12 +2233,12 @@ export default class OtomiStack {
22302233
if (!knowledgeBase) {
22312234
throw new NotExistError(`Knowledge base ${name} not found in team ${teamId}`)
22322235
}
2233-
return knowledgeBase as AplKnowledgeBaseResponse
2236+
return AkamaiKnowledgeBaseCR.fromCR(knowledgeBase).toApiResponse(teamId)
22342237
}
22352238

2236-
getAplKnowledgeBases(teamId: string): AplKnowledgeBaseResponse[] {
2237-
const files = this.fileStore.getTeamResourcesByKindAndTeamId('AkamaiKnowledgeBase', teamId)
2238-
return Array.from(files.values()) as AplKnowledgeBaseResponse[]
2239+
async getAplKnowledgeBases(teamId: string): Promise<AplKnowledgeBaseResponse[]> {
2240+
const knowledgeBases = await listAkamaiKnowledgeBaseCRs(`team-${teamId}`)
2241+
return knowledgeBases.map((kb) => AkamaiKnowledgeBaseCR.fromCR(kb).toApiResponse(teamId, kb.status))
22392242
}
22402243

22412244
private async saveTeamKnowledgeBase(aplTeamObject: AplTeamObject): Promise<AplRecord> {
@@ -2300,12 +2303,12 @@ export default class OtomiStack {
23002303
if (!agent) {
23012304
throw new NotExistError(`Agent ${name} not found in team ${teamId}`)
23022305
}
2303-
return agent as AplAgentResponse
2306+
return AkamaiAgentCR.fromCR(agent).toApiResponse(teamId)
23042307
}
23052308

2306-
getAplAgents(teamId: string): AplAgentResponse[] {
2307-
const files = this.fileStore.getTeamResourcesByKindAndTeamId('AkamaiAgent', teamId)
2308-
return Array.from(files.values()) as AplAgentResponse[]
2309+
async getAplAgents(teamId: string): Promise<AplAgentResponse[]> {
2310+
const agents = await listAkamaiAgentCRs(`team-${teamId}`)
2311+
return agents.map((agent) => AkamaiAgentCR.fromCR(agent).toApiResponse(teamId, agent.status))
23092312
}
23102313

23112314
getAllAplAgents(): AplAgentResponse[] {

0 commit comments

Comments
 (0)