@@ -127,7 +127,7 @@ func (h *DeploymentHandler) DeployAPI(c *gin.Context) {
127127 c .JSON (http .StatusCreated , deployment )
128128}
129129
130- // UndeployDeployment handles POST /api/v1/apis/:apiId/deployments/:deploymentId/ undeploy
130+ // UndeployDeployment handles POST /api/v1/apis/:apiId/deployments/undeploy
131131// Undeploys an active deployment by changing its status to UNDEPLOYED
132132func (h * DeploymentHandler ) UndeployDeployment (c * gin.Context ) {
133133 orgId , exists := middleware .GetOrganizationFromContext (c )
@@ -138,7 +138,8 @@ func (h *DeploymentHandler) UndeployDeployment(c *gin.Context) {
138138 }
139139
140140 apiId := c .Param ("apiId" )
141- deploymentId := c .Param ("deploymentId" )
141+ deploymentId := c .Query ("deploymentId" )
142+ gatewayId := c .Query ("gatewayId" )
142143
143144 if apiId == "" {
144145 c .JSON (http .StatusBadRequest , utils .NewErrorResponse (400 , "Bad Request" ,
@@ -147,11 +148,16 @@ func (h *DeploymentHandler) UndeployDeployment(c *gin.Context) {
147148 }
148149 if deploymentId == "" {
149150 c .JSON (http .StatusBadRequest , utils .NewErrorResponse (400 , "Bad Request" ,
150- "Deployment ID is required" ))
151+ "deploymentId query parameter is required" ))
152+ return
153+ }
154+ if gatewayId == "" {
155+ c .JSON (http .StatusBadRequest , utils .NewErrorResponse (400 , "Bad Request" ,
156+ "gatewayId query parameter is required" ))
151157 return
152158 }
153159
154- deployment , err := h .deploymentService .UndeployDeploymentByHandle (apiId , deploymentId , orgId )
160+ deployment , err := h .deploymentService .UndeployDeploymentByHandle (apiId , deploymentId , gatewayId , orgId )
155161 if err != nil {
156162 if errors .Is (err , constants .ErrAPINotFound ) {
157163 c .JSON (http .StatusNotFound , utils .NewErrorResponse (404 , "Not Found" ,
@@ -173,17 +179,22 @@ func (h *DeploymentHandler) UndeployDeployment(c *gin.Context) {
173179 "No active deployment found for this API on the gateway" ))
174180 return
175181 }
176- log .Printf ("[ERROR] Failed to undeploy: apiId=%s deploymentId=%s error=%v" , apiId , deploymentId , err )
182+ if errors .Is (err , constants .ErrGatewayIDMismatch ) {
183+ c .JSON (http .StatusBadRequest , utils .NewErrorResponse (400 , "Bad Request" ,
184+ "Deployment is bound to a different gateway" ))
185+ return
186+ }
187+ log .Printf ("[ERROR] Failed to undeploy: apiId=%s deploymentId=%s gatewayId=%s error=%v" , apiId , deploymentId , gatewayId , err )
177188 c .JSON (http .StatusInternalServerError , utils .NewErrorResponse (500 , "Internal Server Error" , "Failed to undeploy deployment" ))
178189 return
179190 }
180191
181192 c .JSON (http .StatusOK , deployment )
182193}
183194
184- // RollbackDeployment handles POST /api/v1/apis/:apiId/rollback-deployment
185- // Rolls back to a previous deployment (ARCHIVED or UNDEPLOYED)
186- func (h * DeploymentHandler ) RollbackDeployment (c * gin.Context ) {
195+ // RestoreDeployment handles POST /api/v1/apis/:apiId/deployments/restore
196+ // Restores a previous deployment (ARCHIVED or UNDEPLOYED)
197+ func (h * DeploymentHandler ) RestoreDeployment (c * gin.Context ) {
187198 orgId , exists := middleware .GetOrganizationFromContext (c )
188199 if ! exists {
189200 c .JSON (http .StatusUnauthorized , utils .NewErrorResponse (401 , "Unauthorized" ,
@@ -193,6 +204,7 @@ func (h *DeploymentHandler) RollbackDeployment(c *gin.Context) {
193204
194205 apiId := c .Param ("apiId" )
195206 deploymentId := c .Query ("deploymentId" )
207+ gatewayId := c .Query ("gatewayId" )
196208
197209 if apiId == "" {
198210 c .JSON (http .StatusBadRequest , utils .NewErrorResponse (400 , "Bad Request" ,
@@ -204,8 +216,13 @@ func (h *DeploymentHandler) RollbackDeployment(c *gin.Context) {
204216 "deploymentId query parameter is required" ))
205217 return
206218 }
219+ if gatewayId == "" {
220+ c .JSON (http .StatusBadRequest , utils .NewErrorResponse (400 , "Bad Request" ,
221+ "gatewayId query parameter is required" ))
222+ return
223+ }
207224
208- deployment , err := h .deploymentService .RollbackDeploymentByHandle (apiId , deploymentId , orgId )
225+ deployment , err := h .deploymentService .RestoreDeploymentByHandle (apiId , deploymentId , gatewayId , orgId )
209226 if err != nil {
210227 if errors .Is (err , constants .ErrAPINotFound ) {
211228 c .JSON (http .StatusNotFound , utils .NewErrorResponse (404 , "Not Found" ,
@@ -224,11 +241,16 @@ func (h *DeploymentHandler) RollbackDeployment(c *gin.Context) {
224241 }
225242 if errors .Is (err , constants .ErrDeploymentAlreadyDeployed ) {
226243 c .JSON (http .StatusConflict , utils .NewErrorResponse (409 , "Conflict" ,
227- "Cannot rollback to currently deployed deployment" ))
244+ "Cannot restore currently deployed deployment" ))
245+ return
246+ }
247+ if errors .Is (err , constants .ErrGatewayIDMismatch ) {
248+ c .JSON (http .StatusBadRequest , utils .NewErrorResponse (400 , "Bad Request" ,
249+ "Deployment is bound to a different gateway" ))
228250 return
229251 }
230- log .Printf ("[ERROR] Failed to rollback deployment: apiId=%s deploymentId=%s error=%v" , apiId , deploymentId , err )
231- c .JSON (http .StatusInternalServerError , utils .NewErrorResponse (500 , "Internal Server Error" , "Failed to rollback deployment" ))
252+ log .Printf ("[ERROR] Failed to restore deployment: apiId=%s deploymentId=%s gatewayId=%s error=%v" , apiId , deploymentId , gatewayId , err )
253+ c .JSON (http .StatusInternalServerError , utils .NewErrorResponse (500 , "Internal Server Error" , "Failed to restore deployment" ))
232254 return
233255 }
234256
@@ -375,11 +397,11 @@ func (h *DeploymentHandler) GetDeployments(c *gin.Context) {
375397func (h * DeploymentHandler ) RegisterRoutes (r * gin.Engine ) {
376398 apiGroup := r .Group ("/api/v1/apis/:apiId" )
377399 {
378- apiGroup .POST ("/deploy" , h .DeployAPI )
379- apiGroup .POST ("/rollback-deployment" , h .RollbackDeployment )
400+ apiGroup .POST ("/deployments" , h .DeployAPI )
401+ apiGroup .POST ("/deployments/undeploy" , h .UndeployDeployment )
402+ apiGroup .POST ("/deployments/restore" , h .RestoreDeployment )
380403 apiGroup .GET ("/deployments" , h .GetDeployments )
381404 apiGroup .GET ("/deployments/:deploymentId" , h .GetDeployment )
382- apiGroup .POST ("/deployments/:deploymentId/undeploy" , h .UndeployDeployment )
383405 apiGroup .DELETE ("/deployments/:deploymentId" , h .DeleteDeployment )
384406 }
385407}
0 commit comments