api: IntentionDelete + tests

This commit is contained in:
Mitchell Hashimoto 2018-05-11 22:19:21 -07:00
parent 272211e171
commit f27711a319
No known key found for this signature in database
GPG Key ID: 744E147AA52F5B0A
2 changed files with 33 additions and 2 deletions

View File

@ -154,7 +154,7 @@ func (h *Connect) Intentions(q *QueryOptions) ([]*Intention, *QueryMeta, error)
func (h *Connect) IntentionGet(id string, q *QueryOptions) (*Intention, *QueryMeta, error) {
r := h.c.newRequest("GET", "/v1/connect/intentions/"+id)
r.setQueryOptions(q)
rtt, resp, err := requireOK(h.c.doRequest(r))
rtt, resp, err := h.c.doRequest(r)
if err != nil {
return nil, nil, err
}
@ -164,6 +164,12 @@ func (h *Connect) IntentionGet(id string, q *QueryOptions) (*Intention, *QueryMe
parseQueryMeta(resp, qm)
qm.RequestTime = rtt
if resp.StatusCode == 404 {
return nil, qm, nil
} else if resp.StatusCode != 200 {
return nil, nil, fmt.Errorf("Unexpected response code: %d", resp.StatusCode)
}
var out Intention
if err := decodeBody(resp, &out); err != nil {
return nil, nil, err
@ -171,6 +177,22 @@ func (h *Connect) IntentionGet(id string, q *QueryOptions) (*Intention, *QueryMe
return &out, qm, nil
}
// IntentionDelete deletes a single intention.
func (h *Connect) IntentionDelete(id string, q *WriteOptions) (*WriteMeta, error) {
r := h.c.newRequest("DELETE", "/v1/connect/intentions/"+id)
r.setWriteOptions(q)
rtt, resp, err := requireOK(h.c.doRequest(r))
if err != nil {
return nil, err
}
defer resp.Body.Close()
qm := &WriteMeta{}
qm.RequestTime = rtt
return qm, nil
}
// IntentionMatch returns the list of intentions that match a given source
// or destination. The returned intentions are ordered by precedence where
// result[0] is the highest precedence (if that matches, then that rule overrides

View File

@ -6,7 +6,7 @@ import (
"github.com/stretchr/testify/require"
)
func TestAPI_ConnectIntentionCreateListGet(t *testing.T) {
func TestAPI_ConnectIntentionCreateListGetDelete(t *testing.T) {
t.Parallel()
require := require.New(t)
@ -38,6 +38,15 @@ func TestAPI_ConnectIntentionCreateListGet(t *testing.T) {
actual, _, err = connect.IntentionGet(id, nil)
require.Nil(err)
require.Equal(ixn, actual)
// Delete it
_, err = connect.IntentionDelete(id, nil)
require.Nil(err)
// Get it (should be gone)
actual, _, err = connect.IntentionGet(id, nil)
require.Nil(err)
require.Nil(actual)
}
func TestAPI_ConnectIntentionMatch(t *testing.T) {