Skip to content
This repository has been archived by the owner on Jan 30, 2020. It is now read-only.

Commit

Permalink
api: use cAPI, not reg
Browse files Browse the repository at this point in the history
  • Loading branch information
bcwaldon committed Aug 15, 2014
1 parent 6e16a7f commit 07ecfed
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 76 deletions.
14 changes: 7 additions & 7 deletions api/machines.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@ import (

log "github.com/coreos/fleet/Godeps/_workspace/src/github.com/golang/glog"

"github.com/coreos/fleet/client"
"github.com/coreos/fleet/machine"
"github.com/coreos/fleet/registry"
"github.com/coreos/fleet/schema"
)

func wireUpMachinesResource(mux *http.ServeMux, prefix string, reg registry.Registry) {
func wireUpMachinesResource(mux *http.ServeMux, prefix string, cAPI client.API) {
res := path.Join(prefix, "machines")
mr := machinesResource{reg}
mr := machinesResource{cAPI}
mux.Handle(res, &mr)
}

type machinesResource struct {
reg registry.Registry
cAPI client.API
}

func (mr *machinesResource) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
Expand All @@ -39,7 +39,7 @@ func (mr *machinesResource) ServeHTTP(rw http.ResponseWriter, req *http.Request)
token = &def
}

page, err := getMachinePage(mr.reg, *token)
page, err := getMachinePage(mr.cAPI, *token)
if err != nil {
log.Errorf("Failed fetching page of Machines: %v", err)
sendError(rw, http.StatusInternalServerError, nil)
Expand All @@ -49,8 +49,8 @@ func (mr *machinesResource) ServeHTTP(rw http.ResponseWriter, req *http.Request)
sendResponse(rw, http.StatusOK, page)
}

func getMachinePage(reg registry.Registry, tok PageToken) (*schema.MachinePage, error) {
all, err := reg.Machines()
func getMachinePage(cAPI client.API, tok PageToken) (*schema.MachinePage, error) {
all, err := cAPI.Machines()
if err != nil {
return nil, err
}
Expand Down
7 changes: 5 additions & 2 deletions api/machines_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strconv"
"testing"

"github.com/coreos/fleet/client"
"github.com/coreos/fleet/machine"
"github.com/coreos/fleet/registry"
)
Expand All @@ -17,7 +18,8 @@ func TestMachinesList(t *testing.T) {
{ID: "XXX", PublicIP: "", Metadata: nil},
{ID: "YYY", PublicIP: "1.2.3.4", Metadata: map[string]string{"ping": "pong"}},
})
resource := &machinesResource{fr}
fAPI := &client.RegistryClient{fr}
resource := &machinesResource{fAPI}
rw := httptest.NewRecorder()
req, err := http.NewRequest("GET", "http://example.com", nil)
if err != nil {
Expand Down Expand Up @@ -49,7 +51,8 @@ func TestMachinesList(t *testing.T) {

func TestMachinesListBadNextPageToken(t *testing.T) {
fr := registry.NewFakeRegistry()
resource := &machinesResource{fr}
fAPI := &client.RegistryClient{fr}
resource := &machinesResource{fAPI}
rw := httptest.NewRecorder()
req, err := http.NewRequest("GET", "http://example.com/machines?nextPageToken=EwBMLg==", nil)
if err != nil {
Expand Down
8 changes: 5 additions & 3 deletions api/mux.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,18 @@ import (

log "github.com/coreos/fleet/Godeps/_workspace/src/github.com/golang/glog"

"github.com/coreos/fleet/client"
"github.com/coreos/fleet/registry"
)

func NewServeMux(reg registry.Registry) http.Handler {
sm := http.NewServeMux()
cAPI := &client.RegistryClient{reg}

prefix := "/v1-alpha"
wireUpMachinesResource(sm, prefix, reg)
wireUpStateResource(sm, prefix, reg)
wireUpUnitsResource(sm, prefix, reg)
wireUpMachinesResource(sm, prefix, cAPI)
wireUpStateResource(sm, prefix, cAPI)
wireUpUnitsResource(sm, prefix, cAPI)

sm.HandleFunc(prefix, methodNotAllowedHandler)
sm.HandleFunc("/", baseHandler)
Expand Down
11 changes: 5 additions & 6 deletions api/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@ import (

log "github.com/coreos/fleet/Godeps/_workspace/src/github.com/golang/glog"

"github.com/coreos/fleet/registry"
"github.com/coreos/fleet/client"
"github.com/coreos/fleet/schema"
)

func wireUpStateResource(mux *http.ServeMux, prefix string, reg registry.Registry) {
func wireUpStateResource(mux *http.ServeMux, prefix string, cAPI client.API) {
base := path.Join(prefix, "state")
sr := stateResource{reg, base}
sr := stateResource{cAPI, base}
mux.Handle(base, &sr)
}

type stateResource struct {
reg registry.Registry
cAPI client.API
basePath string
}

Expand All @@ -32,14 +32,13 @@ func (sr *stateResource) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
}

func (sr *stateResource) list(rw http.ResponseWriter, req *http.Request) {
uss, err := sr.reg.UnitStates()
states, err := sr.cAPI.UnitStates()
if err != nil {
log.Errorf("Failed fetching UnitStates: %v", err)
sendError(rw, http.StatusInternalServerError, nil)
return
}

states := schema.MapUnitStatesToSchemaUnitStates(uss)
page := schema.UnitStatePage{States: states}
sendResponse(rw, http.StatusOK, &page)
}
75 changes: 25 additions & 50 deletions api/units.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,19 @@ import (

log "github.com/coreos/fleet/Godeps/_workspace/src/github.com/golang/glog"

"github.com/coreos/fleet/job"
"github.com/coreos/fleet/registry"
"github.com/coreos/fleet/client"
"github.com/coreos/fleet/schema"
"github.com/coreos/fleet/unit"
)

func wireUpUnitsResource(mux *http.ServeMux, prefix string, reg registry.Registry) {
func wireUpUnitsResource(mux *http.ServeMux, prefix string, cAPI client.API) {
base := path.Join(prefix, "units")
ur := unitsResource{reg, base}
ur := unitsResource{cAPI, base}
mux.Handle(base, &ur)
mux.Handle(base+"/", &ur)
}

type unitsResource struct {
reg registry.Registry
cAPI client.API
basePath string
}

Expand Down Expand Up @@ -65,40 +63,40 @@ func (ur *unitsResource) set(rw http.ResponseWriter, req *http.Request, item str
return
}

u, err := ur.reg.Unit(item)
u, err := ur.cAPI.Unit(item)
if err != nil {
log.Errorf("Failed fetching Unit(%s) from Registry: %v", item, err)
sendError(rw, http.StatusInternalServerError, nil)
return
}

var uf *unit.UnitFile
var opts []*schema.UnitOption
if len(su.Options) > 0 {
uf = schema.MapSchemaUnitOptionsToUnitFile(su.Options)
opts = su.Options
}

// TODO(bcwaldon): Assert value of DesiredState is launched, loaded or inactive
ds := job.JobState(su.DesiredState)
ds := su.DesiredState

if u != nil {
ur.update(rw, u, ds)
} else if uf != nil {
ur.create(rw, item, ds, uf)
} else if opts != nil {
ur.create(rw, item, ds, opts)
} else {
sendError(rw, http.StatusConflict, errors.New("unit does not exist and no fileContents provided"))
}
}

func (ur *unitsResource) create(rw http.ResponseWriter, item string, ds job.JobState, uf *unit.UnitFile) {
u := job.Unit{Name: item, Unit: *uf}
if err := ur.reg.CreateUnit(&u); err != nil {
func (ur *unitsResource) create(rw http.ResponseWriter, item, ds string, opts []*schema.UnitOption) {
u := schema.Unit{Name: item, Options: opts}
if err := ur.cAPI.CreateUnit(&u); err != nil {
log.Errorf("Failed creating Unit(%s) in Registry: %v", u.Name, err)
sendError(rw, http.StatusInternalServerError, nil)
return
}

if len(ds) > 0 {
if err := ur.reg.SetUnitTargetState(u.Name, ds); err != nil {
if err := ur.cAPI.SetUnitTargetState(u.Name, ds); err != nil {
log.Errorf("Failed setting target state of Unit(%s): %v", u.Name, err)
sendError(rw, http.StatusInternalServerError, nil)
return
Expand All @@ -108,8 +106,8 @@ func (ur *unitsResource) create(rw http.ResponseWriter, item string, ds job.JobS
rw.WriteHeader(http.StatusNoContent)
}

func (ur *unitsResource) update(rw http.ResponseWriter, u *job.Unit, ds job.JobState) {
if err := ur.reg.SetUnitTargetState(u.Name, ds); err != nil {
func (ur *unitsResource) update(rw http.ResponseWriter, u *schema.Unit, ds string) {
if err := ur.cAPI.SetUnitTargetState(u.Name, ds); err != nil {
log.Errorf("Failed setting target state of Unit(%s): %v", u.Name, err)
sendError(rw, http.StatusInternalServerError, nil)
return
Expand All @@ -119,7 +117,7 @@ func (ur *unitsResource) update(rw http.ResponseWriter, u *job.Unit, ds job.JobS
}

func (ur *unitsResource) destroy(rw http.ResponseWriter, req *http.Request, item string) {
u, err := ur.reg.Unit(item)
u, err := ur.cAPI.Unit(item)
if err != nil {
log.Errorf("Failed fetching Unit(%s): %v", item, err)
sendError(rw, http.StatusInternalServerError, nil)
Expand All @@ -131,7 +129,7 @@ func (ur *unitsResource) destroy(rw http.ResponseWriter, req *http.Request, item
return
}

err = ur.reg.DestroyUnit(item)
err = ur.cAPI.DestroyUnit(item)
if err != nil {
log.Errorf("Failed destroying Unit(%s): %v", item, err)
sendError(rw, http.StatusInternalServerError, nil)
Expand All @@ -142,7 +140,7 @@ func (ur *unitsResource) destroy(rw http.ResponseWriter, req *http.Request, item
}

func (ur *unitsResource) get(rw http.ResponseWriter, req *http.Request, item string) {
u, err := ur.reg.Unit(item)
u, err := ur.cAPI.Unit(item)
if err != nil {
log.Errorf("Failed fetching Unit(%s) from Registry: %v", item, err)
sendError(rw, http.StatusInternalServerError, nil)
Expand All @@ -154,15 +152,7 @@ func (ur *unitsResource) get(rw http.ResponseWriter, req *http.Request, item str
return
}

su, err := ur.reg.ScheduledUnit(item)
if err != nil {
log.Errorf("Failed fetching ScheduledUnit(%s) from Registry: %v", item, err)
sendError(rw, http.StatusInternalServerError, nil)
return
}

s := schema.MapUnitToSchemaUnit(u, su)
sendResponse(rw, http.StatusOK, *s)
sendResponse(rw, http.StatusOK, *u)
}

func (ur *unitsResource) list(rw http.ResponseWriter, req *http.Request) {
Expand All @@ -177,7 +167,7 @@ func (ur *unitsResource) list(rw http.ResponseWriter, req *http.Request) {
token = &def
}

page, err := getUnitPage(ur.reg, *token)
page, err := getUnitPage(ur.cAPI, *token)
if err != nil {
log.Errorf("Failed fetching page of Units: %v", err)
sendError(rw, http.StatusInternalServerError, nil)
Expand All @@ -187,40 +177,25 @@ func (ur *unitsResource) list(rw http.ResponseWriter, req *http.Request) {
sendResponse(rw, http.StatusOK, page)
}

func getUnitPage(reg registry.Registry, tok PageToken) (*schema.UnitPage, error) {
units, err := reg.Units()
func getUnitPage(cAPI client.API, tok PageToken) (*schema.UnitPage, error) {
units, err := cAPI.Units()
if err != nil {
return nil, err
}

sUnits, err := reg.Schedule()
if err != nil {
return nil, err
}

sUnitMap := make(map[string]*job.ScheduledUnit)
for _, sUnit := range sUnits {
sUnit := sUnit
sUnitMap[sUnit.Name] = &sUnit
}

items, next := extractUnitPageData(units, tok)
page := schema.UnitPage{
Units: make([]*schema.Unit, len(items)),
Units: items,
}

if next != nil {
page.NextPageToken = next.Encode()
}

for i, u := range items {
page.Units[i] = schema.MapUnitToSchemaUnit(&u, sUnitMap[u.Name])
}

return &page, nil
}

func extractUnitPageData(all []job.Unit, tok PageToken) (items []job.Unit, next *PageToken) {
func extractUnitPageData(all []*schema.Unit, tok PageToken) (items []*schema.Unit, next *PageToken) {
total := len(all)

startIndex := int((tok.Page - 1) * tok.Limit)
Expand Down
23 changes: 15 additions & 8 deletions api/units_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"strconv"
"testing"

"github.com/coreos/fleet/client"
"github.com/coreos/fleet/job"
"github.com/coreos/fleet/registry"
"github.com/coreos/fleet/schema"
Expand All @@ -27,7 +28,8 @@ func newUnit(t *testing.T, str string) unit.UnitFile {

func TestUnitsSubResourceNotFound(t *testing.T) {
fr := registry.NewFakeRegistry()
ur := &unitsResource{fr, "/units"}
fAPI := &client.RegistryClient{fr}
ur := &unitsResource{fAPI, "/units"}
rr := httptest.NewRecorder()

req, err := http.NewRequest("GET", "/units/foo/bar", nil)
Expand All @@ -49,7 +51,8 @@ func TestUnitsList(t *testing.T) {
{Name: "XXX"},
{Name: "YYY"},
})
resource := &unitsResource{fr, "/units"}
fAPI := &client.RegistryClient{fr}
resource := &unitsResource{fAPI, "/units"}
rw := httptest.NewRecorder()
req, err := http.NewRequest("GET", "http://example.com/units", nil)
if err != nil {
Expand Down Expand Up @@ -85,7 +88,8 @@ func TestUnitsList(t *testing.T) {

func TestUnitsListBadNextPageToken(t *testing.T) {
fr := registry.NewFakeRegistry()
resource := &unitsResource{fr, "/units"}
fAPI := &client.RegistryClient{fr}
resource := &unitsResource{fAPI, "/units"}
rw := httptest.NewRecorder()
req, err := http.NewRequest("GET", "http://example.com/units?nextPageToken=EwBMLg==", nil)
if err != nil {
Expand All @@ -101,10 +105,10 @@ func TestUnitsListBadNextPageToken(t *testing.T) {
}

func TestExtractUnitPage(t *testing.T) {
all := make([]job.Unit, 103)
all := make([]*schema.Unit, 103)
for i := 0; i < 103; i++ {
name := strconv.FormatInt(int64(i), 10)
all[i] = job.Unit{Name: name}
all[i] = &schema.Unit{Name: name}
}

tests := []struct {
Expand Down Expand Up @@ -165,7 +169,8 @@ func TestUnitGet(t *testing.T) {
{Name: "XXX"},
{Name: "YYY"},
})
resource := &unitsResource{fr, "/units"}
fAPI := &client.RegistryClient{fr}
resource := &unitsResource{fAPI, "/units"}

for i, tt := range tests {
rw := httptest.NewRecorder()
Expand Down Expand Up @@ -227,7 +232,8 @@ func TestUnitsDestroy(t *testing.T) {
continue
}

resource := &unitsResource{fr, "/units"}
fAPI := &client.RegistryClient{fr}
resource := &unitsResource{fAPI, "/units"}
rw := httptest.NewRecorder()
resource.destroy(rw, req, tt.arg)

Expand Down Expand Up @@ -342,7 +348,8 @@ func TestUnitsSetDesiredState(t *testing.T) {
req.Body = ioutil.NopCloser(bytes.NewBuffer(enc))
req.Header.Set("Content-Type", "application/json")

resource := &unitsResource{fr, "/units"}
fAPI := &client.RegistryClient{fr}
resource := &unitsResource{fAPI, "/units"}
rw := httptest.NewRecorder()
resource.set(rw, req, tt.arg.Name)

Expand Down

0 comments on commit 07ecfed

Please sign in to comment.