removed remote & server database func now that we are using conf files

This commit is contained in:
Brad Rydzewski 2014-09-24 21:45:30 -07:00
parent 3b59f80f98
commit 9a3f33b9c3
9 changed files with 6 additions and 763 deletions

View file

@ -1,107 +0,0 @@
package database
import (
"database/sql"
"github.com/drone/drone/shared/model"
"github.com/russross/meddler"
)
type RemoteManager interface {
// Find finds the Remote by ID.
Find(id int64) (*model.Remote, error)
// FindHost finds the Remote by hostname.
FindHost(name string) (*model.Remote, error)
// FindHost finds the Remote by type.
FindType(t string) (*model.Remote, error)
// List finds all registered Remotes of the system.
List() ([]*model.Remote, error)
// Insert persists the Remotes to the datastore.
Insert(server *model.Remote) error
// Update persists changes to the Remotes to the datastore.
Update(server *model.Remote) error
// Delete removes the Remotes from the datastore.
Delete(server *model.Remote) error
}
// remoteManager manages a list of remotes in a SQL database.
type remoteManager struct {
*sql.DB
}
// SQL query to retrieve a Remote by remote login.
const findRemoteQuery = `
SELECT *
FROM remotes
WHERE remote_host=?
LIMIT 1
`
// SQL query to retrieve a Remote by remote login.
const findRemoteTypeQuery = `
SELECT *
FROM remotes
WHERE remote_type=?
LIMIT 1
`
// SQL query to retrieve a list of all Remotes.
const listRemoteQuery = `
SELECT *
FROM remotes
ORDER BY remote_type
`
// SQL statement to delete a Remote by ID.
const deleteRemoteStmt = `
DELETE FROM remotes WHERE remote_id=?
`
// NewRemoteManager initiales a new RemoteManager intended to
// manage and persist servers.
func NewRemoteManager(db *sql.DB) RemoteManager {
return &remoteManager{db}
}
func (db *remoteManager) Find(id int64) (*model.Remote, error) {
dst := model.Remote{}
err := meddler.Load(db, "remotes", &dst, id)
return &dst, err
}
func (db *remoteManager) FindHost(host string) (*model.Remote, error) {
dst := model.Remote{}
err := meddler.QueryRow(db, &dst, findRemoteQuery, host)
return &dst, err
}
func (db *remoteManager) FindType(t string) (*model.Remote, error) {
dst := model.Remote{}
err := meddler.QueryRow(db, &dst, findRemoteTypeQuery, t)
return &dst, err
}
func (db *remoteManager) List() ([]*model.Remote, error) {
var dst []*model.Remote
err := meddler.QueryAll(db, &dst, listRemoteQuery)
return dst, err
}
func (db *remoteManager) Insert(remote *model.Remote) error {
return meddler.Insert(db, "remotes", remote)
}
func (db *remoteManager) Update(remote *model.Remote) error {
return meddler.Update(db, "remotes", remote)
}
func (db *remoteManager) Delete(remote *model.Remote) error {
_, err := db.Exec(deleteRemoteStmt, remote.ID)
return err
}

View file

@ -1,196 +0,0 @@
package database
import (
"database/sql"
"testing"
"github.com/drone/drone/shared/model"
)
func TestRemoteFind(t *testing.T) {
setup()
defer teardown()
remotes := NewRemoteManager(db)
remote, err := remotes.Find(1)
if err != nil {
t.Errorf("Want Remote from ID, got %s", err)
}
testRemote(t, remote)
}
func TestRemoteFindHost(t *testing.T) {
setup()
defer teardown()
remotes := NewRemoteManager(db)
remote, err := remotes.FindHost("github.drone.io")
if err != nil {
t.Errorf("Want Remote from Host, got %s", err)
}
testRemote(t, remote)
}
func TestRemoteList(t *testing.T) {
setup()
defer teardown()
remotes := NewRemoteManager(db)
all, err := remotes.List()
if err != nil {
t.Errorf("Want Remotes, got %s", err)
}
var got, want = len(all), 2
if got != want {
t.Errorf("Want %v remotes, got %v", want, got)
}
testRemote(t, all[0])
}
func TestRemoteInsert(t *testing.T) {
setup()
defer teardown()
remote := &model.Remote{ID: 0, Type: "bitbucket.org", Host: "bitbucket.org", URL: "https://bitbucket.org", API: "https://bitbucket.org", Client: "abc", Secret: "123", Open: false}
remotes := NewRemoteManager(db)
if err := remotes.Insert(remote); err != nil {
t.Errorf("Want Remote created, got %s", err)
}
var got, want = remote.ID, int64(3)
if want != got {
t.Errorf("Want Remote ID %v, got %v", want, got)
}
// verify unique remote name constraint
var err = remotes.Insert(&model.Remote{Type: "bitbucket.org", Host: "butbucket.com"})
if err == nil {
t.Error("Want Type unique constraint violated")
}
}
func TestRemoteUpdate(t *testing.T) {
setup()
defer teardown()
remotes := NewRemoteManager(db)
remote, err := remotes.Find(1)
if err != nil {
t.Errorf("Want Remote from ID, got %s", err)
}
// update the remote's address
remote.Client = "abc"
remote.Secret = "123"
remote.Host = "git.drone.io"
remote.URL = "https://git.drone.io"
remote.API = "https://git.drone.io/v3/api"
if err := remotes.Update(remote); err != nil {
t.Errorf("Want Remote updated, got %s", err)
}
updated, _ := remotes.Find(1)
var got, want = remote.Host, remote.Host
if got != want {
t.Errorf("Want updated Host %s, got %s", want, got)
}
got, want = updated.Client, remote.Client
if got != want {
t.Errorf("Want updated Client %s, got %s", want, got)
}
got, want = updated.Secret, remote.Secret
if got != want {
t.Errorf("Want updated Secret %s, got %s", want, got)
}
got, want = updated.Host, remote.Host
if got != want {
t.Errorf("Want updated Host %s, got %s", want, got)
}
got, want = updated.Host, remote.Host
if got != want {
t.Errorf("Want updated Host %s, got %s", want, got)
}
got, want = updated.URL, remote.URL
if got != want {
t.Errorf("Want updated URL %s, got %s", want, got)
}
got, want = updated.API, remote.API
if got != want {
t.Errorf("Want updated API %s, got %s", want, got)
}
}
func TestRemoteDelete(t *testing.T) {
setup()
defer teardown()
remotes := NewRemoteManager(db)
remote, err := remotes.Find(1)
if err != nil {
t.Errorf("Want Remote from ID, got %s", err)
}
// delete the remote
if err := remotes.Delete(remote); err != nil {
t.Errorf("Want Remote deleted, got %s", err)
}
// check to see if the deleted remote is actually gone
if _, err := remotes.Find(1); err != sql.ErrNoRows {
t.Errorf("Want ErrNoRows, got %s", err)
}
}
func testRemote(t *testing.T, remote *model.Remote) {
var got, want = remote.Host, "github.drone.io"
if got != want {
t.Errorf("Want Host %v, got %v", want, got)
}
got, want = remote.Type, "enterprise.github.com"
if got != want {
t.Errorf("Want Type %v, got %v", want, got)
}
got, want = remote.URL, "https://github.drone.io"
if got != want {
t.Errorf("Want URL %v, got %v", want, got)
}
got, want = remote.API, "https://github.drone.io/v3/api"
if got != want {
t.Errorf("Want API %v, got %v", want, got)
}
got, want = remote.Client, "f0b461ca586c27872b43a0685cbc2847"
if got != want {
t.Errorf("Want Access Token %v, got %v", want, got)
}
got, want = remote.Secret, "976f22a5eef7caacb7e678d6c52f49b1"
if got != want {
t.Errorf("Want Token Secret %v, got %v", want, got)
}
var gotBool, wantBool = remote.Open, true
if gotBool != wantBool {
t.Errorf("Want Open %v, got %v", wantBool, gotBool)
}
var gotInt64, wantInt64 = remote.ID, int64(1)
if gotInt64 != wantInt64 {
t.Errorf("Want ID %v, got %v", wantInt64, gotInt64)
}
}

View file

@ -1,111 +0,0 @@
package database
import (
"database/sql"
"github.com/drone/drone/shared/model"
"github.com/russross/meddler"
)
type ServerManager interface {
// Find finds the Server by ID.
Find(id int64) (*model.Server, error)
// FindName finds the Server by name.
FindName(name string) (*model.Server, error)
// FindName finds the Server by name.
FindSMTP() (*model.SMTPServer, error)
// List finds all registered Servers of the system.
List() ([]*model.Server, error)
// Insert persists the Server to the datastore.
Insert(server *model.Server) error
// Update persists changes to the Server to the datastore.
Update(server *model.Server) error
// UpdateSMTP persists changes to the SMTP Server to the datastore.
UpdateSMTP(server *model.SMTPServer) error
// Delete removes the Server from the datastore.
Delete(server *model.Server) error
}
// serverManager manages a list of users in a SQL database.
type serverManager struct {
*sql.DB
}
// SQL query to retrieve a Server by remote login.
const findServerQuery = `
SELECT *
FROM servers
WHERE server_name=?
LIMIT 1
`
// SQL query to retrieve a list of all Servers.
const listServerQuery = `
SELECT *
FROM servers
`
// SQL statement to delete a Server by ID.
const deleteServerStmt = `
DELETE FROM servers WHERE server_id=?
`
// NewServerManager initiales a new ServerManager intended to
// manage and persist servers.
func NewServerManager(db *sql.DB) ServerManager {
return &serverManager{db}
}
func (db *serverManager) Find(id int64) (*model.Server, error) {
dst := model.Server{}
err := meddler.Load(db, "servers", &dst, id)
return &dst, err
}
func (db *serverManager) FindName(name string) (*model.Server, error) {
dst := model.Server{}
err := meddler.QueryRow(db, &dst, findServerQuery, name)
return &dst, err
}
func (db *serverManager) FindSMTP() (*model.SMTPServer, error) {
dst := model.SMTPServer{}
err := meddler.Load(db, "smtp", &dst, 1)
if err != nil && err != sql.ErrNoRows {
return &dst, err
}
return &dst, nil
}
func (db *serverManager) List() ([]*model.Server, error) {
var dst []*model.Server
err := meddler.QueryAll(db, &dst, listServerQuery)
return dst, err
}
func (db *serverManager) Insert(server *model.Server) error {
return meddler.Insert(db, "servers", server)
}
func (db *serverManager) Update(server *model.Server) error {
return meddler.Update(db, "servers", server)
}
func (db *serverManager) UpdateSMTP(server *model.SMTPServer) error {
server.ID = 0
meddler.Insert(db, "smtp", server)
server.ID = 1
return meddler.Update(db, "smtp", server)
}
func (db *serverManager) Delete(server *model.Server) error {
_, err := db.Exec(deleteServerStmt, server.ID)
return err
}

View file

@ -1,201 +0,0 @@
package database
import (
"database/sql"
"testing"
"github.com/drone/drone/shared/model"
)
func TestServerFind(t *testing.T) {
setup()
defer teardown()
servers := NewServerManager(db)
server, err := servers.Find(1)
if err != nil {
t.Errorf("Want Server from ID, got %s", err)
}
testServer(t, server)
}
func TestServerFindName(t *testing.T) {
setup()
defer teardown()
servers := NewServerManager(db)
server, err := servers.FindName("docker1")
if err != nil {
t.Errorf("Want Server from Host, got %s", err)
}
testServer(t, server)
}
func TestServerFindSMTP(t *testing.T) {
setup()
defer teardown()
server := model.SMTPServer{
From: "foo@bar.com",
Host: "127.0.0.1",
User: "foo"}
servers := NewServerManager(db)
if err := servers.UpdateSMTP(&server); err != nil {
t.Errorf("Want SMTP server inserted, got %s", err)
}
if inserted, err := servers.FindSMTP(); err != nil {
t.Errorf("Want SMTP server, got %s", err)
} else if inserted.ID == 0 {
t.Errorf("Want SMTP server inserted")
}
server.Host = "0.0.0.0"
server.User = "bar"
err := servers.UpdateSMTP(&server)
if err := servers.UpdateSMTP(&server); err != nil {
t.Errorf("Want SMTP server updated, got %s", err)
}
updated, err := servers.FindSMTP()
if err != nil {
t.Errorf("Want SMTP server, got %s", err)
}
var want, got = server.Host, updated.Host
if want != got {
t.Errorf("Want SMTP Host %v, got %v", want, got)
}
}
func TestServerList(t *testing.T) {
setup()
defer teardown()
servers := NewServerManager(db)
all, err := servers.List()
if err != nil {
t.Errorf("Want Servers, got %s", err)
}
var got, want = len(all), 2
if got != want {
t.Errorf("Want %v Servers, got %v", want, got)
}
testServer(t, all[0])
}
func TestServerInsert(t *testing.T) {
setup()
defer teardown()
server := &model.Server{Host: "tcp://127.0.0.1:4243", Name: "docker3"}
servers := NewServerManager(db)
if err := servers.Insert(server); err != nil {
t.Errorf("Want Server created, got %s", err)
}
var got, want = server.ID, int64(3)
if want != got {
t.Errorf("Want Server ID %v, got %v", want, got)
}
// verify unique server name constraint
var err = servers.Insert(&model.Server{Host: "tcp://127.0.0.1:4243", Name: "docker3"})
if err == nil {
t.Error("Want Name unique constraint violated")
}
}
func TestServerUpdate(t *testing.T) {
setup()
defer teardown()
servers := NewServerManager(db)
server, err := servers.Find(1)
if err != nil {
t.Errorf("Want Server from ID, got %s", err)
}
// update the server's address
server.Host = "tcp://1.2.3.4:4243"
server.User = "docker"
server.Pass = "123456"
if err := servers.Update(server); err != nil {
t.Errorf("Want Server updated, got %s", err)
}
updated, _ := servers.Find(1)
var got, want = server.Host, server.Host
if got != want {
t.Errorf("Want updated Host %s, got %s", want, got)
}
got, want = updated.User, server.User
if got != want {
t.Errorf("Want updated User %s, got %s", want, got)
}
got, want = updated.Pass, server.Pass
if got != want {
t.Errorf("Want updated Pass %s, got %s", want, got)
}
}
func TestServerDelete(t *testing.T) {
setup()
defer teardown()
servers := NewServerManager(db)
server, err := servers.Find(1)
if err != nil {
t.Errorf("Want Server from ID, got %s", err)
}
// delete the server
if err := servers.Delete(server); err != nil {
t.Errorf("Want Server deleted, got %s", err)
}
// check to see if the deleted server is actually gone
if _, err := servers.Find(1); err != sql.ErrNoRows {
t.Errorf("Want ErrNoRows, got %s", err)
}
}
// testServer is a helper function that compares the server
// to an expected set of fixed field values.
func testServer(t *testing.T, server *model.Server) {
var got, want = server.Host, "tcp://127.0.0.1:4243"
if got != want {
t.Errorf("Want Host %v, got %v", want, got)
}
got, want = server.Name, "docker1"
if got != want {
t.Errorf("Want Name %v, got %v", want, got)
}
got, want = server.User, "root"
if got != want {
t.Errorf("Want User %v, got %v", want, got)
}
got, want = server.Pass, "pa55word"
if got != want {
t.Errorf("Want Pass %v, got %v", want, got)
}
got, want = server.Cert, "/path/to/cert.key"
if got != want {
t.Errorf("Want Cert %v, got %v", want, got)
}
var gotInt64, wantInt64 = server.ID, int64(1)
if gotInt64 != wantInt64 {
t.Errorf("Want ID %v, got %v", wantInt64, gotInt64)
}
}

View file

@ -16,12 +16,11 @@ type HookHandler struct {
users database.UserManager
repos database.RepoManager
commits database.CommitManager
remotes database.RemoteManager
queue chan *model.Request
}
func NewHookHandler(users database.UserManager, repos database.RepoManager, commits database.CommitManager, remotes database.RemoteManager, queue chan *model.Request) *HookHandler {
return &HookHandler{users, repos, commits, remotes, queue}
func NewHookHandler(users database.UserManager, repos database.RepoManager, commits database.CommitManager, queue chan *model.Request) *HookHandler {
return &HookHandler{users, repos, commits, queue}
}
// PostHook receives a post-commit hook from GitHub, Bitbucket, etc

View file

@ -1,39 +0,0 @@
package handler
import (
"encoding/json"
"net/http"
"github.com/drone/drone/plugin/remote"
"github.com/drone/drone/server/database"
"github.com/drone/drone/server/session"
"github.com/gorilla/pat"
)
type RemoteHandler struct {
users database.UserManager
remotes database.RemoteManager
sess session.Session
}
func NewRemoteHandler(users database.UserManager, remotes database.RemoteManager, sess session.Session) *RemoteHandler {
return &RemoteHandler{users, remotes, sess}
}
// GetRemoteLogins gets all remote logins.
// GET /api/remotes/logins
func (h *RemoteHandler) GetRemoteLogins(w http.ResponseWriter, r *http.Request) error {
var list = remote.Registered()
var logins []interface{}
for _, item := range list {
logins = append(logins, struct {
Type string `json:"type"`
Host string `json:"host"`
}{item.GetKind(), item.GetHost()})
}
return json.NewEncoder(w).Encode(&logins)
}
func (h *RemoteHandler) Register(r *pat.Router) {
r.Get("/v1/logins", errorHandler(h.GetRemoteLogins))
}

View file

@ -15,7 +15,6 @@ import (
)
type RepoHandler struct {
remotes database.RemoteManager
commits database.CommitManager
perms database.PermManager
repos database.RepoManager
@ -23,8 +22,8 @@ type RepoHandler struct {
}
func NewRepoHandler(repos database.RepoManager, commits database.CommitManager,
perms database.PermManager, sess session.Session, remotes database.RemoteManager) *RepoHandler {
return &RepoHandler{remotes, commits, perms, repos, sess}
perms database.PermManager, sess session.Session) *RepoHandler {
return &RepoHandler{commits, perms, repos, sess}
}
// GetRepo gets the named repository.

View file

@ -1,97 +0,0 @@
package handler
import (
"encoding/json"
"net/http"
"github.com/drone/drone/server/database"
"github.com/drone/drone/server/session"
"github.com/drone/drone/shared/model"
"github.com/gorilla/pat"
)
type ServerHandler struct {
servers database.ServerManager
sess session.Session
}
func NewServerHandler(servers database.ServerManager, sess session.Session) *ServerHandler {
return &ServerHandler{servers, sess}
}
// GetServers gets all servers.
// GET /api/servers
func (h *ServerHandler) GetServers(w http.ResponseWriter, r *http.Request) error {
// get the user form the session
user := h.sess.User(r)
switch {
case user == nil:
return notAuthorized{}
case user.Admin == false:
return forbidden{}
}
// get all servers
servers, err := h.servers.List()
if err != nil {
return internalServerError{err}
}
return json.NewEncoder(w).Encode(servers)
}
// PostServer creates a new server.
// POST /api/servers
func (h *ServerHandler) PostServer(w http.ResponseWriter, r *http.Request) error {
// get the user form the session
user := h.sess.User(r)
switch {
case user == nil:
return notAuthorized{}
case user.Admin == false:
return forbidden{}
}
// unmarshal the server from the payload
defer r.Body.Close()
in := model.Server{}
if err := json.NewDecoder(r.Body).Decode(&in); err != nil {
return badRequest{err}
}
// insert the server in the database
if err := h.servers.Insert(&in); err != nil {
return internalServerError{err}
}
return json.NewEncoder(w).Encode(&in)
}
// DeleteServers deletes the named server.
// GET /api/servers/:name
func (h *ServerHandler) DeleteServer(w http.ResponseWriter, r *http.Request) error {
name := r.FormValue(":name")
// get the user form the session
user := h.sess.User(r)
switch {
case user == nil:
return notAuthorized{}
case user.Admin == false:
return forbidden{}
}
// get the server
server, err := h.servers.FindName(name)
if err != nil {
return notFound{err}
}
if err := h.servers.Delete(server); err != nil {
return internalServerError{err}
}
w.WriteHeader(http.StatusNoContent)
return nil
}
func (h *ServerHandler) Register(r *pat.Router) {
r.Delete("/v1/servers/:name", errorHandler(h.DeleteServer))
r.Post("/v1/servers", errorHandler(h.PostServer))
r.Get("/v1/servers", errorHandler(h.GetServers))
}

View file

@ -95,8 +95,6 @@ func main() {
users := database.NewUserManager(db)
perms := database.NewPermManager(db)
commits := database.NewCommitManager(db)
servers := database.NewServerManager(db)
remotes := database.NewRemoteManager(db)
// message broker
pubsub := pubsub.NewPubSub()
@ -127,13 +125,11 @@ func main() {
router := pat.New()
handler.NewUsersHandler(users, sess).Register(router)
handler.NewUserHandler(users, repos, commits, sess).Register(router)
handler.NewHookHandler(users, repos, commits, remotes, queue).Register(router)
handler.NewHookHandler(users, repos, commits, queue).Register(router)
handler.NewLoginHandler(users, repos, perms, sess, open).Register(router)
handler.NewCommitHandler(users, repos, commits, perms, sess, queue).Register(router)
handler.NewRepoHandler(repos, commits, perms, sess, remotes).Register(router)
handler.NewRepoHandler(repos, commits, perms, sess).Register(router)
handler.NewBadgeHandler(repos, commits).Register(router)
handler.NewServerHandler(servers, sess).Register(router)
handler.NewRemoteHandler(users, remotes, sess).Register(router)
handler.NewWsHandler(repos, commits, perms, sess, pubsub).Register(router)
box := rice.MustFindBox("app/")