-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #754 from ystia/feature/GH-753_Workflows_steps_rep…
…lays_on_error Workflows steps replays on error
- Loading branch information
Showing
8 changed files
with
171 additions
and
38 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
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,83 @@ | ||
// Copyright 2021 Bull S.A.S. Atos Technologies - Bull, Rue Jean Jaures, B.P.68, 78340, Les Clayes-sous-Bois, France. | ||
// | ||
// 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 tasks | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"net/http" | ||
"path" | ||
"strings" | ||
|
||
"github.com/spf13/cobra" | ||
|
||
"github.com/ystia/yorc/v4/commands/deployments" | ||
"github.com/ystia/yorc/v4/commands/httputil" | ||
"github.com/ystia/yorc/v4/tasks" | ||
) | ||
|
||
func init() { | ||
tasksCmd.AddCommand(updateTaskStepStateCmd) | ||
} | ||
|
||
func updateTaskStepState(deploymentID, taskID, stepName, statusStr string) { | ||
client, err := httputil.GetClient(deployments.ClientConfig) | ||
if err != nil { | ||
httputil.ErrExit(err) | ||
} | ||
|
||
statusStr = strings.ToLower(statusStr) | ||
|
||
// Safety check | ||
_, err = tasks.ParseTaskStepStatus(statusStr) | ||
if err != nil { | ||
httputil.ErrExit(err) | ||
} | ||
|
||
step := &tasks.TaskStep{Status: statusStr} | ||
body, err := json.Marshal(step) | ||
if err != nil { | ||
httputil.ErrExit(err) | ||
} | ||
|
||
url := path.Join("/deployments", deploymentID, "tasks", taskID, "steps", stepName) | ||
request, err := client.NewRequest("PUT", url, bytes.NewBuffer(body)) | ||
if err != nil { | ||
httputil.ErrExit(err) | ||
} | ||
|
||
request.Header.Add("Content-Type", "application/json") | ||
response, err := client.Do(request) | ||
if err != nil { | ||
httputil.ErrExit(err) | ||
} | ||
defer response.Body.Close() | ||
ids := deploymentID + "/" + taskID | ||
httputil.HandleHTTPStatusCode(response, ids, "deployment/task/step", http.StatusOK) | ||
} | ||
|
||
var updateTaskStepStateCmd = &cobra.Command{ | ||
Use: "update-step-state <DeploymentId> <TaskId> <StepName> <state>", | ||
Aliases: []string{"update-state", "update-step", "uss", "us"}, | ||
Short: "Update a deployment task step on error", | ||
Args: cobra.ExactArgs(4), | ||
Long: `Update a task step specifying the deployment id, the task id, the step name and a valid state for the step. | ||
The task step must be on error to be update. | ||
This allows to bypass or retry errors happening in a workflow. So accepted state are either DONE or INITIAL. | ||
Use 'yorc deployment tasks resume' command to restart a workflow after having updated it's errors`, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
updateTaskStepState(args[0], args[1], args[2], args[3]) | ||
}, | ||
} |
60 changes: 60 additions & 0 deletions
60
commands/deployments/tasks/dep_update_task_step_state_test.go
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,60 @@ | ||
// Copyright 2021 Bull S.A.S. Atos Technologies - Bull, Rue Jean Jaures, B.P.68, 78340, Les Clayes-sous-Bois, France. | ||
// | ||
// 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 tasks | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/ystia/yorc/v4/commands/deployments" | ||
"github.com/ystia/yorc/v4/config" | ||
) | ||
|
||
func Test_updateTaskStepState(t *testing.T) { | ||
|
||
s := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { | ||
if strings.Contains(r.URL.Path, "fail") { | ||
rw.WriteHeader(http.StatusBadRequest) | ||
rw.Write([]byte("Some user error")) | ||
return | ||
} | ||
rw.WriteHeader(http.StatusOK) | ||
})) | ||
defer s.Close() | ||
|
||
deployments.ClientConfig = config.Client{ | ||
YorcAPI: strings.Replace(s.URL, "http://", "", 1), | ||
} | ||
|
||
type args struct { | ||
deploymentID string | ||
taskID string | ||
stepName string | ||
statusStr string | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
}{ | ||
{"TestOK", args{"depOK", "taskID", "step", "initial"}}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
updateTaskStepState(tt.args.deploymentID, tt.args.taskID, tt.args.stepName, tt.args.statusStr) | ||
}) | ||
} | ||
} |
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