Skip to content

Commit 79c9e0f

Browse files
author
jmccormick2001
committed
add --all support to pgo delete policy and pgo show policy commands, change GET to POST in API
1 parent 3686824 commit 79c9e0f

File tree

6 files changed

+38
-15
lines changed

6 files changed

+38
-15
lines changed

apiserver.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ func main() {
9090
r.HandleFunc("/policies", policyservice.CreatePolicyHandler)
9191
r.HandleFunc("/showpolicies", policyservice.ShowPolicyHandler).Methods("POST")
9292
//here
93-
r.HandleFunc("/policiesdelete/{name}", policyservice.DeletePolicyHandler).Methods("GET")
93+
r.HandleFunc("/policiesdelete", policyservice.DeletePolicyHandler).Methods("POST")
9494
r.HandleFunc("/workflow/{id}", workflowservice.ShowWorkflowHandler).Methods("GET")
9595
r.HandleFunc("/showpvc", pvcservice.ShowPVCHandler).Methods("POST")
9696
r.HandleFunc("/policies/apply", policyservice.ApplyPolicyHandler).Methods("POST")

apiserver/policyservice/policyservice.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import (
1919
"encoding/json"
2020
apiserver "github.com/crunchydata/postgres-operator/apiserver"
2121
msgs "github.com/crunchydata/postgres-operator/apiservermsgs"
22-
"github.com/gorilla/mux"
2322
log "github.com/sirupsen/logrus"
2423
"k8s.io/apimachinery/pkg/util/validation"
2524
"net/http"
@@ -85,11 +84,13 @@ func CreatePolicyHandler(w http.ResponseWriter, r *http.Request) {
8584
// returns a DeletePolicyResponse
8685
func DeletePolicyHandler(w http.ResponseWriter, r *http.Request) {
8786
var ns string
88-
vars := mux.Vars(r)
8987

90-
policyname := vars["name"]
91-
clientVersion := r.URL.Query().Get("version")
92-
namespace := r.URL.Query().Get("namespace")
88+
var request msgs.DeletePolicyRequest
89+
_ = json.NewDecoder(r.Body).Decode(&request)
90+
91+
policyname := request.PolicyName
92+
clientVersion := request.ClientVersion
93+
namespace := request.Namespace
9394

9495
log.Debugf("DeletePolicyHandler parameters version [%s] name [%s] namespace [%s]", clientVersion, policyname, namespace)
9596

apiservermsgs/policymsgs.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,15 @@ type ShowPolicyResponse struct {
6868
Status
6969
}
7070

71+
// DeletePolicyRequest ...
72+
type DeletePolicyRequest struct {
73+
Selector string
74+
Namespace string
75+
AllFlag bool
76+
ClientVersion string
77+
PolicyName string
78+
}
79+
7180
// DeletePolicyResponse ...
7281
type DeletePolicyResponse struct {
7382
Results []string

pgo/api/policy.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -104,21 +104,22 @@ func CreatePolicy(httpclient *http.Client, SessionCredentials *msgs.BasicAuthCre
104104
return resp, err
105105
}
106106

107-
func DeletePolicy(httpclient *http.Client, arg string, SessionCredentials *msgs.BasicAuthCredentials, ns string) (msgs.DeletePolicyResponse, error) {
107+
func DeletePolicy(httpclient *http.Client, request *msgs.DeletePolicyRequest, SessionCredentials *msgs.BasicAuthCredentials) (msgs.DeletePolicyResponse, error) {
108108

109109
var response msgs.DeletePolicyResponse
110110

111-
url := SessionCredentials.APIServerURL + "/policiesdelete/" + arg + "?version=" + msgs.PGO_VERSION + "&namespace=" + ns
111+
url := SessionCredentials.APIServerURL + "/policiesdelete"
112112

113113
log.Debugf("delete policy called [%s]", url)
114114

115-
action := "GET"
116-
117-
req, err := http.NewRequest(action, url, nil)
115+
action := "POST"
116+
jsonValue, _ := json.Marshal(request)
117+
req, err := http.NewRequest(action, url, bytes.NewBuffer(jsonValue))
118118
if err != nil {
119+
response.Status.Code = msgs.Error
119120
return response, err
120121
}
121-
122+
req.Header.Set("Content-Type", "application/json")
122123
req.SetBasicAuth(SessionCredentials.Username, SessionCredentials.Password)
123124

124125
resp, err := httpclient.Do(req)

pgo/cmd/delete.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ func init() {
107107
deletePgbouncerCmd.Flags().StringVarP(&Selector, "selector", "s", "", "The selector to use for cluster filtering.")
108108
deletePgpoolCmd.Flags().StringVarP(&Selector, "selector", "s", "", "The selector to use for cluster filtering.")
109109
deletePolicyCmd.Flags().BoolVar(&NoPrompt, "no-prompt", false, "No command line confirmation.")
110+
deletePolicyCmd.Flags().BoolVar(&AllFlag, "all", false, "all resources.")
110111
deleteScheduleCmd.Flags().StringVarP(&Selector, "selector", "s", "", "The selector to use for cluster filtering.")
111112
deleteScheduleCmd.Flags().StringVarP(&ScheduleName, "schedule-name", "", "", "The name of the schedule to delete.")
112113
deleteScheduleCmd.Flags().BoolVar(&NoPrompt, "no-prompt", false, "No command line confirmation.")
@@ -197,8 +198,8 @@ var deletePolicyCmd = &cobra.Command{
197198
if Namespace == "" {
198199
Namespace = PGONamespace
199200
}
200-
if len(args) == 0 {
201-
fmt.Println("Error: A policy name is required for this command.")
201+
if len(args) == 0 && !AllFlag {
202+
fmt.Println("Error: A policy name or --all is required for this command.")
202203
} else {
203204
if util.AskForConfirmation(NoPrompt, "") {
204205
deletePolicy(args, Namespace)

pgo/cmd/policy.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,10 +208,21 @@ func deletePolicy(args []string, ns string) {
208208

209209
log.Debugf("deletePolicy called %v", args)
210210

211+
r := msgs.DeletePolicyRequest{}
212+
r.Selector = Selector
213+
r.AllFlag = AllFlag
214+
r.ClientVersion = msgs.PGO_VERSION
215+
r.Namespace = ns
216+
if AllFlag {
217+
args = make([]string, 1)
218+
args[0] = "all"
219+
}
220+
211221
for _, arg := range args {
222+
r.PolicyName = arg
212223
log.Debugf("deleting policy %s", arg)
213224

214-
response, err := api.DeletePolicy(httpclient, arg, &SessionCredentials, ns)
225+
response, err := api.DeletePolicy(httpclient, &r, &SessionCredentials)
215226
if err != nil {
216227
fmt.Println("Error: " + err.Error())
217228
}

0 commit comments

Comments
 (0)