blob: 30aa9597ebf83ea6133745ca7bf48ea307d49405 [file] [log] [blame]
Samuel A. Falvo IIbc0d54a2013-07-08 14:45:21 -07001// TODO(sfalvo): Remove Rackspace-specific Server structure fields and refactor them into a provider-specific access method.
2// Be sure to update godocs accordingly.
3
Samuel A. Falvo II2e2b8772013-07-04 15:40:15 -07004package gophercloud
5
Samuel A. Falvo IIbc0d54a2013-07-08 14:45:21 -07006import (
7 "github.com/racker/perigee"
8)
9
Samuel A. Falvo II1dd740a2013-07-08 15:48:40 -070010// genericServersProvider structures provide the implementation for generic OpenStack-compatible
11// CloudServersProvider interfaces.
12type genericServersProvider struct {
Samuel A. Falvo II2e2b8772013-07-04 15:40:15 -070013 // endpoint refers to the provider's API endpoint base URL. This will be used to construct
14 // and issue queries.
15 endpoint string
16
17 // Test context (if any) in which to issue requests.
18 context *Context
Samuel A. Falvo IIbc0d54a2013-07-08 14:45:21 -070019
20 // access associates this API provider with a set of credentials,
21 // which may be automatically renewed if they near expiration.
22 access AccessProvider
Samuel A. Falvo II2e2b8772013-07-04 15:40:15 -070023}
24
Samuel A. Falvo II1dd740a2013-07-08 15:48:40 -070025// See the CloudServersProvider interface for details.
26func (gcp *genericServersProvider) ListServers() ([]Server, error) {
Samuel A. Falvo IIbc0d54a2013-07-08 14:45:21 -070027 var ss []Server
28
29 url := gcp.endpoint + "/servers"
30 err := perigee.Get(url, perigee.Options{
31 CustomClient: gcp.context.httpClient,
32 Results: &struct{ Servers *[]Server }{&ss},
33 MoreHeaders: map[string]string{
34 "X-Auth-Token": gcp.access.AuthToken(),
35 },
36 })
37 return ss, err
Samuel A. Falvo II2e2b8772013-07-04 15:40:15 -070038}
39
Samuel A. Falvo II02f5e832013-07-10 13:52:27 -070040// See the CloudServersProvider interface for details.
41func (gsp *genericServersProvider) ServerById(id string) (*Server, error) {
42 var s *Server
43
44 url := gsp.endpoint + "/servers/" + id
45 err := perigee.Get(url, perigee.Options{
46 Results: &struct{ Server **Server }{&s},
47 MoreHeaders: map[string]string{
48 "X-Auth-Token": gsp.access.AuthToken(),
49 },
50 })
51 return s, err
52}
53
Samuel A. Falvo IIe91ff6d2013-07-11 15:46:10 -070054// See the CloudServersProvider interface for details.
55func (gsp *genericServersProvider) CreateServer(ns NewServer) (*NewServer, error) {
56 var s *NewServer
57
58 ep := gsp.endpoint + "/servers"
59 err := perigee.Post(ep, perigee.Options{
60 ReqBody: &struct {
61 Server *NewServer `json:"server"`
62 }{&ns},
63 Results: &struct{ Server **NewServer }{&s},
64 MoreHeaders: map[string]string{
65 "X-Auth-Token": gsp.access.AuthToken(),
66 },
67 OkCodes: []int{202},
68 })
69 return s, err
70}
71
Samuel A. Falvo II286e4de2013-07-12 11:33:31 -070072// See the CloudServersProvider interface for details.
73func (gsp *genericServersProvider) DeleteServerById(id string) error {
74 url := gsp.endpoint + "/servers/" + id
75 err := perigee.Delete(url, perigee.Options{
76 MoreHeaders: map[string]string{
77 "X-Auth-Token": gsp.access.AuthToken(),
78 },
79 OkCodes: []int{204},
80 })
81 return err
82}
83
Samuel A. Falvo IIbc0d54a2013-07-08 14:45:21 -070084// RaxBandwidth provides measurement of server bandwidth consumed over a given audit interval.
85type RaxBandwidth struct {
86 AuditPeriodEnd string `json:"audit_period_end"`
87 AuditPeriodStart string `json:"audit_period_start"`
88 BandwidthInbound int64 `json:"bandwidth_inbound"`
89 BandwidthOutbound int64 `json:"bandwidth_outbound"`
90 Interface string `json:"interface"`
91}
92
93// A VersionedAddress denotes either an IPv4 or IPv6 (depending on version indicated)
94// address.
95type VersionedAddress struct {
96 Addr string `json:"addr"`
97 Version int `json:"version"`
98}
99
100// An AddressSet provides a set of public and private IP addresses for a resource.
101// Each address has a version to identify if IPv4 or IPv6.
102type AddressSet struct {
103 Public []VersionedAddress `json:"public"`
104 Private []VersionedAddress `json:"private"`
105}
106
107// Server records represent (virtual) hardware instances (not configurations) accessible by the user.
108//
109// The AccessIPv4 / AccessIPv6 fields provides IP addresses for the server in the IPv4 or IPv6 format, respectively.
110//
111// Addresses provides addresses for any attached isolated networks.
112// The version field indicates whether the IP address is version 4 or 6.
113//
114// Created tells when the server entity was created.
115//
116// The Flavor field includes the flavor ID and flavor links.
117//
118// The compute provisioning algorithm has an anti-affinity property that
119// attempts to spread customer VMs across hosts.
120// Under certain situations,
121// VMs from the same customer might be placed on the same host.
122// The HostId field represents the host your server runs on and
123// can be used to determine this scenario if it is relevant to your application.
124// Note that HostId is unique only per account; it is not globally unique.
125//
126// Id provides the server's unique identifier.
127// This field must be treated opaquely.
128//
129// Image indicates which image is installed on the server.
130//
131// Links provides one or more means of accessing the server.
132//
133// Metadata provides a small key-value store for application-specific information.
134//
135// Name provides a human-readable name for the server.
136//
137// Progress indicates how far along it is towards being provisioned.
138// 100 represents complete, while 0 represents just beginning.
139//
140// Status provides an indication of what the server's doing at the moment.
141// A server will be in ACTIVE state if it's ready for use.
142//
143// OsDcfDiskConfig indicates the server's boot volume configuration.
144// Valid values are:
145// AUTO
146// ----
147// The server is built with a single partition the size of the target flavor disk.
148// The file system is automatically adjusted to fit the entire partition.
149// This keeps things simple and automated.
150// AUTO is valid only for images and servers with a single partition that use the EXT3 file system.
151// This is the default setting for applicable Rackspace base images.
152//
153// MANUAL
154// ------
155// The server is built using whatever partition scheme and file system is in the source image.
156// If the target flavor disk is larger,
157// the remaining disk space is left unpartitioned.
158// This enables images to have non-EXT3 file systems, multiple partitions, and so on,
159// and enables you to manage the disk configuration.
160//
161// RaxBandwidth provides measures of the server's inbound and outbound bandwidth per interface.
162//
163// OsExtStsPowerState provides an indication of the server's power.
164// This field appears to be a set of flag bits:
165//
166// ... 4 3 2 1 0
167// +--//--+---+---+---+---+
168// | .... | 0 | S | 0 | I |
169// +--//--+---+---+---+---+
170// | |
171// | +--- 0=Instance is down.
172// | 1=Instance is up.
173// |
174// +----------- 0=Server is switched ON.
175// 1=Server is switched OFF.
176// (note reverse logic.)
177//
178// Unused bits should be ignored when read, and written as 0 for future compatibility.
179//
180// OsExtStsTaskState and OsExtStsVmState work together
181// to provide visibility in the provisioning process for the instance.
182// Consult Rackspace documentation at
183// http://docs.rackspace.com/servers/api/v2/cs-devguide/content/ch_extensions.html#ext_status
184// for more details. It's too lengthy to include here.
Samuel A. Falvo II2e2b8772013-07-04 15:40:15 -0700185type Server struct {
Samuel A. Falvo IIbc0d54a2013-07-08 14:45:21 -0700186 AccessIPv4 string `json:"accessIPv4"`
187 AccessIPv6 string `json:"accessIPv6"`
188 Addresses AddressSet `json:"addresses"`
189 Created string `json:"created"`
190 Flavor FlavorLink `json:"flavor"`
191 HostId string `json:"hostId"`
192 Id string `json:"id"`
193 Image ImageLink `json:"image"`
194 Links []Link `json:"links"`
195 Metadata interface{} `json:"metadata"`
196 Name string `json:"name"`
197 Progress int `json:"progress"`
198 Status string `json:"status"`
199 TenantId string `json:"tenant_id"`
200 Updated string `json:"updated"`
201 UserId string `json:"user_id"`
202 OsDcfDiskConfig string `json:"OS-DCF:diskConfig"`
203 RaxBandwidth []RaxBandwidth `json:"rax-bandwidth:bandwidth"`
204 OsExtStsPowerState int `json:"OS-EXT-STS:power_state"`
205 OsExtStsTaskState string `json:"OS-EXT-STS:task_state"`
206 OsExtStsVmState string `json:"OS-EXT-STS:vm_state"`
Samuel A. Falvo II2e2b8772013-07-04 15:40:15 -0700207}
Samuel A. Falvo IIe91ff6d2013-07-11 15:46:10 -0700208
209// NewServer structures are used for both requests and responses.
210// The fields discussed below are relevent for server-creation purposes.
211//
212// The Name field contains the desired name of the server.
213// Note that (at present) Rackspace permits more than one server with the same name;
214// however, software should not depend on this.
215// Not only will Rackspace support thank you, so will your own devops engineers.
216// A name is required.
217//
218// The ImageRef field contains the ID of the desired software image to place on the server.
219// This ID must be found in the image slice returned by the Images() function.
220// This field is required.
221//
222// The FlavorRef field contains the ID of the server configuration desired for deployment.
223// This ID must be found in the flavor slice returned by the Flavors() function.
224// This field is required.
225//
226// For OsDcfDiskConfig, refer to the Image or Server structure documentation.
227// This field defaults to "AUTO" if not explicitly provided.
228//
229// Metadata contains a small key/value association of arbitrary data.
230// Neither Rackspace nor OpenStack places significance on this field in any way.
231// This field defaults to an empty map if not provided.
232//
233// Personality specifies the contents of certain files in the server's filesystem.
234// The files and their contents are mapped through a slice of FileConfig structures.
235// If not provided, all filesystem entities retain their image-specific configuration.
236//
237// Networks specifies an affinity for the server's various networks and interfaces.
238// Networks are identified through UUIDs; see NetworkConfig structure documentation for more details.
239// If not provided, network affinity is determined automatically.
240//
241// The AdminPass field may be used to provide a root- or administrator-password
242// during the server provisioning process.
243// If not provided, a random password will be automatically generated and returned in this field.
244//
245// The following fields are intended to be used to communicate certain results about the server being provisioned.
246// When attempting to create a new server, these fields MUST not be provided.
247// They'll be filled in by the response received from the Rackspace APIs.
248//
249// The Id field contains the server's unique identifier.
250// The identifier's scope is best assumed to be bound by the user's account, unless other arrangements have been made with Rackspace.
251//
252// Any Links provided are used to refer to the server specifically by URL.
253// These links are useful for making additional REST calls not explicitly supported by Gorax.
254type NewServer struct {
255 Name string `json:"name",omitempty`
256 ImageRef string `json:"imageRef,omitempty"`
257 FlavorRef string `json:"flavorRef,omitempty"`
258 Metadata interface{} `json:"metadata,omitempty"`
259 Personality []FileConfig `json:"personality,omitempty"`
260 Networks []NetworkConfig `json:"networks,omitempty"`
261 AdminPass string `json:"adminPass,omitempty"`
262 Id string `json:"id,omitempty"`
263 Links []Link `json:"links,omitempty"`
264 OsDcfDiskConfig string `json:"OS-DCF:diskConfig,omitempty"`
265}