1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
|
package capsules
import (
"encoding/json"
"fmt"
"time"
"github.com/gophercloud/gophercloud"
"github.com/gophercloud/gophercloud/pagination"
)
type commonResult struct {
gophercloud.Result
}
// ExtractBase is a function that accepts a result and extracts
// a base a capsule resource.
func (r commonResult) ExtractBase() (*Capsule, error) {
var s *Capsule
err := r.ExtractInto(&s)
return s, err
}
// Extract is a function that accepts a result and extracts a capsule result.
// The result will be returned as an interface{} where it should be able to
// be casted as either a Capsule or CapsuleV132.
func (r commonResult) Extract() (interface{}, error) {
s, err := r.ExtractBase()
if err == nil {
return s, nil
}
if _, ok := err.(*json.UnmarshalTypeError); !ok {
return s, err
}
return r.ExtractV132()
}
// GetResult represents the result of a get operation.
type GetResult struct {
commonResult
}
// CreateResult is the response from a Create operation. Call its Extract
// method to interpret it as a Capsule.
type CreateResult struct {
commonResult
}
// DeleteResult represents the result of a delete operation.
type DeleteResult struct {
gophercloud.ErrResult
}
type CapsulePage struct {
pagination.LinkedPageBase
}
// Represents a Capsule
type Capsule struct {
// UUID for the capsule
UUID string `json:"uuid"`
// User ID for the capsule
UserID string `json:"user_id"`
// Project ID for the capsule
ProjectID string `json:"project_id"`
// cpu for the capsule
CPU float64 `json:"cpu"`
// Memory for the capsule
Memory string `json:"memory"`
// The name of the capsule
MetaName string `json:"meta_name"`
// Indicates whether capsule is currently operational.
Status string `json:"status"`
// Indicates whether capsule is currently operational.
StatusReason string `json:"status_reason"`
// The created time of the capsule.
CreatedAt time.Time `json:"-"`
// The updated time of the capsule.
UpdatedAt time.Time `json:"-"`
// Links includes HTTP references to the itself, useful for passing along to
// other APIs that might want a capsule reference.
Links []interface{} `json:"links"`
// The capsule version
CapsuleVersion string `json:"capsule_version"`
// The capsule restart policy
RestartPolicy string `json:"restart_policy"`
// The capsule metadata labels
MetaLabels map[string]string `json:"meta_labels"`
// The list of containers uuids inside capsule.
ContainersUUIDs []string `json:"containers_uuids"`
// The capsule IP addresses
Addresses map[string][]Address `json:"addresses"`
// The capsule volume attached information
VolumesInfo map[string][]string `json:"volumes_info"`
// The container object inside capsule
Containers []Container `json:"containers"`
// The capsule host
Host string `json:"host"`
}
type Container struct {
// The Container IP addresses
Addresses map[string][]Address `json:"addresses"`
// UUID for the container
UUID string `json:"uuid"`
// User ID for the container
UserID string `json:"user_id"`
// Project ID for the container
ProjectID string `json:"project_id"`
// cpu for the container
CPU float64 `json:"cpu"`
// Memory for the container
Memory string `json:"memory"`
// Image for the container
Image string `json:"image"`
// The container container
Labels map[string]string `json:"labels"`
// The created time of the container
CreatedAt time.Time `json:"-"`
// The updated time of the container
UpdatedAt time.Time `json:"-"`
// The started time of the container
StartedAt time.Time `json:"-"`
// Name for the container
Name string `json:"name"`
// Links includes HTTP references to the itself, useful for passing along to
// other APIs that might want a capsule reference.
Links []interface{} `json:"links"`
// auto remove flag token for the container
AutoRemove bool `json:"auto_remove"`
// Host for the container
Host string `json:"host"`
// Work directory for the container
WorkDir string `json:"workdir"`
// Disk for the container
Disk int `json:"disk"`
// Image pull policy for the container
ImagePullPolicy string `json:"image_pull_policy"`
// Task state for the container
TaskState string `json:"task_state"`
// Host name for the container
HostName string `json:"hostname"`
// Environment for the container
Environment map[string]string `json:"environment"`
// Status for the container
Status string `json:"status"`
// Auto Heal flag for the container
AutoHeal bool `json:"auto_heal"`
// Status details for the container
StatusDetail string `json:"status_detail"`
// Status reason for the container
StatusReason string `json:"status_reason"`
// Image driver for the container
ImageDriver string `json:"image_driver"`
// Command for the container
Command []string `json:"command"`
// Image for the container
Runtime string `json:"runtime"`
// Interactive flag for the container
Interactive bool `json:"interactive"`
// Restart Policy for the container
RestartPolicy map[string]string `json:"restart_policy"`
// Ports information for the container
Ports []int `json:"ports"`
// Security groups for the container
SecurityGroups []string `json:"security_groups"`
}
type Address struct {
PreserveOnDelete bool `json:"preserve_on_delete"`
Addr string `json:"addr"`
Port string `json:"port"`
Version float64 `json:"version"`
SubnetID string `json:"subnet_id"`
}
// NextPageURL is invoked when a paginated collection of capsules has reached
// the end of a page and the pager seeks to traverse over a new one. In order
// to do this, it needs to construct the next page's URL.
func (r CapsulePage) NextPageURL() (string, error) {
var s struct {
Next string `json:"next"`
}
err := r.ExtractInto(&s)
if err != nil {
return "", err
}
return s.Next, nil
}
// IsEmpty checks whether a CapsulePage struct is empty.
func (r CapsulePage) IsEmpty() (bool, error) {
if r.StatusCode == 204 {
return true, nil
}
is, err := ExtractCapsules(r)
if err != nil {
return false, err
}
if v, ok := is.([]Capsule); ok {
return len(v) == 0, nil
}
if v, ok := is.([]CapsuleV132); ok {
return len(v) == 0, nil
}
return false, fmt.Errorf("Unable to determine Capsule type")
}
// ExtractCapsulesBase accepts a Page struct, specifically a CapsulePage struct,
// and extracts the elements into a slice of Capsule structs. In other words,
// a generic collection is mapped into the relevant slice.
func ExtractCapsulesBase(r pagination.Page) ([]Capsule, error) {
var s struct {
Capsules []Capsule `json:"capsules"`
}
err := (r.(CapsulePage)).ExtractInto(&s)
return s.Capsules, err
}
// ExtractCapsules accepts a Page struct, specifically a CapsulePage struct,
// and extracts the elements into an interface.
// This interface should be able to be casted as either a Capsule or
// CapsuleV132 struct
func ExtractCapsules(r pagination.Page) (interface{}, error) {
s, err := ExtractCapsulesBase(r)
if err == nil {
return s, nil
}
if _, ok := err.(*json.UnmarshalTypeError); !ok {
return nil, err
}
return ExtractCapsulesV132(r)
}
func (r *Capsule) UnmarshalJSON(b []byte) error {
type tmp Capsule
// Support for "older" zun time formats.
var s1 struct {
tmp
CreatedAt gophercloud.JSONRFC3339ZNoT `json:"created_at"`
UpdatedAt gophercloud.JSONRFC3339ZNoT `json:"updated_at"`
}
err := json.Unmarshal(b, &s1)
if err == nil {
*r = Capsule(s1.tmp)
r.CreatedAt = time.Time(s1.CreatedAt)
r.UpdatedAt = time.Time(s1.UpdatedAt)
return nil
}
// Support for "new" zun time formats.
var s2 struct {
tmp
CreatedAt gophercloud.JSONRFC3339ZNoTNoZ `json:"created_at"`
UpdatedAt gophercloud.JSONRFC3339ZNoTNoZ `json:"updated_at"`
}
err = json.Unmarshal(b, &s2)
if err != nil {
return err
}
*r = Capsule(s2.tmp)
r.CreatedAt = time.Time(s2.CreatedAt)
r.UpdatedAt = time.Time(s2.UpdatedAt)
return nil
}
func (r *Container) UnmarshalJSON(b []byte) error {
type tmp Container
// Support for "older" zun time formats.
var s1 struct {
tmp
CreatedAt gophercloud.JSONRFC3339ZNoT `json:"created_at"`
UpdatedAt gophercloud.JSONRFC3339ZNoT `json:"updated_at"`
StartedAt gophercloud.JSONRFC3339ZNoT `json:"started_at"`
}
err := json.Unmarshal(b, &s1)
if err == nil {
*r = Container(s1.tmp)
r.CreatedAt = time.Time(s1.CreatedAt)
r.UpdatedAt = time.Time(s1.UpdatedAt)
r.StartedAt = time.Time(s1.StartedAt)
return nil
}
// Support for "new" zun time formats.
var s2 struct {
tmp
CreatedAt gophercloud.JSONRFC3339ZNoTNoZ `json:"created_at"`
UpdatedAt gophercloud.JSONRFC3339ZNoTNoZ `json:"updated_at"`
StartedAt gophercloud.JSONRFC3339ZNoTNoZ `json:"started_at"`
}
err = json.Unmarshal(b, &s2)
if err != nil {
return err
}
*r = Container(s2.tmp)
r.CreatedAt = time.Time(s2.CreatedAt)
r.UpdatedAt = time.Time(s2.UpdatedAt)
r.StartedAt = time.Time(s2.StartedAt)
return nil
}
|