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 #294 from leoswaldo/leoswaldo/topic/returncodes
Browse files Browse the repository at this point in the history
Add proper return codes to /v2.1/{tenant}/servers/{server} GET Method…
  • Loading branch information
markdryan authored Jun 22, 2016
2 parents b5958b7 + be2d08f commit 21de96b
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
24 changes: 20 additions & 4 deletions ciao-controller/compute.go
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,22 @@ func instanceToServer(context *controller, instance *types.Instance) (payloads.S
return server, nil
}

// returnErrorCode returns error codes for the http call
func returnErrorCode(w http.ResponseWriter, httpError int, message string) {
var returnCode payloads.HTTPReturnErrorCode
returnCode.Error.Code = httpError
returnCode.Error.Name = http.StatusText(returnCode.Error.Code)
returnCode.Error.Message = message

b, err := json.Marshal(returnCode)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

http.Error(w, string(b), httpError)
}

func showServerDetails(w http.ResponseWriter, r *http.Request, context *controller) {
vars := mux.Vars(r)
tenant := vars["tenant"]
Expand All @@ -461,24 +477,24 @@ func showServerDetails(w http.ResponseWriter, r *http.Request, context *controll
dumpRequest(r)

if validateToken(context, r) == false {
http.Error(w, "Invalid token", http.StatusInternalServerError)
returnErrorCode(w, http.StatusUnauthorized, "Invalid token")
return
}

instance, err := context.ds.GetInstance(instanceID)
if err != nil {
http.Error(w, "Instance not available", http.StatusInternalServerError)
returnErrorCode(w, http.StatusNotFound, "Instance could not be found")
return
}

if instance.TenantID != tenant {
http.Error(w, "Instance not available", http.StatusInternalServerError)
returnErrorCode(w, http.StatusNotFound, "Instance does not belong to tenant")
return
}

server.Server, err = instanceToServer(context, instance)
if err != nil {
http.Error(w, "Instance not available", http.StatusInternalServerError)
returnErrorCode(w, http.StatusNotFound, "Instance could not be found")
return
}

Expand Down
16 changes: 16 additions & 0 deletions payloads/compute.go
Original file line number Diff line number Diff line change
Expand Up @@ -451,3 +451,19 @@ func NewCiaoEvents() (events CiaoEvents) {
events.Events = []CiaoEvent{}
return
}

// HTTPErrorData represents the HTTP response body for
// a compute API request error.
type HTTPErrorData struct {
Code int `json:"code"`
Name string `json:"name"`
Message string `json:"message"`
}

// HTTPReturnErrorCode represents the unmarshalled version for Return codes
// when a API call is made and you need to return explicit data of
// the call as OpenStack format
// http://developer.openstack.org/api-guide/compute/faults.html
type HTTPReturnErrorCode struct {
Error HTTPErrorData `json:"error"`
}

0 comments on commit 21de96b

Please sign in to comment.