Files
micromdm/command/service.go
mosen bd5d996d73 Commands management plus fixes (#20)
* Add commands index to command datastore.
Add simple test for commands index
Add commands index endpoint

(cherry picked from commit 4e2a2a2)

* Add handler for GET /mdm/commands

(cherry picked from commit ae71b8f)

* Added Find() method to commands datastore so that the request that matches a response can be retrieved by the connect service.
Added Find() method to command service

(cherry picked from commit ebceb28)

* Fix globally scoped vars in command datastore test suite

* groob prefers inline definition of struct members.

* Uppercase CommandUuid
2016-10-07 11:17:47 +11:00

62 lines
1.6 KiB
Go

package command
import "github.com/micromdm/mdm"
// Service defines methods for managing MDM commands
type Service interface {
NewCommand(*mdm.CommandRequest) (*mdm.Payload, error)
NextCommand(udid string) ([]byte, int, error)
DeleteCommand(deviceUDID, commandUUID string) (int, error)
Commands(deviceUDID string) ([]mdm.Payload, error)
Find(commandUUID string) (*mdm.Payload, error)
}
// NewService returns a new command service
func NewService(ds Datastore) Service {
return &service{
db: ds,
}
}
type service struct {
db Datastore
}
func (svc service) NewCommand(request *mdm.CommandRequest) (*mdm.Payload, error) {
// create a payload
payload, err := mdm.NewPayload(request)
if err != nil {
return nil, err
}
// save in redis
err = svc.db.SavePayload(payload)
if err != nil {
return nil, err
}
// add command to a queue in redis
err = svc.db.QueueCommand(request.UDID, payload.CommandUUID)
if err != nil {
return nil, err
}
// return created payload to user
return payload, nil
}
// NextCommand returns an MDM Payload from a list of queued payloads
func (svc service) NextCommand(udid string) ([]byte, int, error) {
return svc.db.NextCommand(udid)
}
// DeleteCommand returns an MDM Payload from a list of queued payloads
func (svc service) DeleteCommand(deviceUDID, commandUUID string) (int, error) {
return svc.db.DeleteCommand(deviceUDID, commandUUID)
}
func (svc service) Commands(deviceUDID string) ([]mdm.Payload, error) {
return svc.db.Commands(deviceUDID)
}
func (svc service) Find(commandUUID string) (*mdm.Payload, error) {
return svc.db.Find(commandUUID)
}