Skip to content
This repository has been archived by the owner on Nov 8, 2019. It is now read-only.

Commit

Permalink
Merge pull request #17 from ingrammicro/feature/13-run-poll-commands-…
Browse files Browse the repository at this point in the history
…to-run-and-report-execution-results

Implement polling process (issue #13)
  • Loading branch information
pcantera authored Nov 29, 2017
2 parents a7ca4f0 + 04732fe commit 52fc16b
Show file tree
Hide file tree
Showing 14 changed files with 829 additions and 18 deletions.
74 changes: 74 additions & 0 deletions api/polling/polling_api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package polling

import (
"encoding/json"
"fmt"
log "github.com/Sirupsen/logrus"
"github.com/ingrammicro/concerto/api/types"
"github.com/ingrammicro/concerto/utils"
)

// PollingService manages polling operations
type PollingService struct {
concertoService utils.ConcertoService
}

// NewPollingService returns a Concerto polling service
func NewPollingService(concertoService utils.ConcertoService) (*PollingService, error) {
if concertoService == nil {
return nil, fmt.Errorf("must initialize ConcertoService before using it")
}

return &PollingService{
concertoService: concertoService,
}, nil
}

// Ping resolves if new command is waiting for execution
func (p *PollingService) Ping() (ping *types.PollingPing, status int, err error) {
log.Debug("Ping")

payload := make(map[string]interface{})
data, status, err := p.concertoService.Post("/command_polling/pings", &payload)
if err != nil {
return nil, status, err
}

if err = json.Unmarshal(data, &ping); err != nil {
return nil, status, err
}

return ping, status, nil
}

// GetNextCommand returns the command to be executed
func (p *PollingService) GetNextCommand() (command *types.PollingCommand, status int, err error) {
log.Debug("GetNextCommand")

data, status, err := p.concertoService.Get("/command_polling/command")
if err != nil {
return nil, status, err
}

if err = json.Unmarshal(data, &command); err != nil {
return nil, status, err
}

return command, status, nil
}

// UpdateCommand updates a command by its ID
func (p *PollingService) UpdateCommand(pollingCommandVector *map[string]interface{}, ID string) (command *types.PollingCommand, status int, err error) {
log.Debug("UpdateCommand")

data, status, err := p.concertoService.Put(fmt.Sprintf("/command_polling/commands/%s", ID), pollingCommandVector)
if err != nil {
return nil, status, err
}

if err = json.Unmarshal(data, &command); err != nil {
return nil, status, err
}

return command, status, nil
}
Loading

0 comments on commit 52fc16b

Please sign in to comment.