blob: 6695047d0eabb6af4eb063e9876ebc4bd6cff52b [file] [log] [blame] [view]
jrperritt57edaf52016-04-13 14:51:11 -05001# WARNING: CURRENTLY NOT SUITABLE FOR CONSUMPTION. API IN FLUX.
Everett Toews430261f2015-10-12 10:11:55 -05002# Gophercloud: an OpenStack SDK for Go
jrperritt55fb5842016-04-13 14:16:08 -05003[![Build Status](https://travis-ci.org/gophercloud/gophercloud.svg?branch=master)](https://travis-ci.org/gophercloud/gophercloud)
Jon Perritt554de822016-03-10 02:56:23 -06004[![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 +02005
jrperritt55fb5842016-04-13 14:16:08 -05006Gophercloud is an OpenStack Go SDK.
Jamie Hannaford2b7bf772014-10-07 12:13:38 +02007
8## Useful links
9
jrperritt55fb5842016-04-13 14:16:08 -050010* [Reference documentation](http://godoc.org/github.com/gophercloud/gophercloud)
Jamie Hannaford2b7bf772014-10-07 12:13:38 +020011* [Effective Go](https://golang.org/doc/effective_go.html)
Jamie Hannaford4eb3f962014-10-07 11:50:00 +020012
13## How to install
14
15Before installing, you need to ensure that your [GOPATH environment variable](https://golang.org/doc/code.html#GOPATH)
Ash Wilson3f2566c2014-10-28 13:20:19 -040016is pointing to an appropriate directory where you want to install Gophercloud:
Jamie Hannaford4eb3f962014-10-07 11:50:00 +020017
18```bash
19mkdir $HOME/go
20export GOPATH=$HOME/go
21```
22
Ash Wilsonf41663d2014-10-28 10:21:33 -040023To protect yourself against changes in your dependencies, we highly recommend choosing a
JackSpirouda9ab5b2015-04-11 10:56:21 -050024[dependency management solution](https://github.com/golang/go/wiki/PackageManagementTools) for
Ash Wilsonf41663d2014-10-28 10:21:33 -040025your projects, such as [godep](https://github.com/tools/godep). Once this is set up, you can install
Ash Wilson3f2566c2014-10-28 13:20:19 -040026Gophercloud as a dependency like so:
Jamie Hannaford4eb3f962014-10-07 11:50:00 +020027
28```bash
jrperritt55fb5842016-04-13 14:16:08 -050029go get github.com/gophercloud/gophercloud
Ash Wilsonf41663d2014-10-28 10:21:33 -040030
jrperritt55fb5842016-04-13 14:16:08 -050031# Edit your code to import relevant packages from "github.com/gophercloud/gophercloud"
Ash Wilsonf41663d2014-10-28 10:21:33 -040032
33godep save ./...
Jamie Hannaford4eb3f962014-10-07 11:50:00 +020034```
35
Ash Wilson220b7df2014-10-28 10:25:32 -040036This will install all the source files you need into a `Godeps/_workspace` directory, which is
37referenceable from your own source files when you use the `godep go` command.
Jamie Hannaford4eb3f962014-10-07 11:50:00 +020038
39## Getting started
40
41### Credentials
42
43Because you'll be hitting an API, you will need to retrieve your OpenStack
44credentials and either store them as environment variables or in your local Go
45files. The first method is recommended because it decouples credential
46information from source code, allowing you to push the latter to your version
47control system without any security risk.
48
49You will need to retrieve the following:
50
51* username
52* password
Jamie Hannaford4eb3f962014-10-07 11:50:00 +020053* a valid Keystone identity URL
54
55For users that have the OpenStack dashboard installed, there's a shortcut. If
56you visit the `project/access_and_security` path in Horizon and click on the
57"Download OpenStack RC File" button at the top right hand corner, you will
58download a bash file that exports all of your access details to environment
59variables. To execute the file, run `source admin-openrc.sh` and you will be
60prompted for your password.
61
62### Authentication
63
64Once you have access to your credentials, you can begin plugging them into
Ash Wilson3f2566c2014-10-28 13:20:19 -040065Gophercloud. The next step is authentication, and this is handled by a base
Jamie Hannaford4eb3f962014-10-07 11:50:00 +020066"Provider" struct. To get one, you can either pass in your credentials
Ash Wilson3f2566c2014-10-28 13:20:19 -040067explicitly, or tell Gophercloud to use environment variables:
Jamie Hannaford4eb3f962014-10-07 11:50:00 +020068
69```go
Jamie Hannaford2b7bf772014-10-07 12:13:38 +020070import (
jrperritt55fb5842016-04-13 14:16:08 -050071 "github.com/gophercloud/gophercloud"
72 "github.com/gophercloud/gophercloud/openstack"
73 "github.com/gophercloud/gophercloud/openstack/utils"
Jamie Hannaford2b7bf772014-10-07 12:13:38 +020074)
75
Jamie Hannaford4eb3f962014-10-07 11:50:00 +020076// Option 1: Pass in the values yourself
77opts := gophercloud.AuthOptions{
78 IdentityEndpoint: "https://my-openstack.com:5000/v2.0",
79 Username: "{username}",
80 Password: "{password}",
Jamie Hannaford4eb3f962014-10-07 11:50:00 +020081}
82
83// Option 2: Use a utility function to retrieve all your environment variables
Jamie Hannaford390555a2014-10-22 17:04:03 +020084opts, err := openstack.AuthOptionsFromEnv()
Jamie Hannaford4eb3f962014-10-07 11:50:00 +020085```
86
87Once you have the `opts` variable, you can pass it in and get back a
88`ProviderClient` struct:
89
90```go
Jamie Hannaford4eb3f962014-10-07 11:50:00 +020091provider, err := openstack.AuthenticatedClient(opts)
92```
93
94The `ProviderClient` is the top-level client that all of your OpenStack services
95derive from. The provider contains all of the authentication details that allow
96your Go code to access the API - such as the base URL and token ID.
97
Jamie Hannaford2b7bf772014-10-07 12:13:38 +020098### Provision a server
99
100Once we have a base Provider, we inject it as a dependency into each OpenStack
101service. In order to work with the Compute API, we need a Compute service
102client; which can be created like so:
103
104```go
105client, err := openstack.NewComputeV2(provider, gophercloud.EndpointOpts{
106 Region: os.Getenv("OS_REGION_NAME"),
107})
108```
109
110We then use this `client` for any Compute API operation we want. In our case,
111we want to provision a new server - so we invoke the `Create` method and pass
Jamie Hannafordb4b70f62014-10-07 12:21:40 +0200112in the flavor ID (hardware specification) and image ID (operating system) we're
Jamie Hannaford2b7bf772014-10-07 12:13:38 +0200113interested in:
114
115```go
jrperritt55fb5842016-04-13 14:16:08 -0500116import "github.com/gophercloud/gophercloud/openstack/compute/v2/servers"
Jamie Hannafordb4b70f62014-10-07 12:21:40 +0200117
Jamie Hannaford2b7bf772014-10-07 12:13:38 +0200118server, err := servers.Create(client, servers.CreateOpts{
119 Name: "My new server!",
120 FlavorRef: "flavor_id",
121 ImageRef: "image_id",
122}).Extract()
Jamie Hannaford2b7bf772014-10-07 12:13:38 +0200123```
124
jrperritt55fb5842016-04-13 14:16:08 -0500125The above code sample creates a new server with the parameters, and embodies the
126new resource in the `server` variable (a
127[`servers.Server`](http://godoc.org/github.com/gophercloud/gophercloud) struct).
Jamie Hannaford2b7bf772014-10-07 12:13:38 +0200128
Jamie Hannaford4eb3f962014-10-07 11:50:00 +0200129### Next steps
130
Jamie Hannafordb4b70f62014-10-07 12:21:40 +0200131Cool! You've handled authentication, got your `ProviderClient` and provisioned
132a new server. You're now ready to use more OpenStack services.
Jamie Hannaford4eb3f962014-10-07 11:50:00 +0200133
134* [Getting started with Compute](http://gophercloud.io/docs/compute)
135* [Getting started with Object Storage](http://gophercloud.io/docs/object-storage)
136* [Getting started with Networking](http://gophercloud.io/docs/networking)
137* [Getting started with Block Storage](http://gophercloud.io/docs/block-storage)
138* [Getting started with Identity](http://gophercloud.io/docs/identity)
139
140## Contributing
141
142Engaging the community and lowering barriers for contributors is something we
143care a lot about. For this reason, we've taken the time to write a [contributing
Jamie Hannafordb4b70f62014-10-07 12:21:40 +0200144guide](./CONTRIBUTING.md) for folks interested in getting involved in our project.
145If you're not sure how you can get involved, feel free to submit an issue or
Jon Perritt9ba988a2015-03-16 09:42:48 -0600146[contact us](https://developer.rackspace.com/support/). You don't need to be a
Jamie Hannafordb4b70f62014-10-07 12:21:40 +0200147Go expert - all members of the community are welcome!
Jamie Hannaford4eb3f962014-10-07 11:50:00 +0200148
149## Help and feedback
150
151If you're struggling with something or have spotted a potential bug, feel free
Jon Perritt9ba988a2015-03-16 09:42:48 -0600152to submit an issue to our [bug tracker](/issues) or [contact us directly](https://developer.rackspace.com/support/).