Skip to content

Commit

Permalink
Adds implementation and test for API: /approvaltask/{approvalTaskName}
Browse files Browse the repository at this point in the history
- This patch adds an API with POST method to update the approval task
  status

- The request body will have
     {
       "namespace": “<namespaceName>”,
       "approved": “true/false”,
     },

and will return name and the status of the approval task

Signed-off-by: Puneet Punamiya ppunamiy@redhat.com
  • Loading branch information
PuneetPunamiya committed Aug 22, 2023
1 parent 171819b commit bcbecd5
Show file tree
Hide file tree
Showing 5 changed files with 408 additions and 1 deletion.
135 changes: 135 additions & 0 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions cmd/approver/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ func main() {
handlers.ListApprovalTask(w, r, dynamicClient)
})

r.Post("/approvaltask/{approvalTaskName}", func(w http.ResponseWriter, r *http.Request) {
handlers.UpdateApprovalTask(w, r, dynamicClient)
})

// Bind to a port and pass our router in
log.Fatal(http.ListenAndServe(":8000", r))
}
6 changes: 5 additions & 1 deletion pkg/handlers/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,14 @@ var (

type ApprovalTask struct {
Name string `json:"name"`
Namespace string `json:"namespace"`
Namespace string `json:"namespace,omitempty"`
Approved bool `json:"approved"`
}

type ApprovalTaskList struct {
Data []ApprovalTask `json:"data"`
}

type ApprovalTaskResult struct {
Data ApprovalTask `json:"data"`
}
81 changes: 81 additions & 0 deletions pkg/handlers/update.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
Copyright 2023 The OpenShift Pipelines Authors
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 handlers

import (
"context"
"encoding/json"
"net/http"

"github.com/go-chi/chi/v5"
"github.com/openshift-pipelines/manual-approval-gate/pkg/apis/approvaltask/v1alpha1"
"github.com/openshift-pipelines/manual-approval-gate/pkg/handlers/app"
kErr "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/dynamic"
)

func UpdateApprovalTask(res http.ResponseWriter, req *http.Request, dynamicClient dynamic.Interface) {
approvalTaskName := chi.URLParam(req, "approvalTaskName")

var requestBody app.ApprovalTask
decoder := json.NewDecoder(req.Body)
err := decoder.Decode(&requestBody)
if err != nil {
http.Error(res, "Invalid request body", http.StatusBadRequest)
return
}

customResource, err := dynamicClient.Resource(app.CustomResourceGVR).Namespace(requestBody.Namespace).Get(context.TODO(), approvalTaskName, metav1.GetOptions{})
if err != nil {
if kErr.IsNotFound(err) {
http.Error(res, "No resource found", http.StatusInternalServerError)
return
} else {
http.Error(res, err.Error(), http.StatusInternalServerError)
return
}
}

at := &v1alpha1.ApprovalTask{}
err = runtime.DefaultUnstructuredConverter.FromUnstructured(customResource.Object, at)
if err != nil {
http.Error(res, err.Error(), http.StatusInternalServerError)
return
}
at.Spec.Approved = requestBody.Approved

_, err = dynamicClient.Resource(app.CustomResourceGVR).Namespace(at.Namespace).Update(context.TODO(), customResource, metav1.UpdateOptions{})
if err != nil {
http.Error(res, err.Error(), http.StatusInternalServerError)
return
}

var approvalTaskStatus = &app.ApprovalTaskResult{
Data: app.ApprovalTask{
Name: approvalTaskName,
Approved: at.Spec.Approved,
},
}

res.WriteHeader(http.StatusOK)
if err := json.NewEncoder(res).Encode(approvalTaskStatus); err != nil {
http.Error(res, err.Error(), http.StatusInternalServerError)
return
}
}
Loading

0 comments on commit bcbecd5

Please sign in to comment.