From 374e6b20b632484366c6f275360743feff1882eb Mon Sep 17 00:00:00 2001 From: Brad Rydzewski Date: Tue, 7 Apr 2015 22:10:44 -0700 Subject: [PATCH] added example RPC client --- datastore/rpc/build.go | 121 ++++++++++++++++++++++++++++++++++++++++ datastore/rpc/client.go | 23 ++++++++ datastore/rpc/repo.go | 102 +++++++++++++++++++++++++++++++++ datastore/rpc/task.go | 78 ++++++++++++++++++++++++++ datastore/rpc/token.go | 38 +++++++++++++ datastore/rpc/user.go | 102 +++++++++++++++++++++++++++++++++ 6 files changed, 464 insertions(+) create mode 100644 datastore/rpc/build.go create mode 100644 datastore/rpc/client.go create mode 100644 datastore/rpc/repo.go create mode 100644 datastore/rpc/task.go create mode 100644 datastore/rpc/token.go create mode 100644 datastore/rpc/user.go diff --git a/datastore/rpc/build.go b/datastore/rpc/build.go new file mode 100644 index 000000000..603d8ed1d --- /dev/null +++ b/datastore/rpc/build.go @@ -0,0 +1,121 @@ +package rpc + +import ( + "time" + + "github.com/drone/drone/common" +) + +type GetBuildReq struct { + Repo string + Build int +} + +type GetBuildResp struct { + Build *common.Build +} + +func (c *Client) GetBuild(repo string, build int) (*common.Build, error) { + req := &GetBuildReq{repo, build} + res := &GetBuildResp{} + err := c.Call("Datastore.GetBuild", req, res) + return res.Build, err +} + +type GetBuildListReq struct { + Repo string +} + +type GetBuildListResp struct { + Builds []*common.Build +} + +func (c *Client) GetBuildList(repo string) ([]*common.Build, error) { + req := &GetBuildListReq{repo} + res := &GetBuildListResp{} + err := c.Call("Datastore.GetBuildList", req, res) + return res.Builds, err +} + +type GetBuildLastReq struct { + Repo string +} + +type GetBuildLastResp struct { + Build *common.Build +} + +func (c *Client) GetBuildLast(repo string) (*common.Build, error) { + req := &GetBuildLastReq{repo} + res := &GetBuildLastResp{} + err := c.Call("Datastore.GetBuildLast", req, res) + return res.Build, err +} + +type GetBuildStatusReq struct { + Repo string + Build int + Status string +} + +type GetBuildStatusResp struct { + Status *common.Status +} + +func (c *Client) GetBuildStatus(repo string, build int, status string) (*common.Status, error) { + req := &GetBuildStatusReq{repo, build, status} + res := &GetBuildStatusResp{} + err := c.Call("Datastore.GetBuildStatus", req, res) + return res.Status, err +} + +type GetBuildStatusListReq struct { + Repo string + Build int +} + +type GetBuildStatusListResp struct { + Statuses []*common.Status +} + +func (c *Client) GetBuildStatusList(repo string, build int) ([]*common.Status, error) { + req := &GetBuildStatusListReq{repo, build} + res := &GetBuildStatusListResp{} + err := c.Call("Datastore.GetBuildStatusList", req, res) + return res.Statuses, err +} + +type InsertBuildReq struct { + Repo string + Build *common.Build +} + +func (c *Client) InsertBuild(repo string, build *common.Build) error { + build.Created = time.Now().UTC().Unix() + build.Updated = time.Now().UTC().Unix() + // TODO need to capture the sequential build number that is generated + req := &InsertBuildReq{repo, build} + return c.Call("Datastore.InsertBuild", req, nil) +} + +type InsertBuildStatusReq struct { + Repo string + Build int + Status *common.Status +} + +func (c *Client) InsertBuildStatus(repo string, build int, status *common.Status) error { + req := &InsertBuildStatusReq{repo, build, status} + return c.Call("Datastore.InsertBuildStatus", req, nil) +} + +type UpdateBuildReq struct { + Repo string + Build *common.Build +} + +func (c *Client) UpdateBuild(repo string, build *common.Build) error { + build.Updated = time.Now().UTC().Unix() + req := &InsertBuildReq{repo, build} + return c.Call("Datastore.UpdateBuild", req, nil) +} diff --git a/datastore/rpc/client.go b/datastore/rpc/client.go new file mode 100644 index 000000000..655508fb6 --- /dev/null +++ b/datastore/rpc/client.go @@ -0,0 +1,23 @@ +package rpc + +import ( + "net" + "net/rpc" +) + +type Client struct { + *rpc.Client +} + +// New returns a new, remote datastore backend that connects +// via tcp and exchanges data using Go's RPC mechanism. +func New(address string) (*Client, error) { + conn, err := net.Dial("tcp", address) + if err != nil { + return nil, err + } + client := &Client{ + rpc.NewClient(conn), + } + return client, nil +} diff --git a/datastore/rpc/repo.go b/datastore/rpc/repo.go new file mode 100644 index 000000000..7351d7625 --- /dev/null +++ b/datastore/rpc/repo.go @@ -0,0 +1,102 @@ +package rpc + +import ( + "time" + + "github.com/drone/drone/common" +) + +type GetRepoReq struct { + Repo string +} + +type GetRepoResp struct { + Repo *common.Repo +} + +func (c *Client) GetRepo(repo string) (*common.Repo, error) { + req := &GetRepoReq{repo} + res := &GetRepoResp{} + err := c.Call("Datastore.GetRepo", req, res) + return res.Repo, err +} + +type GetRepoParamsReq struct { + Repo string +} + +type GetRepoParamsResp struct { + Params map[string]string +} + +func (c *Client) GetRepoParams(repo string) (map[string]string, error) { + req := &GetRepoParamsReq{repo} + res := &GetRepoParamsResp{} + err := c.Call("Datastore.GetRepoParams", req, res) + return res.Params, err +} + +type GetRepoKeysReq struct { + Repo string +} + +type GetRepoKeysResp struct { + Keys *common.Keypair +} + +func (c *Client) GetRepoKeys(repo string) (*common.Keypair, error) { + req := &GetRepoKeysReq{repo} + res := &GetRepoKeysResp{} + err := c.Call("Datastore.GetRepoKeys", req, res) + return res.Keys, err +} + +type UpdateRepoReq struct { + Repo *common.Repo +} + +func (c *Client) UpdateRepo(repo *common.Repo) error { + repo.Updated = time.Now().UTC().Unix() + req := &UpdateRepoReq{repo} + return c.Call("Datastore.UpdateRepo", req, nil) +} + +type InsertRepoReq struct { + User *common.User + Repo *common.Repo +} + +func (c *Client) InsertRepo(user *common.User, repo *common.Repo) error { + repo.Created = time.Now().UTC().Unix() + repo.Updated = time.Now().UTC().Unix() + req := &InsertRepoReq{user, repo} + return c.Call("Datastore.InsertRepo", req, nil) +} + +type UpsertRepoParamsReq struct { + Repo string +} + +func (c *Client) UpsertRepoParams(repo string, params map[string]string) error { + req := &UpsertRepoParamsReq{repo} + return c.Call("Datastore.UpsertRepoParams", req, nil) +} + +type UpsertRepoKeysReq struct { + Repo string + Keys *common.Keypair +} + +func (c *Client) UpsertRepoKeys(repo string, keypair *common.Keypair) error { + req := &UpsertRepoKeysReq{repo, keypair} + return c.Call("Datastore.UpsertRepoKeys", req, nil) +} + +type DeleteRepoReq struct { + Repo *common.Repo +} + +func (c *Client) DeleteRepo(repo *common.Repo) error { + req := &DeleteRepoReq{repo} + return c.Call("Datastore.DeleteRepo", req, nil) +} diff --git a/datastore/rpc/task.go b/datastore/rpc/task.go new file mode 100644 index 000000000..51e3d28c2 --- /dev/null +++ b/datastore/rpc/task.go @@ -0,0 +1,78 @@ +package rpc + +import ( + "github.com/drone/drone/common" +) + +type GetTaskReq struct { + Repo string + Build int + Task int +} + +type GetTaskResp struct { + Task *common.Task +} + +func (c *Client) GetTask(repo string, build int, task int) (*common.Task, error) { + req := &GetTaskReq{repo, build, task} + res := &GetTaskResp{} + err := c.Call("Datastore.GetTask", req, res) + return res.Task, err +} + +type GetTaskLogsReq struct { + Repo string + Build int + Task int +} + +type GetTaskLogsResp struct { + Logs []byte +} + +func (c *Client) GetTaskLogs(repo string, build int, task int) ([]byte, error) { + req := &GetTaskLogsReq{repo, build, task} + res := &GetTaskLogsResp{} + err := c.Call("Datastore.GetTaskLogs", req, res) + return res.Logs, err +} + +type GetTaskListReq struct { + Repo string + Build int +} + +type GetTaskListResp struct { + Tasks []*common.Task +} + +func (c *Client) GetTaskList(repo string, build int) ([]*common.Task, error) { + req := &GetTaskListReq{repo, build} + res := &GetTaskListResp{} + err := c.Call("Datastore.GetTaskList", req, res) + return res.Tasks, err +} + +type UpsertTaskReq struct { + Repo string + Build int + Task *common.Task +} + +func (c *Client) UpsertTask(repo string, build int, task *common.Task) error { + req := &UpsertTaskReq{repo, build, task} + return c.Call("Datastore.UpsertTask", req, nil) +} + +type UpsertTaskLogsReq struct { + Repo string + Build int + Task int + Logs []byte +} + +func (c *Client) UpsertTaskLogs(repo string, build int, task int, log []byte) error { + req := &UpsertTaskLogsReq{repo, build, task, log} + return c.Call("Datastore.UpsertTaskLogs", req, nil) +} diff --git a/datastore/rpc/token.go b/datastore/rpc/token.go new file mode 100644 index 000000000..bef3dd946 --- /dev/null +++ b/datastore/rpc/token.go @@ -0,0 +1,38 @@ +package rpc + +import ( + "github.com/drone/drone/common" +) + +type GetTokenReq struct { + Sha string +} + +type GetTokenResp struct { + Token *common.Token +} + +func (c *Client) GetToken(sha string) (*common.Token, error) { + req := &GetTokenReq{sha} + res := &GetTokenResp{} + err := c.Call("Datastore.GetToken", req, res) + return res.Token, err +} + +type InsertTokenReq struct { + Token *common.Token +} + +func (c *Client) InsertToken(token *common.Token) error { + req := &InsertTokenReq{token} + return c.Call("Datastore.InsertToken", req, nil) +} + +type DeleteTokenReq struct { + Token *common.Token +} + +func (c *Client) DeleteToken(token *common.Token) error { + req := &DeleteTokenReq{token} + return c.Call("Datastore.DeleteToken", req, nil) +} diff --git a/datastore/rpc/user.go b/datastore/rpc/user.go new file mode 100644 index 000000000..7e11c6d7f --- /dev/null +++ b/datastore/rpc/user.go @@ -0,0 +1,102 @@ +package rpc + +import ( + "time" + + "github.com/drone/drone/common" +) + +type GetUserReq struct { + Login string +} + +type GetUserResp struct { + User *common.User +} + +func (c *Client) GetUser(login string) (*common.User, error) { + req := &GetUserReq{login} + res := &GetUserResp{} + err := c.Call("Datastore.GetUser", req, res) + return res.User, err +} + +type GetUserTokensReq struct { + Login string +} + +type GetUserTokensResp struct { + Tokens []*common.Token +} + +func (c *Client) GetUserTokens(login string) ([]*common.Token, error) { + req := &GetUserTokensReq{login} + res := &GetUserTokensResp{} + err := c.Call("Datastore.GetUserTokens", req, res) + return res.Tokens, err +} + +type GetUserReposReq struct { + Login string +} + +type GetUserReposResp struct { + Repos []*common.Repo +} + +func (c *Client) GetUserRepos(login string) ([]*common.Repo, error) { + req := &GetUserReposReq{login} + res := &GetUserReposResp{} + err := c.Call("Datastore.GetUserRepos", req, res) + return res.Repos, err +} + +type GetUserCountResp struct { + Count int +} + +func (c *Client) GetUserCount() (int, error) { + res := &GetUserCountResp{} + err := c.Call("Datastore.GetUserCount", nil, res) + return res.Count, err +} + +type GetUserListResp struct { + Users []*common.User +} + +func (c *Client) GetUserList() ([]*common.User, error) { + res := &GetUserListResp{} + err := c.Call("Datastore.GetUserList", nil, res) + return res.Users, err +} + +type UpdateUserReq struct { + User *common.User +} + +func (c *Client) UpdateUser(user *common.User) error { + user.Updated = time.Now().UTC().Unix() + req := &UpdateUserReq{user} + return c.Call("Datastore.UpdateUser", req, nil) +} + +type InsertUserReq struct { + User *common.User +} + +func (c *Client) InsertUser(user *common.User) error { + user.Created = time.Now().UTC().Unix() + user.Updated = time.Now().UTC().Unix() + req := &InsertUserReq{user} + return c.Call("Datastore.InsertUser", req, nil) +} + +type DeleteUserReq struct { + User *common.User +} + +func (c *Client) DeleteUser(user *common.User) error { + req := &DeleteUserReq{user} + return c.Call("Datastore.DeleteUser", req, nil) +}