| Jamie Hannaford | 6a91bbc | 2014-10-28 15:48:06 +0100 | [diff] [blame^] | 1 | # Upgrading to v1.0.0 release |
| 2 | |
| 3 | With the arrival of this new major version increment, the unfortunate news is |
| 4 | that many breaking changes to existing services have been introduced. The API |
| 5 | has been completely rewritten to make the library more extendible and easy-to-use. |
| 6 | |
| 7 | Below we've compiled specific upgrade instructions for the various services |
| 8 | that existed before. If you have a specific issue that is not addressed below, |
| 9 | please [submit an issue](/issues/new) or |
| 10 | [e-mail our support team](mailto:sdk-support@rackspace.com). |
| 11 | |
| 12 | # Authentication |
| 13 | |
| 14 | One of the major differences that this release introduces is the level of |
| 15 | sub-packaging to differentiate between services and providers. You know have |
| 16 | the option of authenticating with OpenStack and other providers (like Rackspace). |
| 17 | |
| 18 | To authenticate with a vanilla OpenStack installation, you can either specify |
| 19 | your credentials like this: |
| 20 | |
| 21 | ```go |
| 22 | import ( |
| 23 | "github.com/rackspace/gophercloud" |
| 24 | "github.com/rackspace/gophercloud/openstack" |
| 25 | ) |
| 26 | |
| 27 | opts := gophercloud.AuthOptions{ |
| 28 | IdentityEndpoint: "https://my-openstack.com:5000/v2.0", |
| 29 | Username: "{username}", |
| 30 | Password: "{password}", |
| 31 | TenantID: "{tenant_id}", |
| 32 | } |
| 33 | ``` |
| 34 | |
| 35 | Or have them pulled in through environment variables, like this: |
| 36 | |
| 37 | ```go |
| 38 | opts, err := openstack.AuthOptionsFromEnv() |
| 39 | ``` |
| 40 | |
| 41 | Once you have your `AuthOptions` struct, you pass it in to get back a `Provider`, |
| 42 | like so: |
| 43 | |
| 44 | ```go |
| 45 | provider, err := openstack.AuthenticatedClient(opts) |
| 46 | ``` |
| 47 | |
| 48 | This provider is the top-level structure that all services are created from. |
| 49 | |
| 50 | # Compute |
| 51 | |
| 52 | ## Client |
| 53 | |
| 54 | Before you can interact with the Compute API, you need to retrieve a |
| 55 | `gophercloud.ServiceClient` client. To do this: |
| 56 | |
| 57 | ```go |
| 58 | // Define your region, etc. |
| 59 | opts := gophercloud.EndpointOpts{Region: "RegionOne"} |
| 60 | |
| 61 | client, err := openstack.NewComputeV2(provider, opts) |
| 62 | ``` |
| 63 | |
| 64 | ## List servers |
| 65 | |
| 66 | All operations that involve API collections (servers, flavors, images) now use |
| 67 | the `pagination.Pager` interface. This interface represents paginated entities |
| 68 | that can be iterated over. |
| 69 | |
| 70 | Once you have a Pager, you can then pass a callback function into its `EachPage` |
| 71 | method, and this will allow you to traverse over the collection and execute |
| 72 | arbitrary functionality. So, an example with list servers: |
| 73 | |
| 74 | ```go |
| 75 | import ( |
| 76 | "fmt" |
| 77 | "github.com/rackspace/gophercloud/pagination" |
| 78 | "github.com/rackspace/gophercloud/openstack/compute/v2/servers" |
| 79 | ) |
| 80 | |
| 81 | // We have the option of filtering the server list. If we want the full |
| 82 | // collection, leave it as an empty struct |
| 83 | opts := servers.ListOpts{Name: "server_1"} |
| 84 | |
| 85 | // Retrieve a pager (i.e. a paginated collection) |
| 86 | pager := servers.List(client, opts) |
| 87 | |
| 88 | // Define an anonymous function to be executed on each page's iteration |
| 89 | err := pager.EachPage(func(page pagination.Page) (bool, error) { |
| 90 | serverList, err := servers.ExtractServers(page) |
| 91 | |
| 92 | // `s' will be a servers.Server struct |
| 93 | for _, s := range serverList { |
| 94 | fmt.Printf("We have a server. ID=%s, Name=%s", s.ID, s.Name) |
| 95 | } |
| 96 | }) |
| 97 | ``` |
| 98 | |
| 99 | ## Get server details |
| 100 | |
| 101 | ```go |
| 102 | import "github.com/rackspace/gophercloud/openstack/compute/v2/servers" |
| 103 | |
| 104 | // Get the HTTP result |
| 105 | response := servers.Get(client, "server_id") |
| 106 | |
| 107 | // Extract a Server struct from the response |
| 108 | server, err := response.Extract() |
| 109 | ``` |
| 110 | |
| 111 | ## Create server |
| 112 | |
| 113 | ```go |
| 114 | import "github.com/rackspace/gophercloud/openstack/compute/v2/servers" |
| 115 | |
| 116 | // Define our options |
| 117 | opts := servers.CreateOpts{ |
| 118 | Name: "new_server", |
| 119 | FlavorRef: "flavorID", |
| 120 | ImageRef: "imageID", |
| 121 | } |
| 122 | |
| 123 | // Get our response |
| 124 | response := servers.Create(client, opts) |
| 125 | |
| 126 | // Extract |
| 127 | server, err := response.Extract() |
| 128 | ``` |
| 129 | |
| 130 | ## List images |
| 131 | |
| 132 | As with listing servers (see above), you first retrieve a Pager, and then pass |
| 133 | in a callback over each page: |
| 134 | |
| 135 | ```go |
| 136 | import ( |
| 137 | "github.com/rackspace/gophercloud/pagination" |
| 138 | "github.com/rackspace/gophercloud/openstack/compute/v2/images" |
| 139 | ) |
| 140 | |
| 141 | // We have the option of filtering the image list. If we want the full |
| 142 | // collection, leave it as an empty struct |
| 143 | opts := images.ListOpts{ChangesSince: "2014-01-01T01:02:03Z", Name: "Ubuntu 12.04"} |
| 144 | |
| 145 | // Retrieve a pager (i.e. a paginated collection) |
| 146 | pager := images.List(client, opts) |
| 147 | |
| 148 | // Define an anonymous function to be executed on each page's iteration |
| 149 | err := pager.EachPage(func(page pagination.Page) (bool, error) { |
| 150 | imageList, err := images.ExtractImages(page) |
| 151 | |
| 152 | for _, i := range imageList { |
| 153 | // "i" will be a images.Image |
| 154 | } |
| 155 | }) |
| 156 | ``` |
| 157 | |
| 158 | ## List flavors |
| 159 | |
| 160 | ```go |
| 161 | import ( |
| 162 | "github.com/rackspace/gophercloud/pagination" |
| 163 | "github.com/rackspace/gophercloud/openstack/compute/v2/flavors" |
| 164 | ) |
| 165 | |
| 166 | // We have the option of filtering the flavor list. If we want the full |
| 167 | // collection, leave it as an empty struct |
| 168 | opts := flavors.ListOpts{ChangesSince: "2014-01-01T01:02:03Z", MinRAM: 4} |
| 169 | |
| 170 | // Retrieve a pager (i.e. a paginated collection) |
| 171 | pager := flavors.List(client, opts) |
| 172 | |
| 173 | // Define an anonymous function to be executed on each page's iteration |
| 174 | err := pager.EachPage(func(page pagination.Page) (bool, error) { |
| 175 | flavorList, err := networks.ExtractFlavors(page) |
| 176 | |
| 177 | for _, f := range flavorList { |
| 178 | // "f" will be a flavors.Flavor |
| 179 | } |
| 180 | }) |
| 181 | ``` |
| 182 | |
| 183 | ## Change admin password |
| 184 | |
| 185 | ```go |
| 186 | import "github.com/rackspace/gophercloud/openstack/compute/v2/servers" |
| 187 | |
| 188 | result := servers.ChangeAdminPassword(client, "server_id", "newPassword_&123") |
| 189 | ``` |
| 190 | |
| 191 | ## Resize server |
| 192 | |
| 193 | ```go |
| 194 | import "github.com/rackspace/gophercloud/openstack/compute/v2/servers" |
| 195 | |
| 196 | result := servers.Resize(client, "server_id", "new_flavor_id") |
| 197 | ``` |
| 198 | |
| 199 | ## Reboot server |
| 200 | |
| 201 | ```go |
| 202 | import "github.com/rackspace/gophercloud/openstack/compute/v2/servers" |
| 203 | |
| 204 | // You have a choice of two reboot methods: servers.SoftReboot or servers.HardReboot |
| 205 | result := servers.Reboot(client, "server_id", servers.SoftReboot) |
| 206 | ``` |
| 207 | |
| 208 | ## Update server |
| 209 | |
| 210 | ```go |
| 211 | import "github.com/rackspace/gophercloud/openstack/compute/v2/servers" |
| 212 | |
| 213 | opts := servers.UpdateOpts{Name: "new_name"} |
| 214 | |
| 215 | server, err := servers.Update(client, "server_id", opts).Extract() |
| 216 | ``` |
| 217 | |
| 218 | ## Rebuild server |
| 219 | |
| 220 | ```go |
| 221 | import "github.com/rackspace/gophercloud/openstack/compute/v2/servers" |
| 222 | |
| 223 | // You have the option of specifying additional options |
| 224 | opts := RebuildOpts{ |
| 225 | Name: "new_name", |
| 226 | AdminPass: "admin_password", |
| 227 | ImageID: "image_id", |
| 228 | Metadata: map[string]string{"owner": "me"}, |
| 229 | } |
| 230 | |
| 231 | result := servers.Rebuild(client, "server_id", opts) |
| 232 | |
| 233 | // You can extract a servers.Server struct from the HTTP response |
| 234 | server, err := result.Extract() |
| 235 | ``` |
| 236 | |
| 237 | ## List keypairs |
| 238 | |
| 239 | ```go |
| 240 | import "github.com/rackspace/gophercloud/openstack/compute/v2/extensions/keypairs" |
| 241 | |
| 242 | // Retrieve a pager (i.e. a paginated collection) |
| 243 | pager := keypairs.List(client, opts) |
| 244 | |
| 245 | // Define an anonymous function to be executed on each page's iteration |
| 246 | err := pager.EachPage(func(page pagination.Page) (bool, error) { |
| 247 | keyList, err := keypairs.ExtractKeyPairs(page) |
| 248 | |
| 249 | for _, k := range keyList { |
| 250 | // "k" will be a keypairs.KeyPair |
| 251 | } |
| 252 | }) |
| 253 | ``` |
| 254 | |
| 255 | ## Create/delete keypairs |
| 256 | |
| 257 | To create a new keypair, you need to specify its name and, optionally, a |
| 258 | pregenerated OpenSSH-formatted public key. |
| 259 | |
| 260 | ```go |
| 261 | import "github.com/rackspace/gophercloud/openstack/compute/v2/extensions/keypairs" |
| 262 | |
| 263 | opts := keypairs.CreateOpts{ |
| 264 | Name: "new_key", |
| 265 | PublicKey: "...", |
| 266 | } |
| 267 | |
| 268 | response := keypairs.Create(client, opts) |
| 269 | |
| 270 | key, err := response.Extract() |
| 271 | ``` |
| 272 | |
| 273 | To delete an existing keypair: |
| 274 | |
| 275 | ```go |
| 276 | response := keypairs.Delete(client, "keypair_id") |
| 277 | ``` |
| 278 | |
| 279 | ## Create/delete image |
| 280 | |
| 281 | Image management has been shifted to Glance, but unfortunately this service is |
| 282 | not supported as of yet. You can, however, list Compute images like so: |
| 283 | |
| 284 | ```go |
| 285 | import "github.com/rackspace/gophercloud/openstack/compute/v2/images" |
| 286 | |
| 287 | // Retrieve a pager (i.e. a paginated collection) |
| 288 | pager := images.List(client, opts) |
| 289 | |
| 290 | // Define an anonymous function to be executed on each page's iteration |
| 291 | err := pager.EachPage(func(page pagination.Page) (bool, error) { |
| 292 | imageList, err := images.ExtractImages(page) |
| 293 | |
| 294 | for _, i := range imageList { |
| 295 | // "i" will be a images.Image |
| 296 | } |
| 297 | }) |
| 298 | ``` |
| 299 | |
| 300 | ## Delete server |
| 301 | |
| 302 | ```go |
| 303 | import "github.com/rackspace/gophercloud/openstack/compute/v2/servers" |
| 304 | |
| 305 | response := servers.Delete(client, "server_id") |
| 306 | ``` |
| 307 | |
| 308 | ## Rescue server |
| 309 | |
| 310 | The server rescue extension for Compute is not currently supported. |
| 311 | |
| 312 | ## List IP addresses |
| 313 | |
| 314 | This operation is not currently supported. |