-
Notifications
You must be signed in to change notification settings - Fork 8.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge "[FAB-15814] Add endpoint for versioning metadata"
- Loading branch information
Showing
4 changed files
with
114 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
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,50 @@ | ||
/* | ||
Copyright IBM Corp All Rights Reserved. | ||
SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package operations | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/hyperledger/fabric/common/flogging" | ||
) | ||
|
||
type VersionInfoHandler struct { | ||
CommitSHA string `json:"CommitSHA,omitempty"` | ||
Version string `json:"Version,omitempty"` | ||
} | ||
|
||
func (m *VersionInfoHandler) ServeHTTP(resp http.ResponseWriter, req *http.Request) { | ||
switch req.Method { | ||
case http.MethodGet: | ||
m.sendResponse(resp, http.StatusOK, m) | ||
default: | ||
err := fmt.Errorf("invalid request method: %s", req.Method) | ||
m.sendResponse(resp, http.StatusBadRequest, err) | ||
} | ||
} | ||
|
||
type errorResponse struct { | ||
Error string `json:"Error"` | ||
} | ||
|
||
func (m *VersionInfoHandler) sendResponse(resp http.ResponseWriter, code int, payload interface{}) { | ||
if err, ok := payload.(error); ok { | ||
payload = &errorResponse{Error: err.Error()} | ||
} | ||
js, err := json.Marshal(payload) | ||
if err != nil { | ||
logger := flogging.MustGetLogger("operations.runner") | ||
logger.Errorw("failed to encode payload", "error", err) | ||
resp.WriteHeader(http.StatusInternalServerError) | ||
} else { | ||
resp.WriteHeader(code) | ||
resp.Header().Set("Content-Type", "application/json") | ||
resp.Write(js) | ||
} | ||
} |
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,43 @@ | ||
/* | ||
Copyright IBM Corp All Rights Reserved. | ||
SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package operations | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
|
||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
var _ = Describe("Version", func() { | ||
It("returns 200 if the method is GET", func() { | ||
resp := httptest.NewRecorder() | ||
|
||
versionInfoHandler := &VersionInfoHandler{Version: "latest"} | ||
versionInfoHandler.ServeHTTP(resp, &http.Request{Method: http.MethodGet}) | ||
Expect(resp.Result().StatusCode).To(Equal(http.StatusOK)) | ||
Expect(resp.Body).To(MatchJSON(`{"Version": "latest"}`)) | ||
}) | ||
|
||
It("returns 400 when an unsupported method is used", func() { | ||
resp := httptest.NewRecorder() | ||
|
||
versionInfoHandler := &VersionInfoHandler{} | ||
versionInfoHandler.ServeHTTP(resp, &http.Request{Method: http.MethodPut}) | ||
Expect(resp.Result().StatusCode).To(Equal(http.StatusBadRequest)) | ||
Expect(resp.Body).To(MatchJSON(`{"Error": "invalid request method: PUT"}`)) | ||
}) | ||
|
||
It("returns 500 when the payload is invalid JSON", func() { | ||
resp := httptest.NewRecorder() | ||
|
||
versionInfoHandler := &VersionInfoHandler{} | ||
versionInfoHandler.sendResponse(resp, 200, make(chan int)) | ||
Expect(resp.Result().StatusCode).To(Equal(http.StatusInternalServerError)) | ||
}) | ||
}) |