blob: 0a0da59f613ee48b3555d80c37044147b8acafb9 [file] [log] [blame] [view]
Everett Toews430261f2015-10-12 10:11:55 -05001# Gophercloud: an OpenStack SDK for Go
Ash Wilson79ba68b2014-10-24 17:56:27 -04002[![Build Status](https://travis-ci.org/rackspace/gophercloud.svg?branch=master)](https://travis-ci.org/rackspace/gophercloud)
Jamie Hannaford4eb3f962014-10-07 11:50:00 +02003
Ash Wilson3f2566c2014-10-28 13:20:19 -04004Gophercloud is a flexible SDK that allows you to consume and work with OpenStack
Jamie Hannaford4eb3f962014-10-07 11:50:00 +02005clouds 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
Ash Wilson3f2566c2014-10-28 13:20:19 -040012* [Gophercloud homepage](http://gophercloud.io)
Jamie Hannaford2b7bf772014-10-07 12:13:38 +020013* [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)
Ash Wilson3f2566c2014-10-28 13:20:19 -040020is pointing to an appropriate directory where you want to install Gophercloud:
Jamie Hannaford4eb3f962014-10-07 11:50:00 +020021
22```bash
23mkdir $HOME/go
24export GOPATH=$HOME/go
25```
26
Ash Wilsonf41663d2014-10-28 10:21:33 -040027To protect yourself against changes in your dependencies, we highly recommend choosing a
JackSpirouda9ab5b2015-04-11 10:56:21 -050028[dependency management solution](https://github.com/golang/go/wiki/PackageManagementTools) for
Ash Wilsonf41663d2014-10-28 10:21:33 -040029your projects, such as [godep](https://github.com/tools/godep). Once this is set up, you can install
Ash Wilson3f2566c2014-10-28 13:20:19 -040030Gophercloud as a dependency like so:
Jamie Hannaford4eb3f962014-10-07 11:50:00 +020031
32```bash
33go get github.com/rackspace/gophercloud
Ash Wilsonf41663d2014-10-28 10:21:33 -040034
35# Edit your code to import relevant packages from "github.com/rackspace/gophercloud"
36
37godep save ./...
Jamie Hannaford4eb3f962014-10-07 11:50:00 +020038```
39
Ash Wilson220b7df2014-10-28 10:25:32 -040040This will install all the source files you need into a `Godeps/_workspace` directory, which is
41referenceable from your own source files when you use the `godep go` command.
Jamie Hannaford4eb3f962014-10-07 11:50:00 +020042
43## Getting started
44
45### Credentials
46
47Because you'll be hitting an API, you will need to retrieve your OpenStack
48credentials and either store them as environment variables or in your local Go
49files. The first method is recommended because it decouples credential
50information from source code, allowing you to push the latter to your version
51control system without any security risk.
52
53You will need to retrieve the following:
54
55* username
56* password
57* tenant name or tenant ID
58* a valid Keystone identity URL
59
60For users that have the OpenStack dashboard installed, there's a shortcut. If
61you 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
63download a bash file that exports all of your access details to environment
64variables. To execute the file, run `source admin-openrc.sh` and you will be
65prompted for your password.
66
67### Authentication
68
69Once you have access to your credentials, you can begin plugging them into
Ash Wilson3f2566c2014-10-28 13:20:19 -040070Gophercloud. The next step is authentication, and this is handled by a base
Jamie Hannaford4eb3f962014-10-07 11:50:00 +020071"Provider" struct. To get one, you can either pass in your credentials
Ash Wilson3f2566c2014-10-28 13:20:19 -040072explicitly, or tell Gophercloud to use environment variables:
Jamie Hannaford4eb3f962014-10-07 11:50:00 +020073
74```go
Jamie Hannaford2b7bf772014-10-07 12:13:38 +020075import (
76 "github.com/rackspace/gophercloud"
77 "github.com/rackspace/gophercloud/openstack"
78 "github.com/rackspace/gophercloud/openstack/utils"
79)
80
Jamie Hannaford4eb3f962014-10-07 11:50:00 +020081// Option 1: Pass in the values yourself
82opts := 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 Hannaford390555a2014-10-22 17:04:03 +020090opts, err := openstack.AuthOptionsFromEnv()
Jamie Hannaford4eb3f962014-10-07 11:50:00 +020091```
92
93Once you have the `opts` variable, you can pass it in and get back a
94`ProviderClient` struct:
95
96```go
Jamie Hannaford4eb3f962014-10-07 11:50:00 +020097provider, err := openstack.AuthenticatedClient(opts)
98```
99
100The `ProviderClient` is the top-level client that all of your OpenStack services
101derive from. The provider contains all of the authentication details that allow
102your Go code to access the API - such as the base URL and token ID.
103
Jamie Hannaford2b7bf772014-10-07 12:13:38 +0200104### Provision a server
105
106Once we have a base Provider, we inject it as a dependency into each OpenStack
107service. In order to work with the Compute API, we need a Compute service
108client; which can be created like so:
109
110```go
111client, err := openstack.NewComputeV2(provider, gophercloud.EndpointOpts{
112 Region: os.Getenv("OS_REGION_NAME"),
113})
114```
115
116We then use this `client` for any Compute API operation we want. In our case,
117we want to provision a new server - so we invoke the `Create` method and pass
Jamie Hannafordb4b70f62014-10-07 12:21:40 +0200118in the flavor ID (hardware specification) and image ID (operating system) we're
Jamie Hannaford2b7bf772014-10-07 12:13:38 +0200119interested in:
120
121```go
Jamie Hannafordb4b70f62014-10-07 12:21:40 +0200122import "github.com/rackspace/gophercloud/openstack/compute/v2/servers"
123
Jamie Hannaford2b7bf772014-10-07 12:13:38 +0200124server, err := servers.Create(client, servers.CreateOpts{
125 Name: "My new server!",
126 FlavorRef: "flavor_id",
127 ImageRef: "image_id",
128}).Extract()
Jamie Hannaford2b7bf772014-10-07 12:13:38 +0200129```
130
131If you are unsure about what images and flavors are, you can read our [Compute
132Getting Started guide](http://gophercloud.io/docs/compute). The above code
133sample creates a new server with the parameters, and embodies the new resource
134in the `server` variable (a
135[`servers.Server`](http://godoc.org/github.com/rackspace/gophercloud) struct).
136
Jamie Hannaford4eb3f962014-10-07 11:50:00 +0200137### Next steps
138
Jamie Hannafordb4b70f62014-10-07 12:21:40 +0200139Cool! You've handled authentication, got your `ProviderClient` and provisioned
140a new server. You're now ready to use more OpenStack services.
Jamie Hannaford4eb3f962014-10-07 11:50:00 +0200141
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
150Engaging the community and lowering barriers for contributors is something we
151care a lot about. For this reason, we've taken the time to write a [contributing
Jamie Hannafordb4b70f62014-10-07 12:21:40 +0200152guide](./CONTRIBUTING.md) for folks interested in getting involved in our project.
153If you're not sure how you can get involved, feel free to submit an issue or
Jon Perritt9ba988a2015-03-16 09:42:48 -0600154[contact us](https://developer.rackspace.com/support/). You don't need to be a
Jamie Hannafordb4b70f62014-10-07 12:21:40 +0200155Go expert - all members of the community are welcome!
Jamie Hannaford4eb3f962014-10-07 11:50:00 +0200156
157## Help and feedback
158
159If you're struggling with something or have spotted a potential bug, feel free
Jon Perritt9ba988a2015-03-16 09:42:48 -0600160to submit an issue to our [bug tracker](/issues) or [contact us directly](https://developer.rackspace.com/support/).