github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/openstack/db/v1/databases/requests.go (about) 1 package databases 2 3 import ( 4 "context" 5 6 "github.com/vnpaycloud-console/gophercloud/v2" 7 "github.com/vnpaycloud-console/gophercloud/v2/pagination" 8 ) 9 10 // CreateOptsBuilder builds create options 11 type CreateOptsBuilder interface { 12 ToDBCreateMap() (map[string]any, error) 13 } 14 15 // CreateOpts is the struct responsible for configuring a database; often in 16 // the context of an instance. 17 type CreateOpts struct { 18 // Specifies the name of the database. Valid names can be composed 19 // of the following characters: letters (either case); numbers; these 20 // characters '@', '?', '#', ' ' but NEVER beginning a name string; '_' is 21 // permitted anywhere. Prohibited characters that are forbidden include: 22 // single quotes, double quotes, back quotes, semicolons, commas, backslashes, 23 // and forward slashes. 24 Name string `json:"name" required:"true"` 25 // Set of symbols and encodings. The default character set is 26 // "utf8". See http://dev.mysql.com/doc/refman/5.1/en/charset-mysql.html for 27 // supported character sets. 28 CharSet string `json:"character_set,omitempty"` 29 // Set of rules for comparing characters in a character set. The 30 // default value for collate is "utf8_general_ci". See 31 // http://dev.mysql.com/doc/refman/5.1/en/charset-mysql.html for supported 32 // collations. 33 Collate string `json:"collate,omitempty"` 34 } 35 36 // ToMap is a helper function to convert individual DB create opt structures 37 // into sub-maps. 38 func (opts CreateOpts) ToMap() (map[string]any, error) { 39 if len(opts.Name) > 64 { 40 err := gophercloud.ErrInvalidInput{} 41 err.Argument = "databases.CreateOpts.Name" 42 err.Value = opts.Name 43 err.Info = "Must be less than 64 chars long" 44 return nil, err 45 } 46 return gophercloud.BuildRequestBody(opts, "") 47 } 48 49 // BatchCreateOpts allows for multiple databases to created and modified. 50 type BatchCreateOpts []CreateOpts 51 52 // ToDBCreateMap renders a JSON map for creating DBs. 53 func (opts BatchCreateOpts) ToDBCreateMap() (map[string]any, error) { 54 dbs := make([]map[string]any, len(opts)) 55 for i, db := range opts { 56 dbMap, err := db.ToMap() 57 if err != nil { 58 return nil, err 59 } 60 dbs[i] = dbMap 61 } 62 return map[string]any{"databases": dbs}, nil 63 } 64 65 // Create will create a new database within the specified instance. If the 66 // specified instance does not exist, a 404 error will be returned. 67 func Create(ctx context.Context, client *gophercloud.ServiceClient, instanceID string, opts CreateOptsBuilder) (r CreateResult) { 68 b, err := opts.ToDBCreateMap() 69 if err != nil { 70 r.Err = err 71 return 72 } 73 resp, err := client.Post(ctx, baseURL(client, instanceID), &b, nil, nil) 74 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 75 return 76 } 77 78 // List will list all of the databases for a specified instance. Note: this 79 // operation will only return user-defined databases; it will exclude system 80 // databases like "mysql", "information_schema", "lost+found" etc. 81 func List(client *gophercloud.ServiceClient, instanceID string) pagination.Pager { 82 return pagination.NewPager(client, baseURL(client, instanceID), func(r pagination.PageResult) pagination.Page { 83 return DBPage{pagination.LinkedPageBase{PageResult: r}} 84 }) 85 } 86 87 // Delete will permanently delete the database within a specified instance. 88 // All contained data inside the database will also be permanently deleted. 89 func Delete(ctx context.Context, client *gophercloud.ServiceClient, instanceID, dbName string) (r DeleteResult) { 90 resp, err := client.Delete(ctx, dbURL(client, instanceID, dbName), nil) 91 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 92 return 93 }