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