blob: 053200013a1260d8161a687bf8e8616be65d2466 [file] [log] [blame] [view]
Everett Toews430261f2015-10-12 10:11:55 -05001# Gophercloud: an OpenStack SDK for Go
jrperritt55fb5842016-04-13 14:16:08 -05002[![Build Status](https://travis-ci.org/gophercloud/gophercloud.svg?branch=master)](https://travis-ci.org/gophercloud/gophercloud)
Jon Perritt554de822016-03-10 02:56:23 -06003[![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
jrperritt55fb5842016-04-13 14:16:08 -05005Gophercloud is an OpenStack Go SDK.
Jamie Hannaford2b7bf772014-10-07 12:13:38 +02006
7## Useful links
8
jrperritt55fb5842016-04-13 14:16:08 -05009* [Reference documentation](http://godoc.org/github.com/gophercloud/gophercloud)
Jamie Hannaford2b7bf772014-10-07 12:13:38 +020010* [Effective Go](https://golang.org/doc/effective_go.html)
Jamie Hannaford4eb3f962014-10-07 11:50:00 +020011
12## How to install
13
14Before installing, you need to ensure that your [GOPATH environment variable](https://golang.org/doc/code.html#GOPATH)
Ash Wilson3f2566c2014-10-28 13:20:19 -040015is pointing to an appropriate directory where you want to install Gophercloud:
Jamie Hannaford4eb3f962014-10-07 11:50:00 +020016
17```bash
18mkdir $HOME/go
19export GOPATH=$HOME/go
20```
21
Ash Wilsonf41663d2014-10-28 10:21:33 -040022To protect yourself against changes in your dependencies, we highly recommend choosing a
JackSpirouda9ab5b2015-04-11 10:56:21 -050023[dependency management solution](https://github.com/golang/go/wiki/PackageManagementTools) for
Ash Wilsonf41663d2014-10-28 10:21:33 -040024your projects, such as [godep](https://github.com/tools/godep). Once this is set up, you can install
Ash Wilson3f2566c2014-10-28 13:20:19 -040025Gophercloud as a dependency like so:
Jamie Hannaford4eb3f962014-10-07 11:50:00 +020026
27```bash
jrperritt55fb5842016-04-13 14:16:08 -050028go get github.com/gophercloud/gophercloud
Ash Wilsonf41663d2014-10-28 10:21:33 -040029
jrperritt55fb5842016-04-13 14:16:08 -050030# Edit your code to import relevant packages from "github.com/gophercloud/gophercloud"
Ash Wilsonf41663d2014-10-28 10:21:33 -040031
32godep save ./...
Jamie Hannaford4eb3f962014-10-07 11:50:00 +020033```
34
Ash Wilson220b7df2014-10-28 10:25:32 -040035This will install all the source files you need into a `Godeps/_workspace` directory, which is
36referenceable from your own source files when you use the `godep go` command.
Jamie Hannaford4eb3f962014-10-07 11:50:00 +020037
38## Getting started
39
40### Credentials
41
42Because you'll be hitting an API, you will need to retrieve your OpenStack
43credentials and either store them as environment variables or in your local Go
44files. The first method is recommended because it decouples credential
45information from source code, allowing you to push the latter to your version
46control system without any security risk.
47
48You will need to retrieve the following:
49
50* username
51* password
Jamie Hannaford4eb3f962014-10-07 11:50:00 +020052* a valid Keystone identity URL
53
54For users that have the OpenStack dashboard installed, there's a shortcut. If
55you visit the `project/access_and_security` path in Horizon and click on the
56"Download OpenStack RC File" button at the top right hand corner, you will
57download a bash file that exports all of your access details to environment
58variables. To execute the file, run `source admin-openrc.sh` and you will be
59prompted for your password.
60
61### Authentication
62
63Once you have access to your credentials, you can begin plugging them into
Ash Wilson3f2566c2014-10-28 13:20:19 -040064Gophercloud. The next step is authentication, and this is handled by a base
Jamie Hannaford4eb3f962014-10-07 11:50:00 +020065"Provider" struct. To get one, you can either pass in your credentials
Ash Wilson3f2566c2014-10-28 13:20:19 -040066explicitly, or tell Gophercloud to use environment variables:
Jamie Hannaford4eb3f962014-10-07 11:50:00 +020067
68```go
Jamie Hannaford2b7bf772014-10-07 12:13:38 +020069import (
jrperritt55fb5842016-04-13 14:16:08 -050070 "github.com/gophercloud/gophercloud"
71 "github.com/gophercloud/gophercloud/openstack"
72 "github.com/gophercloud/gophercloud/openstack/utils"
Jamie Hannaford2b7bf772014-10-07 12:13:38 +020073)
74
Jamie Hannaford4eb3f962014-10-07 11:50:00 +020075// Option 1: Pass in the values yourself
76opts := gophercloud.AuthOptions{
77 IdentityEndpoint: "https://my-openstack.com:5000/v2.0",
78 Username: "{username}",
79 Password: "{password}",
Jamie Hannaford4eb3f962014-10-07 11:50:00 +020080}
81
82// Option 2: Use a utility function to retrieve all your environment variables
Jamie Hannaford390555a2014-10-22 17:04:03 +020083opts, err := openstack.AuthOptionsFromEnv()
Jamie Hannaford4eb3f962014-10-07 11:50:00 +020084```
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
jrperritt55fb5842016-04-13 14:16:08 -0500115import "github.com/gophercloud/gophercloud/openstack/compute/v2/servers"
Jamie Hannafordb4b70f62014-10-07 12:21:40 +0200116
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
jrperritt55fb5842016-04-13 14:16:08 -0500124The above code sample creates a new server with the parameters, and embodies the
125new resource in the `server` variable (a
126[`servers.Server`](http://godoc.org/github.com/gophercloud/gophercloud) struct).
Jamie Hannaford2b7bf772014-10-07 12:13:38 +0200127
Jamie Hannaford4eb3f962014-10-07 11:50:00 +0200128### Next steps
129
Jamie Hannafordb4b70f62014-10-07 12:21:40 +0200130Cool! You've handled authentication, got your `ProviderClient` and provisioned
131a new server. You're now ready to use more OpenStack services.
Jamie Hannaford4eb3f962014-10-07 11:50:00 +0200132
133* [Getting started with Compute](http://gophercloud.io/docs/compute)
134* [Getting started with Object Storage](http://gophercloud.io/docs/object-storage)
135* [Getting started with Networking](http://gophercloud.io/docs/networking)
136* [Getting started with Block Storage](http://gophercloud.io/docs/block-storage)
137* [Getting started with Identity](http://gophercloud.io/docs/identity)
138
139## Contributing
140
141Engaging the community and lowering barriers for contributors is something we
142care a lot about. For this reason, we've taken the time to write a [contributing
Jamie Hannafordb4b70f62014-10-07 12:21:40 +0200143guide](./CONTRIBUTING.md) for folks interested in getting involved in our project.
144If you're not sure how you can get involved, feel free to submit an issue or
Jon Perritt9ba988a2015-03-16 09:42:48 -0600145[contact us](https://developer.rackspace.com/support/). You don't need to be a
Jamie Hannafordb4b70f62014-10-07 12:21:40 +0200146Go expert - all members of the community are welcome!
Jamie Hannaford4eb3f962014-10-07 11:50:00 +0200147
148## Help and feedback
149
150If you're struggling with something or have spotted a potential bug, feel free
Jon Perritt9ba988a2015-03-16 09:42:48 -0600151to submit an issue to our [bug tracker](/issues) or [contact us directly](https://developer.rackspace.com/support/).