-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
83 lines (70 loc) · 1.89 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package main
import (
"context"
"fmt"
"log"
"github.com/dgrijalva/jwt-go"
pb "github.com/mrturkmencom/gdock/docker/proto"
"google.golang.org/grpc"
)
type Creds struct {
Token string
Insecure bool
}
func (c Creds) GetRequestMetadata(context.Context, ...string) (map[string]string, error) {
return map[string]string{
"token": string(c.Token),
}, nil
}
func (c Creds) RequireTransportSecurity() bool {
return !c.Insecure
}
func main() {
var conn *grpc.ClientConn
token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
"dockerservice": "test",
})
tokenString, err := token.SignedString([]byte("test"))
if err != nil {
fmt.Println("Error creating the token")
}
authCreds := Creds{Token: tokenString}
dialOpts := []grpc.DialOption{}
authCreds.Insecure = true
dialOpts = append(dialOpts,
grpc.WithInsecure(),
grpc.WithPerRPCCredentials(authCreds))
conn, err = grpc.Dial(":4444", dialOpts...)
if err != nil {
log.Fatalf("did not connect: %v", err)
}
defer conn.Close()
ctx := context.Background()
cli := pb.NewDockerClient(conn)
resp, err := cli.Create(ctx, &pb.CreateDockerRequest{
Image: "guacamole/guacd:1.0.0",
Labels: map[string]string{
"hkn": "guacamole_guacd",
},
Resource: &pb.CreateDockerRequestResources{
MemoryMB: 50,
Cpu: 1.0,
},
UseBridge: true,
})
if err != nil {
log.Printf("Error in creating docker container err %v", err)
}
fmt.Printf("Create Container Response %s \n", resp.Msg)
cid := resp.Container.Id
startResp, err := cli.Start(ctx, &pb.StartDockerRequest{Id: cid})
if err != nil {
log.Printf("Error in starting docker container err %v", err)
}
fmt.Printf("Start Container Response %s \n", startResp.Msg)
closeResp, err := cli.Close(ctx, &pb.CloseDockerRequest{Id: cid})
if err != nil {
log.Printf("Error in closing docker container err %v", err)
}
fmt.Printf("Close Container Response %s \n", closeResp.Msg)
}