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