blob: de95a1057a12cd3f825a92ec09ec6ebec3f53af0 [file] [log] [blame] [view]
Everett Toews430261f2015-10-12 10:11:55 -05001# Gophercloud: an OpenStack SDK for Go
Jon Perritt554de822016-03-10 02:56:23 -06002[![Build Status](https://travis-ci.org/rackspace/gophercloud.svg?branch=master)](https://travis-ci.org/rackspace/gophercloud)
3[![Coverage Status](https://coveralls.io/repos/github/gophercloud/gophercloud/badge.svg?branch=master)](https://coveralls.io/github/gophercloud/gophercloud?branch=master)
Jamie Hannaford4eb3f962014-10-07 11:50:00 +02004
Ash Wilson3f2566c2014-10-28 13:20:19 -04005Gophercloud is a flexible SDK that allows you to consume and work with OpenStack
Jamie Hannaford4eb3f962014-10-07 11:50:00 +02006clouds in a simple and idiomatic way using golang. Many services are supported,
7including Compute, Block Storage, Object Storage, Networking, and Identity.
Jamie Hannaford2b7bf772014-10-07 12:13:38 +02008Each service API is backed with getting started guides, code samples, reference
9documentation, unit tests and acceptance tests.
10
11## Useful links
12
Ash Wilson3f2566c2014-10-28 13:20:19 -040013* [Gophercloud homepage](http://gophercloud.io)
Jamie Hannaford2b7bf772014-10-07 12:13:38 +020014* [Reference documentation](http://godoc.org/github.com/rackspace/gophercloud)
15* [Getting started guides](http://gophercloud.io/docs)
16* [Effective Go](https://golang.org/doc/effective_go.html)
Jamie Hannaford4eb3f962014-10-07 11:50:00 +020017
18## How to install
19
20Before installing, you need to ensure that your [GOPATH environment variable](https://golang.org/doc/code.html#GOPATH)
Ash Wilson3f2566c2014-10-28 13:20:19 -040021is pointing to an appropriate directory where you want to install Gophercloud:
Jamie Hannaford4eb3f962014-10-07 11:50:00 +020022
23```bash
24mkdir $HOME/go
25export GOPATH=$HOME/go
26```
27
Ash Wilsonf41663d2014-10-28 10:21:33 -040028To protect yourself against changes in your dependencies, we highly recommend choosing a
JackSpirouda9ab5b2015-04-11 10:56:21 -050029[dependency management solution](https://github.com/golang/go/wiki/PackageManagementTools) for
Ash Wilsonf41663d2014-10-28 10:21:33 -040030your projects, such as [godep](https://github.com/tools/godep). Once this is set up, you can install
Ash Wilson3f2566c2014-10-28 13:20:19 -040031Gophercloud as a dependency like so:
Jamie Hannaford4eb3f962014-10-07 11:50:00 +020032
33```bash
34go get github.com/rackspace/gophercloud
Ash Wilsonf41663d2014-10-28 10:21:33 -040035
36# Edit your code to import relevant packages from "github.com/rackspace/gophercloud"
37
38godep save ./...
Jamie Hannaford4eb3f962014-10-07 11:50:00 +020039```
40
Ash Wilson220b7df2014-10-28 10:25:32 -040041This will install all the source files you need into a `Godeps/_workspace` directory, which is
42referenceable from your own source files when you use the `godep go` command.
Jamie Hannaford4eb3f962014-10-07 11:50:00 +020043
44## Getting started
45
46### Credentials
47
48Because you'll be hitting an API, you will need to retrieve your OpenStack
49credentials and either store them as environment variables or in your local Go
50files. The first method is recommended because it decouples credential
51information from source code, allowing you to push the latter to your version
52control system without any security risk.
53
54You will need to retrieve the following:
55
56* username
57* password
58* tenant name or tenant ID
59* a valid Keystone identity URL
60
61For users that have the OpenStack dashboard installed, there's a shortcut. If
62you visit the `project/access_and_security` path in Horizon and click on the
63"Download OpenStack RC File" button at the top right hand corner, you will
64download a bash file that exports all of your access details to environment
65variables. To execute the file, run `source admin-openrc.sh` and you will be
66prompted for your password.
67
68### Authentication
69
70Once you have access to your credentials, you can begin plugging them into
Ash Wilson3f2566c2014-10-28 13:20:19 -040071Gophercloud. The next step is authentication, and this is handled by a base
Jamie Hannaford4eb3f962014-10-07 11:50:00 +020072"Provider" struct. To get one, you can either pass in your credentials
Ash Wilson3f2566c2014-10-28 13:20:19 -040073explicitly, or tell Gophercloud to use environment variables:
Jamie Hannaford4eb3f962014-10-07 11:50:00 +020074
75```go
Jamie Hannaford2b7bf772014-10-07 12:13:38 +020076import (
77 "github.com/rackspace/gophercloud"
78 "github.com/rackspace/gophercloud/openstack"
79 "github.com/rackspace/gophercloud/openstack/utils"
80)
81
Jamie Hannaford4eb3f962014-10-07 11:50:00 +020082// Option 1: Pass in the values yourself
83opts := gophercloud.AuthOptions{
84 IdentityEndpoint: "https://my-openstack.com:5000/v2.0",
85 Username: "{username}",
86 Password: "{password}",
87 TenantID: "{tenant_id}",
88}
89
90// Option 2: Use a utility function to retrieve all your environment variables
Jamie Hannaford390555a2014-10-22 17:04:03 +020091opts, err := openstack.AuthOptionsFromEnv()
Jamie Hannaford4eb3f962014-10-07 11:50:00 +020092```
93
94Once you have the `opts` variable, you can pass it in and get back a
95`ProviderClient` struct:
96
97```go
Jamie Hannaford4eb3f962014-10-07 11:50:00 +020098provider, err := openstack.AuthenticatedClient(opts)
99```
100
101The `ProviderClient` is the top-level client that all of your OpenStack services
102derive from. The provider contains all of the authentication details that allow
103your Go code to access the API - such as the base URL and token ID.
104
Jamie Hannaford2b7bf772014-10-07 12:13:38 +0200105### Provision a server
106
107Once we have a base Provider, we inject it as a dependency into each OpenStack
108service. In order to work with the Compute API, we need a Compute service
109client; which can be created like so:
110
111```go
112client, err := openstack.NewComputeV2(provider, gophercloud.EndpointOpts{
113 Region: os.Getenv("OS_REGION_NAME"),
114})
115```
116
117We then use this `client` for any Compute API operation we want. In our case,
118we want to provision a new server - so we invoke the `Create` method and pass
Jamie Hannafordb4b70f62014-10-07 12:21:40 +0200119in the flavor ID (hardware specification) and image ID (operating system) we're
Jamie Hannaford2b7bf772014-10-07 12:13:38 +0200120interested in:
121
122```go
Jamie Hannafordb4b70f62014-10-07 12:21:40 +0200123import "github.com/rackspace/gophercloud/openstack/compute/v2/servers"
124
Jamie Hannaford2b7bf772014-10-07 12:13:38 +0200125server, err := servers.Create(client, servers.CreateOpts{
126 Name: "My new server!",
127 FlavorRef: "flavor_id",
128 ImageRef: "image_id",
129}).Extract()
Jamie Hannaford2b7bf772014-10-07 12:13:38 +0200130```
131
132If you are unsure about what images and flavors are, you can read our [Compute
133Getting Started guide](http://gophercloud.io/docs/compute). The above code
134sample creates a new server with the parameters, and embodies the new resource
135in the `server` variable (a
136[`servers.Server`](http://godoc.org/github.com/rackspace/gophercloud) struct).
137
Jamie Hannaford4eb3f962014-10-07 11:50:00 +0200138### Next steps
139
Jamie Hannafordb4b70f62014-10-07 12:21:40 +0200140Cool! You've handled authentication, got your `ProviderClient` and provisioned
141a new server. You're now ready to use more OpenStack services.
Jamie Hannaford4eb3f962014-10-07 11:50:00 +0200142
143* [Getting started with Compute](http://gophercloud.io/docs/compute)
144* [Getting started with Object Storage](http://gophercloud.io/docs/object-storage)
145* [Getting started with Networking](http://gophercloud.io/docs/networking)
146* [Getting started with Block Storage](http://gophercloud.io/docs/block-storage)
147* [Getting started with Identity](http://gophercloud.io/docs/identity)
148
149## Contributing
150
151Engaging the community and lowering barriers for contributors is something we
152care a lot about. For this reason, we've taken the time to write a [contributing
Jamie Hannafordb4b70f62014-10-07 12:21:40 +0200153guide](./CONTRIBUTING.md) for folks interested in getting involved in our project.
154If you're not sure how you can get involved, feel free to submit an issue or
Jon Perritt9ba988a2015-03-16 09:42:48 -0600155[contact us](https://developer.rackspace.com/support/). You don't need to be a
Jamie Hannafordb4b70f62014-10-07 12:21:40 +0200156Go expert - all members of the community are welcome!
Jamie Hannaford4eb3f962014-10-07 11:50:00 +0200157
158## Help and feedback
159
160If you're struggling with something or have spotted a potential bug, feel free
Jon Perritt9ba988a2015-03-16 09:42:48 -0600161to submit an issue to our [bug tracker](/issues) or [contact us directly](https://developer.rackspace.com/support/).