| Everett Toews | 430261f | 2015-10-12 10:11:55 -0500 | [diff] [blame] | 1 | # Gophercloud: an OpenStack SDK for Go |
| Ash Wilson | 79ba68b | 2014-10-24 17:56:27 -0400 | [diff] [blame] | 2 | [](https://travis-ci.org/rackspace/gophercloud) |
| Jamie Hannaford | 4eb3f96 | 2014-10-07 11:50:00 +0200 | [diff] [blame] | 3 | |
| Ash Wilson | 3f2566c | 2014-10-28 13:20:19 -0400 | [diff] [blame] | 4 | Gophercloud is a flexible SDK that allows you to consume and work with OpenStack |
| Jamie Hannaford | 4eb3f96 | 2014-10-07 11:50:00 +0200 | [diff] [blame] | 5 | clouds in a simple and idiomatic way using golang. Many services are supported, |
| 6 | including Compute, Block Storage, Object Storage, Networking, and Identity. |
| Jamie Hannaford | 2b7bf77 | 2014-10-07 12:13:38 +0200 | [diff] [blame] | 7 | Each service API is backed with getting started guides, code samples, reference |
| 8 | documentation, unit tests and acceptance tests. |
| 9 | |
| 10 | ## Useful links |
| 11 | |
| Ash Wilson | 3f2566c | 2014-10-28 13:20:19 -0400 | [diff] [blame] | 12 | * [Gophercloud homepage](http://gophercloud.io) |
| Jamie Hannaford | 2b7bf77 | 2014-10-07 12:13:38 +0200 | [diff] [blame] | 13 | * [Reference documentation](http://godoc.org/github.com/rackspace/gophercloud) |
| 14 | * [Getting started guides](http://gophercloud.io/docs) |
| 15 | * [Effective Go](https://golang.org/doc/effective_go.html) |
| Jamie Hannaford | 4eb3f96 | 2014-10-07 11:50:00 +0200 | [diff] [blame] | 16 | |
| 17 | ## How to install |
| 18 | |
| 19 | 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] | 20 | is pointing to an appropriate directory where you want to install Gophercloud: |
| Jamie Hannaford | 4eb3f96 | 2014-10-07 11:50:00 +0200 | [diff] [blame] | 21 | |
| 22 | ```bash |
| 23 | mkdir $HOME/go |
| 24 | export GOPATH=$HOME/go |
| 25 | ``` |
| 26 | |
| Ash Wilson | f41663d | 2014-10-28 10:21:33 -0400 | [diff] [blame] | 27 | To protect yourself against changes in your dependencies, we highly recommend choosing a |
| JackSpirou | da9ab5b | 2015-04-11 10:56:21 -0500 | [diff] [blame] | 28 | [dependency management solution](https://github.com/golang/go/wiki/PackageManagementTools) for |
| Ash Wilson | f41663d | 2014-10-28 10:21:33 -0400 | [diff] [blame] | 29 | 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] | 30 | Gophercloud as a dependency like so: |
| Jamie Hannaford | 4eb3f96 | 2014-10-07 11:50:00 +0200 | [diff] [blame] | 31 | |
| 32 | ```bash |
| 33 | go get github.com/rackspace/gophercloud |
| Ash Wilson | f41663d | 2014-10-28 10:21:33 -0400 | [diff] [blame] | 34 | |
| 35 | # Edit your code to import relevant packages from "github.com/rackspace/gophercloud" |
| 36 | |
| 37 | godep save ./... |
| Jamie Hannaford | 4eb3f96 | 2014-10-07 11:50:00 +0200 | [diff] [blame] | 38 | ``` |
| 39 | |
| Ash Wilson | 220b7df | 2014-10-28 10:25:32 -0400 | [diff] [blame] | 40 | This will install all the source files you need into a `Godeps/_workspace` directory, which is |
| 41 | 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] | 42 | |
| 43 | ## Getting started |
| 44 | |
| 45 | ### Credentials |
| 46 | |
| 47 | Because you'll be hitting an API, you will need to retrieve your OpenStack |
| 48 | credentials and either store them as environment variables or in your local Go |
| 49 | files. The first method is recommended because it decouples credential |
| 50 | information from source code, allowing you to push the latter to your version |
| 51 | control system without any security risk. |
| 52 | |
| 53 | You will need to retrieve the following: |
| 54 | |
| 55 | * username |
| 56 | * password |
| 57 | * tenant name or tenant ID |
| 58 | * a valid Keystone identity URL |
| 59 | |
| 60 | For users that have the OpenStack dashboard installed, there's a shortcut. If |
| 61 | you visit the `project/access_and_security` path in Horizon and click on the |
| 62 | "Download OpenStack RC File" button at the top right hand corner, you will |
| 63 | download a bash file that exports all of your access details to environment |
| 64 | variables. To execute the file, run `source admin-openrc.sh` and you will be |
| 65 | prompted for your password. |
| 66 | |
| 67 | ### Authentication |
| 68 | |
| 69 | 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] | 70 | 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] | 71 | "Provider" struct. To get one, you can either pass in your credentials |
| Ash Wilson | 3f2566c | 2014-10-28 13:20:19 -0400 | [diff] [blame] | 72 | explicitly, or tell Gophercloud to use environment variables: |
| Jamie Hannaford | 4eb3f96 | 2014-10-07 11:50:00 +0200 | [diff] [blame] | 73 | |
| 74 | ```go |
| Jamie Hannaford | 2b7bf77 | 2014-10-07 12:13:38 +0200 | [diff] [blame] | 75 | import ( |
| 76 | "github.com/rackspace/gophercloud" |
| 77 | "github.com/rackspace/gophercloud/openstack" |
| 78 | "github.com/rackspace/gophercloud/openstack/utils" |
| 79 | ) |
| 80 | |
| Jamie Hannaford | 4eb3f96 | 2014-10-07 11:50:00 +0200 | [diff] [blame] | 81 | // Option 1: Pass in the values yourself |
| 82 | opts := gophercloud.AuthOptions{ |
| 83 | IdentityEndpoint: "https://my-openstack.com:5000/v2.0", |
| 84 | Username: "{username}", |
| 85 | Password: "{password}", |
| 86 | TenantID: "{tenant_id}", |
| 87 | } |
| 88 | |
| 89 | // Option 2: Use a utility function to retrieve all your environment variables |
| Jamie Hannaford | 390555a | 2014-10-22 17:04:03 +0200 | [diff] [blame] | 90 | opts, err := openstack.AuthOptionsFromEnv() |
| Jamie Hannaford | 4eb3f96 | 2014-10-07 11:50:00 +0200 | [diff] [blame] | 91 | ``` |
| 92 | |
| 93 | Once you have the `opts` variable, you can pass it in and get back a |
| 94 | `ProviderClient` struct: |
| 95 | |
| 96 | ```go |
| Jamie Hannaford | 4eb3f96 | 2014-10-07 11:50:00 +0200 | [diff] [blame] | 97 | provider, err := openstack.AuthenticatedClient(opts) |
| 98 | ``` |
| 99 | |
| 100 | The `ProviderClient` is the top-level client that all of your OpenStack services |
| 101 | derive from. The provider contains all of the authentication details that allow |
| 102 | your Go code to access the API - such as the base URL and token ID. |
| 103 | |
| Jamie Hannaford | 2b7bf77 | 2014-10-07 12:13:38 +0200 | [diff] [blame] | 104 | ### Provision a server |
| 105 | |
| 106 | Once we have a base Provider, we inject it as a dependency into each OpenStack |
| 107 | service. In order to work with the Compute API, we need a Compute service |
| 108 | client; which can be created like so: |
| 109 | |
| 110 | ```go |
| 111 | client, err := openstack.NewComputeV2(provider, gophercloud.EndpointOpts{ |
| 112 | Region: os.Getenv("OS_REGION_NAME"), |
| 113 | }) |
| 114 | ``` |
| 115 | |
| 116 | We then use this `client` for any Compute API operation we want. In our case, |
| 117 | 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] | 118 | 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] | 119 | interested in: |
| 120 | |
| 121 | ```go |
| Jamie Hannaford | b4b70f6 | 2014-10-07 12:21:40 +0200 | [diff] [blame] | 122 | import "github.com/rackspace/gophercloud/openstack/compute/v2/servers" |
| 123 | |
| Jamie Hannaford | 2b7bf77 | 2014-10-07 12:13:38 +0200 | [diff] [blame] | 124 | server, err := servers.Create(client, servers.CreateOpts{ |
| 125 | Name: "My new server!", |
| 126 | FlavorRef: "flavor_id", |
| 127 | ImageRef: "image_id", |
| 128 | }).Extract() |
| Jamie Hannaford | 2b7bf77 | 2014-10-07 12:13:38 +0200 | [diff] [blame] | 129 | ``` |
| 130 | |
| 131 | If you are unsure about what images and flavors are, you can read our [Compute |
| 132 | Getting Started guide](http://gophercloud.io/docs/compute). The above code |
| 133 | sample creates a new server with the parameters, and embodies the new resource |
| 134 | in the `server` variable (a |
| 135 | [`servers.Server`](http://godoc.org/github.com/rackspace/gophercloud) struct). |
| 136 | |
| Jamie Hannaford | 4eb3f96 | 2014-10-07 11:50:00 +0200 | [diff] [blame] | 137 | ### Next steps |
| 138 | |
| Jamie Hannaford | b4b70f6 | 2014-10-07 12:21:40 +0200 | [diff] [blame] | 139 | Cool! You've handled authentication, got your `ProviderClient` and provisioned |
| 140 | a new server. You're now ready to use more OpenStack services. |
| Jamie Hannaford | 4eb3f96 | 2014-10-07 11:50:00 +0200 | [diff] [blame] | 141 | |
| 142 | * [Getting started with Compute](http://gophercloud.io/docs/compute) |
| 143 | * [Getting started with Object Storage](http://gophercloud.io/docs/object-storage) |
| 144 | * [Getting started with Networking](http://gophercloud.io/docs/networking) |
| 145 | * [Getting started with Block Storage](http://gophercloud.io/docs/block-storage) |
| 146 | * [Getting started with Identity](http://gophercloud.io/docs/identity) |
| 147 | |
| 148 | ## Contributing |
| 149 | |
| 150 | Engaging the community and lowering barriers for contributors is something we |
| 151 | care a lot about. For this reason, we've taken the time to write a [contributing |
| Jamie Hannaford | b4b70f6 | 2014-10-07 12:21:40 +0200 | [diff] [blame] | 152 | guide](./CONTRIBUTING.md) for folks interested in getting involved in our project. |
| 153 | If you're not sure how you can get involved, feel free to submit an issue or |
| Jon Perritt | 9ba988a | 2015-03-16 09:42:48 -0600 | [diff] [blame] | 154 | [contact us](https://developer.rackspace.com/support/). You don't need to be a |
| Jamie Hannaford | b4b70f6 | 2014-10-07 12:21:40 +0200 | [diff] [blame] | 155 | Go expert - all members of the community are welcome! |
| Jamie Hannaford | 4eb3f96 | 2014-10-07 11:50:00 +0200 | [diff] [blame] | 156 | |
| 157 | ## Help and feedback |
| 158 | |
| 159 | If you're struggling with something or have spotted a potential bug, feel free |
| Jon Perritt | 9ba988a | 2015-03-16 09:42:48 -0600 | [diff] [blame] | 160 | to submit an issue to our [bug tracker](/issues) or [contact us directly](https://developer.rackspace.com/support/). |