本文整理汇总了Golang中github.com/koding/kite.Client.Tell方法的典型用法代码示例。如果您正苦于以下问题:Golang Client.Tell方法的具体用法?Golang Client.Tell怎么用?Golang Client.Tell使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/koding/kite.Client
的用法示例。
在下文中一共展示了Client.Tell方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: CreateSnapshot
func (c *Client) CreateSnapshot(id string, remote *kite.Client) error {
createSnapshotArgs := &args{
MachineId: id,
Provider: c.Provider,
}
resp, err := remote.Tell("createSnapshot", createSnapshotArgs)
if err != nil {
return err
}
var result kloud.ControlResult
err = resp.Unmarshal(&result)
if err != nil {
return err
}
eArgs := kloud.EventArgs([]kloud.EventArg{
{
EventId: createSnapshotArgs.MachineId,
Type: "createSnapshot",
},
})
return listenEvent(eArgs, machinestate.Running, remote)
}
示例2: Stop
func (c *Client) Stop(id string, remote *kite.Client) error {
stopArgs := &args{
MachineId: id,
Provider: c.Provider,
}
resp, err := remote.Tell("stop", stopArgs)
if err != nil {
return err
}
var result kloud.ControlResult
err = resp.Unmarshal(&result)
if err != nil {
return err
}
eArgs := kloud.EventArgs([]kloud.EventArg{
{
EventId: stopArgs.MachineId,
Type: "stop",
},
})
return listenEvent(eArgs, machinestate.Stopped, remote)
}
示例3: TodosHandler
func TodosHandler(app *kite.Client, w http.ResponseWriter, r *http.Request) {
token := r.Header.Get("token")
result, err := app.Tell("todos", token)
if err != nil {
fmt.Println("Failed to get todos", err)
w.WriteHeader(http.StatusUnauthorized)
return
}
w.Write(result.Raw)
}
示例4: SingleMachine
func (c *Cmd) SingleMachine(id string, k *kite.Client) (string, error) {
resp, err := k.Tell(c.command, &KloudArgs{
MachineId: id,
Provider: *c.provider,
})
if err != nil {
return "", err
}
var result stack.ControlResult
err = resp.Unmarshal(&result)
if err != nil {
return "", err
}
return result.EventId, nil
}
示例5: SingleMachine
func (i *Info) SingleMachine(id string, k *kite.Client) (string, error) {
infoArgs := &KloudArgs{
MachineId: id,
Provider: *i.provider,
}
resp, err := k.Tell("info", infoArgs)
if err != nil {
return "", err
}
var result stack.InfoResponse
err = resp.Unmarshal(&result)
if err != nil {
return "", err
}
return result.State.String(), nil
}
示例6: LoginHandler
func LoginHandler(auth *kite.Client, w http.ResponseWriter, r *http.Request) {
params := map[string]string{
"user": r.FormValue("username"),
"pass": r.FormValue("password"),
}
result, err := auth.Tell("login", params)
if err != nil {
fmt.Println("Failed to login:", err)
w.WriteHeader(http.StatusUnauthorized)
return
}
token, _ := result.String()
json, _ := json.Marshal(map[string]string{"token": token})
w.Write(json)
}
示例7: Build
func (c *Client) Build(id string, remote *kite.Client) error {
buildArgs := &args{
MachineId: id,
Provider: c.Provider,
TerraformContext: fmt.Sprintf(`
provider "aws" {
access_key = "${var.aws_access_key}"
secret_key = "${var.aws_secret_key}"
region = "%s"
}
resource "aws_instance" "example" {
ami = "ami-d05e75b8"
instance_type = "%s"
}`, c.region(), c.instanceType()),
}
resp, err := remote.Tell("build", buildArgs)
if err != nil {
return err
}
var result kloud.ControlResult
err = resp.Unmarshal(&result)
if err != nil {
return err
}
eArgs := kloud.EventArgs([]kloud.EventArg{
{
EventId: buildArgs.MachineId,
Type: "build",
},
})
return listenEvent(eArgs, machinestate.Running, remote)
}
示例8: listenEvent
// listenEvent calls the event method of kloud with the given arguments until
// the desiredState is received. It times out if the desired state is not
// reached in 10 miunuts.
func listenEvent(args kloud.EventArgs, desiredState machinestate.State, remote *kite.Client) error {
tryUntil := time.Now().Add(time.Minute * 10)
for {
resp, err := remote.Tell("event", args)
if err != nil {
return err
}
var events []kloud.EventResponse
if err := resp.Unmarshal(&events); err != nil {
return err
}
e := events[0]
if e.Error != nil {
return e.Error
}
// fmt.Printf("e = %+v\n", e)
event := e.Event
if event.Error != "" {
return fmt.Errorf("%s: %s", args[0].Type, event.Error)
}
if event.Status == desiredState {
return nil
}
if time.Now().After(tryUntil) {
return fmt.Errorf("Timeout while waiting for state %s", desiredState)
}
time.Sleep(2 * time.Second)
continue // still pending
}
}
示例9: main
func main() {
authorizer := auth.NewAuthorizer()
authConfig, err := conf.NewConfig("auth")
if err != nil {
fmt.Println("Error getting config", err)
return
}
k := kitewrapper.NewKiteWrapper(authConfig)
err = k.RegisterToKontrol()
if err != nil {
fmt.Println("Failed to register", err)
return
}
var dbKite *kite.Client
go func() {
var err error
dbKite, err = k.FindAndDial("db_accessor")
if err != nil {
fmt.Println("Failed to dial db service", err)
// return
}
}()
// Add our handler method
k.HandleFunc("login", func(r *kite.Request) (interface{}, error) {
var (
params, _ = r.Args.One().Map()
user, _ = params["user"].String()
pass, _ = params["pass"].String()
)
if user == User && pass == Pass {
return authorizer.Token, nil
}
return nil, BadCredentials
})
k.HandleFunc("profile", func(r *kite.Request) (interface{}, error) {
token, err := r.Args.One().String()
if err != nil {
return nil, BadCredentials
}
if !authorizer.Validate(token) {
return nil, BadCredentials
}
result, err := dbKite.Tell("query", "profile")
if err != nil {
return nil, err
}
var (
profileMap, _ = result.Map()
profile = make(map[string]string)
)
for name, value := range profileMap {
val, _ := value.String()
profile[name] = val
}
return profile, nil
})
k.HandleFunc("validateToken", func(r *kite.Request) (interface{}, error) {
token, err := r.Args.One().String()
if err != nil {
return false, BadCredentials
}
return authorizer.Validate(token), nil
})
k.Run()
}