Skip to content

Commit

Permalink
refactor: send organization information in build payload
Browse files Browse the repository at this point in the history
  • Loading branch information
shreddedbacon committed Nov 14, 2023
1 parent 296bf4c commit 79ab2cb
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
28 changes: 27 additions & 1 deletion node-packages/commons/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1140,6 +1140,7 @@ export const getOpenShiftInfoForProject = (project: string): Promise<any> =>
{
project:projectByName(name: "${project}"){
id
organization
openshift {
...${deployTargetMinimalFragment}
}
Expand Down Expand Up @@ -1256,7 +1257,7 @@ export const getEnvironmentsForProject = (
}
`);

export async function getOrganizationById(id: number): Promise<any> {
export async function getOrganizationByIdWithEnvs(id: number): Promise<any> {
const result = await graphqlapi.query(`
{
organization:organizationById(id: ${id}) {
Expand All @@ -1268,6 +1269,7 @@ export async function getOrganizationById(id: number): Promise<any> {
quotaEnvironment
quotaGroup
quotaNotification
quotaRoute
environments {
name
id
Expand All @@ -1288,6 +1290,30 @@ export async function getOrganizationById(id: number): Promise<any> {
return result.organization;
}

export async function getOrganizationById(id: number): Promise<any> {
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[]
Expand Down
18 changes: 15 additions & 3 deletions node-packages/commons/src/tasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
addDeployment,
Project,
DeployTarget,
getOrganizationByIdWithEnvs,
getOrganizationById
} from './api';
import {
Expand Down Expand Up @@ -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: {
Expand All @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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}`
Expand Down

0 comments on commit 79ab2cb

Please sign in to comment.