Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ReplaceSiteListByID method #12

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -2003,6 +2009,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
Expand Down Expand Up @@ -2053,6 +2065,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), "")
Expand Down
33 changes: 32 additions & 1 deletion api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down