Skip to content

Commit

Permalink
add all manifest endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
groob committed Jul 8, 2016
1 parent fb7f157 commit 3dcd780
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 9 deletions.
8 changes: 4 additions & 4 deletions munki/datastore/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ func (r *SimpleRepo) NewManifest(name string) (*munki.Manifest, error) {
}

// SaveManifest saves a manifest to the datastore
func (r *SimpleRepo) SaveManifest(manifest *munki.Manifest) error {
if manifest.Filename == "" {
return errors.New("filename key must be set")
func (r *SimpleRepo) SaveManifest(path string, manifest *munki.Manifest) error {
if path == "" {
return errors.New("must specify a manifest name")
}
manifestPath := fmt.Sprintf("%v/manifests/%v", r.Path, manifest.Filename)
manifestPath := fmt.Sprintf("%v/manifests/%v", r.Path, path)
file, err := os.OpenFile(manifestPath, os.O_WRONLY, 0755)
if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion munki/munki/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ type ManifestStore interface {
AllManifests() (*ManifestCollection, error)
Manifest(name string) (*Manifest, error)
NewManifest(name string) (*Manifest, error)
SaveManifest(manifest *Manifest) error
SaveManifest(path string, manifest *Manifest) error
DeleteManifest(name string) error
}

Expand Down
20 changes: 20 additions & 0 deletions munki/server/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,23 @@ func makeReplaceManifestEndpoint(svc Service) endpoint.Endpoint {
return replaceManifestResponse{Manifest: manifest, Err: err}, nil
}
}

type updateManifestRequest struct {
Path string `plist:"filename" json:"filename"`
*munki.ManifestPayload
}

type updateManifestResponse struct {
*munki.Manifest
Err error `json:"error,omitempty" plist:"error,omitempty"`
}

func (r updateManifestResponse) error() error { return r.Err }

func makeUpdateManifestEndpoint(svc Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
req := request.(updateManifestRequest)
manifest, err := svc.UpdateManifest(ctx, req.Path, req.ManifestPayload)
return updateManifestResponse{Manifest: manifest, Err: err}, nil
}
}
16 changes: 12 additions & 4 deletions munki/server/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type Service interface {
CreateManifest(ctx context.Context, name string, manifest *munki.Manifest) (*munki.Manifest, error)
ReplaceManifest(ctx context.Context, name string, manifest *munki.Manifest) (*munki.Manifest, error)
DeleteManifest(ctx context.Context, name string) error
UpdateManifest(ctx context.Context, name string, payload *munki.ManifestPayload) (*munki.Manifest, error)
}

type service struct {
Expand All @@ -32,8 +33,7 @@ func (svc service) CreateManifest(ctx context.Context, name string, manifest *mu
if err != nil {
return nil, err
}
manifest.Filename = name
if err := svc.repo.SaveManifest(manifest); err != nil {
if err := svc.repo.SaveManifest(name, manifest); err != nil {
return nil, err
}
return manifest, nil
Expand All @@ -50,8 +50,16 @@ func (svc service) ReplaceManifest(ctx context.Context, name string, manifest *m
return svc.CreateManifest(ctx, name, manifest)
}

func (svc service) UpdateManifest(ctx context.Context, name string, manifest *munki.Manifest) (*munki.Manifest, error) {
panic("not implemented")
func (svc service) UpdateManifest(ctx context.Context, name string, payload *munki.ManifestPayload) (*munki.Manifest, error) {
manifest, err := svc.repo.Manifest(name)
if err != nil {
return nil, err
}
manifest.UpdateFromPayload(payload)
if err := svc.repo.SaveManifest(name, manifest); err != nil {
return nil, err
}
return manifest, nil
}

// NewService creates a new munki api service
Expand Down
22 changes: 22 additions & 0 deletions munki/server/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,20 @@ func decodeReplaceManifestRequest(_ context.Context, r *http.Request) (interface
return request, nil
}

func decodeUpdateManifestRequest(_ context.Context, r *http.Request) (interface{}, error) {
var request updateManifestRequest
if err := json.NewDecoder(r.Body).Decode(&request.ManifestPayload); err != nil {
return nil, err
}
vars := mux.Vars(r)
path, ok := vars["path"]
if !ok {
return nil, errBadRouting
}
request.Path = path
return request, nil
}

// ServiceHandler creates an HTTP handler for the munki Service
func ServiceHandler(ctx context.Context, svc Service, logger kitlog.Logger) http.Handler {
opts := []kithttp.ServerOption{
Expand Down Expand Up @@ -107,13 +121,21 @@ func ServiceHandler(ctx context.Context, svc Service, logger kitlog.Logger) http
encodeResponse,
opts...,
)
updateManifestHandler := kithttp.NewServer(
ctx,
makeUpdateManifestEndpoint(svc),
decodeUpdateManifestRequest,
encodeResponse,
opts...,
)
r := mux.NewRouter()
// manifests
r.Handle("/api/v1/manifests/{path}", showManifestHandler).Methods("GET")
r.Handle("/api/v1/manifests", listManifestsHandler).Methods("GET")
r.Handle("/api/v1/manifests", createManifestHandler).Methods("POST")
r.Handle("/api/v1/manifests/{path}", deleteManifestHandler).Methods("DELETE")
r.Handle("/api/v1/manifests/{path}", replaceManifestHandler).Methods("PUT")
r.Handle("/api/v1/manifests/{path}", updateManifestHandler).Methods("PATCH")
return r
}

Expand Down
44 changes: 44 additions & 0 deletions munki/server/transport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,27 @@ func TestShowManifests(t *testing.T) {
testShowManifestHTTP(t, server, "site_none", http.StatusNotFound)
}

func TestUpdateManifest(t *testing.T) {
server, _ := newServer(t)
defer server.Close()
manifests := []*munki.Manifest{
&munki.Manifest{
Filename: "update-manifest",
Catalogs: []string{"production", "testing"},
},
}

for _, m := range manifests {
os.Remove("testdata/testrepo/manifests/" + m.Filename)
testCreateManifestHTTP(t, server, m.Filename, m, http.StatusOK)
m1 := &munki.ManifestPayload{
Catalogs: &[]string{"foo"},
}
testUpdateManifestHTTP(t, server, m.Filename, m1, http.StatusOK)
os.Remove("testdata/testrepo/manifests/" + m.Filename)
}
}

func TestReplaceManifest(t *testing.T) {
server, _ := newServer(t)
defer server.Close()
Expand Down Expand Up @@ -134,6 +155,29 @@ func testReplaceManifestHTTP(t *testing.T, server *httptest.Server, path string,
}
}

func testUpdateManifestHTTP(t *testing.T, server *httptest.Server, path string, m *munki.ManifestPayload, expectedStatus int) {
client := http.DefaultClient
theURL := server.URL + "/api/v1/manifests/" + path
data, err := json.Marshal(m)
if err != nil {
t.Fatal(err)
}
body := ioutil.NopCloser(bytes.NewBuffer(data))
req, err := http.NewRequest("PATCH", theURL, body)
if err != nil {
t.Fatal(err)
}
resp, err := client.Do(req)
if err != nil {
t.Fatal(err)
}
if resp.StatusCode != expectedStatus {
fmt.Println(theURL)
io.Copy(os.Stdout, resp.Body)
t.Fatal("expected", expectedStatus, "got", resp.StatusCode)
}
}

func testDeleteManifestHTTP(t *testing.T, server *httptest.Server, path string, expectedStatus int) {
client := http.DefaultClient
theURL := server.URL + "/api/v1/manifests/" + path
Expand Down

0 comments on commit 3dcd780

Please sign in to comment.