Skip to content

Commit d4664e3

Browse files
committed
feat(ps): support delete pods
1 parent 743c94c commit d4664e3

3 files changed

Lines changed: 57 additions & 0 deletions

File tree

api/ps.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,7 @@ type ContainerState struct {
7272

7373
// PodState defines a collection of container state.
7474
type PodState []ContainerState
75+
76+
type PodIDs struct {
77+
PodIDs string `json:"pod_ids"`
78+
}

ps/ps.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,23 @@ func Describe(c *drycc.Client, appID string, podID string, results int) (api.Pod
9999
return podState, count, reqErr
100100
}
101101

102+
// delete a pod
103+
func Delete(c *drycc.Client, appID string, podIDs string) error {
104+
u := fmt.Sprintf("/v2/apps/%s/pods/", appID)
105+
106+
req := api.PodIDs{PodIDs: podIDs}
107+
108+
body, err := json.Marshal(req)
109+
if err != nil {
110+
return err
111+
}
112+
res, err := c.Request("DELETE", u, body)
113+
if err == nil {
114+
res.Body.Close()
115+
}
116+
return err
117+
}
118+
102119
// ByType organizes processes of an app by process type.
103120
func ByType(processes api.PodsList) api.PodTypes {
104121
var pts api.PodTypes

ps/ps_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ const podStateFixture string = `
6565
}]
6666
}`
6767

68+
const podDeleteExpected string = `{"pod_ids":"test-pod-web"}`
69+
6870
type fakeHTTPServer struct{}
6971

7072
func (fakeHTTPServer) ServeHTTP(res http.ResponseWriter, req *http.Request) {
@@ -79,6 +81,23 @@ func (fakeHTTPServer) ServeHTTP(res http.ResponseWriter, req *http.Request) {
7981
res.Write([]byte(podStateFixture))
8082
return
8183
}
84+
if req.URL.Path == "/v2/apps/example-go/pods/" && req.Method == "DELETE" {
85+
body, err := io.ReadAll(req.Body)
86+
87+
if err != nil {
88+
fmt.Println(err)
89+
res.WriteHeader(http.StatusInternalServerError)
90+
res.Write(nil)
91+
}
92+
if string(body) != podDeleteExpected {
93+
fmt.Printf("Expected '%s', Got '%s'\n", podDeleteExpected, body)
94+
res.WriteHeader(http.StatusInternalServerError)
95+
res.Write(nil)
96+
return
97+
}
98+
res.WriteHeader(http.StatusNoContent)
99+
return
100+
}
82101

83102
fmt.Printf("Unrecognized URL %s\n", req.URL)
84103
res.WriteHeader(http.StatusNotFound)
@@ -301,3 +320,20 @@ func TestByType(t *testing.T) {
301320
t.Errorf("Expected: %v, Got %v", expected, actual)
302321
}
303322
}
323+
324+
func TestPodsDelete(t *testing.T) {
325+
t.Parallel()
326+
327+
handler := fakeHTTPServer{}
328+
server := httptest.NewServer(handler)
329+
defer server.Close()
330+
331+
drycc, err := drycc.New(false, server.URL, "abc")
332+
if err != nil {
333+
t.Fatal(err)
334+
}
335+
336+
if err = Delete(drycc, "example-go", "test-pod-web"); err != nil {
337+
t.Fatal(err)
338+
}
339+
}

0 commit comments

Comments
 (0)