| Jamie Hannaford | b4b70f6 | 2014-10-07 12:21:40 +0200 | [diff] [blame^] | 1 | # gophercloud: the OpenStack SDK for Go |
| 2 | [](https://travis-ci.org/rackspace/gophercloud) |
| Jamie Hannaford | 4eb3f96 | 2014-10-07 11:50:00 +0200 | [diff] [blame] | 3 | |
| 4 | gophercloud is a flexible SDK that allows you to consume and work with OpenStack |
| 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 | |
| 12 | * [gophercloud homepage](http://gophercloud.io) |
| 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) |
| 20 | is pointing to an appropriate directory where you want to install gophercloud: |
| 21 | |
| 22 | ```bash |
| 23 | mkdir $HOME/go |
| 24 | export GOPATH=$HOME/go |
| 25 | ``` |
| 26 | |
| 27 | Once this is set up, you can install the gophercloud package like so: |
| 28 | |
| 29 | ```bash |
| 30 | go get github.com/rackspace/gophercloud |
| 31 | ``` |
| 32 | |
| 33 | This will install all the source files you need into a `pkg` directory, which is |
| 34 | referenceable from your own source files. |
| 35 | |
| 36 | ## Getting started |
| 37 | |
| 38 | ### Credentials |
| 39 | |
| 40 | Because you'll be hitting an API, you will need to retrieve your OpenStack |
| 41 | credentials and either store them as environment variables or in your local Go |
| 42 | files. The first method is recommended because it decouples credential |
| 43 | information from source code, allowing you to push the latter to your version |
| 44 | control system without any security risk. |
| 45 | |
| 46 | You will need to retrieve the following: |
| 47 | |
| 48 | * username |
| 49 | * password |
| 50 | * tenant name or tenant ID |
| 51 | * a valid Keystone identity URL |
| 52 | |
| 53 | For users that have the OpenStack dashboard installed, there's a shortcut. If |
| 54 | you visit the `project/access_and_security` path in Horizon and click on the |
| 55 | "Download OpenStack RC File" button at the top right hand corner, you will |
| 56 | download a bash file that exports all of your access details to environment |
| 57 | variables. To execute the file, run `source admin-openrc.sh` and you will be |
| 58 | prompted for your password. |
| 59 | |
| 60 | ### Authentication |
| 61 | |
| 62 | Once you have access to your credentials, you can begin plugging them into |
| 63 | gophercloud. The next step is authentication, and this is handled by a base |
| 64 | "Provider" struct. To get one, you can either pass in your credentials |
| 65 | explicitly, or tell gophercloud to use environment variables: |
| 66 | |
| 67 | ```go |
| Jamie Hannaford | 2b7bf77 | 2014-10-07 12:13:38 +0200 | [diff] [blame] | 68 | import ( |
| 69 | "github.com/rackspace/gophercloud" |
| 70 | "github.com/rackspace/gophercloud/openstack" |
| 71 | "github.com/rackspace/gophercloud/openstack/utils" |
| 72 | ) |
| 73 | |
| Jamie Hannaford | 4eb3f96 | 2014-10-07 11:50:00 +0200 | [diff] [blame] | 74 | // Option 1: Pass in the values yourself |
| 75 | opts := gophercloud.AuthOptions{ |
| 76 | IdentityEndpoint: "https://my-openstack.com:5000/v2.0", |
| 77 | Username: "{username}", |
| 78 | Password: "{password}", |
| 79 | TenantID: "{tenant_id}", |
| 80 | } |
| 81 | |
| 82 | // Option 2: Use a utility function to retrieve all your environment variables |
| Jamie Hannaford | 4eb3f96 | 2014-10-07 11:50:00 +0200 | [diff] [blame] | 83 | opts, err := utils.AuthOptions() |
| 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 |
| Jamie Hannaford | b4b70f6 | 2014-10-07 12:21:40 +0200 | [diff] [blame^] | 115 | import "github.com/rackspace/gophercloud/openstack/compute/v2/servers" |
| 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 | |
| 124 | If you are unsure about what images and flavors are, you can read our [Compute |
| 125 | Getting Started guide](http://gophercloud.io/docs/compute). The above code |
| 126 | sample creates a new server with the parameters, and embodies the new resource |
| 127 | in the `server` variable (a |
| 128 | [`servers.Server`](http://godoc.org/github.com/rackspace/gophercloud) struct). |
| 129 | |
| Jamie Hannaford | 4eb3f96 | 2014-10-07 11:50:00 +0200 | [diff] [blame] | 130 | ### Next steps |
| 131 | |
| Jamie Hannaford | b4b70f6 | 2014-10-07 12:21:40 +0200 | [diff] [blame^] | 132 | Cool! You've handled authentication, got your `ProviderClient` and provisioned |
| 133 | a new server. You're now ready to use more OpenStack services. |
| Jamie Hannaford | 4eb3f96 | 2014-10-07 11:50:00 +0200 | [diff] [blame] | 134 | |
| 135 | * [Getting started with Compute](http://gophercloud.io/docs/compute) |
| 136 | * [Getting started with Object Storage](http://gophercloud.io/docs/object-storage) |
| 137 | * [Getting started with Networking](http://gophercloud.io/docs/networking) |
| 138 | * [Getting started with Block Storage](http://gophercloud.io/docs/block-storage) |
| 139 | * [Getting started with Identity](http://gophercloud.io/docs/identity) |
| 140 | |
| 141 | ## Contributing |
| 142 | |
| 143 | Engaging the community and lowering barriers for contributors is something we |
| 144 | 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^] | 145 | guide](./CONTRIBUTING.md) for folks interested in getting involved in our project. |
| 146 | If you're not sure how you can get involved, feel free to submit an issue or |
| 147 | [e-mail us](mailto:sdk-support@rackspace.com) privately. You don't need to be a |
| 148 | Go expert - all members of the community are welcome! |
| Jamie Hannaford | 4eb3f96 | 2014-10-07 11:50:00 +0200 | [diff] [blame] | 149 | |
| 150 | ## Help and feedback |
| 151 | |
| 152 | If you're struggling with something or have spotted a potential bug, feel free |
| 153 | to submit an issue to our [bug tracker](/issues) or e-mail us directly at |
| Jamie Hannaford | b4b70f6 | 2014-10-07 12:21:40 +0200 | [diff] [blame^] | 154 | [sdk-support@rackspace.com](mailto:sdk-support@rackspace.com). |