github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/openstack/db/v1/users/requests.go (about) 1 package users 2 3 import ( 4 "context" 5 6 "github.com/vnpaycloud-console/gophercloud/v2" 7 db "github.com/vnpaycloud-console/gophercloud/v2/openstack/db/v1/databases" 8 "github.com/vnpaycloud-console/gophercloud/v2/pagination" 9 ) 10 11 // CreateOptsBuilder is the top-level interface for creating JSON maps. 12 type CreateOptsBuilder interface { 13 ToUserCreateMap() (map[string]any, error) 14 } 15 16 // CreateOpts is the struct responsible for configuring a new user; often in the 17 // context of an instance. 18 type CreateOpts struct { 19 // Specifies a name for the user. Valid names can be composed 20 // of the following characters: letters (either case); numbers; these 21 // characters '@', '?', '#', ' ' but NEVER beginning a name string; '_' is 22 // permitted anywhere. Prohibited characters that are forbidden include: 23 // single quotes, double quotes, back quotes, semicolons, commas, backslashes, 24 // and forward slashes. Spaces at the front or end of a user name are also 25 // not permitted. 26 Name string `json:"name" required:"true"` 27 // Specifies a password for the user. 28 Password string `json:"password" required:"true"` 29 // An array of databases that this user will connect to. The 30 // "name" field is the only requirement for each option. 31 Databases db.BatchCreateOpts `json:"databases,omitempty"` 32 // Specifies the host from which a user is allowed to connect to 33 // the database. Possible values are a string containing an IPv4 address or 34 // "%" to allow connecting from any host. Optional; the default is "%". 35 Host string `json:"host,omitempty"` 36 } 37 38 // ToMap is a convenience function for creating sub-maps for individual users. 39 func (opts CreateOpts) ToMap() (map[string]any, error) { 40 if opts.Name == "root" { 41 err := gophercloud.ErrInvalidInput{} 42 err.Argument = "users.CreateOpts.Name" 43 err.Value = "root" 44 err.Info = "root is a reserved user name and cannot be used" 45 return nil, err 46 } 47 return gophercloud.BuildRequestBody(opts, "") 48 } 49 50 // BatchCreateOpts allows multiple users to be created at once. 51 type BatchCreateOpts []CreateOpts 52 53 // ToUserCreateMap will generate a JSON map. 54 func (opts BatchCreateOpts) ToUserCreateMap() (map[string]any, error) { 55 users := make([]map[string]any, len(opts)) 56 for i, opt := range opts { 57 user, err := opt.ToMap() 58 if err != nil { 59 return nil, err 60 } 61 users[i] = user 62 } 63 return map[string]any{"users": users}, nil 64 } 65 66 // Create asynchronously provisions a new user for the specified database 67 // instance based on the configuration defined in CreateOpts. If databases are 68 // assigned for a particular user, the user will be granted all privileges 69 // for those specified databases. "root" is a reserved name and cannot be used. 70 func Create(ctx context.Context, client *gophercloud.ServiceClient, instanceID string, opts CreateOptsBuilder) (r CreateResult) { 71 b, err := opts.ToUserCreateMap() 72 if err != nil { 73 r.Err = err 74 return 75 } 76 resp, err := client.Post(ctx, baseURL(client, instanceID), &b, nil, nil) 77 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 78 return 79 } 80 81 // List will list all the users associated with a specified database instance, 82 // along with their associated databases. This operation will not return any 83 // system users or administrators for a database. 84 func List(client *gophercloud.ServiceClient, instanceID string) pagination.Pager { 85 return pagination.NewPager(client, baseURL(client, instanceID), func(r pagination.PageResult) pagination.Page { 86 return UserPage{pagination.LinkedPageBase{PageResult: r}} 87 }) 88 } 89 90 // Delete will permanently delete a user from a specified database instance. 91 func Delete(ctx context.Context, client *gophercloud.ServiceClient, instanceID, userName string) (r DeleteResult) { 92 resp, err := client.Delete(ctx, userURL(client, instanceID, userName), nil) 93 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 94 return 95 }