github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/db/v1/databases/requests.go (about)

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