diff --git a/src/git.ts b/src/git.ts index 93d013844..79f20dd47 100644 --- a/src/git.ts +++ b/src/git.ts @@ -238,7 +238,12 @@ export class Git { fileMap: FileMap, unsetBlankAttributes?: boolean, ): Promise> { - const jsonPathsValuesPublic = jsonpath.nodes(config, fileMap.jsonPathExpression) + let jsonPathsValuesPublic + if (fileMap.kind === 'AplTeamPolicy') { + jsonPathsValuesPublic = jsonpath.nodes(config, '$.teamConfig.*.*') + } else { + jsonPathsValuesPublic = jsonpath.nodes(config, fileMap.jsonPathExpression) + } await Promise.all( jsonPathsValuesPublic.map(async (node) => { const nodePath = node.path diff --git a/src/otomi-stack.ts b/src/otomi-stack.ts index d44799cd2..c59101703 100644 --- a/src/otomi-stack.ts +++ b/src/otomi-stack.ts @@ -759,24 +759,10 @@ export default class OtomiStack { await this.git.saveConfig(repo, fileMap) } - async saveTeamPolicies(teamId: string, data: AplPolicyResponse[]): Promise { + async saveTeamPolicy(teamId: string, data: AplPolicyResponse): Promise { debug(`Saving AplTeamPolicy for team ${teamId}`) - const teamPolicies = {} - data.forEach((policy) => { - teamPolicies[policy.metadata.name] = policy.spec - }) - const manifest = { - kind: 'AplTeamPolicy', - metadata: { - name: teamId, - labels: { - 'apl.io/teamId': teamId, - }, - }, - spec: teamPolicies, - } - const configKey = 'policies' - const repo = this.createTeamConfigInRepo(teamId, configKey, manifest) + const configKey = data.metadata.name + const repo = this.createTeamConfigInRepo(teamId, configKey, data) const fileMap = getFileMaps('').find((fm) => fm.kind === 'AplTeamPolicy')! await this.git.saveConfig(repo, fileMap) } @@ -1586,8 +1572,7 @@ export default class OtomiStack { const policy = patch ? this.repoService.getTeamConfigService(teamId).patchPolicies(policyId, data) : this.repoService.getTeamConfigService(teamId).updatePolicies(policyId, data as AplPolicyRequest) - const teamPolicies = this.getTeamAplPolicies(teamId) - await this.saveTeamPolicies(teamId, teamPolicies) + await this.saveTeamPolicy(teamId, policy) await this.doTeamDeployment( teamId, (teamService) => { diff --git a/src/repo.ts b/src/repo.ts index 1245be220..3920ebe4d 100755 --- a/src/repo.ts +++ b/src/repo.ts @@ -53,7 +53,8 @@ export function getResourceName(fileMap: FileMap, jsonPath: jsonpath.PathCompone return resourceName } - if (fileMap.resourceGroup === 'team') { + // Custom workaround for teamPolicy because it is a mapItem + if (fileMap.resourceGroup === 'team' && fileMap.kind !== 'AplTeamPolicy') { resourceName = getTeamNameFromJsonPath(jsonPath) return resourceName } else { @@ -384,11 +385,11 @@ export function getFileMaps(envDir: string): Array { { kind: 'AplTeamPolicy', envDir, - jsonPathExpression: '$.teamConfig.*.policies', - pathGlob: `${envDir}/env/teams/*/policies.yaml`, + jsonPathExpression: '$.teamConfig.*.policies[*]', + pathGlob: `${envDir}/env/teams/*/policies/*.yaml`, processAs: 'mapItem', resourceGroup: 'team', - resourceDir: '.', + resourceDir: 'policies', loadToSpec: true, v2: true, }, @@ -506,6 +507,13 @@ export async function loadFileToSpec( } else { ref.push(data?.spec) } + } else if (fileMap.kind === 'AplTeamPolicy') { + const ref: Record = get(spec, jsonPath) + const policy = { + [data?.metadata?.name]: data?.spec, + } + const newRef = merge(cloneDeep(ref), policy) + set(spec, jsonPath, newRef) } else { const ref: Record = get(spec, jsonPath) // 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 await deps.loadToSpec(spec, fileMap) }), ) + return spec }