Skip to content

Commit 712bff9

Browse files
committed
refactor: yaml and autogen route cleanup
1 parent 71f5068 commit 712bff9

File tree

2 files changed

+25
-14
lines changed

2 files changed

+25
-14
lines changed

services/api/src/resources/environment/helpers.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,13 +93,13 @@ export const Helpers = (sqlClientPool: Pool) => {
9393
);
9494

9595
// remove all route associations from environment
96-
// but leave the routes in the api attached to the project
97-
await routeHelpers(sqlClientPool).removeAllRoutesFromEnvironment(eid)
9896
// delete any autogenerated routes, these are not reusable and are only associated to the environment they are on
9997
await routeHelpers(sqlClientPool).deleteAutogeneratedRoutesForEnvironment(eid)
10098
// delete any lagoon.yml managed routes from the api, if they want to manage them in the api
10199
// they should be modified in the api to update the source to the api to nullify the lagoon.yml source
102100
await routeHelpers(sqlClientPool).deleteLagoonYAMLRoutesForEnvironment(eid)
101+
// but leave the routes in the api attached to the project
102+
await routeHelpers(sqlClientPool).removeAllRoutesFromEnvironment(eid)
103103

104104
// delete the environment backups rows
105105
// logger.debug(`deleting environment ${name}/id:${eid}/project:${pid} environment backups`)

services/api/src/resources/routes/resolvers.ts

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -560,7 +560,7 @@ export const removeRouteFromEnvironment: ResolverFn = async (
560560
environment,
561561
}
562562
},
563-
{ sqlClientPool, hasPermission, userActivityLogger }
563+
{ sqlClientPool, hasPermission, userActivityLogger, adminScopes }
564564
) => {
565565
const projectId = await projectHelpers(sqlClientPool).getProjectIdByName(project)
566566
const projectData = await projectHelpers(sqlClientPool).getProjectById(projectId)
@@ -570,17 +570,28 @@ export const removeRouteFromEnvironment: ResolverFn = async (
570570
const env = await environmentHelpers(sqlClientPool).getEnvironmentByNameAndProject(environment, projectId)
571571
const environmentData = env[0]
572572

573+
const existsProject = await query(
574+
sqlClientPool,
575+
Sql.selectRouteByDomainAndProjectID(domain, projectId)
576+
)
577+
if (existsProject.length == 0) {
578+
throw Error(`Route doesn't exist on this project`)
579+
}
580+
const route = existsProject[0]
581+
582+
if (!adminScopes.platformOwner) {
583+
if (route.source.toLowerCase() === RouteSource.YAML) {
584+
throw Error(`This route cannot be removed from the environment as it is managed by a lagoon.yml file`)
585+
}
586+
if (route.source.toLowerCase() === RouteSource.AUTOGENERATED) {
587+
throw Error(`Cannot remove autogenerated routes from an environment using this endpoint`)
588+
}
589+
}
590+
573591
const routeId = await Helpers(sqlClientPool).removeRouteFromEnvironment(domain, environmentData.id)
574592

575593
const ret = await query(sqlClientPool, Sql.selectRouteByID(routeId));
576-
const route = ret[0];
577-
578-
if (route.source.toLowerCase() === RouteSource.YAML) {
579-
throw Error(`This route cannot be removed from the environment as it is managed by a lagoon.yml file`)
580-
}
581-
if (route.source.toLowerCase() === RouteSource.AUTOGENERATED) {
582-
throw Error(`Cannot remove autogenerated routes from an environment using this endpoint`)
583-
}
594+
const returnRoute = ret[0];
584595

585596
const auditLog: AuditLog = {
586597
resource: {
@@ -599,18 +610,18 @@ export const removeRouteFromEnvironment: ResolverFn = async (
599610
if (projectData.organization) {
600611
auditLog.organizationId = projectData.organization;
601612
}
602-
userActivityLogger(`User removed route '${route.domain}' from environment '${environmentData.name}'`, {
613+
userActivityLogger(`User removed route '${returnRoute.domain}' from environment '${environmentData.name}'`, {
603614
project: '',
604615
event: 'api:removeRouteFromEnvironment',
605616
payload: {
606617
project: projectData.id,
607618
environment: environmentData.id,
608-
route: route.id,
619+
route: returnRoute.id,
609620
...auditLog
610621
}
611622
});
612623

613-
return route;
624+
return returnRoute;
614625
}
615626

616627
/*

0 commit comments

Comments
 (0)