-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement redeployment of missing deployments
- Loading branch information
1 parent
cbef36f
commit c8c045e
Showing
9 changed files
with
413 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* Copyright 2021 InfAI (CC SES) | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package api | ||
|
||
import ( | ||
"encoding/json" | ||
"github.com/SENERGY-Platform/process-sync/pkg/configuration" | ||
"github.com/SENERGY-Platform/process-sync/pkg/controller" | ||
"github.com/julienschmidt/httprouter" | ||
"log" | ||
"net/http" | ||
) | ||
|
||
func init() { | ||
endpoints = append(endpoints, SyncEndpoints) | ||
} | ||
|
||
func SyncEndpoints(config configuration.Config, ctrl *controller.Controller, router *httprouter.Router) { | ||
resource := "/sync" | ||
|
||
router.POST(resource+"/deployments/:networkId", func(writer http.ResponseWriter, request *http.Request, params httprouter.Params) { | ||
networkId := params.ByName("networkId") | ||
token, err, errCode := ctrl.ApiCheckAccessReturnToken(request, networkId, "a") | ||
if err != nil { | ||
http.Error(writer, err.Error(), errCode) | ||
return | ||
} | ||
err, errCode = ctrl.ApiSyncDeployments(token, networkId) | ||
if err != nil { | ||
http.Error(writer, err.Error(), errCode) | ||
return | ||
} | ||
writer.Header().Set("Content-Type", "application/json; charset=utf-8") | ||
err = json.NewEncoder(writer).Encode(true) | ||
if err != nil { | ||
log.Println("ERROR: unable to encode response", err) | ||
} | ||
return | ||
}) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/* | ||
* Copyright 2025 InfAI (CC SES) | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package controller | ||
|
||
import ( | ||
"errors" | ||
"github.com/SENERGY-Platform/process-sync/pkg/configuration" | ||
"github.com/SENERGY-Platform/process-sync/pkg/database" | ||
"github.com/SENERGY-Platform/process-sync/pkg/model" | ||
"github.com/SENERGY-Platform/process-sync/pkg/model/camundamodel" | ||
"net/http" | ||
) | ||
|
||
func (this *Controller) ApiSyncDeployments(token string, networkId string) (error, int) { | ||
err := this.db.RemovePlaceholderDeployments(networkId) | ||
if err != nil { | ||
return err, http.StatusInternalServerError | ||
} | ||
|
||
var limit int64 = 1000 | ||
var offset int64 = 0 | ||
errorList := []error{} | ||
for { | ||
deployments, err := this.db.ListDeployments([]string{networkId}, limit, offset, "id.asc") | ||
if err != nil { | ||
return err, http.StatusInternalServerError | ||
} | ||
for _, deployment := range deployments { | ||
if deployment.SyncInfo.MarkedForDelete { | ||
err = this.mgw.SendDeploymentDeleteCommand(networkId, deployment.Id) | ||
if err != nil { | ||
errorList = append(errorList, err) | ||
continue | ||
} | ||
} | ||
if deployment.SyncInfo.MarkedAsMissing { | ||
metadata, err := this.db.ReadDeploymentMetadata(networkId, deployment.Id) | ||
if err != nil && !errors.Is(err, database.ErrNotFound) { | ||
errorList = append(errorList, err) | ||
continue | ||
} | ||
if err != nil { | ||
continue | ||
} | ||
now := configuration.TimeNow() | ||
err = this.db.SaveDeployment(model.Deployment{ | ||
Deployment: camundamodel.Deployment{ | ||
Id: "placeholder-" + configuration.Id(), | ||
Name: deployment.Name, | ||
Source: "senergy", | ||
DeploymentTime: now, | ||
TenantId: "senergy", | ||
}, | ||
SyncInfo: model.SyncInfo{ | ||
NetworkId: networkId, | ||
IsPlaceholder: true, | ||
MarkedForDelete: false, | ||
SyncDate: now, | ||
}, | ||
}) | ||
if err != nil { | ||
errorList = append(errorList, err) | ||
continue | ||
} | ||
err = this.mgw.SendDeploymentCommand(networkId, metadata.DeploymentModel) | ||
if err != nil { | ||
errorList = append(errorList, err) | ||
continue | ||
} | ||
this.deleteDeployment(networkId, deployment.Id) | ||
} | ||
} | ||
if int64(len(deployments)) <= limit { | ||
err = errors.Join(errorList...) | ||
if err != nil { | ||
return err, http.StatusInternalServerError | ||
} | ||
return nil, http.StatusOK | ||
} | ||
offset += limit | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.