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