forked from fl00r/go-tarantool-1.6
-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathexample_test.go
243 lines (199 loc) · 5.37 KB
/
example_test.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
// Run Tarantool Common Edition before example execution:
//
// Terminal 1:
// $ cd box
// $ TEST_TNT_LISTEN=127.0.0.1:3013 tarantool testdata/config.lua
//
// Terminal 2:
// $ go test -v example_test.go
package box_test
import (
"context"
"fmt"
"log"
"time"
"github.com/tarantool/go-tarantool/v2"
"github.com/tarantool/go-tarantool/v2/box"
)
func ExampleBox_Info() {
dialer := tarantool.NetDialer{
Address: "127.0.0.1:3013",
User: "test",
Password: "test",
}
ctx, cancel := context.WithTimeout(context.Background(), 500*time.Millisecond)
client, err := tarantool.Connect(ctx, dialer, tarantool.Opts{})
cancel()
if err != nil {
log.Fatalf("Failed to connect: %s", err)
}
// You can use Info Request type.
fut := client.Do(box.NewInfoRequest())
resp := &box.InfoResponse{}
err = fut.GetTyped(resp)
if err != nil {
log.Fatalf("Failed get box info: %s", err)
}
// Or use simple Box implementation.
b := box.New(client)
info, err := b.Info()
if err != nil {
log.Fatalf("Failed get box info: %s", err)
}
if info.UUID != resp.Info.UUID {
log.Fatalf("Box info uuids are not equal")
}
fmt.Printf("Box info uuids are equal\n")
fmt.Printf("Current box ro: %+v", resp.Info.RO)
// Output:
// Box info uuids are equal
// Current box ro: false
}
func ExampleSchemaUser_Exists() {
dialer := tarantool.NetDialer{
Address: "127.0.0.1:3013",
User: "test",
Password: "test",
}
ctx := context.Background()
client, err := tarantool.Connect(ctx, dialer, tarantool.Opts{})
if err != nil {
log.Fatalf("Failed to connect: %s", err)
}
// You can use UserExistsRequest type and call it directly.
fut := client.Do(box.NewUserExistsRequest("user"))
resp := &box.UserExistsResponse{}
err = fut.GetTyped(resp)
if err != nil {
log.Fatalf("Failed get box schema user exists with error: %s", err)
}
// Or use simple User implementation.
b := box.New(client)
exists, err := b.Schema().User().Exists(ctx, "user")
if err != nil {
log.Fatalf("Failed get box schema user exists with error: %s", err)
}
if exists != resp.Exists {
log.Fatalf("Box schema users exists are not equal")
}
fmt.Printf("Box schema users exists are equal\n")
fmt.Printf("Current exists state: %+v", exists)
// Output:
// Box schema users exists are equal
// Current exists state: false
}
func ExampleSchemaUser_Create() {
// Connect to Tarantool.
dialer := tarantool.NetDialer{
Address: "127.0.0.1:3013",
User: "test",
Password: "test",
}
ctx := context.Background()
client, err := tarantool.Connect(ctx, dialer, tarantool.Opts{})
if err != nil {
log.Fatalf("Failed to connect: %s", err)
}
// Create SchemaUser.
schemaUser := box.NewSchemaUser(client)
// Create a new user.
username := "new_user"
options := box.UserCreateOptions{
IfNotExists: true,
Password: "secure_password",
}
err = schemaUser.Create(ctx, username, options)
if err != nil {
log.Fatalf("Failed to create user: %s", err)
}
fmt.Printf("User '%s' created successfully\n", username)
// Output:
// User 'new_user' created successfully
}
func ExampleSchemaUser_Drop() {
// Connect to Tarantool.
dialer := tarantool.NetDialer{
Address: "127.0.0.1:3013",
User: "test",
Password: "test",
}
ctx := context.Background()
client, err := tarantool.Connect(ctx, dialer, tarantool.Opts{})
if err != nil {
log.Fatalf("Failed to connect: %s", err)
}
// Create SchemaUser.
schemaUser := box.NewSchemaUser(client)
// Drop an existing user.
username := "new_user"
options := box.UserDropOptions{
IfExists: true,
}
err = schemaUser.Drop(ctx, username, options)
if err != nil {
log.Fatalf("Failed to drop user: %s", err)
}
exists, err := schemaUser.Exists(ctx, username)
if err != nil {
log.Fatalf("Failed to get user exists: %s", err)
}
fmt.Printf("User '%s' dropped successfully\n", username)
fmt.Printf("User '%s' exists status: %v \n", username, exists)
// Output:
// User 'new_user' dropped successfully
// User 'new_user' exists status: false
}
func ExampleSchemaUser_Password() {
// Connect to Tarantool.
dialer := tarantool.NetDialer{
Address: "127.0.0.1:3013",
User: "test",
Password: "test",
}
ctx := context.Background()
client, err := tarantool.Connect(ctx, dialer, tarantool.Opts{})
if err != nil {
log.Fatalf("Failed to connect: %s", err)
}
// Create SchemaUser.
schemaUser := box.NewSchemaUser(client)
// Get the password hash.
password := "my-password"
passwordHash, err := schemaUser.Password(ctx, password)
if err != nil {
log.Fatalf("Failed to get password hash: %s", err)
}
fmt.Printf("Password '%s' hash: %s", password, passwordHash)
// Output:
// Password 'my-password' hash: 3PHNAQGFWFo0KRfToxNgDXHj2i8=
}
func ExampleSchemaUser_Info() {
// Connect to Tarantool.
dialer := tarantool.NetDialer{
Address: "127.0.0.1:3013",
User: "test",
Password: "test",
}
ctx := context.Background()
client, err := tarantool.Connect(ctx, dialer, tarantool.Opts{})
if err != nil {
log.Fatalf("Failed to connect: %s", err)
}
// Create SchemaUser.
schemaUser := box.NewSchemaUser(client)
info, err := schemaUser.Info(ctx, "test")
if err != nil {
log.Fatalf("Failed to get password hash: %s", err)
}
hasSuper := false
for _, i := range info {
if i.Name == "super" && i.Type == box.PrivilegeRole {
hasSuper = true
}
}
if hasSuper {
fmt.Printf("User have super privileges")
}
// Output:
// User have super privileges
}