本文整理匯總了Golang中bosh/handler.Request類的典型用法代碼示例。如果您正苦於以下問題:Golang Request類的具體用法?Golang Request怎麽用?Golang Request使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了Request類的8個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: Dispatch
func (dispatcher concreteActionDispatcher) Dispatch(req boshhandler.Request) (resp boshhandler.Response) {
action, err := dispatcher.actionFactory.Create(req.Method)
switch {
case err != nil:
resp = boshhandler.NewExceptionResponse("unknown message %s", req.Method)
dispatcher.logger.Error("Action Dispatcher", "Unknown action %s", req.Method)
case action.IsAsynchronous():
task := dispatcher.taskService.StartTask(func() (value interface{}, err error) {
value, err = dispatcher.actionRunner.Run(action, req.GetPayload())
return
})
resp = boshhandler.NewValueResponse(boshtask.TaskStateValue{
AgentTaskId: task.Id,
State: task.State,
})
default:
value, err := dispatcher.actionRunner.Run(action, req.GetPayload())
if err != nil {
err = bosherr.WrapError(err, "Action Failed %s", req.Method)
resp = boshhandler.NewExceptionResponse(err.Error())
dispatcher.logger.Error("Action Dispatcher", err.Error())
return
}
resp = boshhandler.NewValueResponse(value)
}
return
}
示例2: statusHandler
func (d *dummyNatsJobSupervisor) statusHandler(req boshhandler.Request) boshhandler.Response {
switch req.Method {
case "set_dummy_status":
// Do not unmarshal message until determining its method
var body map[string]string
err := json.Unmarshal(req.GetPayload(), &body)
if err != nil {
return boshhandler.NewExceptionResponse(err.Error())
}
d.status = body["status"]
if d.status == "failing" && d.jobFailureHandler != nil {
d.jobFailureHandler(boshalert.MonitAlert{
ID: "fake-monit-alert",
Service: "fake-monit-service",
Event: "failing",
Action: "start",
Date: "Sun, 22 May 2011 20:07:41 +0500",
Description: "fake-monit-description",
})
}
return boshhandler.NewValueResponse("ok")
default:
return nil
}
}
示例3: dispatchAsynchronousAction
func (dispatcher concreteActionDispatcher) dispatchAsynchronousAction(
action boshaction.Action,
req boshhandler.Request,
) boshhandler.Response {
dispatcher.logger.Info(actionDispatcherLogTag, "Running async action %s", req.Method)
var task boshtask.Task
var err error
runTask := func() (interface{}, error) {
return dispatcher.actionRunner.Run(action, req.GetPayload())
}
cancelTask := func(_ boshtask.Task) error { return action.Cancel() }
// Certain long-running tasks (e.g. configure_networks) must be resumed
// after agent restart so that API consumers do not need to know
// if agent is restarted midway through the task.
if action.IsPersistent() {
dispatcher.logger.Info(actionDispatcherLogTag, "Running persistent action %s", req.Method)
task, err = dispatcher.taskService.CreateTask(runTask, cancelTask, dispatcher.removeTaskInfo)
if err != nil {
err = bosherr.WrapError(err, "Create Task Failed %s", req.Method)
dispatcher.logger.Error(actionDispatcherLogTag, err.Error())
return boshhandler.NewExceptionResponse(err)
}
taskInfo := boshtask.TaskInfo{
TaskID: task.ID,
Method: req.Method,
Payload: req.GetPayload(),
}
err = dispatcher.taskManager.AddTaskInfo(taskInfo)
if err != nil {
err = bosherr.WrapError(err, "Action Failed %s", req.Method)
dispatcher.logger.Error(actionDispatcherLogTag, err.Error())
return boshhandler.NewExceptionResponse(err)
}
} else {
task, err = dispatcher.taskService.CreateTask(runTask, cancelTask, nil)
if err != nil {
err = bosherr.WrapError(err, "Create Task Failed %s", req.Method)
dispatcher.logger.Error(actionDispatcherLogTag, err.Error())
return boshhandler.NewExceptionResponse(err)
}
}
dispatcher.taskService.StartTask(task)
return boshhandler.NewValueResponse(boshtask.TaskStateValue{
AgentTaskID: task.ID,
State: task.State,
})
}
示例4: statusHandler
func (d *dummyNatsJobSupervisor) statusHandler(req boshhandler.Request) boshhandler.Response {
if req.Method != "set_dummy_status" {
return nil
}
var body map[string]string
json.Unmarshal(req.GetPayload(), &body)
d.status = body["status"]
return nil
}
示例5: dispatchSynchronousAction
func (dispatcher concreteActionDispatcher) dispatchSynchronousAction(
action boshaction.Action,
req boshhandler.Request,
) boshhandler.Response {
dispatcher.logger.Info(actionDispatcherLogTag, "Running sync action %s", req.Method)
value, err := dispatcher.actionRunner.Run(action, req.GetPayload())
if err != nil {
err = bosherr.WrapError(err, "Action Failed %s", req.Method)
dispatcher.logger.Error(actionDispatcherLogTag, err.Error())
return boshhandler.NewExceptionResponse(err)
}
return boshhandler.NewValueResponse(value)
}
示例6:
"crypto/tls"
"errors"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"io/ioutil"
"net/http"
"net/url"
"strings"
"time"
)
var _ = Describe("HTTPSHandler", func() {
var (
serverURL string
handler HTTPSHandler
fs *fakesys.FakeFileSystem
receivedRequest boshhandler.Request
httpClient http.Client
)
BeforeEach(func() {
serverURL = "https://user:[email protected]:6900"
mbusURL, _ := url.Parse(serverURL)
logger := boshlog.NewLogger(boshlog.LevelNone)
fs = fakesys.NewFakeFileSystem()
dirProvider := boshdir.NewDirectoriesProvider("/var/vcap")
handler = NewHTTPSHandler(mbusURL, logger, fs, dirProvider)
go handler.Start(func(req boshhandler.Request) (resp boshhandler.Response) {
receivedRequest = req
return boshhandler.NewValueResponse("expected value")
示例7: Dispatch
func (dispatcher concreteActionDispatcher) Dispatch(req boshhandler.Request) boshhandler.Response {
action, err := dispatcher.actionFactory.Create(req.Method)
switch {
case err != nil:
dispatcher.logger.Error("Action Dispatcher", "Unknown action %s", req.Method)
return boshhandler.NewExceptionResponse("unknown message %s", req.Method)
case action.IsAsynchronous():
dispatcher.logger.Error("Action Dispatcher", "Running async action %s", req.Method)
var task boshtask.Task
runTask := func() (interface{}, error) {
return dispatcher.actionRunner.Run(action, req.GetPayload())
}
// Certain long-running tasks (e.g. configure_networks) must be resumed
// after agent restart so that API consumers do not need to know
// if agent is restarted midway through the task.
if action.IsPersistent() {
dispatcher.logger.Error("Action Dispatcher", "Running persistent action %s", req.Method)
task, err = dispatcher.taskService.CreateTask(runTask, dispatcher.removeTaskInfo)
if err != nil {
err = bosherr.WrapError(err, "Create Task Failed %s", req.Method)
dispatcher.logger.Error("Action Dispatcher", err.Error())
return boshhandler.NewExceptionResponse(err.Error())
}
taskInfo := boshtask.TaskInfo{
TaskId: task.Id,
Method: req.Method,
Payload: req.GetPayload(),
}
err = dispatcher.taskManager.AddTaskInfo(taskInfo)
if err != nil {
err = bosherr.WrapError(err, "Action Failed %s", req.Method)
dispatcher.logger.Error("Action Dispatcher", err.Error())
return boshhandler.NewExceptionResponse(err.Error())
}
} else {
task, err = dispatcher.taskService.CreateTask(runTask, nil)
if err != nil {
err = bosherr.WrapError(err, "Create Task Failed %s", req.Method)
dispatcher.logger.Error("Action Dispatcher", err.Error())
return boshhandler.NewExceptionResponse(err.Error())
}
}
dispatcher.taskService.StartTask(task)
return boshhandler.NewValueResponse(boshtask.TaskStateValue{
AgentTaskId: task.Id,
State: task.State,
})
default:
dispatcher.logger.Debug("Action Dispatcher", "Running sync action %s", req.Method)
value, err := dispatcher.actionRunner.Run(action, req.GetPayload())
if err != nil {
err = bosherr.WrapError(err, "Action Failed %s", req.Method)
dispatcher.logger.Error("Action Dispatcher", err.Error())
return boshhandler.NewExceptionResponse(err.Error())
}
return boshhandler.NewValueResponse(value)
}
}
示例8: init
func init() {
Describe("actionDispatcher", func() {
var (
logger boshlog.Logger
taskService *faketask.FakeService
taskManager *faketask.FakeManager
actionFactory *fakeaction.FakeFactory
actionRunner *fakeaction.FakeRunner
dispatcher ActionDispatcher
)
BeforeEach(func() {
logger = boshlog.NewLogger(boshlog.LEVEL_NONE)
taskService = faketask.NewFakeService()
taskManager = faketask.NewFakeManager()
actionFactory = fakeaction.NewFakeFactory()
actionRunner = &fakeaction.FakeRunner{}
dispatcher = NewActionDispatcher(logger, taskService, taskManager, actionFactory, actionRunner)
})
It("responds with exception when the method is unknown", func() {
actionFactory.RegisterActionErr("fake-action", errors.New("fake-create-error"))
req := boshhandler.NewRequest("fake-reply", "fake-action", []byte{})
resp := dispatcher.Dispatch(req)
boshassert.MatchesJsonString(GinkgoT(), resp, `{"exception":{"message":"unknown message fake-action"}}`)
})
Context("when action is synchronous", func() {
var (
req boshhandler.Request
)
BeforeEach(func() {
req = boshhandler.NewRequest("fake-reply", "fake-action", []byte("fake-payload"))
actionFactory.RegisterAction("fake-action", &fakeaction.TestAction{Asynchronous: false})
})
It("handles synchronous action", func() {
actionRunner.RunValue = "fake-value"
resp := dispatcher.Dispatch(req)
Expect(req.GetPayload()).To(Equal(actionRunner.RunPayload))
Expect(boshhandler.NewValueResponse("fake-value")).To(Equal(resp))
})
It("handles synchronous action when err", func() {
actionRunner.RunErr = errors.New("fake-run-error")
resp := dispatcher.Dispatch(req)
expectedJson := fmt.Sprintf("{\"exception\":{\"message\":\"Action Failed %s: fake-run-error\"}}", req.Method)
boshassert.MatchesJsonString(GinkgoT(), resp, expectedJson)
})
})
Context("when action is asynchronous", func() {
var (
req boshhandler.Request
action *fakeaction.TestAction
)
BeforeEach(func() {
req = boshhandler.NewRequest("fake-reply", "fake-action", []byte("fake-payload"))
action = &fakeaction.TestAction{Asynchronous: true}
actionFactory.RegisterAction("fake-action", action)
})
Context("when action is not persistent", func() {
BeforeEach(func() {
action.Persistent = false
})
It("responds with task id and state", func() {
resp := dispatcher.Dispatch(req)
boshassert.MatchesJsonString(GinkgoT(), resp,
`{"value":{"agent_task_id":"fake-generated-task-id","state":"running"}}`)
})
It("starts running created task", func() {
dispatcher.Dispatch(req)
Expect(len(taskService.StartedTasks)).To(Equal(1))
Expect(taskService.StartedTasks["fake-generated-task-id"]).ToNot(BeNil())
})
It("returns create task error", func() {
taskService.CreateTaskErr = errors.New("fake-create-task-error")
resp := dispatcher.Dispatch(req)
respJson, err := json.Marshal(resp)
Expect(err).ToNot(HaveOccurred())
Expect(string(respJson)).To(ContainSubstring("fake-create-task-error"))
})
It("return run value to the task", func() {
actionRunner.RunValue = "fake-value"
dispatcher.Dispatch(req)
value, err := taskService.StartedTasks["fake-generated-task-id"].TaskFunc()
Expect(value).To(Equal("fake-value"))
Expect(err).ToNot(HaveOccurred())
//.........這裏部分代碼省略.........