From 79ab2cb1032a5a94b018186b1f64cd26edcfdfdb Mon Sep 17 00:00:00 2001 From: shreddedbacon Date: Wed, 15 Nov 2023 09:55:07 +1100 Subject: [PATCH] refactor: send organization information in build payload --- node-packages/commons/src/api.ts | 28 +++++++++++++++++++++++++++- node-packages/commons/src/tasks.ts | 18 +++++++++++++++--- 2 files changed, 42 insertions(+), 4 deletions(-) diff --git a/node-packages/commons/src/api.ts b/node-packages/commons/src/api.ts index 2bd3866ab0..f88ec4517e 100644 --- a/node-packages/commons/src/api.ts +++ b/node-packages/commons/src/api.ts @@ -1140,6 +1140,7 @@ export const getOpenShiftInfoForProject = (project: string): Promise => { project:projectByName(name: "${project}"){ id + organization openshift { ...${deployTargetMinimalFragment} } @@ -1256,7 +1257,7 @@ export const getEnvironmentsForProject = ( } `); -export async function getOrganizationById(id: number): Promise { +export async function getOrganizationByIdWithEnvs(id: number): Promise { const result = await graphqlapi.query(` { organization:organizationById(id: ${id}) { @@ -1268,6 +1269,7 @@ export async function getOrganizationById(id: number): Promise { quotaEnvironment quotaGroup quotaNotification + quotaRoute environments { name id @@ -1288,6 +1290,30 @@ export async function getOrganizationById(id: number): Promise { return result.organization; } +export async function getOrganizationById(id: number): Promise { + const result = await graphqlapi.query(` + { + organization:organizationById(id: ${id}) { + id + name + friendlyName + description + quotaProject + quotaEnvironment + quotaGroup + quotaRoute + quotaNotification + } + } + `); + + if (!result || !result.organization) { + throw new OrganizationNotFound(`Cannot find organization ${id}`); + } + + return result.organization; +} + export const setEnvironmentServices = ( environment: number, services: string[] diff --git a/node-packages/commons/src/tasks.ts b/node-packages/commons/src/tasks.ts index 3be8efba20..c9547ff90b 100644 --- a/node-packages/commons/src/tasks.ts +++ b/node-packages/commons/src/tasks.ts @@ -18,6 +18,7 @@ import { addDeployment, Project, DeployTarget, + getOrganizationByIdWithEnvs, getOrganizationById } from './api'; import { @@ -601,13 +602,23 @@ export const getControllerBuildData = async function(deployData: any) { // encode some values so they get sent to the controllers nicely const sshKeyBase64 = new Buffer(deployPrivateKey.replace(/\\n/g, "\n")).toString('base64') const [routerPattern, envVars, projectVars] = await getEnvironmentsRouterPatternAndVariables( - result.project, + lagoonProjectData, environment.addOrUpdateEnvironment, deployTarget.openshift, bulkId, bulkName, buildPriority, buildVariables, bulkType.Deploy ) + let organization = {} + if (lagoonProjectData.organization != null) { + const curOrg = await getOrganizationById(lagoonProjectData.organization); + organization = { + name: curOrg.name, + id: curOrg.id, + } + } + + // this is what will be returned and sent to the controllers via message queue, it is the lagoonbuild controller spec var buildDeployData: any = { metadata: { @@ -631,6 +642,7 @@ export const getControllerBuildData = async function(deployData: any) { project: { id: lagoonProjectData.id, name: projectName, + organization: organization, gitUrl: gitUrl, uiLink: deployment.addDeployment.uiLink, environment: environmentName, @@ -704,7 +716,7 @@ export const getEnvironmentsRouterPatternAndVariables = async function name( if (project.organization) { // check the environment quota, this prevents environments being deployed by the api or webhooks const curOrg = await getOrganizationById(project.organization); - project.envVariables.push({"name":"LAGOON_ROUTE_QUOTA", "value":curOrg.quotaRoute.toString(), "scope":"internal_system"}) + project.envVariables.push({"name":"LAGOON_ROUTE_QUOTA", "value":`"${curOrg.quotaRoute}"`, "scope":"internal_system"}) } // handle any bulk deploy related injections here @@ -771,7 +783,7 @@ export const createDeployTask = async function(deployData: any) { // if this would be a new environment, check it against the environment quota if (!environments.project.environments.map(e => e.name).find(i => i === branchName)) { // check the environment quota, this prevents environments being deployed by the api or webhooks - const curOrg = await getOrganizationById(project.organization); + const curOrg = await getOrganizationByIdWithEnvs(project.organization); if (curOrg.environments.length >= curOrg.quotaEnvironment && curOrg.quotaEnvironment != -1) { throw new OrganizationEnvironmentLimit( `'${branchName}' would exceed organization environment quota: ${curOrg.environments.length}/${curOrg.quotaEnvironment}`