66//
77// Terminal 2:
88// $ go test -v example_test.go
9+
910package box_test
1011
1112import (
@@ -18,7 +19,7 @@ import (
1819 "github.com/tarantool/go-tarantool/v2/box"
1920)
2021
21- func Example () {
22+ func ExampleBox_Info () {
2223 dialer := tarantool.NetDialer {
2324 Address : "127.0.0.1:3013" ,
2425 User : "test" ,
@@ -55,6 +56,188 @@ func Example() {
5556 log .Fatalf ("Box info uuids are not equal" )
5657 }
5758
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 .New (client ).Schema ().User ()
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 .New (client ).Schema ().User ()
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+ exists , err := schemaUser .Exists (ctx , username )
169+ if err != nil {
170+ log .Fatalf ("Failed to get user exists: %s" , err )
171+ }
172+
173+ fmt .Printf ("User '%s' dropped successfully\n " , username )
174+ fmt .Printf ("User '%s' exists status: %v \n " , username , exists )
175+ // Output:
176+ // User 'new_user' dropped successfully
177+ // User 'new_user' exists status: false
178+ }
179+
180+ func ExampleSchemaUser_Password () {
181+ // Connect to Tarantool.
182+ dialer := tarantool.NetDialer {
183+ Address : "127.0.0.1:3013" ,
184+ User : "test" ,
185+ Password : "test" ,
186+ }
187+ ctx := context .Background ()
188+
189+ client , err := tarantool .Connect (ctx , dialer , tarantool.Opts {})
190+ if err != nil {
191+ log .Fatalf ("Failed to connect: %s" , err )
192+ }
193+
194+ // Create SchemaUser.
195+ schemaUser := box .New (client ).Schema ().User ()
196+
197+ // Get the password hash.
198+ password := "my-password"
199+ passwordHash , err := schemaUser .Password (ctx , password )
200+ if err != nil {
201+ log .Fatalf ("Failed to get password hash: %s" , err )
202+ }
203+
204+ fmt .Printf ("Password '%s' hash: %s" , password , passwordHash )
205+ // Output:
206+ // Password 'my-password' hash: 3PHNAQGFWFo0KRfToxNgDXHj2i8=
207+ }
208+
209+ func ExampleSchemaUser_Info () {
210+ // Connect to Tarantool.
211+ dialer := tarantool.NetDialer {
212+ Address : "127.0.0.1:3013" ,
213+ User : "test" ,
214+ Password : "test" ,
215+ }
216+ ctx := context .Background ()
217+
218+ client , err := tarantool .Connect (ctx , dialer , tarantool.Opts {})
219+ if err != nil {
220+ log .Fatalf ("Failed to connect: %s" , err )
221+ }
222+
223+ // Create SchemaUser.
224+ schemaUser := box .New (client ).Schema ().User ()
225+
226+ info , err := schemaUser .Info (ctx , "test" )
227+ if err != nil {
228+ log .Fatalf ("Failed to get password hash: %s" , err )
229+ }
230+
231+ hasSuper := false
232+ for _ , i := range info {
233+ if i .Name == "super" && i .Type == box .PrivilegeRole {
234+ hasSuper = true
235+ }
236+ }
237+
238+ if hasSuper {
239+ fmt .Printf ("User have super privileges" )
240+ }
241+ // Output:
242+ // User have super privileges
60243}
0 commit comments