6
6
//
7
7
// Terminal 2:
8
8
// $ go test -v example_test.go
9
+
9
10
package box_test
10
11
11
12
import (
@@ -18,7 +19,7 @@ import (
18
19
"github.com/tarantool/go-tarantool/v2/box"
19
20
)
20
21
21
- func Example () {
22
+ func ExampleBox_Info () {
22
23
dialer := tarantool.NetDialer {
23
24
Address : "127.0.0.1:3013" ,
24
25
User : "test" ,
@@ -55,6 +56,145 @@ func Example() {
55
56
log .Fatalf ("Box info uuids are not equal" )
56
57
}
57
58
58
- fmt .Printf ("Box info uuids are equal" )
59
- fmt .Printf ("Current box info: %+v\n " , resp .Info )
59
+ fmt .Printf ("Box info uuids are equal\n " )
60
+ fmt .Printf ("Current box ro: %+v" , resp .Info .RO )
61
+ // Output:
62
+ // Box info uuids are equal
63
+ // Current box ro: false
64
+ }
65
+
66
+ func ExampleSchemaUser_Exists () {
67
+ dialer := tarantool.NetDialer {
68
+ Address : "127.0.0.1:3013" ,
69
+ User : "test" ,
70
+ Password : "test" ,
71
+ }
72
+ ctx := context .Background ()
73
+
74
+ client , err := tarantool .Connect (ctx , dialer , tarantool.Opts {})
75
+
76
+ if err != nil {
77
+ log .Fatalf ("Failed to connect: %s" , err )
78
+ }
79
+
80
+ // You can use UserExistsRequest type and call it directly.
81
+ fut := client .Do (box .NewUserExistsRequest ("user" ))
82
+
83
+ resp := & box.UserExistsResponse {}
84
+
85
+ err = fut .GetTyped (resp )
86
+ if err != nil {
87
+ log .Fatalf ("Failed get box schema user exists with error: %s" , err )
88
+ }
89
+
90
+ // Or use simple User implementation.
91
+ b := box .New (client )
92
+ exists , err := b .Schema ().User ().Exists (ctx , "user" )
93
+ if err != nil {
94
+ log .Fatalf ("Failed get box schema user exists with error: %s" , err )
95
+ }
96
+
97
+ if exists != resp .Exists {
98
+ log .Fatalf ("Box schema users exists are not equal" )
99
+ }
100
+
101
+ fmt .Printf ("Box schema users exists are equal\n " )
102
+ fmt .Printf ("Current exists state: %+v" , exists )
103
+ // Output:
104
+ // Box schema users exists are equal
105
+ // Current exists state: false
106
+ }
107
+
108
+ func ExampleSchemaUser_Create () {
109
+ // Connect to Tarantool.
110
+ dialer := tarantool.NetDialer {
111
+ Address : "127.0.0.1:3013" ,
112
+ User : "test" ,
113
+ Password : "test" ,
114
+ }
115
+ ctx := context .Background ()
116
+
117
+ client , err := tarantool .Connect (ctx , dialer , tarantool.Opts {})
118
+ if err != nil {
119
+ log .Fatalf ("Failed to connect: %s" , err )
120
+ }
121
+
122
+ // Create SchemaUser.
123
+ schemaUser := box .NewSchemaUser (client )
124
+
125
+ // Create a new user.
126
+ username := "new_user"
127
+ options := box.UserCreateOptions {
128
+ IfNotExists : true ,
129
+ Password : "secure_password" ,
130
+ }
131
+ err = schemaUser .Create (ctx , username , options )
132
+ if err != nil {
133
+ log .Fatalf ("Failed to create user: %s" , err )
134
+ }
135
+
136
+ fmt .Printf ("User '%s' created successfully\n " , username )
137
+ // Output:
138
+ // User 'new_user' created successfully
139
+ }
140
+
141
+ func ExampleSchemaUser_Drop () {
142
+ // Connect to Tarantool.
143
+ dialer := tarantool.NetDialer {
144
+ Address : "127.0.0.1:3013" ,
145
+ User : "test" ,
146
+ Password : "test" ,
147
+ }
148
+ ctx := context .Background ()
149
+
150
+ client , err := tarantool .Connect (ctx , dialer , tarantool.Opts {})
151
+ if err != nil {
152
+ log .Fatalf ("Failed to connect: %s" , err )
153
+ }
154
+
155
+ // Create SchemaUser.
156
+ schemaUser := box .NewSchemaUser (client )
157
+
158
+ // Drop an existing user.
159
+ username := "new_user"
160
+ options := box.UserDropOptions {
161
+ IfExists : true ,
162
+ }
163
+ err = schemaUser .Drop (ctx , username , options )
164
+ if err != nil {
165
+ log .Fatalf ("Failed to drop user: %s" , err )
166
+ }
167
+
168
+ fmt .Printf ("User '%s' dropped successfully\n " , username )
169
+ // Output:
170
+ // User 'new_user' dropped successfully
171
+ }
172
+
173
+ func ExampleSchemaUser_Password () {
174
+ // Connect to Tarantool.
175
+ dialer := tarantool.NetDialer {
176
+ Address : "127.0.0.1:3013" ,
177
+ User : "test" ,
178
+ Password : "test" ,
179
+ }
180
+ ctx := context .Background ()
181
+
182
+ client , err := tarantool .Connect (ctx , dialer , tarantool.Opts {})
183
+ if err != nil {
184
+ log .Fatalf ("Failed to connect: %s" , err )
185
+ }
186
+
187
+ // Create SchemaUser.
188
+ schemaUser := box .NewSchemaUser (client )
189
+
190
+ // Get the password hash.
191
+ password := "my-password"
192
+ passwordHash , err := schemaUser .Password (ctx , password )
193
+ if err != nil {
194
+ log .Fatalf ("Failed to get password hash: %s" , err )
195
+ }
196
+
197
+ fmt .Printf ("Password '%s' hash: %s" , password , passwordHash )
198
+ // Output:
199
+ // Password 'my-password' hash: 3PHNAQGFWFo0KRfToxNgDXHj2i8=
60
200
}
0 commit comments