Skip to content

Commit 601612b

Browse files
committed
fix: loading platform and team resources by kind
1 parent e7bddd2 commit 601612b

File tree

2 files changed

+78
-45
lines changed

2 files changed

+78
-45
lines changed

src/fileStore/file-store.ts

Lines changed: 55 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -175,36 +175,69 @@ export class FileStore {
175175
return filePath
176176
}
177177

178-
// Generic method for all resources (platform and team)
179-
getByKind(kind: AplKind, teamId?: string): Map<string, AplObject> {
178+
// Get platform resources (no team scope - e.g., AplUser, settings)
179+
getPlatformResourcesByKind(kind: AplKind): Map<string, AplObject> {
180180
const fileMap = getFileMapForKind(kind)
181181
if (!fileMap) {
182182
throw new Error(`Unknown kind: ${kind}`)
183183
}
184184

185+
// Platform resources don't have {teamId} in their path
186+
// e.g., 'env/settings/{name}.yaml' → 'env/settings/'
187+
const prefix = fileMap.pathTemplate.replace('{name}.yaml', '')
185188
const result = new Map<string, AplObject>()
186189

187-
if (teamId) {
188-
// Specific team: use exact prefix match (e.g., 'env/teams/team1/workloads/')
189-
const prefix = fileMap.pathTemplate.replace('{teamId}', teamId).replace('{name}.yaml', '')
190-
for (const filePath of this.store.keys()) {
191-
if (filePath.startsWith(prefix) && filePath.endsWith('.yaml')) {
192-
const content = this.store.get(filePath)
193-
if (content) result.set(filePath, content)
194-
}
190+
for (const filePath of this.store.keys()) {
191+
if (filePath.startsWith(prefix) && filePath.endsWith('.yaml')) {
192+
const content = this.store.get(filePath)
193+
if (content) result.set(filePath, content)
195194
}
196-
} else {
197-
// All teams: match pattern with any teamId (e.g., 'env/teams/*/workloads/')
198-
const pattern = fileMap.pathTemplate.replace('{teamId}', '').replace('{name}.yaml', '')
199-
const [beforeTeam, afterTeam] = pattern.split('//')
200-
for (const filePath of this.store.keys()) {
201-
const matchesPattern = afterTeam
202-
? filePath.startsWith(beforeTeam) && filePath.includes(afterTeam) && filePath.endsWith('.yaml')
203-
: filePath.startsWith(beforeTeam) && filePath.endsWith('.yaml')
204-
if (matchesPattern) {
205-
const content = this.store.get(filePath)
206-
if (content) result.set(filePath, content)
207-
}
195+
}
196+
197+
return result
198+
}
199+
200+
// Get ALL team resources across all teams (e.g., all workloads, all services)
201+
getAllTeamResourcesByKind(kind: AplKind): Map<string, AplObject> {
202+
const fileMap = getFileMapForKind(kind)
203+
if (!fileMap) {
204+
throw new Error(`Unknown kind: ${kind}`)
205+
}
206+
207+
// Extract resource path segment from template
208+
// e.g., 'env/teams/{teamId}/workloads/{name}.yaml' → '/workloads'
209+
const parts = fileMap.pathTemplate.split('{teamId}')
210+
const resourcePath = parts[1].replace('/{name}.yaml', '')
211+
const result = new Map<string, AplObject>()
212+
213+
// Match any path containing this resource segment
214+
// e.g., matches 'env/teams/*/workloads/*.yaml'
215+
for (const filePath of this.store.keys()) {
216+
if (filePath.includes(resourcePath) && filePath.endsWith('.yaml')) {
217+
const content = this.store.get(filePath)
218+
if (content) result.set(filePath, content)
219+
}
220+
}
221+
222+
return result
223+
}
224+
225+
getTeamResourcesByKindAndTeamId(kind: AplKind, teamId: string): Map<string, AplObject> {
226+
const fileMap = getFileMapForKind(kind)
227+
if (!fileMap) {
228+
throw new Error(`Unknown kind: ${kind}`)
229+
}
230+
231+
// Generate exact prefix for this team
232+
// e.g., 'env/teams/team1/workloads/'
233+
const prefix = fileMap.pathTemplate.replace('{teamId}', teamId).replace('{name}.yaml', '')
234+
const result = new Map<string, AplObject>()
235+
236+
// Exact prefix match
237+
for (const filePath of this.store.keys()) {
238+
if (filePath.startsWith(prefix) && filePath.endsWith('.yaml')) {
239+
const content = this.store.get(filePath)
240+
if (content) result.set(filePath, content)
208241
}
209242
}
210243

src/otomi-stack.ts

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ export default class OtomiStack {
383383
const fileMap = settingsFileMaps.get(key)
384384
if (!fileMap) return // Skip unknown keys
385385

386-
const files = this.fileStore.getByKind(fileMap.kind)
386+
const files = this.fileStore.getPlatformResourcesByKind(fileMap.kind)
387387
for (const [, content] of files) {
388388
settings[key] = content?.spec || content
389389
}
@@ -399,7 +399,7 @@ export default class OtomiStack {
399399

400400
// No keys specified: fetch all settings
401401
for (const [name, fileMap] of settingsFileMaps.entries()) {
402-
const files = this.fileStore.getByKind(fileMap.kind)
402+
const files = this.fileStore.getPlatformResourcesByKind(fileMap.kind)
403403
for (const [, content] of files) {
404404
settings[name] = content?.spec || content
405405
}
@@ -643,7 +643,7 @@ export default class OtomiStack {
643643
const teamIds = this.fileStore.getTeamIds()
644644

645645
for (const teamId of teamIds) {
646-
const settingsFiles = this.fileStore.getByKind('AplTeamSettingSet', teamId)
646+
const settingsFiles = this.fileStore.getTeamResourcesByKindAndTeamId('AplTeamSettingSet', teamId)
647647
for (const [, content] of settingsFiles) {
648648
// v1 format: return spec directly
649649
const team = getV1ObjectFromApl(content as AplTeamSettingsResponse) as Team
@@ -667,7 +667,7 @@ export default class OtomiStack {
667667

668668
for (const teamId of teamIds) {
669669
if (teamId === 'admin') continue
670-
const settingsFiles = this.fileStore.getByKind('AplTeamSettingSet', teamId)
670+
const settingsFiles = this.fileStore.getTeamResourcesByKindAndTeamId('AplTeamSettingSet', teamId)
671671
for (const [, content] of settingsFiles) {
672672
if (content) {
673673
// Return full v2 object with password removed
@@ -859,7 +859,7 @@ export default class OtomiStack {
859859
}
860860

861861
getTeamAplNetpols(teamId: string): AplNetpolResponse[] {
862-
const files = this.fileStore.getByKind('AplTeamNetworkControl', teamId)
862+
const files = this.fileStore.getTeamResourcesByKindAndTeamId('AplTeamNetworkControl', teamId)
863863
return Array.from(files.values()) as AplNetpolResponse[]
864864
}
865865

@@ -868,7 +868,7 @@ export default class OtomiStack {
868868
}
869869

870870
getAllAplNetpols(): AplNetpolResponse[] {
871-
const files = this.fileStore.getByKind('AplTeamNetworkControl')
871+
const files = this.fileStore.getAllTeamResourcesByKind('AplTeamNetworkControl')
872872
return Array.from(files.values()) as AplNetpolResponse[]
873873
}
874874

@@ -936,7 +936,7 @@ export default class OtomiStack {
936936
}
937937

938938
getAllUsers(sessionUser: SessionUser): Array<User> {
939-
const files = this.fileStore.getByKind('AplUser')
939+
const files = this.fileStore.getPlatformResourcesByKind('AplUser')
940940
const aplObjects = Array.from(files.values()) as AplObject[]
941941
const users = aplObjects.map((aplObject) => {
942942
return { ...aplObject.spec, id: aplObject.metadata.name } as User
@@ -970,7 +970,7 @@ export default class OtomiStack {
970970
const user: User = { ...data, id: userId, initialPassword }
971971

972972
// Get existing users' emails
973-
const files = this.fileStore.getByKind('AplUser')
973+
const files = this.fileStore.getPlatformResourcesByKind('AplUser')
974974
let existingUsersEmail = Array.from(files.values()).map((aplObject: AplObject) => aplObject.spec.email)
975975

976976
if (!env.isDev) {
@@ -1120,7 +1120,7 @@ export default class OtomiStack {
11201120
}
11211121

11221122
getTeamAplCodeRepos(teamId: string): AplCodeRepoResponse[] {
1123-
const files = this.fileStore.getByKind('AplTeamCodeRepo', teamId)
1123+
const files = this.fileStore.getTeamResourcesByKindAndTeamId('AplTeamCodeRepo', teamId)
11241124
return Array.from(files.values()) as AplCodeRepoResponse[]
11251125
}
11261126

@@ -1129,7 +1129,7 @@ export default class OtomiStack {
11291129
}
11301130

11311131
getAllAplCodeRepos(): AplCodeRepoResponse[] {
1132-
const files = this.fileStore.getByKind('AplTeamCodeRepo')
1132+
const files = this.fileStore.getAllTeamResourcesByKind('AplTeamCodeRepo')
11331133
return Array.from(files.values()) as AplCodeRepoResponse[]
11341134
}
11351135

@@ -1298,7 +1298,7 @@ export default class OtomiStack {
12981298
}
12991299

13001300
getTeamAplBuilds(teamId: string): AplBuildResponse[] {
1301-
const files = this.fileStore.getByKind('AplTeamBuild', teamId)
1301+
const files = this.fileStore.getTeamResourcesByKindAndTeamId('AplTeamBuild', teamId)
13021302
return Array.from(files.values()) as AplBuildResponse[]
13031303
}
13041304

@@ -1307,7 +1307,7 @@ export default class OtomiStack {
13071307
}
13081308

13091309
getAllAplBuilds(): AplBuildResponse[] {
1310-
const files = this.fileStore.getByKind('AplTeamBuild')
1310+
const files = this.fileStore.getAllTeamResourcesByKind('AplTeamBuild')
13111311
return Array.from(files.values()) as AplBuildResponse[]
13121312
}
13131313

@@ -1387,7 +1387,7 @@ export default class OtomiStack {
13871387
}
13881388

13891389
getTeamAplPolicies(teamId: string): AplPolicyResponse[] {
1390-
const files = this.fileStore.getByKind('AplTeamPolicy', teamId)
1390+
const files = this.fileStore.getTeamResourcesByKindAndTeamId('AplTeamPolicy', teamId)
13911391
return Array.from(files.values()) as AplPolicyResponse[]
13921392
}
13931393

@@ -1401,7 +1401,7 @@ export default class OtomiStack {
14011401
}
14021402

14031403
getAllAplPolicies(): AplPolicyResponse[] {
1404-
const files = this.fileStore.getByKind('AplTeamPolicy')
1404+
const files = this.fileStore.getAllTeamResourcesByKind('AplTeamPolicy')
14051405
return Array.from(files.values()) as AplPolicyResponse[]
14061406
}
14071407

@@ -1610,7 +1610,7 @@ export default class OtomiStack {
16101610
}
16111611

16121612
getTeamAplWorkloads(teamId: string): AplWorkloadResponse[] {
1613-
const files = this.fileStore.getByKind('AplTeamWorkload', teamId)
1613+
const files = this.fileStore.getTeamResourcesByKindAndTeamId('AplTeamWorkload', teamId)
16141614
return Array.from(files.values()) as AplWorkloadResponse[]
16151615
}
16161616

@@ -1632,7 +1632,7 @@ export default class OtomiStack {
16321632
}
16331633

16341634
getAllAplWorkloads(): AplWorkloadResponse[] {
1635-
const files = this.fileStore.getByKind('AplTeamWorkload')
1635+
const files = this.fileStore.getAllTeamResourcesByKind('AplTeamWorkload')
16361636
return Array.from(files.values()) as AplWorkloadResponse[]
16371637
}
16381638

@@ -1738,7 +1738,7 @@ export default class OtomiStack {
17381738
}
17391739

17401740
getAllAplServices(): AplServiceResponse[] {
1741-
const files = this.fileStore.getByKind('AplTeamService')
1741+
const files = this.fileStore.getAllTeamResourcesByKind('AplTeamService')
17421742
return Array.from(files.values()) as AplServiceResponse[]
17431743
}
17441744

@@ -1747,7 +1747,7 @@ export default class OtomiStack {
17471747
}
17481748

17491749
getTeamAplServices(teamId: string): AplServiceResponse[] {
1750-
const files = this.fileStore.getByKind('AplTeamService', teamId)
1750+
const files = this.fileStore.getTeamResourcesByKindAndTeamId('AplTeamService', teamId)
17511751
return Array.from(files.values()) as AplServiceResponse[]
17521752
}
17531753

@@ -2155,7 +2155,7 @@ export default class OtomiStack {
21552155
}
21562156

21572157
getAllAplSealedSecrets(): AplSecretResponse[] {
2158-
const files = this.fileStore.getByKind('AplTeamSecret')
2158+
const files = this.fileStore.getAllTeamResourcesByKind('AplTeamSecret')
21592159
return Array.from(files.values()) as AplSecretResponse[]
21602160
}
21612161

@@ -2167,7 +2167,7 @@ export default class OtomiStack {
21672167
}
21682168

21692169
getAplSealedSecrets(teamId: string): AplSecretResponse[] {
2170-
const files = this.fileStore.getByKind('AplTeamSecret', teamId)
2170+
const files = this.fileStore.getTeamResourcesByKindAndTeamId('AplTeamSecret', teamId)
21712171
return Array.from(files.values()) as AplSecretResponse[]
21722172
}
21732173

@@ -2229,7 +2229,7 @@ export default class OtomiStack {
22292229
}
22302230

22312231
getAplKnowledgeBases(teamId: string): AplKnowledgeBaseResponse[] {
2232-
const files = this.fileStore.getByKind('AkamaiKnowledgeBase', teamId)
2232+
const files = this.fileStore.getTeamResourcesByKindAndTeamId('AkamaiKnowledgeBase', teamId)
22332233
return Array.from(files.values()) as AplKnowledgeBaseResponse[]
22342234
}
22352235

@@ -2294,12 +2294,12 @@ export default class OtomiStack {
22942294
}
22952295

22962296
getAplAgents(teamId: string): AplAgentResponse[] {
2297-
const files = this.fileStore.getByKind('AkamaiAgent', teamId)
2297+
const files = this.fileStore.getTeamResourcesByKindAndTeamId('AkamaiAgent', teamId)
22982298
return Array.from(files.values()) as AplAgentResponse[]
22992299
}
23002300

23012301
getAllAplAgents(): AplAgentResponse[] {
2302-
const files = this.fileStore.getByKind('AkamaiAgent')
2302+
const files = this.fileStore.getAllTeamResourcesByKind('AkamaiAgent')
23032303
return Array.from(files.values()) as AplAgentResponse[]
23042304
}
23052305

0 commit comments

Comments
 (0)