diff --git a/controller-rpc.go b/controller-rpc.go index 91ceb18a6..4d2357f11 100644 --- a/controller-rpc.go +++ b/controller-rpc.go @@ -18,6 +18,7 @@ package main import ( "errors" + "net" "net/http" "net/url" "os" @@ -173,16 +174,20 @@ func (s *controllerRPCService) ResetAuth(r *http.Request, args *AuthArgs, reply } func proxyRequest(method, host string, ssl bool, res interface{}) *probe.Error { - u := &url.URL{ - Scheme: func() string { - if ssl { - return "https" - } - return "http" - }(), - Host: host, - Path: "/rpc", + u := &url.URL{} + if ssl { + u.Scheme = "https" + } else { + u.Scheme = "http" } + u.Host = host + if _, _, err := net.SplitHostPort(host); err == nil { + u.Host = host + } else { + u.Host = host + ":9002" + } + u.Path = "/rpc" + op := rpcOperation{ Method: method, Request: ServerArg{}, @@ -203,47 +208,36 @@ func proxyRequest(method, host string, ssl bool, res interface{}) *probe.Error { } func (s *controllerRPCService) AddServer(r *http.Request, args *ControllerArgs, res *ServerRep) error { - for _, host := range args.Hosts { - err := proxyRequest("Server.Add", host, args.SSL, res) - if err != nil { - return probe.WrapError(err) - } - s.serverList = append(s.serverList, *res) + err := proxyRequest("Server.Add", args.Host, args.SSL, res) + if err != nil { + return probe.WrapError(err) } + s.serverList = append(s.serverList, *res) return nil } func (s *controllerRPCService) GetServerMemStats(r *http.Request, args *ControllerArgs, res *MemStatsRep) error { - for _, host := range args.Hosts { - err := proxyRequest("Server.MemStats", host, args.SSL, res) - if err != nil { - return probe.WrapError(err) - } - return nil + err := proxyRequest("Server.MemStats", args.Host, args.SSL, res) + if err != nil { + return probe.WrapError(err) } - return errors.New("Invalid argument") + return nil } func (s *controllerRPCService) GetServerDiskStats(r *http.Request, args *ControllerArgs, res *DiskStatsRep) error { - for _, host := range args.Hosts { - err := proxyRequest("Server.DiskStats", host, args.SSL, res) - if err != nil { - return probe.WrapError(err) - } - return nil + err := proxyRequest("Server.DiskStats", args.Host, args.SSL, res) + if err != nil { + return probe.WrapError(err) } - return errors.New("Invalid argument") + return nil } func (s *controllerRPCService) GetServerSysInfo(r *http.Request, args *ControllerArgs, res *SysInfoRep) error { - for _, host := range args.Hosts { - err := proxyRequest("Server.SysInfo", host, args.SSL, res) - if err != nil { - return probe.WrapError(err) - } - return nil + err := proxyRequest("Server.SysInfo", args.Host, args.SSL, res) + if err != nil { + return probe.WrapError(err) } - return errors.New("Invalid argument") + return nil } func (s *controllerRPCService) ListServers(r *http.Request, args *ControllerArgs, res *ListRep) error { @@ -252,12 +246,9 @@ func (s *controllerRPCService) ListServers(r *http.Request, args *ControllerArgs } func (s *controllerRPCService) GetServerVersion(r *http.Request, args *ControllerArgs, res *VersionRep) error { - for _, host := range args.Hosts { - err := proxyRequest("Server.Version", host, args.SSL, res) - if err != nil { - return probe.WrapError(err) - } - return nil + err := proxyRequest("Server.Version", args.Host, args.SSL, res) + if err != nil { + return probe.WrapError(err) } - return errors.New("Invalid argument") + return nil } diff --git a/controller_rpc_test.go b/controller_rpc_test.go index f83ceb673..30a736d97 100644 --- a/controller_rpc_test.go +++ b/controller_rpc_test.go @@ -65,7 +65,7 @@ func (s *ControllerRPCSuite) TearDownSuite(c *C) { func (s *ControllerRPCSuite) TestMemStats(c *C) { op := rpcOperation{ Method: "Controller.GetServerMemStats", - Request: ControllerArgs{Hosts: []string{s.url.Host}}, + Request: ControllerArgs{Host: s.url.Host}, } req, err := newRPCRequest(testControllerRPC.URL+"/rpc", op, http.DefaultTransport) c.Assert(err, IsNil) @@ -83,7 +83,7 @@ func (s *ControllerRPCSuite) TestMemStats(c *C) { func (s *ControllerRPCSuite) TestDiskStats(c *C) { op := rpcOperation{ Method: "Controller.GetServerDiskStats", - Request: ControllerArgs{Hosts: []string{s.url.Host}}, + Request: ControllerArgs{Host: s.url.Host}, } req, err := newRPCRequest(testControllerRPC.URL+"/rpc", op, http.DefaultTransport) c.Assert(err, IsNil) @@ -101,7 +101,7 @@ func (s *ControllerRPCSuite) TestDiskStats(c *C) { func (s *ControllerRPCSuite) TestSysInfo(c *C) { op := rpcOperation{ Method: "Controller.GetServerSysInfo", - Request: ControllerArgs{Hosts: []string{s.url.Host}}, + Request: ControllerArgs{Host: s.url.Host}, } req, err := newRPCRequest(testControllerRPC.URL+"/rpc", op, http.DefaultTransport) c.Assert(err, IsNil) @@ -119,7 +119,7 @@ func (s *ControllerRPCSuite) TestSysInfo(c *C) { func (s *ControllerRPCSuite) TestServerList(c *C) { op := rpcOperation{ Method: "Controller.ListServers", - Request: ControllerArgs{Hosts: []string{s.url.Host}}, + Request: ControllerArgs{Host: s.url.Host}, } req, err := newRPCRequest(testControllerRPC.URL+"/rpc", op, http.DefaultTransport) c.Assert(err, IsNil) @@ -137,7 +137,7 @@ func (s *ControllerRPCSuite) TestServerList(c *C) { func (s *ControllerRPCSuite) TestServerAdd(c *C) { op := rpcOperation{ Method: "Controller.AddServer", - Request: ControllerArgs{Hosts: []string{s.url.Host}}, + Request: ControllerArgs{Host: s.url.Host}, } req, err := newRPCRequest(testControllerRPC.URL+"/rpc", op, http.DefaultTransport) c.Assert(err, IsNil) diff --git a/rpc-definitions.go b/rpc-definitions.go index 5edd1a75a..0273babee 100644 --- a/rpc-definitions.go +++ b/rpc-definitions.go @@ -38,9 +38,9 @@ type ServerArg struct{} // ControllerArgs controller params type ControllerArgs struct { - Hosts []string `json:"hosts"` // hosts is a collection of host or host:port - SSL bool `json:"ssl"` - ID string `json:"id"` + Host string `json:"host"` // host or host:port + SSL bool `json:"ssl"` + ID string `json:"id"` } //// RPC replies