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