diff --git a/drone-go/drone/client.go b/drone-go/drone/client.go index bcc527b26..2d9f9efa2 100644 --- a/drone-go/drone/client.go +++ b/drone-go/drone/client.go @@ -35,11 +35,7 @@ const ( pathUsers = "%s/api/users" pathUser = "%s/api/users/%s" pathBuildQueue = "%s/api/builds" - pathServers = "%s/api/servers" - pathServer = "%s/api/servers/%s" - pathScalerPause = "%s/api/pause" - pathScalerResume = "%s/api/resume" - pathVarz = "%s/varz" + pathQueue = "%s/api/queue" pathVersion = "%s/version" ) @@ -48,53 +44,6 @@ type client struct { addr string } -// // Options provides a list of client options. -// type Options struct { -// token string -// proxy string -// pool *x509.CertPool -// conf *tls.Config -// skip bool -// } -// -// // Option defines client options. -// type Option func(opts *Options) -// -// // WithToken returns an option to set the token. -// func WithToken(token string) Option { -// return func(opts *Options) { -// opts.token = token -// } -// } -// -// // WithTLS returns an option to use custom tls configuration. -// func WithTLS(conf *tls.Config) Option { -// return func(opts *Options) { -// opts.conf = conf -// } -// } -// -// // WithSocks returns a client option to provide a socks5 proxy. -// func WithSocks(proxy string) Option { -// return func(opts *Options) { -// opts.proxy = proxy -// } -// } -// -// // WithSkipVerify returns a client option to skip ssl verification. -// func WithSkipVerify(skip bool) Option { -// return func(opts *Options) { -// opts.skip = skip -// } -// } -// -// // WithCertPool returns a client option to provide a custom cert pool. -// func WithCertPool(pool *x509.CertPool) Option { -// return func(opts *Options) { -// opts.pool = pool -// } -// } - // New returns a client at the specified url. func New(uri string) Client { return &client{http.DefaultClient, strings.TrimSuffix(uri, "/")} @@ -406,52 +355,9 @@ func (c *client) SecretDelete(owner, name, secret string) error { return c.delete(uri) } -// Server returns the named servers details. -func (c *client) Server(name string) (*Server, error) { - out := new(Server) - uri := fmt.Sprintf(pathServer, c.addr, name) - err := c.get(uri, &out) - return out, err -} - -// ServerList returns a list of all active build servers. -func (c *client) ServerList() ([]*Server, error) { - var out []*Server - uri := fmt.Sprintf(pathServers, c.addr) - err := c.get(uri, &out) - return out, err -} - -// ServerCreate creates a new server. -func (c *client) ServerCreate() (*Server, error) { - out := new(Server) - uri := fmt.Sprintf(pathServers, c.addr) - err := c.post(uri, nil, out) - return out, err -} - -// ServerDelete terminates a server. -func (c *client) ServerDelete(name string) error { - uri := fmt.Sprintf(pathServer, c.addr, name) - return c.delete(uri) -} - -// AutoscalePause pauses the autoscaler. -func (c *client) AutoscalePause() error { - uri := fmt.Sprintf(pathScalerPause, c.addr) - return c.post(uri, nil, nil) -} - -// AutoscaleResume resumes the autoscaler. -func (c *client) AutoscaleResume() error { - uri := fmt.Sprintf(pathScalerResume, c.addr) - return c.post(uri, nil, nil) -} - -// AutoscaleVersion resumes the autoscaler. -func (c *client) AutoscaleVersion() (*Version, error) { - out := new(Version) - uri := fmt.Sprintf(pathVersion, c.addr) +func (c *client) QueueInfo() (*Info, error) { + out := new(Info) + uri := fmt.Sprintf(pathQueue+"/info", c.addr) err := c.get(uri, out) return out, err } diff --git a/drone-go/drone/client_test.go b/drone-go/drone/client_test.go new file mode 100644 index 000000000..0e3b6d640 --- /dev/null +++ b/drone-go/drone/client_test.go @@ -0,0 +1,46 @@ +package drone + +import ( + "fmt" + "net/http" + "net/http/httptest" + "testing" +) + +func Test_QueueInfo(t *testing.T) { + fixtureHandler := func(w http.ResponseWriter, r *http.Request) { + fmt.Fprint(w, `{ + "pending": null, + "running": [ + { + "id": "4696", + "data": "", + "labels": { + "platform": "linux/amd64", + "repo": "laszlocph/drone-oss-08" + }, + "Dependencies": [], + "DepStatus": {}, + "RunOn": null + } + ], + "stats": { + "worker_count": 3, + "pending_count": 0, + "running_count": 1, + "completed_count": 0 + }, + "Paused": false + }`) + } + + ts := httptest.NewServer(http.HandlerFunc(fixtureHandler)) + defer ts.Close() + + client := NewClient(ts.URL, http.DefaultClient) + + info, err := client.QueueInfo() + if info.Stats.Workers != 3 { + t.Errorf("Unexpected worker count: %v, %v", info, err) + } +} diff --git a/drone-go/drone/interface.go b/drone-go/drone/interface.go index 0b24d7f10..1682bd339 100644 --- a/drone-go/drone/interface.go +++ b/drone-go/drone/interface.go @@ -123,24 +123,6 @@ type Client interface { // SecretDelete deletes a secret. SecretDelete(owner, name, secret string) error - // Server returns the named servers details. - Server(name string) (*Server, error) - - // ServerList returns a list of all active build servers. - ServerList() ([]*Server, error) - - // ServerCreate creates a new server. - ServerCreate() (*Server, error) - - // ServerDelete terminates a server. - ServerDelete(name string) error - - // AutoscalePause pauses the autoscaler. - AutoscalePause() error - - // AutoscaleResume resumes the autoscaler. - AutoscaleResume() error - - // AutoscaleVersion returns the autoscaler version. - AutoscaleVersion() (*Version, error) + // QueueInfo returns the queue state. + QueueInfo() (*Info, error) } diff --git a/drone-go/drone/types.go b/drone-go/drone/types.go index b8050008b..7919a8748 100644 --- a/drone-go/drone/types.go +++ b/drone-go/drone/types.go @@ -140,33 +140,21 @@ type ( Email string `json:"author_email,omitempty"` } - // Server represents a server node. - Server struct { - ID string `json:"id"` - Provider string `json:"provider"` - State string `json:"state"` - Name string `json:"name"` - Image string `json:"image"` - Region string `json:"region"` - Size string `json:"size"` - Address string `json:"address"` - Capacity int `json:"capacity"` - Secret string `json:"secret"` - Error string `json:"error"` - CAKey []byte `json:"ca_key"` - CACert []byte `json:"ca_cert"` - TLSKey []byte `json:"tls_key"` - TLSCert []byte `json:"tls_cert"` - Created int64 `json:"created"` - Updated int64 `json:"updated"` - Started int64 `json:"started"` - Stopped int64 `json:"stopped"` - } - // Version provides system version details. Version struct { Source string `json:"source,omitempty"` Version string `json:"version,omitempty"` Commit string `json:"commit,omitempty"` } + + // Info provides queue stats. + Info struct { + Stats struct { + Workers int `json:"worker_count"` + Pending int `json:"pending_count"` + Running int `json:"running_count"` + Complete int `json:"completed_count"` + } `json:"stats"` + Paused bool `json:"paused,omitempty"` + } ) diff --git a/router/router.go b/router/router.go index 2fa37b3b2..2d3dc2a4e 100644 --- a/router/router.go +++ b/router/router.go @@ -140,16 +140,12 @@ func Load(mux *httptreemux.ContextMux, middleware ...gin.HandlerFunc) http.Handl ) } - info := e.Group("/api/info") + queue := e.Group("/api/queue") { - info.GET("/queue", + queue.GET("/info", session.MustAdmin(), server.GetQueueInfo, ) - } - - queue := e.Group("/api/queue") - { queue.GET("/pause", session.MustAdmin(), server.PauseQueue,