Skip to content
This repository has been archived by the owner on Jul 16, 2020. It is now read-only.

Commit

Permalink
Merge pull request #754 from kaccardi/topic/fix-delete-response
Browse files Browse the repository at this point in the history
ciao-controller: send ErrInstanceNotAssigned when needed
  • Loading branch information
markdryan authored Oct 28, 2016
2 parents cb53cb7 + dda3334 commit 1819257
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 19 deletions.
6 changes: 3 additions & 3 deletions ciao-controller/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (c *controller) restartInstance(instanceID string) error {
}

if i.NodeID == "" {
return errors.New("Cannot restart instance not assigned to Node")
return types.ErrInstanceNotAssigned
}

if i.State != "exited" {
Expand All @@ -60,7 +60,7 @@ func (c *controller) stopInstance(instanceID string) error {
}

if i.NodeID == "" {
return errors.New("Cannot stop instance not assigned to Node")
return types.ErrInstanceNotAssigned
}

if i.State == payloads.ComputeStatusPending {
Expand All @@ -79,7 +79,7 @@ func (c *controller) deleteInstance(instanceID string) error {
}

if i.NodeID == "" {
return errors.New("Cannot delete instance not assigned to Node")
return types.ErrInstanceNotAssigned
}

go c.client.DeleteInstance(instanceID, i.NodeID)
Expand Down
2 changes: 1 addition & 1 deletion ciao-controller/compute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ func testDeleteServer(t *testing.T, httpExpectedStatus int, httpExpectedErrorSta
}

func TestDeleteServer(t *testing.T) {
testDeleteServer(t, http.StatusNoContent, http.StatusInternalServerError, true)
testDeleteServer(t, http.StatusNoContent, http.StatusForbidden, true)
}

func TestDeleteServerInvalidToken(t *testing.T) {
Expand Down
20 changes: 15 additions & 5 deletions ciao-controller/openstack_compute.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,11 +192,11 @@ func (c *controller) DeleteServer(tenant string, server string) error {
}

err = c.deleteInstance(server)
if err != nil {
return err
if err == types.ErrInstanceNotAssigned {
return compute.ErrInstanceNotAvailable
}

return nil
return err
}

func (c *controller) StartServer(tenant string, ID string) error {
Expand All @@ -209,7 +209,12 @@ func (c *controller) StartServer(tenant string, ID string) error {
return compute.ErrServerOwner
}

return c.restartInstance(ID)
err = c.restartInstance(ID)
if err == types.ErrInstanceNotAssigned {
return compute.ErrInstanceNotAvailable
}

return err
}

func (c *controller) StopServer(tenant string, ID string) error {
Expand All @@ -222,7 +227,12 @@ func (c *controller) StopServer(tenant string, ID string) error {
return compute.ErrServerOwner
}

return c.stopInstance(ID)
err = c.stopInstance(ID)
if err == types.ErrInstanceNotAssigned {
return compute.ErrInstanceNotAvailable
}

return err
}

func (c *controller) ListFlavors(tenant string) (compute.Flavors, error) {
Expand Down
3 changes: 3 additions & 0 deletions ciao-controller/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -508,4 +508,7 @@ var (

// ErrInstanceNotFound is returned when an instance is not found.
ErrInstanceNotFound = errors.New("Instance not found")

// ErrInstanceNotAssigned is returned when an instance is not assigned to a node.
ErrInstanceNotAssigned = errors.New("Cannot perform operation: instance not assigned to Node")
)
19 changes: 9 additions & 10 deletions openstack/compute/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,25 +72,24 @@ type SecurityGroup struct {

// These errors can be returned by the Service interface
var (
ErrQuota = errors.New("Tenant over quota")
ErrTenantNotFound = errors.New("Tenant not found")
ErrServerNotFound = errors.New("Server not found")
ErrServerOwner = errors.New("You are not server owner")
ErrQuota = errors.New("Tenant over quota")
ErrTenantNotFound = errors.New("Tenant not found")
ErrServerNotFound = errors.New("Server not found")
ErrServerOwner = errors.New("You are not server owner")
ErrInstanceNotAvailable = errors.New("Instance not currently available for this operation")
)

// errorResponse maps service error responses to http responses.
// this helper function can help functions avoid having to switch
// on return values all the time.
func errorResponse(err error) APIResponse {
switch err {
case ErrQuota:
return APIResponse{http.StatusForbidden, nil}
case ErrTenantNotFound:
return APIResponse{http.StatusNotFound, nil}
case ErrServerNotFound:
case ErrTenantNotFound, ErrServerNotFound:
return APIResponse{http.StatusNotFound, nil}
case ErrServerOwner:

case ErrQuota, ErrServerOwner, ErrInstanceNotAvailable:
return APIResponse{http.StatusForbidden, nil}

default:
return APIResponse{http.StatusInternalServerError, nil}
}
Expand Down

0 comments on commit 1819257

Please sign in to comment.