blob: 74a4cd4a2f9ac46aedc3607b558a03e33b6d8ba3 [file] [log] [blame] [view]
Jamie Hannafordb4b70f62014-10-07 12:21:40 +02001# gophercloud: the OpenStack SDK for Go
2[![Build Status](https://travis-ci.org/rackspace/gophercloud.svg?branch=v0.2.0)](https://travis-ci.org/rackspace/gophercloud)
Jamie Hannaford4eb3f962014-10-07 11:50:00 +02003
4gophercloud is a flexible SDK that allows you to consume and work with OpenStack
5clouds in a simple and idiomatic way using golang. Many services are supported,
6including Compute, Block Storage, Object Storage, Networking, and Identity.
Jamie Hannaford2b7bf772014-10-07 12:13:38 +02007Each service API is backed with getting started guides, code samples, reference
8documentation, 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 Hannaford4eb3f962014-10-07 11:50:00 +020016
17## How to install
18
19Before installing, you need to ensure that your [GOPATH environment variable](https://golang.org/doc/code.html#GOPATH)
20is pointing to an appropriate directory where you want to install gophercloud:
21
22```bash
23mkdir $HOME/go
24export GOPATH=$HOME/go
25```
26
27Once this is set up, you can install the gophercloud package like so:
28
29```bash
30go get github.com/rackspace/gophercloud
31```
32
33This will install all the source files you need into a `pkg` directory, which is
34referenceable from your own source files.
35
36## Getting started
37
38### Credentials
39
40Because you'll be hitting an API, you will need to retrieve your OpenStack
41credentials and either store them as environment variables or in your local Go
42files. The first method is recommended because it decouples credential
43information from source code, allowing you to push the latter to your version
44control system without any security risk.
45
46You will need to retrieve the following:
47
48* username
49* password
50* tenant name or tenant ID
51* a valid Keystone identity URL
52
53For users that have the OpenStack dashboard installed, there's a shortcut. If
54you 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
56download a bash file that exports all of your access details to environment
57variables. To execute the file, run `source admin-openrc.sh` and you will be
58prompted for your password.
59
60### Authentication
61
62Once you have access to your credentials, you can begin plugging them into
63gophercloud. 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
65explicitly, or tell gophercloud to use environment variables:
66
67```go
Jamie Hannaford2b7bf772014-10-07 12:13:38 +020068import (
69 "github.com/rackspace/gophercloud"
70 "github.com/rackspace/gophercloud/openstack"
71 "github.com/rackspace/gophercloud/openstack/utils"
72)
73
Jamie Hannaford4eb3f962014-10-07 11:50:00 +020074// Option 1: Pass in the values yourself
75opts := 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 Hannaford4eb3f962014-10-07 11:50:00 +020083opts, err := utils.AuthOptions()
84```
85
86Once you have the `opts` variable, you can pass it in and get back a
87`ProviderClient` struct:
88
89```go
Jamie Hannaford4eb3f962014-10-07 11:50:00 +020090provider, err := openstack.AuthenticatedClient(opts)
91```
92
93The `ProviderClient` is the top-level client that all of your OpenStack services
94derive from. The provider contains all of the authentication details that allow
95your Go code to access the API - such as the base URL and token ID.
96
Jamie Hannaford2b7bf772014-10-07 12:13:38 +020097### Provision a server
98
99Once we have a base Provider, we inject it as a dependency into each OpenStack
100service. In order to work with the Compute API, we need a Compute service
101client; which can be created like so:
102
103```go
104client, err := openstack.NewComputeV2(provider, gophercloud.EndpointOpts{
105 Region: os.Getenv("OS_REGION_NAME"),
106})
107```
108
109We then use this `client` for any Compute API operation we want. In our case,
110we want to provision a new server - so we invoke the `Create` method and pass
Jamie Hannafordb4b70f62014-10-07 12:21:40 +0200111in the flavor ID (hardware specification) and image ID (operating system) we're
Jamie Hannaford2b7bf772014-10-07 12:13:38 +0200112interested in:
113
114```go
Jamie Hannafordb4b70f62014-10-07 12:21:40 +0200115import "github.com/rackspace/gophercloud/openstack/compute/v2/servers"
116
Jamie Hannaford2b7bf772014-10-07 12:13:38 +0200117server, err := servers.Create(client, servers.CreateOpts{
118 Name: "My new server!",
119 FlavorRef: "flavor_id",
120 ImageRef: "image_id",
121}).Extract()
Jamie Hannaford2b7bf772014-10-07 12:13:38 +0200122```
123
124If you are unsure about what images and flavors are, you can read our [Compute
125Getting Started guide](http://gophercloud.io/docs/compute). The above code
126sample creates a new server with the parameters, and embodies the new resource
127in the `server` variable (a
128[`servers.Server`](http://godoc.org/github.com/rackspace/gophercloud) struct).
129
Jamie Hannaford4eb3f962014-10-07 11:50:00 +0200130### Next steps
131
Jamie Hannafordb4b70f62014-10-07 12:21:40 +0200132Cool! You've handled authentication, got your `ProviderClient` and provisioned
133a new server. You're now ready to use more OpenStack services.
Jamie Hannaford4eb3f962014-10-07 11:50:00 +0200134
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
143Engaging the community and lowering barriers for contributors is something we
144care a lot about. For this reason, we've taken the time to write a [contributing
Jamie Hannafordb4b70f62014-10-07 12:21:40 +0200145guide](./CONTRIBUTING.md) for folks interested in getting involved in our project.
146If 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
148Go expert - all members of the community are welcome!
Jamie Hannaford4eb3f962014-10-07 11:50:00 +0200149
150## Help and feedback
151
152If you're struggling with something or have spotted a potential bug, feel free
153to submit an issue to our [bug tracker](/issues) or e-mail us directly at
Jamie Hannafordb4b70f62014-10-07 12:21:40 +0200154[sdk-support@rackspace.com](mailto:sdk-support@rackspace.com).