Add basic controller code, initiating json rpc connection getting list of disks and memstats for now.

This commit is contained in:
Harshavardhana
2015-07-05 16:55:55 -07:00
parent 75a32d1c01
commit 7f0c14f2b7
5 changed files with 170 additions and 8 deletions

72
pkg/controller/client.go Normal file
View File

@@ -0,0 +1,72 @@
/*
* Minimalist Object Storage, (C) 2015 Minio, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package controller
import (
"encoding/json"
"net/http"
jsonrpc "github.com/gorilla/rpc/v2/json"
"github.com/minio/minio/pkg/iodine"
"github.com/minio/minio/pkg/server/rpc"
)
// GetDisks get disks info of the server at given url
func GetDisks(url string) ([]string, error) {
op := RPCOps{
Method: "DiskInfo.Get",
Request: rpc.Args{Request: ""},
}
req, err := NewRequest(url, op, http.DefaultTransport)
if err != nil {
return nil, iodine.New(err, nil)
}
resp, err := req.Do()
if err != nil {
return nil, iodine.New(err, nil)
}
defer resp.Body.Close()
var reply rpc.DiskInfoReply
if err := jsonrpc.DecodeClientResponse(resp.Body, &reply); err != nil {
return nil, iodine.New(err, nil)
}
return reply.Disks, nil
}
// GetMemStats get system info of the server at given url
func GetMemStats(url string) ([]byte, error) {
op := RPCOps{
Method: "SysInfo.Get",
Request: rpc.Args{Request: ""},
}
req, err := NewRequest(url, op, http.DefaultTransport)
if err != nil {
return nil, iodine.New(err, nil)
}
resp, err := req.Do()
if err != nil {
return nil, iodine.New(err, nil)
}
defer resp.Body.Close()
var reply rpc.SysInfoReply
if err := jsonrpc.DecodeClientResponse(resp.Body, &reply); err != nil {
return nil, iodine.New(err, nil)
}
return json.MarshalIndent(reply, "", "\t")
}
// Add more functions here for many replies

66
pkg/controller/rpc.go Normal file
View File

@@ -0,0 +1,66 @@
/*
* Minimalist Object Storage, (C) 2015 Minio, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package controller
import (
"bytes"
"net/http"
"github.com/gorilla/rpc/v2/json"
"github.com/minio/minio/pkg/iodine"
)
// RPCOps RPC operation
type RPCOps struct {
Method string
Request interface{}
}
// RPCRequest rpc client request
type RPCRequest struct {
req *http.Request
transport http.RoundTripper
}
// NewRequest initiate a new client RPC request
func NewRequest(url string, op RPCOps, transport http.RoundTripper) (*RPCRequest, error) {
params, err := json.EncodeClientRequest(op.Method, op.Request)
if err != nil {
return nil, iodine.New(err, nil)
}
req, err := http.NewRequest("POST", url, bytes.NewReader(params))
if err != nil {
return nil, iodine.New(err, nil)
}
rpcReq := &RPCRequest{}
rpcReq.req = req
rpcReq.req.Header.Set("Content-Type", "application/json")
if transport == nil {
transport = http.DefaultTransport
}
rpcReq.transport = transport
return rpcReq, nil
}
// Do - make a http connection
func (r RPCRequest) Do() (*http.Response, error) {
resp, err := r.transport.RoundTrip(r.req)
if err != nil {
return nil, iodine.New(err, nil)
}
return resp, nil
}

View File

@@ -24,11 +24,6 @@ import (
"github.com/minio/minio/pkg/server/rpc"
)
// Get api
func getAPI() api.Minio {
return api.New()
}
// registerAPI - register all the object API handlers to their respective paths
func registerAPI(mux *router.Router, a api.Minio) http.Handler {
mux.HandleFunc("/", a.ListBucketsHandler).Methods("GET")
@@ -107,7 +102,7 @@ func registerRPC(mux *router.Router, s *rpc.Server) http.Handler {
// getAPIHandler api handler
func getAPIHandler(conf api.Config) (http.Handler, api.Minio) {
mux := router.NewRouter()
minioAPI := getAPI()
minioAPI := api.New()
apiHandler := registerAPI(mux, minioAPI)
apiHandler = registerCustomMiddleware(apiHandler, conf)
return apiHandler, minioAPI