| Everett Toews | 430261f | 2015-10-12 10:11:55 -0500 | [diff] [blame] | 1 | # Gophercloud: an OpenStack SDK for Go |
| jrperritt | 55fb584 | 2016-04-13 14:16:08 -0500 | [diff] [blame] | 2 | [](https://travis-ci.org/gophercloud/gophercloud) |
| Jon Perritt | 554de82 | 2016-03-10 02:56:23 -0600 | [diff] [blame] | 3 | [](https://coveralls.io/github/gophercloud/gophercloud?branch=master) |
| Jamie Hannaford | 4eb3f96 | 2014-10-07 11:50:00 +0200 | [diff] [blame] | 4 | |
| jrperritt | 55fb584 | 2016-04-13 14:16:08 -0500 | [diff] [blame] | 5 | Gophercloud is an OpenStack Go SDK. |
| Jamie Hannaford | 2b7bf77 | 2014-10-07 12:13:38 +0200 | [diff] [blame] | 6 | |
| 7 | ## Useful links |
| 8 | |
| Krzysztof Szukiełojć | 3f41d08 | 2017-05-07 14:43:06 +0200 | [diff] [blame^] | 9 | * [Reference documentation](http://godoc.org/gerrit.mcp.mirantis.net/debian/gophercloud.git) |
| Jamie Hannaford | 2b7bf77 | 2014-10-07 12:13:38 +0200 | [diff] [blame] | 10 | * [Effective Go](https://golang.org/doc/effective_go.html) |
| Jamie Hannaford | 4eb3f96 | 2014-10-07 11:50:00 +0200 | [diff] [blame] | 11 | |
| 12 | ## How to install |
| 13 | |
| 14 | Before installing, you need to ensure that your [GOPATH environment variable](https://golang.org/doc/code.html#GOPATH) |
| Ash Wilson | 3f2566c | 2014-10-28 13:20:19 -0400 | [diff] [blame] | 15 | is pointing to an appropriate directory where you want to install Gophercloud: |
| Jamie Hannaford | 4eb3f96 | 2014-10-07 11:50:00 +0200 | [diff] [blame] | 16 | |
| 17 | ```bash |
| 18 | mkdir $HOME/go |
| 19 | export GOPATH=$HOME/go |
| 20 | ``` |
| 21 | |
| Ash Wilson | f41663d | 2014-10-28 10:21:33 -0400 | [diff] [blame] | 22 | To protect yourself against changes in your dependencies, we highly recommend choosing a |
| JackSpirou | da9ab5b | 2015-04-11 10:56:21 -0500 | [diff] [blame] | 23 | [dependency management solution](https://github.com/golang/go/wiki/PackageManagementTools) for |
| Ash Wilson | f41663d | 2014-10-28 10:21:33 -0400 | [diff] [blame] | 24 | your projects, such as [godep](https://github.com/tools/godep). Once this is set up, you can install |
| Ash Wilson | 3f2566c | 2014-10-28 13:20:19 -0400 | [diff] [blame] | 25 | Gophercloud as a dependency like so: |
| Jamie Hannaford | 4eb3f96 | 2014-10-07 11:50:00 +0200 | [diff] [blame] | 26 | |
| 27 | ```bash |
| Krzysztof Szukiełojć | 3f41d08 | 2017-05-07 14:43:06 +0200 | [diff] [blame^] | 28 | go get gerrit.mcp.mirantis.net/debian/gophercloud.git |
| Ash Wilson | f41663d | 2014-10-28 10:21:33 -0400 | [diff] [blame] | 29 | |
| Krzysztof Szukiełojć | 3f41d08 | 2017-05-07 14:43:06 +0200 | [diff] [blame^] | 30 | # Edit your code to import relevant packages from "gerrit.mcp.mirantis.net/debian/gophercloud.git" |
| Ash Wilson | f41663d | 2014-10-28 10:21:33 -0400 | [diff] [blame] | 31 | |
| 32 | godep save ./... |
| Jamie Hannaford | 4eb3f96 | 2014-10-07 11:50:00 +0200 | [diff] [blame] | 33 | ``` |
| 34 | |
| Ash Wilson | 220b7df | 2014-10-28 10:25:32 -0400 | [diff] [blame] | 35 | This will install all the source files you need into a `Godeps/_workspace` directory, which is |
| 36 | referenceable from your own source files when you use the `godep go` command. |
| Jamie Hannaford | 4eb3f96 | 2014-10-07 11:50:00 +0200 | [diff] [blame] | 37 | |
| 38 | ## Getting started |
| 39 | |
| 40 | ### Credentials |
| 41 | |
| 42 | Because you'll be hitting an API, you will need to retrieve your OpenStack |
| 43 | credentials and either store them as environment variables or in your local Go |
| 44 | files. The first method is recommended because it decouples credential |
| 45 | information from source code, allowing you to push the latter to your version |
| 46 | control system without any security risk. |
| 47 | |
| 48 | You will need to retrieve the following: |
| 49 | |
| 50 | * username |
| 51 | * password |
| Jamie Hannaford | 4eb3f96 | 2014-10-07 11:50:00 +0200 | [diff] [blame] | 52 | * a valid Keystone identity URL |
| 53 | |
| 54 | For users that have the OpenStack dashboard installed, there's a shortcut. If |
| 55 | you visit the `project/access_and_security` path in Horizon and click on the |
| 56 | "Download OpenStack RC File" button at the top right hand corner, you will |
| 57 | download a bash file that exports all of your access details to environment |
| 58 | variables. To execute the file, run `source admin-openrc.sh` and you will be |
| 59 | prompted for your password. |
| 60 | |
| 61 | ### Authentication |
| 62 | |
| 63 | Once you have access to your credentials, you can begin plugging them into |
| Ash Wilson | 3f2566c | 2014-10-28 13:20:19 -0400 | [diff] [blame] | 64 | Gophercloud. The next step is authentication, and this is handled by a base |
| Jamie Hannaford | 4eb3f96 | 2014-10-07 11:50:00 +0200 | [diff] [blame] | 65 | "Provider" struct. To get one, you can either pass in your credentials |
| Ash Wilson | 3f2566c | 2014-10-28 13:20:19 -0400 | [diff] [blame] | 66 | explicitly, or tell Gophercloud to use environment variables: |
| Jamie Hannaford | 4eb3f96 | 2014-10-07 11:50:00 +0200 | [diff] [blame] | 67 | |
| 68 | ```go |
| Jamie Hannaford | 2b7bf77 | 2014-10-07 12:13:38 +0200 | [diff] [blame] | 69 | import ( |
| Krzysztof Szukiełojć | 3f41d08 | 2017-05-07 14:43:06 +0200 | [diff] [blame^] | 70 | "gerrit.mcp.mirantis.net/debian/gophercloud.git" |
| Krzysztof Szukiełojć | 24a29ce | 2017-05-07 14:24:02 +0200 | [diff] [blame] | 71 | "gerrit.mcp.mirantis.net/debian/gophercloud.git/openstack" |
| 72 | "gerrit.mcp.mirantis.net/debian/gophercloud.git/openstack/utils" |
| Jamie Hannaford | 2b7bf77 | 2014-10-07 12:13:38 +0200 | [diff] [blame] | 73 | ) |
| 74 | |
| Jamie Hannaford | 4eb3f96 | 2014-10-07 11:50:00 +0200 | [diff] [blame] | 75 | // Option 1: Pass in the values yourself |
| 76 | opts := gophercloud.AuthOptions{ |
| Monty Taylor | 9a5595b | 2017-03-13 13:04:29 -0500 | [diff] [blame] | 77 | IdentityEndpoint: "https://openstack.example.com:5000/v2.0", |
| Jamie Hannaford | 4eb3f96 | 2014-10-07 11:50:00 +0200 | [diff] [blame] | 78 | Username: "{username}", |
| 79 | Password: "{password}", |
| Jamie Hannaford | 4eb3f96 | 2014-10-07 11:50:00 +0200 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | // Option 2: Use a utility function to retrieve all your environment variables |
| Jamie Hannaford | 390555a | 2014-10-22 17:04:03 +0200 | [diff] [blame] | 83 | opts, err := openstack.AuthOptionsFromEnv() |
| Jamie Hannaford | 4eb3f96 | 2014-10-07 11:50:00 +0200 | [diff] [blame] | 84 | ``` |
| 85 | |
| 86 | Once you have the `opts` variable, you can pass it in and get back a |
| 87 | `ProviderClient` struct: |
| 88 | |
| 89 | ```go |
| Jamie Hannaford | 4eb3f96 | 2014-10-07 11:50:00 +0200 | [diff] [blame] | 90 | provider, err := openstack.AuthenticatedClient(opts) |
| 91 | ``` |
| 92 | |
| 93 | The `ProviderClient` is the top-level client that all of your OpenStack services |
| 94 | derive from. The provider contains all of the authentication details that allow |
| 95 | your Go code to access the API - such as the base URL and token ID. |
| 96 | |
| Jamie Hannaford | 2b7bf77 | 2014-10-07 12:13:38 +0200 | [diff] [blame] | 97 | ### Provision a server |
| 98 | |
| 99 | Once we have a base Provider, we inject it as a dependency into each OpenStack |
| 100 | service. In order to work with the Compute API, we need a Compute service |
| 101 | client; which can be created like so: |
| 102 | |
| 103 | ```go |
| 104 | client, err := openstack.NewComputeV2(provider, gophercloud.EndpointOpts{ |
| 105 | Region: os.Getenv("OS_REGION_NAME"), |
| 106 | }) |
| 107 | ``` |
| 108 | |
| 109 | We then use this `client` for any Compute API operation we want. In our case, |
| 110 | we want to provision a new server - so we invoke the `Create` method and pass |
| Jamie Hannaford | b4b70f6 | 2014-10-07 12:21:40 +0200 | [diff] [blame] | 111 | in the flavor ID (hardware specification) and image ID (operating system) we're |
| Jamie Hannaford | 2b7bf77 | 2014-10-07 12:13:38 +0200 | [diff] [blame] | 112 | interested in: |
| 113 | |
| 114 | ```go |
| Krzysztof Szukiełojć | 24a29ce | 2017-05-07 14:24:02 +0200 | [diff] [blame] | 115 | import "gerrit.mcp.mirantis.net/debian/gophercloud.git/openstack/compute/v2/servers" |
| Jamie Hannaford | b4b70f6 | 2014-10-07 12:21:40 +0200 | [diff] [blame] | 116 | |
| Jamie Hannaford | 2b7bf77 | 2014-10-07 12:13:38 +0200 | [diff] [blame] | 117 | server, err := servers.Create(client, servers.CreateOpts{ |
| 118 | Name: "My new server!", |
| 119 | FlavorRef: "flavor_id", |
| 120 | ImageRef: "image_id", |
| 121 | }).Extract() |
| Jamie Hannaford | 2b7bf77 | 2014-10-07 12:13:38 +0200 | [diff] [blame] | 122 | ``` |
| 123 | |
| jrperritt | 55fb584 | 2016-04-13 14:16:08 -0500 | [diff] [blame] | 124 | The above code sample creates a new server with the parameters, and embodies the |
| 125 | new resource in the `server` variable (a |
| Krzysztof Szukiełojć | 3f41d08 | 2017-05-07 14:43:06 +0200 | [diff] [blame^] | 126 | [`servers.Server`](http://godoc.org/gerrit.mcp.mirantis.net/debian/gophercloud.git) struct). |
| Jamie Hannaford | 2b7bf77 | 2014-10-07 12:13:38 +0200 | [diff] [blame] | 127 | |
| jrperritt | ed2f96c | 2017-01-20 13:02:54 -0600 | [diff] [blame] | 128 | ## Advanced Usage |
| 129 | |
| 130 | Have a look at the [FAQ](./FAQ.md) for some tips on customizing the way Gophercloud works. |
| 131 | |
| jrperritt | 9b7b9e6 | 2016-07-11 22:30:50 -0500 | [diff] [blame] | 132 | ## Backwards-Compatibility Guarantees |
| Jamie Hannaford | 4eb3f96 | 2014-10-07 11:50:00 +0200 | [diff] [blame] | 133 | |
| jrperritt | 9b7b9e6 | 2016-07-11 22:30:50 -0500 | [diff] [blame] | 134 | None. Vendor it and write tests covering the parts you use. |
| Jamie Hannaford | 4eb3f96 | 2014-10-07 11:50:00 +0200 | [diff] [blame] | 135 | |
| 136 | ## Contributing |
| 137 | |
| jrperritt | 10f33e9 | 2016-11-03 17:03:52 -0500 | [diff] [blame] | 138 | See the [contributing guide](./.github/CONTRIBUTING.md). |
| Jamie Hannaford | 4eb3f96 | 2014-10-07 11:50:00 +0200 | [diff] [blame] | 139 | |
| 140 | ## Help and feedback |
| 141 | |
| 142 | If you're struggling with something or have spotted a potential bug, feel free |
| jrperritt | 9b7b9e6 | 2016-07-11 22:30:50 -0500 | [diff] [blame] | 143 | to submit an issue to our [bug tracker](/issues). |