diff --git a/configuration/config.go b/configuration/config.go index 17949a5..f7ed94b 100644 --- a/configuration/config.go +++ b/configuration/config.go @@ -86,6 +86,7 @@ func InitConfig() error { apiUpdateInterval = flag.String("api-update-interval", "10s" /* 10 sec */, "Interval in which API URL is queried for status updates of ICs") k8sRancherURL = flag.String("k8s-rancher-url", "https://rancher.k8s.eonerc.rwth-aachen.de", "URL of Rancher instance that is used to deploy the backend") k8sClusterName = flag.String("k8s-cluster-name", "local", "Name of the Kubernetes cluster where the backend is deployed") + staleICTime = flag.String("stale-ic-time", "1h" /* 1 hour */, "Time after which an IC is considered stale") ) flag.Parse() @@ -121,6 +122,7 @@ func InitConfig() error { "apiupdateinterval": *apiUpdateInterval, "k8s.rancher-url": *k8sRancherURL, "k8s.cluster-name": *k8sClusterName, + "staleictime": *staleICTime, } if *dbClear { diff --git a/routes/infrastructure-component/ic_endpoints.go b/routes/infrastructure-component/ic_endpoints.go index a14a6ac..86660b2 100644 --- a/routes/infrastructure-component/ic_endpoints.go +++ b/routes/infrastructure-component/ic_endpoints.go @@ -24,7 +24,9 @@ package infrastructure_component import ( "log" "net/http" + "time" + "git.rwth-aachen.de/acs/public/villas/web-backend-go/configuration" "git.rwth-aachen.de/acs/public/villas/web-backend-go/database" "git.rwth-aachen.de/acs/public/villas/web-backend-go/helper" "github.com/gin-gonic/gin" @@ -232,21 +234,27 @@ func deleteIC(c *gin.Context) { // Check if IC is managed externally if s.ManagedExternally { - // if so: refuse deletion - helper.BadRequestError(c, "delete for externally managed IC not possible with this endpoint - use /ic/{ICID}/action endpoint instead to request deletion of the component") - return + + staleICTime, _ := configuration.GlobalConfig.String("staleictime") + staleDuration, err := time.ParseDuration(staleICTime) + if err != nil { + helper.InternalServerError(c, "deleting externally managed IC not possible, no or erroneous stale IC time parameter provided in API config") + return + } + + // check if external IC is stale + if time.Now().Sub(s.UpdatedAt).Seconds() < staleDuration.Seconds() { + // IC is NOT stale, refuse deletion + helper.BadRequestError(c, "delete for externally managed non-stale IC not possible with this endpoint - use /ic/{ICID}/action endpoint instead to request deletion of the component") + return + } } // Delete the IC err := s.delete() - if helper.DBError(c, err) { - return - } else if err != nil { - helper.InternalServerError(c, "Unable to send delete action: "+err.Error()) - return + if !helper.DBError(c, err) { + c.JSON(http.StatusOK, gin.H{"ic": s.InfrastructureComponent}) } - - c.JSON(http.StatusOK, gin.H{"ic": s.InfrastructureComponent}) } // getConfigsOfIC godoc