// Actions takes a list of ActionTags, and returns the full Action for // each ID. func (a *ActionAPI) Actions(arg params.Entities) (params.ActionResults, error) { response := params.ActionResults{Results: make([]params.ActionResult, len(arg.Entities))} for i, entity := range arg.Entities { currentResult := &response.Results[i] tag, err := names.ParseTag(entity.Tag) if err != nil { currentResult.Error = common.ServerError(common.ErrBadId) continue } actionTag, ok := tag.(names.ActionTag) if !ok { currentResult.Error = common.ServerError(common.ErrBadId) continue } action, err := a.state.ActionByTag(actionTag) if err != nil { currentResult.Error = common.ServerError(common.ErrBadId) continue } receiverTag, err := names.ActionReceiverTag(action.Receiver()) if err != nil { currentResult.Error = common.ServerError(err) continue } response.Results[i] = common.MakeActionResult(receiverTag, action) } return response, nil }
// Cancel attempts to cancel enqueued Actions from running. func (a *ActionAPI) Cancel(arg params.Entities) (params.ActionResults, error) { if err := a.check.ChangeAllowed(); err != nil { return params.ActionResults{}, errors.Trace(err) } response := params.ActionResults{Results: make([]params.ActionResult, len(arg.Entities))} for i, entity := range arg.Entities { currentResult := &response.Results[i] tag, err := names.ParseTag(entity.Tag) if err != nil { currentResult.Error = common.ServerError(common.ErrBadId) continue } actionTag, ok := tag.(names.ActionTag) if !ok { currentResult.Error = common.ServerError(common.ErrBadId) continue } action, err := a.state.ActionByTag(actionTag) if err != nil { currentResult.Error = common.ServerError(err) continue } result, err := action.Finish(state.ActionResults{Status: state.ActionCancelled, Message: "action cancelled via the API"}) if err != nil { currentResult.Error = common.ServerError(err) continue } receiverTag, err := names.ActionReceiverTag(result.Receiver()) if err != nil { currentResult.Error = common.ServerError(err) continue } response.Results[i] = common.MakeActionResult(receiverTag, result) } return response, nil }
func (a *ActionAPI) FindActionsByNames(arg params.FindActionsByNames) (params.ActionsByNames, error) { response := params.ActionsByNames{Actions: make([]params.ActionsByName, len(arg.ActionNames))} for i, name := range arg.ActionNames { currentResult := &response.Actions[i] currentResult.Name = name actions, err := a.state.FindActionsByName(name) if err != nil { currentResult.Error = common.ServerError(err) continue } for _, action := range actions { recvTag, err := names.ActionReceiverTag(action.Receiver()) if err != nil { currentResult.Actions = append(currentResult.Actions, params.ActionResult{Error: common.ServerError(err)}) continue } currentAction := common.MakeActionResult(recvTag, action) currentResult.Actions = append(currentResult.Actions, currentAction) } } return response, nil }
// Enqueue takes a list of Actions and queues them up to be executed by // the designated ActionReceiver, returning the params.Action for each // enqueued Action, or an error if there was a problem enqueueing the // Action. func (a *ActionAPI) Enqueue(arg params.Actions) (params.ActionResults, error) { if err := a.check.ChangeAllowed(); err != nil { return params.ActionResults{}, errors.Trace(err) } tagToActionReceiver := common.TagToActionReceiverFn(a.state.FindEntity) response := params.ActionResults{Results: make([]params.ActionResult, len(arg.Actions))} for i, action := range arg.Actions { currentResult := &response.Results[i] receiver, err := tagToActionReceiver(action.Receiver) if err != nil { currentResult.Error = common.ServerError(err) continue } enqueued, err := receiver.AddAction(action.Name, action.Parameters) if err != nil { currentResult.Error = common.ServerError(err) continue } response.Results[i] = common.MakeActionResult(receiver.Tag(), enqueued) } return response, nil }