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