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

Update interface name, mac address, or vlan. #35

Merged
merged 3 commits into from
Apr 13, 2016
Merged
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
20 changes: 20 additions & 0 deletions controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -649,6 +649,26 @@ func (c *controller) checkCreds() error {
return nil
}

func (c *controller) put(path string, params url.Values) (interface{}, error) {
path = EnsureTrailingSlash(path)
requestID := nextRequestID()
logger.Tracef("request %x: PUT %s%s, params: %s", requestID, c.client.APIURL, path, params.Encode())
bytes, err := c.client.Put(&url.URL{Path: path}, params)
if err != nil {
logger.Tracef("response %x: error: %q", requestID, err.Error())
logger.Tracef("error detail: %#v", err)
return nil, errors.Trace(err)
}
logger.Tracef("response %x: %s", requestID, string(bytes))

var parsed interface{}
err = json.Unmarshal(bytes, &parsed)
if err != nil {
return nil, errors.Trace(err)
}
return parsed, nil
}

func (c *controller) post(path, op string, params url.Values) (interface{}, error) {
bytes, err := c._postRaw(path, op, params, nil)
if err != nil {
Expand Down
45 changes: 45 additions & 0 deletions interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,51 @@ func (i *interface_) EffectiveMTU() int {
return i.effectiveMTU
}

// UpdateInterfaceArgs is an argument struct for calling Interface.Update.
type UpdateInterfaceArgs struct {
Name string
MACAddress string
VLAN VLAN
}

func (a *UpdateInterfaceArgs) vlanID() int {
if a.VLAN == nil {
return 0
}
return a.VLAN.ID()
}

// Update implements Interface.
func (i *interface_) Update(args UpdateInterfaceArgs) error {
var empty UpdateInterfaceArgs
if args == empty {
return nil
}
params := NewURLParams()
params.MaybeAdd("name", args.Name)
params.MaybeAdd("mac_address", args.MACAddress)
params.MaybeAddInt("vlan", args.vlanID())
source, err := i.controller.put(i.resourceURI, params.Values)
if err != nil {
if svrErr, ok := errors.Cause(err).(ServerError); ok {
switch svrErr.StatusCode {
case http.StatusNotFound:
return errors.Wrap(err, NewNoMatchError(svrErr.BodyMessage))
case http.StatusForbidden:
return errors.Wrap(err, NewPermissionError(svrErr.BodyMessage))
}
}
return NewUnexpectedError(err)
}

response, err := readInterface(i.controller.apiVersion, source)
if err != nil {
return errors.Trace(err)
}
i.updateFrom(response)
return nil
}

// Delete implements Interface.
func (i *interface_) Delete() error {
err := i.controller.delete(i.resourceURI)
Expand Down
54 changes: 54 additions & 0 deletions interface_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,60 @@ func (s *interfaceSuite) TestUnlinkSubnetUnknown(c *gc.C) {
c.Assert(err.Error(), gc.Equals, "unexpected: ServerError: 405 Method Not Allowed (wat?)")
}

func (s *interfaceSuite) TestUpdateNoChangeNoRequest(c *gc.C) {
server, iface := s.getServerAndNewInterface(c)
count := server.RequestCount()
err := iface.Update(UpdateInterfaceArgs{})
c.Assert(err, jc.ErrorIsNil)
c.Assert(server.RequestCount(), gc.Equals, count)
}

func (s *interfaceSuite) TestUpdateMissing(c *gc.C) {
_, iface := s.getServerAndNewInterface(c)
err := iface.Update(UpdateInterfaceArgs{Name: "eth2"})
c.Check(err, jc.Satisfies, IsNoMatchError)
}

func (s *interfaceSuite) TestUpdateForbidden(c *gc.C) {
server, iface := s.getServerAndNewInterface(c)
server.AddPutResponse(iface.resourceURI, http.StatusForbidden, "bad user")
err := iface.Update(UpdateInterfaceArgs{Name: "eth2"})
c.Check(err, jc.Satisfies, IsPermissionError)
c.Check(err.Error(), gc.Equals, "bad user")
}

func (s *interfaceSuite) TestUpdateUnknown(c *gc.C) {
server, iface := s.getServerAndNewInterface(c)
server.AddPutResponse(iface.resourceURI, http.StatusMethodNotAllowed, "wat?")
err := iface.Update(UpdateInterfaceArgs{Name: "eth2"})
c.Check(err, jc.Satisfies, IsUnexpectedError)
c.Assert(err.Error(), gc.Equals, "unexpected: ServerError: 405 Method Not Allowed (wat?)")
}

func (s *interfaceSuite) TestUpdateGood(c *gc.C) {
server, iface := s.getServerAndNewInterface(c)
// The changed information is there just for the test to show that the response
// is parsed and the interface updated
response := updateJSONMap(c, interfaceResponse, map[string]interface{}{
"name": "eth42",
})
server.AddPutResponse(iface.resourceURI, http.StatusOK, response)
args := UpdateInterfaceArgs{
Name: "eth42",
MACAddress: "c3-52-51-b4-50-cd",
VLAN: &fakeVLAN{id: 13},
}
err := iface.Update(args)
c.Check(err, jc.ErrorIsNil)
c.Check(iface.Name(), gc.Equals, "eth42")

request := server.LastRequest()
form := request.PostForm
c.Assert(form.Get("name"), gc.Equals, "eth42")
c.Assert(form.Get("mac_address"), gc.Equals, "c3-52-51-b4-50-cd")
c.Assert(form.Get("vlan"), gc.Equals, "13")
}

const (
interfacesResponse = "[" + interfaceResponse + "]"
interfaceResponse = `
Expand Down
3 changes: 3 additions & 0 deletions interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,9 @@ type Interface interface {
// Params is a JSON field, and defaults to an empty string, but is almost
// always a JSON object in practice. Gleefully ignoring it until we need it.

// Update the name, mac address or VLAN.
Update(UpdateInterfaceArgs) error

// Delete this interface.
Delete() error

Expand Down
20 changes: 20 additions & 0 deletions testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ type SimpleTestServer struct {

getResponses map[string][]simpleResponse
getResponseIndex map[string]int
putResponses map[string][]simpleResponse
putResponseIndex map[string]int
postResponses map[string][]simpleResponse
postResponseIndex map[string]int
deleteResponses map[string][]simpleResponse
Expand All @@ -104,6 +106,8 @@ func NewSimpleServer() *SimpleTestServer {
server := &SimpleTestServer{
getResponses: make(map[string][]simpleResponse),
getResponseIndex: make(map[string]int),
putResponses: make(map[string][]simpleResponse),
putResponseIndex: make(map[string]int),
postResponses: make(map[string][]simpleResponse),
postResponseIndex: make(map[string]int),
deleteResponses: make(map[string][]simpleResponse),
Expand All @@ -118,6 +122,11 @@ func (s *SimpleTestServer) AddGetResponse(path string, status int, body string)
s.getResponses[path] = append(s.getResponses[path], simpleResponse{status: status, body: body})
}

func (s *SimpleTestServer) AddPutResponse(path string, status int, body string) {
logger.Debugf("add put response for: %s, %d", path, status)
s.putResponses[path] = append(s.putResponses[path], simpleResponse{status: status, body: body})
}

func (s *SimpleTestServer) AddPostResponse(path string, status int, body string) {
logger.Debugf("add post response for: %s, %d", path, status)
s.postResponses[path] = append(s.postResponses[path], simpleResponse{status: status, body: body})
Expand All @@ -136,6 +145,10 @@ func (s *SimpleTestServer) LastRequest() *http.Request {
return s.requests[pos]
}

func (s *SimpleTestServer) RequestCount() int {
return len(s.requests)
}

func (s *SimpleTestServer) handler(writer http.ResponseWriter, request *http.Request) {
method := request.Method
var (
Expand All @@ -151,6 +164,13 @@ func (s *SimpleTestServer) handler(writer http.ResponseWriter, request *http.Req
if err != nil {
panic(err) // it is a test, panic should be fine
}
case "PUT":
responses = s.putResponses
responseIndex = s.putResponseIndex
err = request.ParseForm()
if err != nil {
panic(err)
}
case "POST":
responses = s.postResponses
responseIndex = s.postResponseIndex
Expand Down