// GetAuthKeys get access key id and secret access key func GetAuthKeys(url string) ([]byte, error) { op := RPCOps{ Method: "Auth.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() defer closeResp(resp) if err != nil { return nil, iodine.New(err, nil) } var reply rpc.AuthReply if err := jsonrpc.DecodeClientResponse(resp.Body, &reply); err != nil { return nil, iodine.New(err, nil) } authConfig := &auth.Config{} authConfig.Version = "0.0.1" authConfig.Users = make(map[string]*auth.User) user := &auth.User{} user.Name = "testuser" user.AccessKeyID = reply.AccessKeyID user.SecretAccessKey = reply.SecretAccessKey authConfig.Users[reply.AccessKeyID] = user if err := auth.SaveConfig(authConfig); err != nil { return nil, iodine.New(err, nil) } return json.MarshalIndent(reply, "", "\t") }
// SetDonut - set donut config func SetDonut(url, hostname string, disks []string) error { op := RPCOps{ Method: "Donut.Set", Request: rpc.DonutArgs{ Hostname: hostname, Disks: disks, Name: "default", MaxSize: 512000000, }, } req, err := NewRequest(url, op, http.DefaultTransport) if err != nil { return iodine.New(err, nil) } resp, err := req.Do() defer closeResp(resp) if err != nil { return iodine.New(err, nil) } var reply rpc.Reply if err := jsonrpc.DecodeClientResponse(resp.Body, &reply); err != nil { return iodine.New(err, nil) } return reply.Error }
func proxyRequest(method, host string, ssl bool, res interface{}) *probe.Error { 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{}, } request, err := newRPCRequest(u.String(), op, nil) if err != nil { return err.Trace() } var resp *http.Response resp, err = request.Do() if err != nil { return err.Trace() } if err := json.DecodeClientResponse(resp.Body, res); err != nil { return probe.NewError(err) } return nil }
// Do holds a JSON-RPC request. func Do(url, method string, args *openstack.Args) error { message, err := json.EncodeClientRequest(method, args) checkErr(err) req, err := http.NewRequest("POST", url, bytes.NewBuffer(message)) checkErr(err) req.Header.Set("Content-Type", "application/json") client := new(http.Client) resp, err := client.Do(req) if err != nil { log.Printf("Error in sending request to %s. %s", url, err) return err } defer resp.Body.Close() err = json.DecodeClientResponse(resp.Body, &result) if err != nil { log.Printf("Couldn't decode response. %s", err) return err } log.Printf("url: %s, method: %s, args: %s", url, method, args) return nil }
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", } op := rpcOperation{ Method: method, Request: ServerArg{}, } request, err := newRPCRequest(u.String(), op, nil) if err != nil { return err.Trace() } var resp *http.Response resp, err = request.Do() if err != nil { return err.Trace() } if err := json.DecodeClientResponse(resp.Body, res); err != nil { return probe.NewError(err) } return nil }
func (rc *RpcClient) Execute(method string, req, res interface{}) error { buf, _ := json.EncodeClientRequest(method, req) body := bytes.NewBuffer(buf) log.Println(body) httpreq, _ := http.NewRequest("POST", rc.Address, body) httpreq.Header.Set("Content-Type", "application/json") response, err := rc.Httpclient.Do(httpreq) if err != nil { return err } return json.DecodeClientResponse(response.Body, res) }
func (s *ControllerRPCSuite) TestDiskStats(c *C) { op := rpcOperation{ Method: "Controller.GetServerDiskStats", Request: ControllerArgs{Hosts: []string{s.url.Host}}, } req, err := newRPCRequest(testControllerRPC.URL+"/rpc", op, http.DefaultTransport) c.Assert(err, IsNil) c.Assert(req.Get("Content-Type"), Equals, "application/json") resp, err := req.Do() c.Assert(err, IsNil) c.Assert(resp.StatusCode, Equals, http.StatusOK) var reply MemStatsRep c.Assert(json.DecodeClientResponse(resp.Body, &reply), IsNil) resp.Body.Close() c.Assert(reply, Not(DeepEquals), DiskStatsRep{}) }
func (s *ControllerRPCSuite) TestServerAdd(c *C) { op := rpcOperation{ Method: "Controller.AddServer", Request: ControllerArgs{Host: s.url.Host}, } req, err := newRPCRequest(s.config, testControllerRPC.URL+"/rpc", op, http.DefaultTransport) c.Assert(err, IsNil) c.Assert(req.Get("Content-Type"), Equals, "application/json") resp, err := req.Do() c.Assert(err, IsNil) c.Assert(resp.StatusCode, Equals, http.StatusOK) var reply DefaultRep c.Assert(json.DecodeClientResponse(resp.Body, &reply), IsNil) resp.Body.Close() c.Assert(reply, Not(DeepEquals), DefaultRep{nil, "Added"}) }
func (s *MyRPCSuite) TestSysInfo(c *C) { op := controller.RPCOps{ Method: "SysInfo.Get", Request: rpc.Args{Request: ""}, } req, err := controller.NewRequest(testRPCServer.URL+"/rpc", op, http.DefaultTransport) c.Assert(err, IsNil) c.Assert(req.Get("Content-Type"), Equals, "application/json") resp, err := req.Do() c.Assert(err, IsNil) c.Assert(resp.StatusCode, Equals, http.StatusOK) var reply rpc.SysInfoReply c.Assert(jsonrpc.DecodeClientResponse(resp.Body, &reply), IsNil) resp.Body.Close() c.Assert(reply, Not(DeepEquals), rpc.SysInfoReply{}) }
func (s *MySuite) TestAuth(c *C) { op := rpc.Operation{ Method: "Auth.Get", Request: rpc.Args{Request: ""}, } req, err := rpc.NewRequest(testRPCServer.URL+"/rpc", op, http.DefaultTransport) c.Assert(err, IsNil) c.Assert(req.Get("Content-Type"), Equals, "application/json") resp, err := req.Do() c.Assert(err, IsNil) c.Assert(resp.StatusCode, Equals, http.StatusOK) var reply rpc.AuthReply c.Assert(jsonrpc.DecodeClientResponse(resp.Body, &reply), IsNil) resp.Body.Close() c.Assert(reply, Not(DeepEquals), rpc.AuthReply{}) c.Assert(len(reply.AccessKeyID), Equals, 20) c.Assert(len(reply.SecretAccessKey), Equals, 40) }
// GetSysInfo get system status of the server at given url func GetSysInfo(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() defer closeResp(resp) if err != nil { return nil, iodine.New(err, nil) } var reply rpc.SysInfoReply if err := jsonrpc.DecodeClientResponse(resp.Body, &reply); err != nil { return nil, iodine.New(err, nil) } return json.MarshalIndent(reply, "", "\t") }
// 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 memory status of the server at given url func GetMemStats(url string) ([]byte, *probe.Error) { op := RPCOps{ Method: "MemStats.Get", Request: rpc.Args{Request: ""}, } req, perr := NewRequest(url, op, http.DefaultTransport) if perr != nil { return nil, perr.Trace() } resp, perr := req.Do() defer closeResp(resp) if perr != nil { return nil, perr.Trace() } var reply rpc.MemStatsReply if err := jsonrpc.DecodeClientResponse(resp.Body, &reply); err != nil { return nil, probe.NewError(err) } jsonRespBytes, err := json.MarshalIndent(reply, "", "\t") if err != nil { return nil, probe.NewError(err) } return jsonRespBytes, nil }
func (s *ControllerRPCSuite) TestAuth(c *C) { op := rpcOperation{ Method: "Controller.GenerateAuth", Request: AuthArgs{User: "******"}, } req, err := newRPCRequest(testControllerRPC.URL+"/rpc", op, http.DefaultTransport) c.Assert(err, IsNil) c.Assert(req.Get("Content-Type"), Equals, "application/json") resp, err := req.Do() c.Assert(err, IsNil) c.Assert(resp.StatusCode, Equals, http.StatusOK) var reply AuthRep c.Assert(json.DecodeClientResponse(resp.Body, &reply), IsNil) resp.Body.Close() c.Assert(reply, Not(DeepEquals), AuthRep{}) c.Assert(len(reply.AccessKeyID), Equals, 20) c.Assert(len(reply.SecretAccessKey), Equals, 40) c.Assert(len(reply.Name), Not(Equals), 0) op = rpcOperation{ Method: "Controller.FetchAuth", Request: AuthArgs{User: "******"}, } req, err = newRPCRequest(testControllerRPC.URL+"/rpc", op, http.DefaultTransport) c.Assert(err, IsNil) c.Assert(req.Get("Content-Type"), Equals, "application/json") resp, err = req.Do() c.Assert(err, IsNil) c.Assert(resp.StatusCode, Equals, http.StatusOK) var newReply AuthRep c.Assert(json.DecodeClientResponse(resp.Body, &newReply), IsNil) resp.Body.Close() c.Assert(newReply, Not(DeepEquals), AuthRep{}) c.Assert(reply.AccessKeyID, Equals, newReply.AccessKeyID) c.Assert(reply.SecretAccessKey, Equals, newReply.SecretAccessKey) c.Assert(len(reply.Name), Not(Equals), 0) op = rpcOperation{ Method: "Controller.ResetAuth", Request: AuthArgs{User: "******"}, } req, err = newRPCRequest(testControllerRPC.URL+"/rpc", op, http.DefaultTransport) c.Assert(err, IsNil) c.Assert(req.Get("Content-Type"), Equals, "application/json") resp, err = req.Do() c.Assert(err, IsNil) c.Assert(resp.StatusCode, Equals, http.StatusOK) var resetReply AuthRep c.Assert(json.DecodeClientResponse(resp.Body, &resetReply), IsNil) resp.Body.Close() c.Assert(newReply, Not(DeepEquals), AuthRep{}) c.Assert(reply.AccessKeyID, Not(Equals), resetReply.AccessKeyID) c.Assert(reply.SecretAccessKey, Not(Equals), resetReply.SecretAccessKey) c.Assert(len(reply.Name), Not(Equals), 0) // these operations should fail /// generating access for existing user fails op = rpcOperation{ Method: "Controller.GenerateAuth", Request: AuthArgs{User: "******"}, } req, err = newRPCRequest(testControllerRPC.URL+"/rpc", op, http.DefaultTransport) c.Assert(err, IsNil) c.Assert(req.Get("Content-Type"), Equals, "application/json") resp, err = req.Do() c.Assert(err, IsNil) c.Assert(resp.StatusCode, Equals, http.StatusBadRequest) /// null user provided invalid op = rpcOperation{ Method: "Controller.GenerateAuth", Request: AuthArgs{User: ""}, } req, err = newRPCRequest(testControllerRPC.URL+"/rpc", op, http.DefaultTransport) c.Assert(err, IsNil) c.Assert(req.Get("Content-Type"), Equals, "application/json") resp, err = req.Do() c.Assert(err, IsNil) c.Assert(resp.StatusCode, Equals, http.StatusBadRequest) }