Skip to content

Commit 670450f

Browse files
authored
feat: update repo to work with single policy file (#693)
1 parent ce30a4c commit 670450f

File tree

3 files changed

+23
-24
lines changed

3 files changed

+23
-24
lines changed

src/git.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,12 @@ export class Git {
238238
fileMap: FileMap,
239239
unsetBlankAttributes?: boolean,
240240
): Promise<Promise<void>> {
241-
const jsonPathsValuesPublic = jsonpath.nodes(config, fileMap.jsonPathExpression)
241+
let jsonPathsValuesPublic
242+
if (fileMap.kind === 'AplTeamPolicy') {
243+
jsonPathsValuesPublic = jsonpath.nodes(config, '$.teamConfig.*.*')
244+
} else {
245+
jsonPathsValuesPublic = jsonpath.nodes(config, fileMap.jsonPathExpression)
246+
}
242247
await Promise.all(
243248
jsonPathsValuesPublic.map(async (node) => {
244249
const nodePath = node.path

src/otomi-stack.ts

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -759,24 +759,10 @@ export default class OtomiStack {
759759
await this.git.saveConfig(repo, fileMap)
760760
}
761761

762-
async saveTeamPolicies(teamId: string, data: AplPolicyResponse[]): Promise<void> {
762+
async saveTeamPolicy(teamId: string, data: AplPolicyResponse): Promise<void> {
763763
debug(`Saving AplTeamPolicy for team ${teamId}`)
764-
const teamPolicies = {}
765-
data.forEach((policy) => {
766-
teamPolicies[policy.metadata.name] = policy.spec
767-
})
768-
const manifest = {
769-
kind: 'AplTeamPolicy',
770-
metadata: {
771-
name: teamId,
772-
labels: {
773-
'apl.io/teamId': teamId,
774-
},
775-
},
776-
spec: teamPolicies,
777-
}
778-
const configKey = 'policies'
779-
const repo = this.createTeamConfigInRepo(teamId, configKey, manifest)
764+
const configKey = data.metadata.name
765+
const repo = this.createTeamConfigInRepo(teamId, configKey, data)
780766
const fileMap = getFileMaps('').find((fm) => fm.kind === 'AplTeamPolicy')!
781767
await this.git.saveConfig(repo, fileMap)
782768
}
@@ -1586,8 +1572,7 @@ export default class OtomiStack {
15861572
const policy = patch
15871573
? this.repoService.getTeamConfigService(teamId).patchPolicies(policyId, data)
15881574
: this.repoService.getTeamConfigService(teamId).updatePolicies(policyId, data as AplPolicyRequest)
1589-
const teamPolicies = this.getTeamAplPolicies(teamId)
1590-
await this.saveTeamPolicies(teamId, teamPolicies)
1575+
await this.saveTeamPolicy(teamId, policy)
15911576
await this.doTeamDeployment(
15921577
teamId,
15931578
(teamService) => {

src/repo.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ export function getResourceName(fileMap: FileMap, jsonPath: jsonpath.PathCompone
5353
return resourceName
5454
}
5555

56-
if (fileMap.resourceGroup === 'team') {
56+
// Custom workaround for teamPolicy because it is a mapItem
57+
if (fileMap.resourceGroup === 'team' && fileMap.kind !== 'AplTeamPolicy') {
5758
resourceName = getTeamNameFromJsonPath(jsonPath)
5859
return resourceName
5960
} else {
@@ -384,11 +385,11 @@ export function getFileMaps(envDir: string): Array<FileMap> {
384385
{
385386
kind: 'AplTeamPolicy',
386387
envDir,
387-
jsonPathExpression: '$.teamConfig.*.policies',
388-
pathGlob: `${envDir}/env/teams/*/policies.yaml`,
388+
jsonPathExpression: '$.teamConfig.*.policies[*]',
389+
pathGlob: `${envDir}/env/teams/*/policies/*.yaml`,
389390
processAs: 'mapItem',
390391
resourceGroup: 'team',
391-
resourceDir: '.',
392+
resourceDir: 'policies',
392393
loadToSpec: true,
393394
v2: true,
394395
},
@@ -506,6 +507,13 @@ export async function loadFileToSpec(
506507
} else {
507508
ref.push(data?.spec)
508509
}
510+
} else if (fileMap.kind === 'AplTeamPolicy') {
511+
const ref: Record<string, any> = get(spec, jsonPath)
512+
const policy = {
513+
[data?.metadata?.name]: data?.spec,
514+
}
515+
const newRef = merge(cloneDeep(ref), policy)
516+
set(spec, jsonPath, newRef)
509517
} else {
510518
const ref: Record<string, any> = get(spec, jsonPath)
511519
// TODO: Remove workaround for Team settings currently relying on id in console
@@ -563,6 +571,7 @@ export async function loadValues(envDir: string, deps = { loadToSpec }): Promise
563571
await deps.loadToSpec(spec, fileMap)
564572
}),
565573
)
574+
566575
return spec
567576
}
568577

0 commit comments

Comments
 (0)