forked from ns1/ns1-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a75e0b3
commit 1138b90
Showing
3 changed files
with
322 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package rest | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"gopkg.in/ns1/ns1-go.v2/rest/model/dhcp" | ||
"net/http" | ||
) | ||
|
||
// OptionDefService handles the 'scope group' endpoints. | ||
type OptionDefService service | ||
|
||
// List returns a list of all option definitions. | ||
// | ||
// NS1 API docs: https://ns1.com/api#getlist-dhcp-option-definitions | ||
func (s *OptionDefService) List() ([]dhcp.OptionDef, *http.Response, error) { | ||
req, err := s.client.NewRequest(http.MethodGet, "dhcp/optiondef", nil) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
ods := make([]dhcp.OptionDef, 0) | ||
resp, err := s.client.Do(req, &ods) | ||
return ods, resp, err | ||
} | ||
|
||
// Get returns the option definition corresponding to the provided ID. | ||
// | ||
// NS1 API docs: https://ns1.com/api#getview-dhcp-option-definition | ||
func (s *OptionDefService) Get(odSpace, odKey string) (*dhcp.OptionDef, *http.Response, error) { | ||
reqPath := fmt.Sprintf("dhcp/optiondef/%s/%s", odSpace, odKey) | ||
req, err := s.client.NewRequest(http.MethodGet, reqPath, nil) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
od := &dhcp.OptionDef{} | ||
var resp *http.Response | ||
resp, err = s.client.Do(req, od) | ||
if err != nil { | ||
return nil, resp, err | ||
} | ||
|
||
return od, resp, nil | ||
} | ||
|
||
// Create creates or updates an option definition. | ||
// The FriendlyName, Description, Code, Schema.Type fields are required. | ||
// | ||
// NS1 API docs: https://ns1.com/api#putcreate-an-custom-dhcp-option-definition | ||
func (s *OptionDefService) Create(od *dhcp.OptionDef, odSpace, odKey string) (*dhcp.OptionDef, *http.Response, error) { | ||
switch { | ||
case od.FriendlyName == "": | ||
return nil, nil, errors.New("the FriendlyName field is required") | ||
case od.Description == "": | ||
return nil, nil, errors.New("the Description field is required") | ||
case od.Code == 0: | ||
return nil, nil, errors.New("the Code field is required") | ||
case od.Schema.Type == "": | ||
return nil, nil, errors.New("the Schema.Type field is required") | ||
} | ||
|
||
reqPath := fmt.Sprintf("dhcp/optiondef/%s/%s", odSpace, odKey) | ||
req, err := s.client.NewRequest(http.MethodPut, reqPath, od) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
respOd := new(dhcp.OptionDef) | ||
var resp *http.Response | ||
resp, err = s.client.Do(req, respOd) | ||
if err != nil { | ||
return nil, resp, err | ||
} | ||
|
||
return respOd, resp, nil | ||
} | ||
|
||
// Delete removes a option definition entirely. | ||
// | ||
// NS1 API docs: https://ns1.com/api#deletedelete-a-custom-dhcp-option-definition | ||
func (s *OptionDefService) Delete(odSpace, odKey string) (*http.Response, error) { | ||
reqPath := fmt.Sprintf("dhcp/optiondef/%s/%s", odSpace, odKey) | ||
req, err := s.client.NewRequest(http.MethodDelete, reqPath, nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return s.client.Do(req, nil) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
package rest_test | ||
|
||
import ( | ||
"gopkg.in/ns1/ns1-go.v2/rest/model/dhcp" | ||
"net/http" | ||
"testing" | ||
|
||
"gopkg.in/ns1/ns1-go.v2/mockns1" | ||
api "gopkg.in/ns1/ns1-go.v2/rest" | ||
) | ||
|
||
func TestDHCPOptionDef(t *testing.T) { | ||
mock, doer, err := mockns1.New(t) | ||
if err != nil { | ||
t.Fatalf("Error creating mock service: %v", err) | ||
} | ||
defer mock.Shutdown() | ||
|
||
client := api.NewClient(doer, api.SetEndpoint("https://"+mock.Address+"/v1/")) | ||
|
||
t.Run("List", func(t *testing.T) { | ||
defer mock.ClearTestCases() | ||
|
||
client.FollowPagination = true | ||
sgs := []dhcp.OptionDef{ | ||
{ | ||
Code: 1, | ||
Description: "a", | ||
FriendlyName: "a", | ||
Schema: dhcp.OptionDefSchema{ | ||
Type: dhcp.SchemaTypeString, | ||
}, | ||
}, | ||
{ | ||
Code: 2, | ||
Description: "b", | ||
FriendlyName: "b", | ||
Schema: dhcp.OptionDefSchema{ | ||
Type: dhcp.SchemaTypeString, | ||
}, | ||
}, | ||
} | ||
err := mock.AddTestCase(http.MethodGet, "/dhcp/optiondef", http.StatusOK, nil, nil, "", sgs) | ||
if err != nil { | ||
t.Fatalf("error adding test case: %v", err) | ||
} | ||
|
||
respSgs, _, err := client.OptionDef.List() | ||
if err != nil { | ||
t.Fatalf("error listing DHCP option definitions: %v", err) | ||
} | ||
if len(respSgs) != len(sgs) { | ||
t.Errorf("wrong length: want=%d, got=%d", len(sgs), len(respSgs)) | ||
} | ||
|
||
for i, sg := range respSgs { | ||
if sg.FriendlyName != sgs[i].FriendlyName || sg.Code != sgs[i].Code || | ||
sg.Description != sgs[i].Description || sg.Schema.Type != sgs[i].Schema.Type { | ||
t.Errorf("Incorrect data for option definition %d: want=%+v, got=%+v", i, sgs[i], sg) | ||
} | ||
} | ||
}) | ||
|
||
t.Run("Get", func(t *testing.T) { | ||
defer mock.ClearTestCases() | ||
|
||
client.FollowPagination = false | ||
od := dhcp.OptionDef{ | ||
Code: 1, | ||
Description: "a", | ||
FriendlyName: "a", | ||
Schema: dhcp.OptionDefSchema{ | ||
Type: dhcp.SchemaTypeString, | ||
}, | ||
} | ||
|
||
err := mock.AddTestCase(http.MethodGet, "/dhcp/optiondef/space/key", http.StatusOK, nil, nil, "", od) | ||
if err != nil { | ||
t.Fatalf("error adding test case: %v", err) | ||
} | ||
|
||
respOD, _, err := client.OptionDef.Get("space", "key") | ||
if err != nil { | ||
t.Fatalf("error getting scpe group: %v", err) | ||
} | ||
if od.FriendlyName != respOD.FriendlyName || od.Code != respOD.Code || | ||
od.Description != respOD.Description || od.Schema.Type != respOD.Schema.Type { | ||
t.Errorf("wrong option def returned, want=%+v, got=%+v", od, respOD) | ||
} | ||
}) | ||
|
||
t.Run("Create", func(t *testing.T) { | ||
defer mock.ClearTestCases() | ||
|
||
t.Run("RequiredParams", func(t *testing.T) { | ||
od := &dhcp.OptionDef{} | ||
_, _, err = client.OptionDef.Create(od, "space", "key") | ||
if err == nil { | ||
t.Errorf("expected a missing code to result in an error") | ||
} | ||
|
||
od = &dhcp.OptionDef{ | ||
Code: 1, | ||
} | ||
_, _, err = client.OptionDef.Create(od, "space", "key") | ||
if err == nil { | ||
t.Errorf("expected a missing friendly name to result in an error") | ||
} | ||
|
||
od = &dhcp.OptionDef{ | ||
Code: 1, | ||
Description: "a", | ||
} | ||
_, _, err = client.OptionDef.Create(od, "space", "key") | ||
if err == nil { | ||
t.Errorf("expected a missing description to result in an error") | ||
} | ||
|
||
od = &dhcp.OptionDef{ | ||
Code: 1, | ||
Description: "a", | ||
FriendlyName: "a", | ||
} | ||
_, _, err = client.OptionDef.Create(od, "space", "key") | ||
if err == nil { | ||
t.Errorf("expected a missing schema type to result in an error") | ||
} | ||
}) | ||
|
||
od := &dhcp.OptionDef{ | ||
Code: 1, | ||
Description: "a", | ||
FriendlyName: "a", | ||
Schema: dhcp.OptionDefSchema{ | ||
Type: dhcp.SchemaTypeString, | ||
}, | ||
} | ||
err := mock.AddTestCase(http.MethodPut, "/dhcp/optiondef/space/key", http.StatusCreated, nil, nil, od, od) | ||
if err != nil { | ||
t.Fatalf("error adding test case: %v", err) | ||
} | ||
respOD, _, err := client.OptionDef.Create(od, "space", "key") | ||
if err != nil { | ||
t.Fatalf("error creating option definition: %v", err) | ||
} | ||
if od.FriendlyName != respOD.FriendlyName || od.Code != respOD.Code || | ||
od.Description != respOD.Description || od.Schema.Type != respOD.Schema.Type { | ||
t.Errorf("wrong option def returned, want=%+v, got=%+v", od, respOD) | ||
} | ||
}) | ||
|
||
t.Run("Delete", func(t *testing.T) { | ||
defer mock.ClearTestCases() | ||
|
||
err := mock.AddTestCase(http.MethodDelete, "/dhcp/optiondef/space/key", http.StatusNoContent, nil, nil, "", nil) | ||
if err != nil { | ||
t.Fatalf("error adding test case: %v", err) | ||
} | ||
_, err = client.OptionDef.Delete("space", "key") | ||
if err != nil { | ||
t.Fatalf("error deleting option definition: %v", err) | ||
} | ||
}) | ||
} |