/Godeps/_workspace/src/github.com/rackspace/gophercloud/rackspace/db/v1/instances/requests.go

https://gitlab.com/vectorci/kubernetes · Go · 199 lines · 114 code · 38 blank · 47 comment · 18 complexity · 296763bf1477e7216b01aba884b2f376 MD5 · raw file

  1. package instances
  2. import (
  3. "github.com/rackspace/gophercloud"
  4. osDBs "github.com/rackspace/gophercloud/openstack/db/v1/databases"
  5. os "github.com/rackspace/gophercloud/openstack/db/v1/instances"
  6. osUsers "github.com/rackspace/gophercloud/openstack/db/v1/users"
  7. "github.com/rackspace/gophercloud/pagination"
  8. "github.com/rackspace/gophercloud/rackspace/db/v1/backups"
  9. )
  10. // CreateOpts is the struct responsible for configuring a new database instance.
  11. type CreateOpts struct {
  12. // Either the integer UUID (in string form) of the flavor, or its URI
  13. // reference as specified in the response from the List() call. Required.
  14. FlavorRef string
  15. // Specifies the volume size in gigabytes (GB). The value must be between 1
  16. // and 300. Required.
  17. Size int
  18. // Name of the instance to create. The length of the name is limited to
  19. // 255 characters and any characters are permitted. Optional.
  20. Name string
  21. // A slice of database information options.
  22. Databases osDBs.CreateOptsBuilder
  23. // A slice of user information options.
  24. Users osUsers.CreateOptsBuilder
  25. // ID of the configuration group to associate with the instance. Optional.
  26. ConfigID string
  27. // Options to configure the type of datastore the instance will use. This is
  28. // optional, and if excluded will default to MySQL.
  29. Datastore *os.DatastoreOpts
  30. // Specifies the backup ID from which to restore the database instance. There
  31. // are some things to be aware of before using this field. When you execute
  32. // the Restore Backup operation, a new database instance is created to store
  33. // the backup whose ID is specified by the restorePoint attribute. This will
  34. // mean that:
  35. // - All users, passwords and access that were on the instance at the time of
  36. // the backup will be restored along with the databases.
  37. // - You can create new users or databases if you want, but they cannot be
  38. // the same as the ones from the instance that was backed up.
  39. RestorePoint string
  40. ReplicaOf string
  41. }
  42. func (opts CreateOpts) ToInstanceCreateMap() (map[string]interface{}, error) {
  43. instance, err := os.CreateOpts{
  44. FlavorRef: opts.FlavorRef,
  45. Size: opts.Size,
  46. Name: opts.Name,
  47. Databases: opts.Databases,
  48. Users: opts.Users,
  49. }.ToInstanceCreateMap()
  50. if err != nil {
  51. return nil, err
  52. }
  53. instance = instance["instance"].(map[string]interface{})
  54. if opts.ConfigID != "" {
  55. instance["configuration"] = opts.ConfigID
  56. }
  57. if opts.Datastore != nil {
  58. ds, err := opts.Datastore.ToMap()
  59. if err != nil {
  60. return nil, err
  61. }
  62. instance["datastore"] = ds
  63. }
  64. if opts.RestorePoint != "" {
  65. instance["restorePoint"] = map[string]string{"backupRef": opts.RestorePoint}
  66. }
  67. if opts.ReplicaOf != "" {
  68. instance["replica_of"] = opts.ReplicaOf
  69. }
  70. return map[string]interface{}{"instance": instance}, nil
  71. }
  72. // Create asynchronously provisions a new database instance. It requires the
  73. // user to specify a flavor and a volume size. The API service then provisions
  74. // the instance with the requested flavor and sets up a volume of the specified
  75. // size, which is the storage for the database instance.
  76. //
  77. // Although this call only allows the creation of 1 instance per request, you
  78. // can create an instance with multiple databases and users. The default
  79. // binding for a MySQL instance is port 3306.
  80. func Create(client *gophercloud.ServiceClient, opts os.CreateOptsBuilder) CreateResult {
  81. return CreateResult{os.Create(client, opts)}
  82. }
  83. // ListOpts specifies all of the query options to be used when returning a list
  84. // of database instances.
  85. type ListOpts struct {
  86. // IncludeHA includes or excludes High Availability instances from the result set
  87. IncludeHA bool `q:"include_ha"`
  88. // IncludeReplicas includes or excludes Replica instances from the result set
  89. IncludeReplicas bool `q:"include_replicas"`
  90. }
  91. // ToInstanceListQuery formats a ListOpts into a query string.
  92. func (opts ListOpts) ToInstanceListQuery() (string, error) {
  93. q, err := gophercloud.BuildQueryString(opts)
  94. if err != nil {
  95. return "", err
  96. }
  97. return q.String(), nil
  98. }
  99. // List retrieves the status and information for all database instances.
  100. func List(client *gophercloud.ServiceClient, opts *ListOpts) pagination.Pager {
  101. url := baseURL(client)
  102. if opts != nil {
  103. query, err := opts.ToInstanceListQuery()
  104. if err != nil {
  105. return pagination.Pager{Err: err}
  106. }
  107. url += query
  108. }
  109. createPageFn := func(r pagination.PageResult) pagination.Page {
  110. return os.InstancePage{pagination.LinkedPageBase{PageResult: r}}
  111. }
  112. return pagination.NewPager(client, url, createPageFn)
  113. }
  114. // GetDefaultConfig lists the default configuration settings from the template
  115. // that was applied to the specified instance. In a sense, this is the vanilla
  116. // configuration setting applied to an instance. Further configuration can be
  117. // applied by associating an instance with a configuration group.
  118. func GetDefaultConfig(client *gophercloud.ServiceClient, id string) ConfigResult {
  119. var res ConfigResult
  120. _, res.Err = client.Request("GET", configURL(client, id), gophercloud.RequestOpts{
  121. JSONResponse: &res.Body,
  122. OkCodes: []int{200},
  123. })
  124. return res
  125. }
  126. // AssociateWithConfigGroup associates a specified instance to a specified
  127. // configuration group. If any of the parameters within a configuration group
  128. // require a restart, then the instance will transition into a restart.
  129. func AssociateWithConfigGroup(client *gophercloud.ServiceClient, instanceID, configGroupID string) UpdateResult {
  130. reqBody := map[string]string{
  131. "configuration": configGroupID,
  132. }
  133. var res UpdateResult
  134. _, res.Err = client.Request("PUT", resourceURL(client, instanceID), gophercloud.RequestOpts{
  135. JSONBody: map[string]map[string]string{"instance": reqBody},
  136. OkCodes: []int{202},
  137. })
  138. return res
  139. }
  140. // DetachFromConfigGroup will detach an instance from all config groups.
  141. func DetachFromConfigGroup(client *gophercloud.ServiceClient, instanceID string) UpdateResult {
  142. return AssociateWithConfigGroup(client, instanceID, "")
  143. }
  144. // ListBackups will list all the backups for a specified database instance.
  145. func ListBackups(client *gophercloud.ServiceClient, instanceID string) pagination.Pager {
  146. pageFn := func(r pagination.PageResult) pagination.Page {
  147. return backups.BackupPage{pagination.SinglePageBase(r)}
  148. }
  149. return pagination.NewPager(client, backupsURL(client, instanceID), pageFn)
  150. }
  151. // DetachReplica will detach a specified replica instance from its source
  152. // instance, effectively allowing it to operate independently. Detaching a
  153. // replica will restart the MySQL service on the instance.
  154. func DetachReplica(client *gophercloud.ServiceClient, replicaID string) DetachResult {
  155. var res DetachResult
  156. _, res.Err = client.Request("PATCH", resourceURL(client, replicaID), gophercloud.RequestOpts{
  157. JSONBody: map[string]interface{}{"instance": map[string]string{"replica_of": "", "slave_of": ""}},
  158. OkCodes: []int{202},
  159. })
  160. return res
  161. }