|
| 1 | +// Copyright 2025 The Casibase Authors. All Rights Reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package controllers |
| 16 | + |
| 17 | +import ( |
| 18 | + "encoding/json" |
| 19 | + |
| 20 | + "github.com/casibase/casibase/object" |
| 21 | + "github.com/casibase/casibase/util" |
| 22 | +) |
| 23 | + |
| 24 | +type ApplicationDeploymentRequest struct { |
| 25 | + Owner string `json:"owner"` |
| 26 | + Name string `json:"name"` |
| 27 | + DisplayName string `json:"displayName"` |
| 28 | + Template string `json:"template"` |
| 29 | + Parameters string `json:"parameters"` |
| 30 | +} |
| 31 | + |
| 32 | +// GetApplications |
| 33 | +// @Title GetApplications |
| 34 | +// @Tag Application API |
| 35 | +// @Description get applications |
| 36 | +// @Param owner query string true "The owner of applications" |
| 37 | +// @Success 200 {array} object.Application The Response object |
| 38 | +// @router /get-applications [get] |
| 39 | +func (c *ApiController) GetApplications() { |
| 40 | + owner := c.Input().Get("owner") |
| 41 | + |
| 42 | + res, err := object.GetApplications(owner) |
| 43 | + if err != nil { |
| 44 | + c.ResponseError(err.Error()) |
| 45 | + return |
| 46 | + } |
| 47 | + |
| 48 | + c.ResponseOk(res) |
| 49 | +} |
| 50 | + |
| 51 | +// GetApplication |
| 52 | +// @Title GetApplication |
| 53 | +// @Tag Application API |
| 54 | +// @Description get application |
| 55 | +// @Param id query string true "The id of application" |
| 56 | +// @Success 200 {object} object.Application The Response object |
| 57 | +// @router /get-application [get] |
| 58 | +func (c *ApiController) GetApplication() { |
| 59 | + id := c.Input().Get("id") |
| 60 | + |
| 61 | + res, err := object.GetApplication(util.GetOwnerAndNameFromId(id)) |
| 62 | + if err != nil { |
| 63 | + c.ResponseError(err.Error()) |
| 64 | + return |
| 65 | + } |
| 66 | + |
| 67 | + c.ResponseOk(res) |
| 68 | +} |
| 69 | + |
| 70 | +// UpdateApplication |
| 71 | +// @Title UpdateApplication |
| 72 | +// @Tag Application API |
| 73 | +// @Description update application |
| 74 | +// @Param id query string true "The id (owner/name) of the application" |
| 75 | +// @Param body body object.Application true "The details of the application" |
| 76 | +// @Success 200 {object} controllers.Response The Response object |
| 77 | +// @router /update-application [post] |
| 78 | +func (c *ApiController) UpdateApplication() { |
| 79 | + id := c.Input().Get("id") |
| 80 | + |
| 81 | + var application object.Application |
| 82 | + err := json.Unmarshal(c.Ctx.Input.RequestBody, &application) |
| 83 | + if err != nil { |
| 84 | + c.ResponseError(err.Error()) |
| 85 | + return |
| 86 | + } |
| 87 | + |
| 88 | + success, err := object.UpdateApplication(id, &application) |
| 89 | + if err != nil { |
| 90 | + c.ResponseError(err.Error()) |
| 91 | + return |
| 92 | + } |
| 93 | + |
| 94 | + c.ResponseOk(success) |
| 95 | +} |
| 96 | + |
| 97 | +// AddApplication |
| 98 | +// @Title AddApplication |
| 99 | +// @Tag Application API |
| 100 | +// @Description add application |
| 101 | +// @Param body body object.Application true "The details of the application" |
| 102 | +// @Success 200 {object} controllers.Response The Response object |
| 103 | +// @router /add-application [post] |
| 104 | +func (c *ApiController) AddApplication() { |
| 105 | + var req ApplicationDeploymentRequest |
| 106 | + err := json.Unmarshal(c.Ctx.Input.RequestBody, &req) |
| 107 | + if err != nil { |
| 108 | + c.ResponseError(err.Error()) |
| 109 | + return |
| 110 | + } |
| 111 | + |
| 112 | + if req.Owner == "" || req.Name == "" || req.Template == "" { |
| 113 | + c.ResponseError("Missing required parameters") |
| 114 | + return |
| 115 | + } |
| 116 | + |
| 117 | + // Verify template exists |
| 118 | + template, err := object.GetTemplate(req.Owner, req.Template) |
| 119 | + if err != nil { |
| 120 | + c.ResponseError(err.Error()) |
| 121 | + return |
| 122 | + } |
| 123 | + |
| 124 | + if template == nil { |
| 125 | + c.ResponseError("The Template not found") |
| 126 | + return |
| 127 | + } |
| 128 | + |
| 129 | + // Create new application |
| 130 | + application := &object.Application{ |
| 131 | + Owner: req.Owner, |
| 132 | + Name: req.Name, |
| 133 | + DisplayName: req.DisplayName, |
| 134 | + Template: req.Template, |
| 135 | + Parameters: req.Parameters, |
| 136 | + Status: "Not Deployed", |
| 137 | + } |
| 138 | + |
| 139 | + success, err := object.AddApplication(application) |
| 140 | + if err != nil { |
| 141 | + c.ResponseError(err.Error()) |
| 142 | + return |
| 143 | + } |
| 144 | + |
| 145 | + c.ResponseOk(success) |
| 146 | +} |
| 147 | + |
| 148 | +// DeleteApplication |
| 149 | +// @Title DeleteApplication |
| 150 | +// @Tag Application API |
| 151 | +// @Description delete application |
| 152 | +// @Param body body object.Application true "The details of the application" |
| 153 | +// @Success 200 {object} controllers.Response The Response object |
| 154 | +// @router /delete-application [post] |
| 155 | +func (c *ApiController) DeleteApplication() { |
| 156 | + var application object.Application |
| 157 | + err := json.Unmarshal(c.Ctx.Input.RequestBody, &application) |
| 158 | + if err != nil { |
| 159 | + c.ResponseError(err.Error()) |
| 160 | + return |
| 161 | + } |
| 162 | + |
| 163 | + success, err := object.DeleteApplication(application.Owner, application.Name) |
| 164 | + if err != nil { |
| 165 | + c.ResponseError(err.Error()) |
| 166 | + return |
| 167 | + } |
| 168 | + |
| 169 | + c.ResponseOk(success) |
| 170 | +} |
| 171 | + |
| 172 | +// DeployApplication |
| 173 | +// @Title DeployApplication |
| 174 | +// @Tag Application API |
| 175 | +// @Description deploy application |
| 176 | +// @Param body body ApplicationDeploymentRequest true "The deployment request details" |
| 177 | +// @Success 200 {object} controllers.Response The Response object |
| 178 | +// @router /deploy-application [post] |
| 179 | +func (c *ApiController) DeployApplication() { |
| 180 | + var req ApplicationDeploymentRequest |
| 181 | + err := json.Unmarshal(c.Ctx.Input.RequestBody, &req) |
| 182 | + if err != nil { |
| 183 | + c.ResponseError(err.Error()) |
| 184 | + return |
| 185 | + } |
| 186 | + |
| 187 | + if req.Owner == "" || req.Name == "" || req.Template == "" { |
| 188 | + c.ResponseError("Missing required parameters") |
| 189 | + return |
| 190 | + } |
| 191 | + |
| 192 | + // Get the existing application |
| 193 | + application, err := object.GetApplication(req.Owner, req.Name) |
| 194 | + if err != nil { |
| 195 | + c.ResponseError(err.Error()) |
| 196 | + return |
| 197 | + } |
| 198 | + |
| 199 | + // Application must exist before deployment |
| 200 | + if application == nil { |
| 201 | + c.ResponseError("Application not found. Please create the application first.") |
| 202 | + return |
| 203 | + } |
| 204 | + |
| 205 | + // Update application with new template and parameters |
| 206 | + application.Template = req.Template |
| 207 | + application.Parameters = req.Parameters |
| 208 | + |
| 209 | + success, err := object.UpdateApplication(req.Owner+"/"+req.Name, application) |
| 210 | + if err != nil { |
| 211 | + c.ResponseError(err.Error()) |
| 212 | + return |
| 213 | + } |
| 214 | + if !success { |
| 215 | + c.ResponseError("Failed to update application") |
| 216 | + return |
| 217 | + } |
| 218 | + |
| 219 | + // Deploy the application |
| 220 | + success, err = object.DeployApplication(application) |
| 221 | + if err != nil { |
| 222 | + c.ResponseError(err.Error()) |
| 223 | + return |
| 224 | + } |
| 225 | + |
| 226 | + c.ResponseOk(success) |
| 227 | +} |
| 228 | + |
| 229 | +// UndeployApplication |
| 230 | +// @Title UndeployApplication |
| 231 | +// @Tag Application API |
| 232 | +// @Description delete application deployment |
| 233 | +// @Param body body ApplicationDeploymentRequest true "The deployment request details" |
| 234 | +// @Success 200 {object} controllers.Response The Response object |
| 235 | +// @router /undeploy-application [post] |
| 236 | +func (c *ApiController) UndeployApplication() { |
| 237 | + var req ApplicationDeploymentRequest |
| 238 | + err := json.Unmarshal(c.Ctx.Input.RequestBody, &req) |
| 239 | + if err != nil { |
| 240 | + c.ResponseError(err.Error()) |
| 241 | + return |
| 242 | + } |
| 243 | + |
| 244 | + if req.Owner == "" || req.Name == "" { |
| 245 | + c.ResponseError("Missing required parameters") |
| 246 | + return |
| 247 | + } |
| 248 | + |
| 249 | + success, err := object.UndeployApplication(req.Owner, req.Name) |
| 250 | + if err != nil { |
| 251 | + c.ResponseError(err.Error()) |
| 252 | + return |
| 253 | + } |
| 254 | + |
| 255 | + c.ResponseOk(success) |
| 256 | +} |
| 257 | + |
| 258 | +// GetApplicationStatus |
| 259 | +// @Title GetApplicationStatus |
| 260 | +// @Tag Application API |
| 261 | +// @Description get application deployment status |
| 262 | +// @Param id query string true "The id (owner/name) of the application" |
| 263 | +// @Success 200 {object} object.DeploymentStatus The Response object |
| 264 | +// @router /get-application-status [get] |
| 265 | +func (c *ApiController) GetApplicationStatus() { |
| 266 | + id := c.Input().Get("id") |
| 267 | + owner, name := util.GetOwnerAndNameFromId(id) |
| 268 | + |
| 269 | + status, err := object.GetApplicationStatus(owner, name) |
| 270 | + if err != nil { |
| 271 | + c.ResponseError(err.Error()) |
| 272 | + return |
| 273 | + } |
| 274 | + |
| 275 | + c.ResponseOk(status) |
| 276 | +} |
0 commit comments