From 2df1dea0db91cf464b8ab7d495cd69d96737ae24 Mon Sep 17 00:00:00 2001 From: Jake Barba Date: Wed, 18 Nov 2020 11:04:06 -0700 Subject: [PATCH 1/3] Add ReplaceSiteListByID method --- api.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/api.go b/api.go index 51b27d3..394ce8f 100644 --- a/api.go +++ b/api.go @@ -2003,6 +2003,12 @@ type Entries struct { Deletions []string `json:"deletions,omitempty"` // List deletions } +// ReplaceListBody replace list +type ReplaceListBody struct { + Description string `json:"description,omitempty"` //Optional list description + Entries []string `json:"entries,omitempty"` //List entries +} + // ResponseListBody contains the response from creating the list type ResponseListBody struct { CreateListBody @@ -2053,6 +2059,20 @@ func (sc *Client) UpdateSiteListByID(corpName, siteName string, id string, body return getResponseListBody(resp) } +// ReplaceSiteListByID replaces a site list and returns a response +func (sc *Client) ReplaceSiteListByID(corpName, siteName string, id string, body ReplaceListBody) (ResponseListBody, error) { + b, err := json.Marshal(body) + if err != nil { + return ResponseListBody{}, err + } + resp, err := sc.doRequest("PUT", fmt.Sprintf("/v0/corps/%s/sites/%s/lists/%s", corpName, siteName, id), string(b)) + if err != nil { + return ResponseListBody{}, err + } + return getResponseListBody(resp) +} + + // DeleteSiteListByID deletes a rule and returns an error func (sc *Client) DeleteSiteListByID(corpName, siteName string, id string) error { _, err := sc.doRequest("DELETE", fmt.Sprintf("/v0/corps/%s/sites/%s/lists/%s", corpName, siteName, id), "") From 80686e5a9336c8f3d1ca86a3d241ebee1cc40654 Mon Sep 17 00:00:00 2001 From: Jake Barba Date: Wed, 18 Nov 2020 11:48:23 -0700 Subject: [PATCH 2/3] Add tests for replace --- api_test.go | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/api_test.go b/api_test.go index 84862be..29b3fe1 100644 --- a/api_test.go +++ b/api_test.go @@ -352,7 +352,7 @@ func compareSiteListBody(sl1, sl2 CreateListBody) bool { return true } -func TestCreateReadUpdateDeleteSiteList(t *testing.T) { +func TestCreateReadUpdateReplaceDeleteSiteList(t *testing.T) { sc := NewTokenClient(testcreds.email, testcreds.token) corp := testcreds.corp site := testcreds.site @@ -411,6 +411,37 @@ func TestCreateReadUpdateDeleteSiteList(t *testing.T) { if !reflect.DeepEqual(updatedSiteListBody, readall.Data[0].CreateListBody) { t.Error("Site list body not equal") } + + replaceSiteListBody := ReplaceListBody{ + Description: "Some IPs we using to replace a list", + Entries: []string{"3.4.5.6", "4.5.6.7"}, + } + + replaceresp, err := sc.ReplaceSiteListByID(corp, site, readresp.ID, replaceSiteListBody) + if err != nil { + t.Fatal(err) + } + + replacedSiteListBody := CreateListBody{ + Name: "My new list", + Type: "ip", + Description: "Some IPs we using to replace a list", + Entries: []string{ + "3.4.5.6", + "4.5.6.7", + }, + } + if !reflect.DeepEqual(replacedSiteListBody, replaceresp.CreateListBody) { + t.Error("Site list body not equal") + } + readall, err = sc.GetAllSiteLists(corp, site) + if len(readall.Data) != 1 { + t.Error() + } + if !reflect.DeepEqual(replacedSiteListBody, readall.Data[0].CreateListBody) { + t.Error("Site list body not equal") + } + err = sc.DeleteSiteListByID(corp, site, readresp.ID) if err != nil { t.Fatal(err) From 027a85d058a5824446396df004ce7a51d4829c7e Mon Sep 17 00:00:00 2001 From: Jake Barba Date: Fri, 20 Nov 2020 16:41:14 -0700 Subject: [PATCH 3/3] Implement PUT call in doRequest client --- api.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/api.go b/api.go index 394ce8f..5340dc6 100644 --- a/api.go +++ b/api.go @@ -104,6 +104,12 @@ func (sc *Client) doRequest(method, url, reqBody string) ([]byte, error) { if resp.StatusCode != http.StatusOK { return body, errMsg(body) } + case "PUT": + switch resp.StatusCode { + case http.StatusOK: + default: + return body, errMsg(body) + } case "POST": switch resp.StatusCode { case http.StatusOK, http.StatusCreated, http.StatusNoContent: