From d1d89116f1eee523f93e311185ef3df9861c55a5 Mon Sep 17 00:00:00 2001 From: Anis Elleuch Date: Mon, 23 Jan 2017 17:56:06 +0100 Subject: [PATCH] admin: Add version to service Status API response (#3605) Add server's version field to service status API: "version":{ "version":"DEVELOPMENT.GOGET", "commitID":"DEVELOPMENT.GOGET" } --- cmd/admin-handlers.go | 26 +++++++++++++++++++++++++- cmd/admin-handlers_test.go | 7 +++++-- docs/admin-api/management-api.md | 2 +- pkg/madmin/API.md | 8 +++++--- pkg/madmin/service.go | 28 +++++++++++++++++++++------- 5 files changed, 57 insertions(+), 14 deletions(-) diff --git a/cmd/admin-handlers.go b/cmd/admin-handlers.go index ffd64c2ec..2639d7c3b 100644 --- a/cmd/admin-handlers.go +++ b/cmd/admin-handlers.go @@ -46,6 +46,18 @@ const ( mgmtDryRun mgmtQueryKey = "dry-run" ) +// ServerVersion - server version +type ServerVersion struct { + Version string `json:"version"` + CommitID string `json:"commitID"` +} + +// ServerStatus - contains the response of service status API +type ServerStatus struct { + StorageInfo StorageInfo `json:"storageInfo"` + ServerVersion ServerVersion `json:"serverVersion"` +} + // ServiceStatusHandler - GET /?service // HTTP header x-minio-operation: status // ---------- @@ -57,8 +69,20 @@ func (adminAPI adminAPIHandlers) ServiceStatusHandler(w http.ResponseWriter, r * writeErrorResponse(w, adminAPIErr, r.URL) return } + + // Fetch storage backend information storageInfo := newObjectLayerFn().StorageInfo() - jsonBytes, err := json.Marshal(storageInfo) + // Fetch server version + serverVersion := ServerVersion{Version: Version, CommitID: CommitID} + + // Create API response + serverStatus := ServerStatus{ + StorageInfo: storageInfo, + ServerVersion: serverVersion, + } + + // Marshal API response + jsonBytes, err := json.Marshal(serverStatus) if err != nil { writeErrorResponse(w, ErrInternalError, r.URL) errorIf(err, "Failed to marshal storage info into json.") diff --git a/cmd/admin-handlers_test.go b/cmd/admin-handlers_test.go index ee6376f86..250c144ca 100644 --- a/cmd/admin-handlers_test.go +++ b/cmd/admin-handlers_test.go @@ -227,8 +227,11 @@ func testServicesCmdHandler(cmd cmdType, args map[string]interface{}, t *testing adminTestBed.mux.ServeHTTP(rec, req) if cmd == statusCmd { - expectedInfo := newObjectLayerFn().StorageInfo() - receivedInfo := StorageInfo{} + expectedInfo := ServerStatus{ + StorageInfo: newObjectLayerFn().StorageInfo(), + ServerVersion: ServerVersion{Version: Version, CommitID: CommitID}, + } + receivedInfo := ServerStatus{} if jsonErr := json.Unmarshal(rec.Body.Bytes(), &receivedInfo); jsonErr != nil { t.Errorf("Failed to unmarshal StorageInfo - %v", jsonErr) } diff --git a/docs/admin-api/management-api.md b/docs/admin-api/management-api.md index dc0167c82..90f38b848 100644 --- a/docs/admin-api/management-api.md +++ b/docs/admin-api/management-api.md @@ -25,7 +25,7 @@ * Status - GET /?service - x-minio-operation: status - - Response: On success 200, return json formatted StorageInfo object. + - Response: On success 200, return json formatted object which contains StorageInfo and ServerVersion structures * SetCredentials - GET /?service diff --git a/pkg/madmin/API.md b/pkg/madmin/API.md index c54fd292e..420f20984 100644 --- a/pkg/madmin/API.md +++ b/pkg/madmin/API.md @@ -74,9 +74,11 @@ Fetch service status, replies disk space used, backend type and total disks offl | Param | Type | Description | |---|---|---| -|`st.Total` | _int64_ | Total disk space. | -|`st.Free` | _int64_ | Free disk space. | -|`st.Backend`| _struct{}_ | Represents backend type embedded structure. | +|`st.ServerVersion.Version` | _string_ | Server version. | +|`st.ServerVersion.CommitID` | _string_ | Server commit id. | +|`st.StorageInfo.Total` | _int64_ | Total disk space. | +|`st.StorageInfo.Free` | _int64_ | Free disk space. | +|`st.StorageInfo.Backend`| _struct{}_ | Represents backend type embedded structure. | | Param | Type | Description | |---|---|---| diff --git a/pkg/madmin/service.go b/pkg/madmin/service.go index 442ab3ffb..d5c732cb1 100644 --- a/pkg/madmin/service.go +++ b/pkg/madmin/service.go @@ -41,8 +41,8 @@ const ( // Add your own backend. ) -// ServiceStatusMetadata - represents total capacity of underlying storage. -type ServiceStatusMetadata struct { +// StorageInfo - represents total capacity of underlying storage. +type StorageInfo struct { // Total disk space. Total int64 // Free available disk space. @@ -60,10 +60,23 @@ type ServiceStatusMetadata struct { } } +// ServerVersion - server version +type ServerVersion struct { + Version string `json:"version"` + CommitID string `json:"commitID"` +} + +// ServiceStatusMetadata - contains the response of service status API +type ServiceStatusMetadata struct { + StorageInfo StorageInfo `json:"storageInfo"` + ServerVersion ServerVersion `json:"serverVersion"` +} + // ServiceStatus - Connect to a minio server and call Service Status Management API // to fetch server's storage information represented by ServiceStatusMetadata structure func (adm *AdminClient) ServiceStatus() (ServiceStatusMetadata, error) { + // Prepare web service request reqData := requestData{} reqData.queryValues = make(url.Values) reqData.queryValues.Set("service", "") @@ -72,29 +85,30 @@ func (adm *AdminClient) ServiceStatus() (ServiceStatusMetadata, error) { // Execute GET on bucket to list objects. resp, err := adm.executeMethod("GET", reqData) - defer closeResponse(resp) if err != nil { return ServiceStatusMetadata{}, err } + // Check response http status code if resp.StatusCode != http.StatusOK { return ServiceStatusMetadata{}, errors.New("Got HTTP Status: " + resp.Status) } + // Unmarshal the server's json response + var serviceStatus ServiceStatusMetadata + respBytes, err := ioutil.ReadAll(resp.Body) if err != nil { return ServiceStatusMetadata{}, err } - var storageInfo ServiceStatusMetadata - - err = json.Unmarshal(respBytes, &storageInfo) + err = json.Unmarshal(respBytes, &serviceStatus) if err != nil { return ServiceStatusMetadata{}, err } - return storageInfo, nil + return serviceStatus, nil } // ServiceRestart - Call Service Restart API to restart a specified Minio server